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

Diff for /src/usr.bin/compress/main.c between version 1.103 and 1.104

version 1.103, 2022/10/23 19:06:35 version 1.104, 2022/10/26 00:40:40
Line 488 
Line 488 
         exit(rc);          exit(rc);
 }  }
   
 int  static int
   open_input(const char *in)
   {
           int fd;
   
           fd = pipin ? dup(STDIN_FILENO) : open(in, O_RDONLY);
           if (fd == -1) {
                   if (verbose >= 0)
                           warn("%s", in);
                   return -1;
           }
           if (decomp && !force && isatty(fd)) {
                   if (verbose >= 0)
                           warnx("%s: won't read compressed data from terminal",
                               in);
                   close(fd);
                   return -1;
           }
   
           return fd;
   }
   
   static int
   open_output(const char *out, int *errorp, int *oreg)
   {
           struct stat sb;
           int fd, error = FAILURE;
   
           if (cat) {
                   fd = dup(STDOUT_FILENO);
                   if (fd == -1)
                           goto bad;
                   sb.st_mode = 0;
           } else {
                   fd = open(out, O_WRONLY, S_IWUSR);
                   if (fd != -1) {
                           if (fstat(fd, &sb) == -1)
                                   goto bad;
                           if (!force && S_ISREG(sb.st_mode) && !permission(out)) {
                                   error = WARNING;
                                   goto bad;
                           }
                           if (ftruncate(fd, 0) == -1)
                                   goto bad;
                   } else {
                           fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR);
                           if (fd == -1 || fstat(fd, &sb) == -1)
                                   goto bad;
                   }
           }
   
           *oreg = S_ISREG(sb.st_mode);
           *errorp = SUCCESS;
           return fd;
   bad:
           if (error == FAILURE && verbose >= 0)
                   warn("%s", out);
           if (fd != -1)
                   close(fd);
           *errorp = FAILURE;
           return -1;
   }
   
   static int
 docompress(const char *in, char *out, const struct compressor *method,  docompress(const char *in, char *out, const struct compressor *method,
     int bits, struct stat *sb)      int bits, struct stat *sb)
 {  {
 #ifndef SMALL  #ifndef SMALL
         u_char buf[Z_BUFSIZE];          u_char buf[Z_BUFSIZE];
         char namebuf[PATH_MAX];          char namebuf[PATH_MAX];
         char *name;          char *name = NULL;
         int error, ifd, ofd, oreg;          int error, ifd, ofd, oreg;
         void *cookie;          void *cookie;
         ssize_t nr;          ssize_t nr;
         u_int32_t mtime;          u_int32_t mtime = 0;
         struct z_info info;          struct z_info info;
         struct stat osb;  
   
         mtime = 0;          ifd = open_input(in);
         oreg = 0;          if (ifd == -1)
         error = SUCCESS;  
         name = NULL;  
         cookie  = NULL;  
   
         if (pipin)  
                 ifd = dup(STDIN_FILENO);  
         else  
                 ifd = open(in, O_RDONLY);  
         if (ifd == -1) {  
                 if (verbose >= 0)  
                         warn("%s", in);  
                 return (FAILURE);                  return (FAILURE);
         }  
   
         if (cat)          ofd = open_output(out, &error, &oreg);
                 ofd = dup(STDOUT_FILENO);  
         else {  
                 if (stat(out, &osb) == 0) {  
                         oreg = S_ISREG(osb.st_mode);  
                         if (!force && oreg && !permission(out)) {  
                                 (void) close(ifd);  
                                 return (WARNING);  
                         }  
                 }  
                 ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR);  
         }  
         if (ofd == -1) {          if (ofd == -1) {
                 if (verbose >= 0)                  close(ifd);
                         warn("%s", out);                  return error;
                 (void) close(ifd);  
                 return (FAILURE);  
         }          }
   
         if (method != M_COMPRESS && !force && isatty(ofd)) {          if (method != M_COMPRESS && !force && isatty(ofd)) {
Line 642 
Line 679 
         void *cookie;          void *cookie;
         ssize_t nr;          ssize_t nr;
         struct z_info info;          struct z_info info;
         struct stat osb;  
   
         oreg = 0;          ifd = open_input(in);
         error = SUCCESS;          if (ifd == -1)
         cookie = NULL;                  return (FAILURE);
   
         if (pipin)  
                 ifd = dup(STDIN_FILENO);  
         else  
                 ifd = open(in, O_RDONLY);  
         if (ifd == -1) {  
                 if (verbose >= 0)  
                         warn("%s", in);  
                 return -1;  
         }  
   
         if (!force && isatty(ifd)) {  
                 if (verbose >= 0)  
                         warnx("%s: won't read compressed data from terminal",  
                             in);  
                 close (ifd);  
                 return -1;  
         }  
   
         if ((method = check_method(ifd)) == NULL) {          if ((method = check_method(ifd)) == NULL) {
                 if (verbose >= 0)                  if (verbose >= 0)
                         warnx("%s: unrecognized file format", in);                          warnx("%s: unrecognized file format", in);
Line 692 
Line 710 
                 cat = 0;                        /* XXX should -c override? */                  cat = 0;                        /* XXX should -c override? */
         }          }
   
         if (testmode)          if (testmode) {
                 ofd = -1;                  ofd = -1;
         else {                  oreg = 0;
                 if (cat)                  error = SUCCESS;
                         ofd = dup(STDOUT_FILENO);          } else {
                 else {                  ofd = open_output(out, &error, &oreg);
                         if (stat(out, &osb) == 0) {  
                                 oreg = S_ISREG(osb.st_mode);  
                                 if (!force && oreg && !permission(out)) {  
                                         (void) close(ifd);  
                                         return (WARNING);  
                                 }  
                         }  
                         ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR);  
                         if (ofd != -1)  
                                 oreg = 1;  
                 }  
                 if (ofd == -1) {                  if (ofd == -1) {
                         if (verbose >= 0)  
                                 warn("%s", in);  
                         method->close(cookie, NULL, NULL, NULL);                          method->close(cookie, NULL, NULL, NULL);
                         return (FAILURE);                          return error;
                 }                  }
         }          }
   

Legend:
Removed from v.1.103  
changed lines
  Added in v.1.104