[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.10 and 1.11

version 1.10, 2001/01/29 01:58:07 version 1.11, 2001/05/01 13:26:59
Line 479 
Line 479 
 };  };
   
 /*  /*
  * these things had better be contiguous, because we're going to refer to the  
  * end of dirbuf + 1 byte  
  */  
 struct dirent   dirbuf;  
 char            dirdummy;  
   
 /*  
  * return list of file names that match the name in buf.   * return list of file names that match the name in buf.
  * System V version.  listing is a flag indicating whether the   * System V version.  listing is a flag indicating whether the
  * list is being used for printing a listing rather than   * list is being used for printing a listing rather than
Line 499 
Line 492 
         int             listing;          int             listing;
 {  {
         char           *dir, *file, *cp;          char           *dir, *file, *cp;
         int             len, i, preflen;          int             len, preflen;
         int             fp;          DIR            *dirp;
           struct dirent  *dent;
         LIST           *last;          LIST           *last;
         struct filelist *current;          struct filelist *current;
         char            prefixx[NFILEN + 1];          char            prefixx[NFILEN + 1];
Line 576 
Line 570 
          * every file in the directory is relatively expensive.           * every file in the directory is relatively expensive.
          */           */
   
         fp = open(dir, 0);          dirp = opendir(dir);
         if (fp < 0) {          if (dirp == NULL)
                 return (NULL);                  return (NULL);
         }  
         last = NULL;          last = NULL;
         /* clear entry after last so we can treat d_name as ASCIZ */  
         dirbuf.d_name[MAXNAMLEN] = 0;          while ((dent = readdir(dirp)) != NULL) {
         while (1) {                  if (dent->d_namlen < len || memcmp(cp, dent->d_name, len) != 0)
                 if (read(fp, &dirbuf, sizeof(struct dirent)) <= 0) {  
                         break;  
                 }  
                 if (dirbuf.d_ino == 0)  /* entry not allocated */  
                         continue;                          continue;
                 for (i = 0; i < len; ++i) {  
                         if (cp[i] != dirbuf.d_name[i])  
                                 break;  
                 }  
                 if (i < len)  
                         continue;  
                 current = (struct filelist *) malloc(sizeof(struct filelist));                  current = (struct filelist *) malloc(sizeof(struct filelist));
                   if (snprintf(current->fl_name, sizeof(current->fl_name),
                       "%s%s", prefixx, dent->d_name) > sizeof(current->fl_name)) {
                           free(current);
                           continue;
                   }
                 current->fl_l.l_next = last;                  current->fl_l.l_next = last;
                 current->fl_l.l_name = current->fl_name;                  current->fl_l.l_name = current->fl_name;
                 last = (LIST *) current;                  last = (LIST *) current;
                 strcpy(current->fl_name, prefixx);  
                 strcat(current->fl_name, dirbuf.d_name);  
                 if (listing) {                  if (listing) {
                           if (dent->d_type == DT_DIR) {
                                   strcat(current->fl_name, "/");
                                   continue;
                           } else if (dent->d_type != DT_UNKNOWN)
                                   continue;
   
                         statbuf.st_mode = 0;                          statbuf.st_mode = 0;
                         strcpy(statname, dir);                          if (snprintf(statname, sizeof(statname), "%s/%s",
                         strcat(statname, "/");                              dir, dent->d_name) > sizeof(statname) - 1) {
                         strcat(statname, dirbuf.d_name);                                  continue;
                         stat(statname, &statbuf);                          }
                           if (stat(statname, &statbuf) < 0)
                                   continue;
                         if (statbuf.st_mode & 040000)                          if (statbuf.st_mode & 040000)
                                 strcat(current->fl_name, "/");                                  strcat(current->fl_name, "/");
                         else if (statbuf.st_mode & 0100)  
                                 strcat(current->fl_name, "*");  
                 }                  }
         }          }
         close(fp);          closedir(dirp);
   
         return (last);          return (last);
 }  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11