diff --git a/cp-Linux/Makefile b/cp-Linux/Makefile new file mode 100644 index 0000000..87c3bdf --- /dev/null +++ b/cp-Linux/Makefile @@ -0,0 +1,90 @@ +#------------------------------------------------------------------------------# +# This makefile was generated by 'cbp2make' tool rev.147 # +#------------------------------------------------------------------------------# + + +WORKDIR = `pwd` + +CC = gcc +CXX = g++ +AR = ar +LD = g++ +WINDRES = windres + +INC = +CFLAGS = -Wall -fexceptions +RESINC = +LIBDIR = +LIB = +LDFLAGS = + +INC_DEBUG = $(INC) +CFLAGS_DEBUG = $(CFLAGS) -g +RESINC_DEBUG = $(RESINC) +RCFLAGS_DEBUG = $(RCFLAGS) +LIBDIR_DEBUG = $(LIBDIR) +LIB_DEBUG = $(LIB) +LDFLAGS_DEBUG = $(LDFLAGS) +OBJDIR_DEBUG = obj/Debug +DEP_DEBUG = +OUT_DEBUG = bin/Debug/cp-Linux + +INC_RELEASE = $(INC) +CFLAGS_RELEASE = $(CFLAGS) -O2 +RESINC_RELEASE = $(RESINC) +RCFLAGS_RELEASE = $(RCFLAGS) +LIBDIR_RELEASE = $(LIBDIR) +LIB_RELEASE = $(LIB) +LDFLAGS_RELEASE = $(LDFLAGS) -s +OBJDIR_RELEASE = obj/Release +DEP_RELEASE = +OUT_RELEASE = bin/Release/cp-Linux + +OBJ_DEBUG = $(OBJDIR_DEBUG)/main.o + +OBJ_RELEASE = $(OBJDIR_RELEASE)/main.o + +all: debug release + +clean: clean_debug clean_release + +before_debug: + test -d bin/Debug || mkdir -p bin/Debug + test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG) + +after_debug: + +debug: before_debug out_debug after_debug + +out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG) + $(LD) $(LIBDIR_DEBUG) -o $(OUT_DEBUG) $(OBJ_DEBUG) $(LDFLAGS_DEBUG) $(LIB_DEBUG) + +$(OBJDIR_DEBUG)/main.o: main.cpp + $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c main.cpp -o $(OBJDIR_DEBUG)/main.o + +clean_debug: + rm -f $(OBJ_DEBUG) $(OUT_DEBUG) + rm -rf bin/Debug + rm -rf $(OBJDIR_DEBUG) + +before_release: + test -d bin/Release || mkdir -p bin/Release + test -d $(OBJDIR_RELEASE) || mkdir -p $(OBJDIR_RELEASE) + +after_release: + +release: before_release out_release after_release + +out_release: before_release $(OBJ_RELEASE) $(DEP_RELEASE) + $(LD) $(LIBDIR_RELEASE) -o $(OUT_RELEASE) $(OBJ_RELEASE) $(LDFLAGS_RELEASE) $(LIB_RELEASE) + +$(OBJDIR_RELEASE)/main.o: main.cpp + $(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c main.cpp -o $(OBJDIR_RELEASE)/main.o + +clean_release: + rm -f $(OBJ_RELEASE) $(OUT_RELEASE) + rm -rf bin/Release + rm -rf $(OBJDIR_RELEASE) + +.PHONY: before_debug after_debug clean_debug before_release after_release clean_release + diff --git a/cp-Linux/bin/Debug/test.txt b/cp-Linux/bin/Debug/test.txt new file mode 100644 index 0000000..8d40480 --- /dev/null +++ b/cp-Linux/bin/Debug/test.txt @@ -0,0 +1 @@ +abv34asdfasdfasdfdsf123456 \ No newline at end of file diff --git a/cp-Linux/bin/Debug/test2.txt b/cp-Linux/bin/Debug/test2.txt new file mode 100644 index 0000000..8d40480 --- /dev/null +++ b/cp-Linux/bin/Debug/test2.txt @@ -0,0 +1 @@ +abv34asdfasdfasdfdsf123456 \ No newline at end of file diff --git a/cp-Linux/cp-Linux.cbp b/cp-Linux/cp-Linux.cbp new file mode 100644 index 0000000..e1d50fd --- /dev/null +++ b/cp-Linux/cp-Linux.cbp @@ -0,0 +1,43 @@ + + + + + + diff --git a/cp-Linux/main.cpp b/cp-Linux/main.cpp new file mode 100644 index 0000000..b823a05 --- /dev/null +++ b/cp-Linux/main.cpp @@ -0,0 +1,55 @@ +#include // read, write +#include // ENOENT +#include // O_RDONLY and O_WRONLY + +void WriteStr(const char * str) +{ + // A simple wrapper to output text using Write + int length = 0; + const char * cpyptr = str; + while(*cpyptr++) // This doesn't work the way you would expect it + length++; + write(1, str, length); +} + +class FileWrapper +{ +private: + int _handle; + +public: + FileWrapper(const char * file, int flags) { this->_handle = open(file, flags); } + ~FileWrapper() { close(this->_handle); } + int getHandle() { return this->_handle; } +}; + +int main(int argc, char * args[]) +{ + // Parameter checking + if (argc != 3) + { + WriteStr("Please enter two parameters - cp src dst"); + return 0; + } + + // Open the source + // A little RAII + FileWrapper src(args[1], O_RDONLY); + if (src.getHandle() == ENOENT) + { + WriteStr("Could not find file!"); + return 0; + } + + FileWrapper dst(args[2], O_WRONLY | O_CREAT); + + int numofbytesread; + char buffer[20]; + + // Stream the file instead of reading it into memory + // The buffer parameter is a void pointer which means it can take anything - in this case we just use a char array + while ((numofbytesread = read(src.getHandle(), buffer, 20))) + write(dst.getHandle(), buffer, numofbytesread); + + return 0; +} diff --git a/cp-test/Debug/test.txt b/cp-test/Debug/test.txt new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/cp-test/Debug/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/cp-test/cp-test.sln b/cp-test/cp-test.sln new file mode 100644 index 0000000..174283a --- /dev/null +++ b/cp-test/cp-test.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cp-test", "cp-test\cp-test.vcxproj", "{95FF3465-32A5-4FF2-941C-4D7A163019A1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {95FF3465-32A5-4FF2-941C-4D7A163019A1}.Debug|Win32.ActiveCfg = Debug|Win32 + {95FF3465-32A5-4FF2-941C-4D7A163019A1}.Debug|Win32.Build.0 = Debug|Win32 + {95FF3465-32A5-4FF2-941C-4D7A163019A1}.Release|Win32.ActiveCfg = Release|Win32 + {95FF3465-32A5-4FF2-941C-4D7A163019A1}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/cp/cp/main.cpp b/cp/cp/main.cpp index 765340e..a651ac0 100644 --- a/cp/cp/main.cpp +++ b/cp/cp/main.cpp @@ -7,7 +7,7 @@ void WriteStr(const char * str) DWORD byteswritten; int length = 0; const char * cpyptr = str; - while(*cpyptr++) + while(*cpyptr++) // This doesn't work the way you would expect it length++; WriteFile(stdout2, str, length, &byteswritten, NULL); } @@ -19,7 +19,7 @@ private: public: FileWrapper(const char * file, DWORD access, DWORD creation) { this->_handle = CreateFileA(file, access, 0, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL); } - ~FileWrapper() { CloseHandle(this->_handle); } + ~FileWrapper() { CloseHandle(this->_handle); } // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx HANDLE getHandle() { return this->_handle; } }; @@ -52,7 +52,8 @@ int main(int argc, char * args[]) while(true) { // The buffer parameter is a void pointer which means it can take anything - in this case we just use a char array - ReadFile(src.getHandle(), buffer, 20, &numberofbytesread, NULL); + // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx + ReadFile(src.getHandle(), buffer, 20, &numberofbytesread, NULL); // http://stackoverflow.com/questions/12655120/readfile-function-from-win32-api if (numberofbytesread == 0) break; WriteFile(dst.getHandle(), buffer, numberofbytesread, &numberofbyteswritten, NULL);