[BACK]Return to mandocdb.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/mandocdb.c between version 1.150 and 1.151

version 1.150, 2014/06/19 00:45:37 version 1.151, 2014/06/20 01:21:48
Line 506  main(int argc, char *argv[])
Line 506  main(int argc, char *argv[])
                                 goto out;                                  goto out;
                         if (0 == treescan())                          if (0 == treescan())
                                 goto out;                                  goto out;
                         if (0 == set_basedir(dirs.paths[j]))  
                                 goto out;  
                         if (0 == dbopen(0))                          if (0 == dbopen(0))
                                 goto out;                                  goto out;
   
Line 2374  static int
Line 2372  static int
 set_basedir(const char *targetdir)  set_basedir(const char *targetdir)
 {  {
         static char      startdir[PATH_MAX];          static char      startdir[PATH_MAX];
         static int       fd;          static int       getcwd_status;  /* 1 = ok, 2 = failure */
           static int       chdir_status;  /* 1 = changed directory */
         char            *cp;          char            *cp;
   
         /*          /*
          * Remember where we started by keeping a fd open to the origin           * Remember the original working directory, if possible.
          * path component: throughout this utility, we chdir() a lot to           * This will be needed if the second or a later directory
          * handle relative paths, and by doing this, we can return to           * on the command line is given as a relative path.
          * the starting point.           * Do not error out if the current directory is not
            * searchable: Maybe it won't be needed after all.
          */           */
         if ('\0' == *startdir) {          if (0 == getcwd_status) {
                 if (NULL == getcwd(startdir, PATH_MAX)) {                  if (NULL == getcwd(startdir, sizeof(startdir))) {
                           getcwd_status = 2;
                           (void)strlcpy(startdir, strerror(errno),
                               sizeof(startdir));
                   } else
                           getcwd_status = 1;
           }
   
           /*
            * We are leaving the old base directory.
            * Do not use it any longer, not even for messages.
            */
           *basedir = '\0';
   
           /*
            * If and only if the directory was changed earlier and
            * the next directory to process is given as a relative path,
            * first go back, or bail out if that is impossible.
            */
           if (chdir_status && '/' != *targetdir) {
                   if (2 == getcwd_status) {
                         exitcode = (int)MANDOCLEVEL_SYSERR;                          exitcode = (int)MANDOCLEVEL_SYSERR;
                         say("", "&getcwd");                          say("", "getcwd: %s", startdir);
                         return(0);                          return(0);
                 }                  }
                 if (-1 == (fd = open(startdir, O_RDONLY, 0))) {                  if (-1 == chdir(startdir)) {
                         exitcode = (int)MANDOCLEVEL_SYSERR;                          exitcode = (int)MANDOCLEVEL_SYSERR;
                         say("", "&open %s", startdir);  
                         return(0);  
                 }  
         } else {  
                 if (-1 == fd)  
                         return(0);  
                 if (-1 == fchdir(fd)) {  
                         close(fd);  
                         basedir[0] = '\0';  
                         exitcode = (int)MANDOCLEVEL_SYSERR;  
                         say("", "&chdir %s", startdir);                          say("", "&chdir %s", startdir);
                         return(0);                          return(0);
                 }                  }
         }          }
   
           /*
            * Always resolve basedir to the canonicalized absolute
            * pathname and append a trailing slash, such that
            * we can reliably check whether files are inside.
            */
         if (NULL == realpath(targetdir, basedir)) {          if (NULL == realpath(targetdir, basedir)) {
                 basedir[0] = '\0';  
                 exitcode = (int)MANDOCLEVEL_BADARG;                  exitcode = (int)MANDOCLEVEL_BADARG;
                 say("", "&%s: realpath", targetdir);                  say("", "&%s: realpath", targetdir);
                 return(0);                  return(0);
Line 2415  set_basedir(const char *targetdir)
Line 2430  set_basedir(const char *targetdir)
                 say("", "&chdir");                  say("", "&chdir");
                 return(0);                  return(0);
         }          }
           chdir_status = 1;
         cp = strchr(basedir, '\0');          cp = strchr(basedir, '\0');
         if ('/' != cp[-1]) {          if ('/' != cp[-1]) {
                 if (cp - basedir >= PATH_MAX - 1) {                  if (cp - basedir >= PATH_MAX - 1) {
Line 2437  say(const char *file, const char *format, ...)
Line 2453  say(const char *file, const char *format, ...)
         if ('\0' != *basedir)          if ('\0' != *basedir)
                 fprintf(stderr, "%s", basedir);                  fprintf(stderr, "%s", basedir);
         if ('\0' != *basedir && '\0' != *file)          if ('\0' != *basedir && '\0' != *file)
                 fputs("//", stderr);                  fputc('/', stderr);
         if ('\0' != *file)          if ('\0' != *file)
                 fprintf(stderr, "%s", file);                  fprintf(stderr, "%s", file);
   

Legend:
Removed from v.1.150  
changed lines
  Added in v.1.151

CVSweb