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

Diff for /src/usr.bin/mg/dir.c between version 1.19 and 1.20

version 1.19, 2008/06/13 20:07:40 version 1.20, 2012/11/27 19:46:46
Line 9 
Line 9 
  *              Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987   *              Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
  */   */
   
   #include <sys/stat.h>
   
 #include "def.h"  #include "def.h"
   
 static char      mgcwd[NFILEN];  static char      mgcwd[NFILEN];
Line 73 
Line 75 
         if (strlcpy(buf, mgcwd, len) >= len)          if (strlcpy(buf, mgcwd, len) >= len)
                 return (FALSE);                  return (FALSE);
   
           return (TRUE);
   }
   
   /* Create the directory and it's parents. */
   /* ARGSUSED */
   int
   makedir(int f, int n)
   {
           struct stat      sb;
           int              finished, ishere;
           mode_t           dir_mode, mode, oumask;
           char             bufc[NFILEN], path[MAXPATHLEN];
           char            *slash, *tpath, *fpath;
   
           if (getbufcwd(bufc, sizeof(bufc)) != TRUE)
                   return (ABORT);
           if ((tpath = eread("Make directory: ", bufc, NFILEN,
               EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
                   return (ABORT);
           else if (tpath[0] == '\0')
                   return (FALSE);
   
           if ((fpath = expandtilde(tpath)) == NULL)
                   return (FALSE);
   
           (void)strlcpy(path, fpath, sizeof(path));
           free(fpath);
   
           slash = path;
           oumask = umask(0);
           mode = 0777 & ~oumask;
           dir_mode = mode | S_IWUSR | S_IXUSR;
   
           for (;;) {
                   slash += strspn(slash, "/");
                   slash += strcspn(slash, "/");
   
                   finished = (*slash == '\0');
                   *slash = '\0';
   
                   ishere = !stat(path, &sb);
                   if (!finished && ishere && S_ISDIR(sb.st_mode)) {
                           *slash = '/';
                           continue;
                   }
   
                   if (mkdir(path, finished ? mode : dir_mode) == 0) {
                           if (mode > 0777 && chmod(path, mode) < 0) {
                                   umask(oumask);
                                   return (ABORT);
                           }
                   } else {
                           if (!ishere || !S_ISDIR(sb.st_mode)) {
                                   if (!ishere)
                                           ewprintf("Creating directory: "
                                               "permission denied, %s", path);
                                   else
                                           eerase();
   
                                   umask(oumask);
                                   return (FALSE);
                           }
                   }
   
                   if (finished)
                           break;
   
                   *slash = '/';
           }
   
           eerase();
           umask(oumask);
         return (TRUE);          return (TRUE);
 }  }

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.20