comparison cpy.c @ 7:e5a905df7e0b draft

Uploaded
author david-hoover
date Thu, 04 Oct 2012 14:48:02 -0400
parents c772c8912663
children
comparison
equal deleted inserted replaced
6:cbe8bdc3bdac 7:e5a905df7e0b
262 262
263 int copyFile(char *src, char *dst) 263 int copyFile(char *src, char *dst)
264 { 264 {
265 // printf("Copying %s to %s\n",src,dst); 265 // printf("Copying %s to %s\n",src,dst);
266 266
267 int c; 267 char *c;
268 FILE *fs,*ft; 268 FILE *fs,*ft;
269 struct stat fileStat; 269 struct stat fileStat;
270 size_t st=10000000;
271 int numr,numw;
270 272
271 /* must be able to stat file */ 273 /* must be able to stat file */
272 if (stat(src, &fileStat) < 0) 274 if (stat(src, &fileStat) < 0)
273 { 275 {
274 fprintf(stderr,"%s: No such file\n",src); 276 fprintf(stderr,"%s: No such file\n",src);
290 ft = fopen(dst,"w"); 292 ft = fopen(dst,"w");
291 if (ft==NULL) { 293 if (ft==NULL) {
292 fprintf(stderr,"Can't write to file %s!\n",dst); 294 fprintf(stderr,"Can't write to file %s!\n",dst);
293 exit(1); 295 exit(1);
294 } 296 }
295 297
296 c = getc(fs); 298 c=malloc(st);
297 while (c != EOF) { 299 while(feof(fs)==0)
298 putc(c,ft); 300 {
299 c = getc(fs); 301 if((numr=fread(c,1,st,fs))!=st)
300 } 302 {
303 if(ferror(fs)!=0)
304 {
305 fprintf(stderr,"read file error.\n");
306 exit(1);
307 }
308 else if(feof(fs)!=0);
309 }
310 if((numw=fwrite(c,1,numr,ft))!=numr)
311 {
312 fprintf(stderr,"write file error.\n");
313 exit(1);
314 }
315 }
301 316
302 fclose(fs); 317 fclose(fs);
303 fclose(ft); 318 fclose(ft);
319 free(c);
304 320
305 return 0; 321 return 0;
306 322
307 } 323 }
308 324
360 { 376 {
361 fprintf(stderr,"usage: cpy source dest user\n"); 377 fprintf(stderr,"usage: cpy source dest user\n");
362 exit(1); 378 exit(1);
363 } 379 }
364 380
381 if (!strncmp(argv[2],"/data",5) || !strncmp(argv[2],"/home",5))
382 {
383
365 /* is filepath writeable by given user? */ 384 /* is filepath writeable by given user? */
366 strcpy(newfile,argv[2]); 385 strcpy(newfile,argv[2]);
367 dir = dirname(newfile); 386 dir = dirname(newfile);
368 if (!notWriteable(dir,argv[3])) 387 if (!notWriteable(dir,argv[3]))
369 { 388 {
370 389
371 /* is filepath readable by given user? */ 390 /* is filepath readable by given user? */
372 explodePath(dir,argv[3]); 391 explodePath(dir,argv[3]);
373 392
374 /* dest file must not exist yet */ 393 /* dest file must not exist yet */
375 if (!(stat(argv[2], &fileStat) < 0)) 394 if (!(stat(argv[2], &fileStat) < 0))
376 { 395 {
377 fprintf(stderr,"%s: File already exists.\n",argv[2]); 396 fprintf(stderr,"%s: File already exists.\n",argv[2]);
378 exit(1); 397 exit(1);
379 } 398 }
380 399
381 /* copy the file and change the owner */ 400 /* copy the file and change the owner */
382 copyFile(argv[1],argv[2]); 401 copyFile(argv[1],argv[2]);
383 chownToGivenUser(argv[2],argv[3]); 402 chownToGivenUser(argv[2],argv[3]);
403 }
404 else
405 {
406 fprintf(stderr,"%s: Permission denied.\n",argv[2]);
407 exit(1);
408 }
384 } 409 }
385 else 410 else
386 { 411 {
387 fprintf(stderr,"%s: Permission denied.\n",argv[2]); 412 fprintf(stderr,"%s: Permission denied.\n",argv[2]);
388 exit(1); 413 exit(1);