Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : file_copier.c
  4. Author : Sebastian Rudek
  5. Version : 0.9
  6. Description : File Copier. Is able to copy binary files also reversely
  7. (which basically is nonsens)
  8. ============================================================================
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19.  
  20.  
  21. #define BOOL short
  22. #define FALSE 0
  23. #define TRUE 1
  24.  
  25. /**
  26. * Copies a file from the source (in_file) to the destination (out_file). Internally, it uses
  27. * a char buffer to store the data which is read temporarily and write it to a file.
  28. * a file can be copied reversely, as well, if the boolean "copy_reversely" is set to true.
  29. * the function will then read the file from the last byte to the first and write it byte-wise.
  30. * the overloaded variable buffer_size will then be ignored.
  31. * @param string in_file sourcefile
  32. * @param string out_file destination
  33. * @param unsigned long buffer_size size of the buffer, into which parts of the file are read. minimum value: 1 byte
  34. * @param BOOL copy_reversely
  35. * @return BOOL True when copy operation succeeds, otherwise false.
  36. */
  37. BOOL copy_file(const char* in_file, const char* out_file, unsigned long buffer_size, BOOL copy_reversely);
  38. /**
  39. * function provided for convenience.
  40. * calls the copy_file() function with the additional parameters of buffersize=1 and
  41. * copy_reversely=true.
  42. * @param in_file string with the path to the file which should be copied reversely into the
  43. * @param out_file (string with the path to the output file).
  44. */
  45. BOOL copy_file_reversely(const char* in_file, const char* out_file);
  46.  
  47.  
  48.  
  49. BOOL copy_file(const char* in_file, const char* out_file, unsigned long buffer_size,BOOL copy_reversely)
  50. {
  51. int fhandle_in, fhandle_out;
  52.  
  53. unsigned long _buffer_size = 0;
  54.  
  55. //buffer nicht groesser als 128 kb zulassen
  56. if(buffer_size < 1 || buffer_size > 131072)
  57. {
  58. fprintf(stderr,"Buffersize ist ungueltig\n");
  59. return FALSE;
  60. }
  61.  
  62. /*wenn von vorne nach hinten kopiert werden soll, dann soll die überladene buffersize
  63. * ignoriert werden und byteweise (aufgabenstellung) gelesen werden.*/
  64. if(copy_reversely)
  65. _buffer_size = 1;
  66. else
  67. _buffer_size = buffer_size;
  68.  
  69.  
  70.  
  71. if((fhandle_in = open(in_file,O_RDONLY)) != -1
  72. && (fhandle_out = creat (out_file, 0600)) != -1)
  73. {
  74. int dateigroesse=0;
  75. int wieviel_gelesen = 0;
  76. int i = 0;
  77.  
  78.  
  79. char *buffer = (char*) malloc (_buffer_size);
  80.  
  81. if(buffer == NULL)
  82. {
  83. fprintf(stderr,"kann den buffer nicht anlegen\n");
  84. return FALSE;
  85. }
  86.  
  87.  
  88. /*dateigroesse bestimmen*/
  89. dateigroesse=lseek(fhandle_in,0L,SEEK_END);
  90. //wenn das file nicht von hinten kopiert werden soll,
  91. //dann seitze den fhandle nicht zurück sondern bleibe am schluss
  92. if(!copy_reversely)
  93. lseek(fhandle_in,0L,SEEK_SET);
  94.  
  95. //solange die laufvariable kleiner der
  96. //dateigröße ist, also noch nicht das ganze file durch ist...
  97. while(i < dateigroesse)
  98. {
  99.  
  100. if(copy_reversely)
  101. {
  102. lseek(fhandle_in,-(i+1),SEEK_END);
  103. }
  104.  
  105. //...lese daten in den buffer, und zwar immer so viel wie
  106. //dieser groß ist.
  107. //die funktion read gibt die zahl der tatsächlich gelesenen
  108. //bytes zurück und kann im schritt "write" verwendet werden
  109. //um entsprechend viele bytes korrekt in das file zu schreiben
  110. wieviel_gelesen = read(fhandle_in,(char*)buffer,_buffer_size);
  111. write(fhandle_out, (char*)buffer,wieviel_gelesen);
  112.  
  113. //die laufvariable muss um den wert von read(), also die gelesenen
  114. //bytes erhöht werden
  115. i+=wieviel_gelesen;
  116. }
  117. //ein- und ausgabedateien schließen
  118. close(fhandle_in);close(fhandle_out);
  119.  
  120. if(buffer != NULL)
  121. free(buffer);
  122.  
  123. return TRUE;
  124. }
  125. else
  126. {
  127. fprintf(stderr,"konnte eingabedatei oder ausgabedatei nicht oeffnen\n");
  128.  
  129.  
  130. return FALSE;
  131. }
  132.  
  133.  
  134. return FALSE;
  135. }
  136.  
  137. BOOL copy_file_reversely(const char* in_file, const char* out_file)
  138. {
  139.  
  140. return copy_file(in_file,out_file, 1,TRUE);
  141.  
  142. }
  143.  
  144. int main(int argc, char **argv)
  145. {
  146.  
  147. printf("SEBBOS FILE COPIER\n");
  148.  
  149. //copy_file("/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer","/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer_copy",1);
  150. unsigned long bufferSize = 2;
  151.  
  152. if(argc <= 3)
  153. {
  154. fprintf(stdout,"Usage: file_copier <source> <destination> <buffersize>\n\n"
  155. "source - string\n"
  156. "destination - string\n"
  157. "buffersize - int (max. 131072 bytes)\n\n");
  158. return EXIT_FAILURE;
  159. }
  160.  
  161. //"/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer"
  162. //"/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer_copy"
  163.  
  164. memcpy((char*)&bufferSize,argv[3],sizeof(int));
  165.  
  166. if(copy_file(argv[1],
  167. argv[2],
  168. bufferSize,
  169. TRUE))
  170. {
  171. fprintf(stdout,"Dateien erfolgreich kopiert.\n\n");
  172. }
  173. else
  174. {
  175. fprintf(stderr,"Ein Fehler beim kopieren der Dateien trat auf.\n\n");
  176. }
  177.  
  178.  
  179. return EXIT_SUCCESS;
  180. }
Add Comment
Please, Sign In to add comment