diff --git a/inclass-cp-example/linux.c b/inclass-cp-example/linux.c new file mode 100644 index 0000000..99c503c --- /dev/null +++ b/inclass-cp-example/linux.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +// cp2 file1 file2 +int main(int argc, char * args[]) +{ + int infile, outfile; + int numofbytesread; + char buffer[20]; + + infile = open(args[1], O_RDONLY, 0700); + if (infile == ENOENT) + { + printf("Could not find file!"); + return 0; + } + + outfile = open(args[2], O_WRONLY | O_CREAT, 0700); + + while((numofbytesread = read(infile, buffer, 20))) + write(outfile, buffer, numofbytesread); + + close(infile); + close(outfile); + + return 0; +} \ No newline at end of file diff --git a/inclass-cp-example/windows.c b/inclass-cp-example/windows.c new file mode 100644 index 0000000..6d1882c --- /dev/null +++ b/inclass-cp-example/windows.c @@ -0,0 +1,36 @@ +#include +#include +#define false 0 +#define true 1 + +// cp2 file1 file2 +int main(int argc, char * args[]) +{ + HANDLE infile; + HANDLE outfile; + DWORD numberofbytesread; + DWORD numberofbyteswritten; + char buffer[20]; + + infile = CreateFileA(args[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (GetLastError() == ERROR_FILE_NOT_FOUND) + { + printf("Could not find file!"); + return 0; + } + + outfile = CreateFileA(args[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + while(true) + { + ReadFile(infile, buffer, 20, &numberofbytesread, NULL); + if (numberofbytesread == 0) + break; + WriteFile(outfile, buffer, numberofbytesread, &numberofbyteswritten, NULL); + } + + CloseHandle(infile); + CloseHandle(outfile); + + return 0; +} \ No newline at end of file