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

Diff for /src/usr.bin/newsyslog/newsyslog.c between version 1.56 and 1.57

version 1.56, 2002/09/19 21:22:59 version 1.57, 2002/09/21 23:19:43
Line 190 
Line 190 
 void child_killer(int);  void child_killer(int);
 void run_command(char *);  void run_command(char *);
 void send_signal(char *, int);  void send_signal(char *, int);
   char *lstat_log(char *, size_t, int);
   int stat_suffix(char *, size_t, char *, struct stat *, int (*)());
   
 int  int
 main(int argc, char **argv)  main(int argc, char **argv)
Line 701 
Line 703 
 dotrim(struct conf_entry *ent)  dotrim(struct conf_entry *ent)
 {  {
         char    file1[MAXPATHLEN], file2[MAXPATHLEN];          char    file1[MAXPATHLEN], file2[MAXPATHLEN];
         char    zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];          char    oldlog[MAXPATHLEN], *suffix;
         char    oldlog[MAXPATHLEN];  
         int     fd;          int     fd;
         struct  stat sb;  
         int     numdays = ent->numlogs;          int     numdays = ent->numlogs;
   
         /* Is there a separate backup dir? */          /* Is there a separate backup dir? */
Line 716 
Line 716 
   
         /* Remove oldest log (may not exist) */          /* Remove oldest log (may not exist) */
         (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);          (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
         (void)snprintf(zfile1, sizeof(zfile1), "%s.%d%s", oldlog, numdays,          (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog, numdays,
             COMPRESS_POSTFIX);              COMPRESS_POSTFIX);
   
         if (noaction) {          if (noaction) {
                 printf("\trm -f %s %s\n", file1, zfile1);                  printf("\trm -f %s %s\n", file1, file2);
         } else {          } else {
                 (void)unlink(file1);                  (void)unlink(file1);
                 (void)unlink(zfile1);                  (void)unlink(file2);
         }          }
   
         /* Move down log files */          /* Move down log files */
         while (numdays--) {          while (numdays--) {
                 (void)strlcpy(file2, file1, sizeof(file2));                  /*
                    * If both the compressed archive or the non-compressed archive
                    * exist, we one or the other based on the CE_COMPACT flag.
                    */
                 (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);                  (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
                 (void)strlcpy(zfile1, file1, sizeof(zfile1));                  suffix = lstat_log(file1, sizeof(file1), ent->flags);
                 (void)strlcpy(zfile2, file2, sizeof(zfile2));                  if (suffix == NULL)
                 if (lstat(file1, &sb)) {                          continue;
                         (void)strlcat(zfile1, COMPRESS_POSTFIX, sizeof(zfile1));                  (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog,
                         (void)strlcat(zfile2, COMPRESS_POSTFIX, sizeof(zfile2));                      numdays + 1, suffix);
                         if (lstat(zfile1, &sb))  
                                 continue;  
                 }  
                 if (noaction) {                  if (noaction) {
                         printf("\tmv %s %s\n", zfile1, zfile2);                          printf("\tmv %s %s\n", file1, file2);
                         printf("\tchmod %o %s\n", ent->permissions, zfile2);                          printf("\tchmod %o %s\n", ent->permissions, file2);
                         if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)                          if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                                 printf("\tchown %u:%u %s\n",                                  printf("\tchown %u:%u %s\n",
                                     ent->uid, ent->gid, zfile2);                                      ent->uid, ent->gid, file2);
                 } else {                  } else {
                         if (rename(zfile1, zfile2))                          if (rename(file1, file2))
                                 warn("can't mv %s to %s", zfile1, zfile2);                                  warn("can't mv %s to %s", file1, file2);
                         if (chmod(zfile2, ent->permissions))                          if (chmod(file2, ent->permissions))
                                 warn("can't chmod %s", zfile2);                                  warn("can't chmod %s", file2);
                         if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)                          if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                                 if (chown(zfile2, ent->uid, ent->gid))                                  if (chown(file2, ent->uid, ent->gid))
                                         warn("can't chown %s", zfile2);                                          warn("can't chown %s", file2);
                 }                  }
         }          }
         if (!noaction && !(ent->flags & CE_BINARY))          if (!noaction && !(ent->flags & CE_BINARY))
Line 780 
Line 781 
                 else if (unlink(ent->log))                  else if (unlink(ent->log))
                         warn("can't rm %s", ent->log);                          warn("can't rm %s", ent->log);
         } else {          } else {
                   (void)snprintf(file1, sizeof(file1), "%s.0", oldlog);
                 if (noaction)                  if (noaction)
                         printf("\tmv %s to %s\n", ent->log, file1);                          printf("\tmv %s to %s\n", ent->log, file1);
                 else if (rename(ent->log, file1))                  else if (rename(ent->log, file1))
Line 855 
Line 857 
 age_old_log(struct conf_entry *ent)  age_old_log(struct conf_entry *ent)
 {  {
         struct stat sb;          struct stat sb;
         char tmp[MAXPATHLEN];          char file[MAXPATHLEN];
   
         if (ent->backdir != NULL)          if (ent->backdir != NULL)
                 snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir, ent->logbase);                  (void)snprintf(file, sizeof(file), "%s/%s.0", ent->backdir,
         else {                      ent->logbase);
                 strlcpy(tmp, ent->log, sizeof(tmp));          else
                 strlcat(tmp, ".0", sizeof(tmp));                  (void)snprintf(file, sizeof(file), "%s.0", ent->log);
           if (ent->flags & CE_COMPACT) {
                   if (stat_suffix(file, sizeof(file), COMPRESS_POSTFIX, &sb,
                       stat) < 0 && stat(file, &sb) < 0)
                           return (-1);
           } else {
                   if (stat(file, &sb) < 0 && stat_suffix(file, sizeof(file),
                       COMPRESS_POSTFIX, &sb, stat) < 0)
                           return (-1);
         }          }
         if (ent->flags & CE_COMPACT)  
                 strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp));  
         if (stat(tmp, &sb) < 0)  
                 return (-1);  
         return ((int)(timenow - sb.st_mtime + 1800) / 3600);          return ((int)(timenow - sb.st_mtime + 1800) / 3600);
 }  }
   
Line 1025 
Line 1031 
         while (waitpid(-1, &status, WNOHANG) > 0)          while (waitpid(-1, &status, WNOHANG) > 0)
                 ;                  ;
         errno = save_errno;          errno = save_errno;
   }
   
   int
   stat_suffix(char *file, size_t size, char *suffix, struct stat *sp, int (*func)())
   {
           size_t n;
   
           n = strlcat(file, suffix, size);
           if (n < size && func(file, sp) == 0)
                   return (0);
           file[n - strlen(suffix)] = '\0';
           return (-1);
   }
   
   /*
    * lstat() a log, possibily appending a suffix; order is based on flags.
    * Returns the suffix appended (may be empty string) or NULL if no file.
    */
   char *
   lstat_log(char *file, size_t size, int flags)
   {
           struct stat sb;
   
           if (flags & CE_COMPACT) {
                   if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                           return (COMPRESS_POSTFIX);
                   if (lstat(file, &sb) == 0)
                           return ("");
           } else {
                   if (lstat(file, &sb) == 0)
                           return ("");
                   if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                           return (COMPRESS_POSTFIX);
   
           }
           return (NULL);
 }  }

Legend:
Removed from v.1.56  
changed lines
  Added in v.1.57