#include #include #include #include #include #include #include char * progname = "CP-NG"; char * version = "CP-NewGeneration-0.01a"; char * dst_path; char * src_path; int buffer; void *RunCopy( void *ptr ); void usage(char *progname) { fprintf(stderr, "%s\n" "by Maciej \"warlock\" Drobniuch\n" "maciej[ _at_ ]drobniuch.pl\n" "Usage:\n" " %s []\n" "\n" "Available cp-ng options:\n" " --version Display version number and exit\n" " --src Define the source path/file\n" " --dst Define the destination path/file\n" " --prompt Prompt in case of overwrite/resume (default y)\n" " --progress Show progress [%]\n" " --pb Show a progress bar\n" " --max Set the maximum transfer rate(in kbytes)\n" " --buffer Set size of the copy buffer (default 6440)\n", version, progname, progname); exit(1); } void CopyFile() { char * dst; char * src; if( src_path != "" & dst_path != "") { FILE *plik1; FILE *plik2; src = src_path; dst = dst_path; int f_size, f_dst, pos, x; char buffer [6440]; plik1 = fopen(src,"r"); plik2 = fopen(dst, "a+"); struct stat stat_p; struct stat stat_d; stat(src, &stat_p); stat(dst, &stat_d); f_size = stat_p.st_size; f_dst = stat_d.st_size; pos = f_dst; x = 6430; while(pos != f_size) { if(f_size < x) { x = f_size; } else if ((f_size-pos)<6431) { x = f_size-pos; } fseek(plik1, pos, SEEK_SET); fread(&buffer, x, 1, plik1); fwrite(&buffer, x,1, plik2); pos += x; } fclose(plik1); fclose(plik2); src_path = ""; dst_path = ""; pthread_exit(0); } } main(int argc, char *argv[], char *envp[]) { char *message1 = "Thread 1"; printf("Starting..."); while(1){ static struct option long_options[] = { {"src", 1, 0, 0}, {"dst", 1, 0, 0}, {"prompt", 1, 0, 0}, {"progress", 0, 0, 0}, {"pb", 0, 0, 0}, {"max", 1, 0, 0}, {"buffer", 1, 0, 0}, {"version", 0, 0, 0}, {"help", 0, 0, 0}, {0, 0, 0, 0} }; int option_index = 0; int c; c = getopt_long (argc, argv, "", long_options, &option_index); if( c == -1 ) { if( argc == 1 ) { usage(argv[0]); } break; } switch( c ) { case 0: if(long_options[option_index].name == "help") { usage(argv[0]); } else if(long_options[option_index].name == "src") { src_path = optarg; } else if(long_options[option_index].name == "dst") { dst_path = optarg; } else if(long_options[option_index].name == "prompt") { printf("prompt %s", optarg); } else if(long_options[option_index].name == "progress") { printf("progress %s", optarg); } else if(long_options[option_index].name == "pb") { printf("pb %s", optarg); } else if(long_options[option_index].name == "max") { printf("max %s", optarg); } else if(long_options[option_index].name == "buffer") { printf("buffer %s", optarg); } else if(long_options[option_index].name == "version") { printf("version %s", optarg); printf ("\n"); break; case '0': case '1': case '?': usage(argv[0]); break; default: usage(argv[0]); } } } pthread_t thread1; pthread_create( &thread1, NULL, RunCopy, (void*) message1); pthread_join( thread1, NULL); printf("Done.\n"); } void *RunCopy( void *ptr ) { if (dst_path != "" & src_path != "") { CopyFile(); } }