summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <>2024-09-13 17:41:45 +0200
committerOskar <>2024-09-13 17:41:45 +0200
commit62a39029a5604525d8c465aedcf14bbacd07e78b (patch)
treecbbc1db74f4b737dba18b00593a7f0851c1a91af
parent8d2c11b626736648c86ad09865118a2da9a0149d (diff)
tried out a version of ncat but with linux system calls read/write
-rw-r--r--read_print_file.cpp80
-rwxr-xr-xrpfbin0 -> 1842992 bytes
2 files changed, 57 insertions, 23 deletions
diff --git a/read_print_file.cpp b/read_print_file.cpp
index 327c500..1930096 100644
--- a/read_print_file.cpp
+++ b/read_print_file.cpp
@@ -3,6 +3,7 @@
#include <fstream>
#include <vector>
#include <unistd.h>
+#include <fcntl.h>
#define USAGE "rpf [-1][-2][-3][-4][-5][-6] [FILE]"
#define VEC_BUF_SIZE 1048576 // 1 MiB
@@ -14,7 +15,7 @@ void PrintFile(const std::filesystem::path &path) {
std::cout << "Cannot open file" << std::endl;
return;
}
-
+
thisfile.seekg(0, std::ios::end); // seek to end
auto size = thisfile.tellg(); // get size
std::string data(size, '\0'); // make string of appropriate size
@@ -42,12 +43,12 @@ void PrintFileBUFFERED_SINGLE_CHAR(const std::filesystem::path &path) { // extre
for(auto a = vecbuf.begin() ; a != vecbuf.end() ; ++a) {
*a = thisfile.get();
}
-
+
for(const auto &a : vecbuf) {
std::cout << a;
- }
+ }
}
-
+
std::cout << "File size: " << size << std::endl;
thisfile.close();
}
@@ -69,7 +70,7 @@ void PrintFileBUFFERED_READ(const std::filesystem::path &path) {
thisfile.read(vecbuf.data(), vecbuf.size());
std::cout.write(vecbuf.data(), thisfile.gcount());
}
-
+
std::cout << "File size: " << size << std::endl;
thisfile.close();
}
@@ -91,7 +92,7 @@ void PrintFileBUFFERED_READ_ARRAY(const std::filesystem::path &path) {
thisfile.read(arrbuf, VEC_BUF_SIZE);
std::cout.write(arrbuf, thisfile.gcount());
}
-
+
std::cout << "File size: " << size << std::endl;
thisfile.close();
}
@@ -113,12 +114,12 @@ void PrintFileBUFFERED_READ_STRING(const std::filesystem::path &path) {
thisfile.read(&strbuf[0], VEC_BUF_SIZE);
std::cout.write(&strbuf[0], thisfile.gcount());
}
-
+
std::cout << "File size: " << size << std::endl;
thisfile.close();
}
-void NCAT(const std::filesystem::path &path) {
+void ncat(const std::filesystem::path &path) {
FILE *file;
const char *filename = path.c_str();
@@ -139,7 +140,29 @@ void NCAT(const std::filesystem::path &path) {
std::cout << "File size: " << size << std::endl;
fclose(file);
-}
+}
+
+void ncat_write(std::filesystem::path &path) {
+
+ int file;
+ const char *filename = path.c_str();
+ file = open(filename, O_RDONLY);
+ if (file == -1) {
+ fprintf(stderr, "Cannot open file.\n");
+ return;
+ }
+
+ auto size = lseek(file, 0, SEEK_END);
+ lseek(file, 0, SEEK_SET);
+ char buffer[VEC_BUF_SIZE];
+ ssize_t bytes_read;
+ while ((bytes_read = read(file, buffer, VEC_BUF_SIZE)) > 0) {
+ write(0, buffer, bytes_read);
+ }
+
+ std::cout << "File size: " << size << std::endl;
+ close(file);
+}
inline void usageOUT() {
std::cerr << USAGE << std::endl;
@@ -159,46 +182,52 @@ int main (int argc, char **argv) {
int four = 0;
int five = 0;
int six = 0;
- while ((opt = getopt(argc, argv, "123456")) != -1) {
+ int seven = 0;
+ while ((opt = getopt(argc, argv, "1234567")) != -1) {
switch (opt) {
case '1':
one = 1;
-
+
break;
case '2':
two = 1;
-
+
break;
case '3':
three = 1;
-
+
break;
case '4':
four = 1;
-
+
break;
case '5':
-
+
five = 1;
-
+
break;
case '6':
-
+
six = 1;
-
+
+ break;
+ case '7':
+
+ seven = 1;
+
break;
}
}
- if((one + two + three + four + five + six) > 1) {
+ if((one + two + three + four + five + six + seven) > 1) {
usageOUT();
return EXIT_FAILURE;
}
-
+
int index;
std::filesystem::path path;
for (index = optind ; index < argc ; index++) {
@@ -214,7 +243,7 @@ int main (int argc, char **argv) {
PrintFileBUFFERED_SINGLE_CHAR(path);
std::cout << "PrintFileBSC" << std::endl;
}
-
+
if(three == 1) {
PrintFileBUFFERED_READ(path);
std::cout << "PrintFileBR" << std::endl;
@@ -226,7 +255,7 @@ int main (int argc, char **argv) {
}
if(five == 1) {
- NCAT(path);
+ ncat(path);
std::cout << "NCAT" << std::endl;
}
@@ -234,6 +263,11 @@ int main (int argc, char **argv) {
PrintFileBUFFERED_READ_STRING(path);
std::cout << "PrintFileBRS" << std::endl;
}
-
+
+ if(seven == 1) {
+ ncat_write(path);
+ std::cout << "NCAT_WRITE" << std::endl;
+ }
+
return EXIT_SUCCESS;
}
diff --git a/rpf b/rpf
new file mode 100755
index 0000000..5615157
--- /dev/null
+++ b/rpf
Binary files differ