[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.91 and 1.92

version 1.91, 2012/06/14 17:21:22 version 1.92, 2012/06/15 17:52:42
Line 24 
Line 24 
   
 static char *bkuplocation(const char *);  static char *bkuplocation(const char *);
 static int   bkupleavetmp(const char *);  static int   bkupleavetmp(const char *);
   char        *expandtilde(const char *);
   
 static char *bkupdir;  static char *bkupdir;
 static int   leavetmp = 0;      /* 1 = leave any '~' files in tmp dir */  static int   leavetmp = 0;      /* 1 = leave any '~' files in tmp dir */
Line 281 
Line 282 
 char *  char *
 adjustname(const char *fn, int slashslash)  adjustname(const char *fn, int slashslash)
 {  {
         struct stat      statbuf;  
         static char      fnb[MAXPATHLEN];          static char      fnb[MAXPATHLEN];
         const char      *cp, *ep = NULL;          const char      *cp, *ep = NULL;
         char             user[LOGIN_NAME_MAX], path[MAXPATHLEN];          char            *path;
         size_t           ulen, plen;  
   
         path[0] = '\0';  
   
         if (slashslash == TRUE) {          if (slashslash == TRUE) {
                 cp = fn + strlen(fn) - 1;                  cp = fn + strlen(fn) - 1;
                 for (; cp >= fn; cp--) {                  for (; cp >= fn; cp--) {
Line 302 
Line 299 
                                 ep = NULL;                                  ep = NULL;
                 }                  }
         }          }
           if ((path = expandtilde(fn)) == NULL)
         /*  
          * Next, expand file names beginning with '~', if appropriate:  
          *   1, if ./~fn exists, continue without expanding tilde.  
          *   2, otherwise, if username 'fn' exists, expand tilde with home  
          *      directory path.  
          *   3, otherwise, continue and create new buffer called ~fn.  
          */  
         if (fn[0] == '~' && stat(fn, &statbuf) != 0) {  
                 struct passwd *pw;  
   
                 cp = strchr(fn, '/');  
                 if (cp == NULL)  
                         cp = fn + strlen(fn); /* point to the NUL byte */  
                 ulen = cp - &fn[1];  
                 if (ulen >= sizeof(user)) {  
                         ewprintf("Login name too long");  
                         return (NULL);  
                 }  
                 if (ulen == 0) /* ~/ or ~ */  
                         (void)strlcpy(user, getlogin(), sizeof(user));  
                 else { /* ~user/ or ~user */  
                         memcpy(user, &fn[1], ulen);  
                         user[ulen] = '\0';  
                 }  
                 pw = getpwnam(user);  
                 if (pw != NULL) {  
                         plen = strlcpy(path, pw->pw_dir, sizeof(path));  
                         if (plen == 0 || path[plen - 1] != '/') {  
                                 if (strlcat(path, "/", sizeof(path)) >=  
                                     sizeof(path)) {  
                                         ewprintf("Path too long");  
                                         return (NULL);  
                                 }  
                         }  
                         fn = cp;  
                         if (*fn == '/')  
                                 fn++;  
                 }  
         }  
         if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {  
                 ewprintf("Path too long");  
                 return (NULL);                  return (NULL);
         }  
   
         if (realpath(path, fnb) == NULL)          if (realpath(path, fnb) == NULL)
                 (void)strlcpy(fnb, path, sizeof(fnb));                  (void)strlcpy(fnb, path, sizeof(fnb));
   
           free(path);
         return (fnb);          return (fnb);
 }  }
   
Line 730 
Line 686 
                 return (TRUE);                  return (TRUE);
   
         return (FALSE);          return (FALSE);
   }
   
   /*
    * Expand file names beginning with '~' if appropriate:
    *   1, if ./~fn exists, continue without expanding tilde.
    *   2, else, if username 'fn' exists, expand tilde with home directory path.
    *   3, otherwise, continue and create new buffer called ~fn.
    */
   char *
   expandtilde(const char *fn)
   {
           struct passwd   *pw;
           struct stat      statbuf;
           const char      *cp;
           char             user[LOGIN_NAME_MAX], path[NFILEN], *ret;
           size_t           ulen, plen;
   
           path[0] = '\0';
   
           if (fn[0] != '~' || stat(fn, &statbuf) == 0) {
                   if ((ret = strndup(fn, NFILEN)) == NULL)
                           return (NULL);
                   return(ret);
           }
           cp = strchr(fn, '/');
           if (cp == NULL)
                   cp = fn + strlen(fn); /* point to the NUL byte */
           ulen = cp - &fn[1];
           if (ulen >= sizeof(user)) {
                   if ((ret = strndup(fn, NFILEN)) == NULL)
                           return (NULL);
                   return(ret);
           }
           if (ulen == 0) /* ~/ or ~ */
                   (void)strlcpy(user, getlogin(), sizeof(user));
           else { /* ~user/ or ~user */
                   memcpy(user, &fn[1], ulen);
                   user[ulen] = '\0';
           }
           pw = getpwnam(user);
           if (pw != NULL) {
                   plen = strlcpy(path, pw->pw_dir, sizeof(path));
                   if (plen == 0 || path[plen - 1] != '/') {
                           if (strlcat(path, "/", sizeof(path)) >= sizeof(path)) {
                                   ewprintf("Path too long");
                                   return (NULL);
                           }
                   }
                   fn = cp;
                   if (*fn == '/')
                           fn++;
           }
           if (strlcat(path, fn, sizeof(path)) >= sizeof(path)) {
                   ewprintf("Path too long");
                   return (NULL);
           }
           if ((ret = strndup(path, NFILEN)) == NULL)
                   return (NULL);
   
           return (ret);
 }  }

Legend:
Removed from v.1.91  
changed lines
  Added in v.1.92