[BACK]Return to fileio.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Diff for /src/usr.bin/mg/fileio.c between version 1.29 and 1.30

version 1.29, 2002/04/22 04:27:37 version 1.30, 2002/04/22 05:27:39
Line 174 
Line 174 
         }          }
         if (stat(fn, &sb) == -1) {          if (stat(fn, &sb) == -1) {
                 ewprintf("Can't stat %s : %s", fn, strerror(errno));                  ewprintf("Can't stat %s : %s", fn, strerror(errno));
                 free(nname);  
                 return (FALSE);                  return (FALSE);
         }          }
   
Line 192 
Line 191 
         }          }
         while ((nread = read(from, buf, sizeof(buf))) > 0) {          while ((nread = read(from, buf, sizeof(buf))) > 0) {
                 if (write(to, buf, nread) != nread) {                  if (write(to, buf, nread) != nread) {
                         nread = -1;                      nread = -1;
                         break;                      break;
                 }                  }
         }          }
         serrno = errno;          serrno = errno;
Line 397 
Line 396 
 int  int
 copy(char *frname, char *toname)  copy(char *frname, char *toname)
 {  {
         pid_t   pid;          int ifd, ofd, n;
         int     status;          char buf[BUFSIZ];
           mode_t mode = DEFFILEMODE;      /* XXX?? */
           struct stat orig;
   
         switch ((pid = vfork())) {          if ((ifd = open(frname, O_RDONLY)) == -1)
         case -1:                  return (FALSE);
                 return -1;          if (fstat(ifd, &orig) == -1) {
         case 0:                  ewprintf("fstat: %s", strerror(errno));
                 execl("/bin/cp", "cp", frname, toname, (char *)NULL);                  close(ifd);
                 _exit(1);       /* shouldn't happen */                  return (FALSE);
         default:  
                 waitpid(pid, &status, 0);  
                 return (WIFEXITED(status) && WEXITSTATUS(status) == 0);  
         }          }
   
           if ((ofd = open(toname, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
                   close(ifd);
                   return (FALSE);
           }
           while ((n = read(ifd, buf, sizeof buf)) > 0) {
                   if (write(ofd, buf, n) != n) {
                           ewprintf("write error : %s", strerror(errno));
                           break;
                   }
           }
           if (fchmod(ofd, orig.st_mode) == -1)
                   ewprintf("Cannot set original mode : %s", strerror(errno));
   
           if (n == -1) {
                   ewprintf("Read error : %s", strerror(errno));
                   close(ifd);
                   close(ofd);
                   return (FALSE);
           }
           /*
            * It is "normal" for this to fail since we can't garantee that
            * we will be running as root
            */
           if (fchown(ofd, orig.st_uid, orig.st_gid) && errno != EPERM)
                   ewprintf("Cannot set owner : %s", strerror(errno));
   
           (void) close(ifd);
           (void) close(ofd);
   
           return (TRUE);
 }  }
   
 /*  /*

Legend:
Removed from v.1.29  
changed lines
  Added in v.1.30