Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : file_copier.c
- Author : Sebastian Rudek
- Version : 0.9
- Description : File Copier. Is able to copy binary files also reversely
- (which basically is nonsens)
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #define BOOL short
- #define FALSE 0
- #define TRUE 1
- /**
- * Copies a file from the source (in_file) to the destination (out_file). Internally, it uses
- * a char buffer to store the data which is read temporarily and write it to a file.
- * a file can be copied reversely, as well, if the boolean "copy_reversely" is set to true.
- * the function will then read the file from the last byte to the first and write it byte-wise.
- * the overloaded variable buffer_size will then be ignored.
- * @param string in_file sourcefile
- * @param string out_file destination
- * @param unsigned long buffer_size size of the buffer, into which parts of the file are read. minimum value: 1 byte
- * @param BOOL copy_reversely
- * @return BOOL True when copy operation succeeds, otherwise false.
- */
- BOOL copy_file(const char* in_file, const char* out_file, unsigned long buffer_size, BOOL copy_reversely);
- /**
- * function provided for convenience.
- * calls the copy_file() function with the additional parameters of buffersize=1 and
- * copy_reversely=true.
- * @param in_file string with the path to the file which should be copied reversely into the
- * @param out_file (string with the path to the output file).
- */
- BOOL copy_file_reversely(const char* in_file, const char* out_file);
- BOOL copy_file(const char* in_file, const char* out_file, unsigned long buffer_size,BOOL copy_reversely)
- {
- int fhandle_in, fhandle_out;
- unsigned long _buffer_size = 0;
- //buffer nicht groesser als 128 kb zulassen
- if(buffer_size < 1 || buffer_size > 131072)
- {
- fprintf(stderr,"Buffersize ist ungueltig\n");
- return FALSE;
- }
- /*wenn von vorne nach hinten kopiert werden soll, dann soll die überladene buffersize
- * ignoriert werden und byteweise (aufgabenstellung) gelesen werden.*/
- if(copy_reversely)
- _buffer_size = 1;
- else
- _buffer_size = buffer_size;
- if((fhandle_in = open(in_file,O_RDONLY)) != -1
- && (fhandle_out = creat (out_file, 0600)) != -1)
- {
- int dateigroesse=0;
- int wieviel_gelesen = 0;
- int i = 0;
- char *buffer = (char*) malloc (_buffer_size);
- if(buffer == NULL)
- {
- fprintf(stderr,"kann den buffer nicht anlegen\n");
- return FALSE;
- }
- /*dateigroesse bestimmen*/
- dateigroesse=lseek(fhandle_in,0L,SEEK_END);
- //wenn das file nicht von hinten kopiert werden soll,
- //dann seitze den fhandle nicht zurück sondern bleibe am schluss
- if(!copy_reversely)
- lseek(fhandle_in,0L,SEEK_SET);
- //solange die laufvariable kleiner der
- //dateigröße ist, also noch nicht das ganze file durch ist...
- while(i < dateigroesse)
- {
- if(copy_reversely)
- {
- lseek(fhandle_in,-(i+1),SEEK_END);
- }
- //...lese daten in den buffer, und zwar immer so viel wie
- //dieser groß ist.
- //die funktion read gibt die zahl der tatsächlich gelesenen
- //bytes zurück und kann im schritt "write" verwendet werden
- //um entsprechend viele bytes korrekt in das file zu schreiben
- wieviel_gelesen = read(fhandle_in,(char*)buffer,_buffer_size);
- write(fhandle_out, (char*)buffer,wieviel_gelesen);
- //die laufvariable muss um den wert von read(), also die gelesenen
- //bytes erhöht werden
- i+=wieviel_gelesen;
- }
- //ein- und ausgabedateien schließen
- close(fhandle_in);close(fhandle_out);
- if(buffer != NULL)
- free(buffer);
- return TRUE;
- }
- else
- {
- fprintf(stderr,"konnte eingabedatei oder ausgabedatei nicht oeffnen\n");
- return FALSE;
- }
- return FALSE;
- }
- BOOL copy_file_reversely(const char* in_file, const char* out_file)
- {
- return copy_file(in_file,out_file, 1,TRUE);
- }
- int main(int argc, char **argv)
- {
- printf("SEBBOS FILE COPIER\n");
- //copy_file("/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer","/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer_copy",1);
- unsigned long bufferSize = 2;
- if(argc <= 3)
- {
- fprintf(stdout,"Usage: file_copier <source> <destination> <buffersize>\n\n"
- "source - string\n"
- "destination - string\n"
- "buffersize - int (max. 131072 bytes)\n\n");
- return EXIT_FAILURE;
- }
- //"/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer"
- //"/home/sebastian/Sem3/BS/workspace/Aufgabe_11/Debug/bloer_copy"
- memcpy((char*)&bufferSize,argv[3],sizeof(int));
- if(copy_file(argv[1],
- argv[2],
- bufferSize,
- TRUE))
- {
- fprintf(stdout,"Dateien erfolgreich kopiert.\n\n");
- }
- else
- {
- fprintf(stderr,"Ein Fehler beim kopieren der Dateien trat auf.\n\n");
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment