diff cpy.c @ 7:e5a905df7e0b draft

Uploaded
author david-hoover
date Thu, 04 Oct 2012 14:48:02 -0400
parents c772c8912663
children
line wrap: on
line diff
--- a/cpy.c	Thu Mar 08 09:18:40 2012 -0500
+++ b/cpy.c	Thu Oct 04 14:48:02 2012 -0400
@@ -264,9 +264,11 @@
 {
 //    printf("Copying %s to %s\n",src,dst);
 
-    int c;
+    char *c;
     FILE *fs,*ft;
     struct stat fileStat;
+    size_t st=10000000;
+    int numr,numw;
 
 /* must be able to stat file */
     if (stat(src, &fileStat) < 0)
@@ -292,15 +294,29 @@
         fprintf(stderr,"Can't write to file %s!\n",dst);
         exit(1);
     }
-
-    c = getc(fs);
-    while (c != EOF) {
-        putc(c,ft);
-        c = getc(fs);
-   }
+   
+    c=malloc(st);
+    while(feof(fs)==0)
+    {	
+        if((numr=fread(c,1,st,fs))!=st)
+        {
+            if(ferror(fs)!=0)
+            {
+                fprintf(stderr,"read file error.\n");
+                exit(1);
+            }
+            else if(feof(fs)!=0);
+        }
+        if((numw=fwrite(c,1,numr,ft))!=numr)
+        {
+            fprintf(stderr,"write file error.\n");
+            exit(1);
+        }
+    }	
 
    fclose(fs);
    fclose(ft);
+   free(c);
 
    return 0;
 
@@ -362,25 +378,34 @@
         exit(1);
     }
 
+    if (!strncmp(argv[2],"/data",5) || !strncmp(argv[2],"/home",5))
+    {
+
 /* is filepath writeable by given user? */
-    strcpy(newfile,argv[2]);
-    dir = dirname(newfile);
-    if (!notWriteable(dir,argv[3]))
-    { 
+      strcpy(newfile,argv[2]);
+      dir = dirname(newfile);
+      if (!notWriteable(dir,argv[3]))
+      { 
  
 /* is filepath readable by given user? */
-      explodePath(dir,argv[3]);
+        explodePath(dir,argv[3]);
 
 /* dest file must not exist yet */
-    if (!(stat(argv[2], &fileStat) < 0))
-    {
-        fprintf(stderr,"%s: File already exists.\n",argv[2]);
-        exit(1);
-    }
+      if (!(stat(argv[2], &fileStat) < 0))
+      {
+          fprintf(stderr,"%s: File already exists.\n",argv[2]);
+          exit(1);
+      }
 
 /* copy the file and change the owner */
-      copyFile(argv[1],argv[2]);
-      chownToGivenUser(argv[2],argv[3]);
+        copyFile(argv[1],argv[2]);
+        chownToGivenUser(argv[2],argv[3]);
+      }
+      else 
+      {
+        fprintf(stderr,"%s: Permission denied.\n",argv[2]);
+        exit(1);
+      }
     }
     else 
     {