add cpio to subdirectory list
[unix-history] / usr / src / bin / csh / file.c
index 81f13b1..447fcc2 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char *sccsid = "@(#)file.c      5.2 (Berkeley) %G%";
+static char *sccsid = "@(#)file.c      5.8 (Berkeley) %G%";
 #endif
 
 #ifdef FILEC
 #endif
 
 #ifdef FILEC
@@ -30,11 +30,12 @@ static char *sccsid = "@(#)file.c   5.2 (Berkeley) %G%";
 
 typedef enum {LIST, RECOGNIZE} COMMAND;
 
 
 typedef enum {LIST, RECOGNIZE} COMMAND;
 
-static struct tchars  tchars;          /* INT, QUIT, XON, XOFF, EOF, BRK */
+int    sortscmp();                     /* defined in sh.glob.c */
 
 /*
  * Put this here so the binary can be patched with adb to enable file
 
 /*
  * Put this here so the binary can be patched with adb to enable file
- * completion by default.
+ * completion by default.  Filec controls completion, nobeep controls
+ * ringing the terminal bell on incomplete expansions.
  */
 bool filec = 0;
 
  */
 bool filec = 0;
 
@@ -43,19 +44,16 @@ setup_tty(on)
        int on;
 {
        struct sgttyb sgtty;
        int on;
 {
        struct sgttyb sgtty;
-       int omask;
+       static struct tchars tchars;    /* INT, QUIT, XON, XOFF, EOF, BRK */
 
 
-       omask = sigblock(sigmask(SIGINT));
        if (on) {
                (void) ioctl(SHIN, TIOCGETC, (char *)&tchars);
                tchars.t_brkc = ESC;
                (void) ioctl(SHIN, TIOCSETC, (char *)&tchars);
                /*
        if (on) {
                (void) ioctl(SHIN, TIOCGETC, (char *)&tchars);
                tchars.t_brkc = ESC;
                (void) ioctl(SHIN, TIOCSETC, (char *)&tchars);
                /*
-                * This is a useful feature in it's own right...
-                * The shell makes sure that the tty is not in some weird state
-                * and fixes it if it is.  But it should be noted that the
-                * tenex routine will not work correctly in CBREAK or RAW mode
-                * so this code below is, therefore, mandatory.
+                * This must be done after every command: if
+                * the tty gets into raw or cbreak mode the user
+                * can't even type 'reset'.
                 */
                (void) ioctl(SHIN, TIOCGETP, (char *)&sgtty);
                if (sgtty.sg_flags & (RAW|CBREAK)) {
                 */
                (void) ioctl(SHIN, TIOCGETP, (char *)&sgtty);
                if (sgtty.sg_flags & (RAW|CBREAK)) {
@@ -66,7 +64,6 @@ setup_tty(on)
                tchars.t_brkc = -1;
                (void) ioctl(SHIN, TIOCSETC, (char *)&tchars);
        }
                tchars.t_brkc = -1;
                (void) ioctl(SHIN, TIOCSETC, (char *)&tchars);
        }
-       (void) sigsetmask (omask);
 }
 
 /*
 }
 
 /*
@@ -76,7 +73,7 @@ static
 back_to_col_1()
 {
        struct sgttyb tty, tty_normal;
 back_to_col_1()
 {
        struct sgttyb tty, tty_normal;
-       int omask;
+       long omask;
 
        omask = sigblock(sigmask(SIGINT));
        (void) ioctl(SHIN, TIOCGETP, (char *)&tty);
 
        omask = sigblock(sigmask(SIGINT));
        (void) ioctl(SHIN, TIOCGETP, (char *)&tty);
@@ -97,7 +94,7 @@ pushback(string)
 {
        register char *p;
        struct sgttyb tty, tty_normal;
 {
        register char *p;
        struct sgttyb tty, tty_normal;
-       int omask;
+       long omask;
 
        omask = sigblock(sigmask(SIGINT));
        (void) ioctl(SHOUT, TIOCGETP, (char *)&tty);
 
        omask = sigblock(sigmask(SIGINT));
        (void) ioctl(SHOUT, TIOCGETP, (char *)&tty);
@@ -116,6 +113,7 @@ pushback(string)
  * Des is a string whose maximum length is count.
  * Always null terminate.
  */
  * Des is a string whose maximum length is count.
  * Always null terminate.
  */
+static
 catn(des, src, count)
        register char *des, *src;
        register count;
 catn(des, src, count)
        register char *des, *src;
        register count;
@@ -129,17 +127,11 @@ catn(des, src, count)
        *des = '\0';
 }
 
        *des = '\0';
 }
 
-static
-max(a, b)
-{
-
-       return (a > b ? a : b);
-}
-
 /*
  * Like strncpy but always leave room for trailing \0
  * and always null terminate.
  */
 /*
  * Like strncpy but always leave room for trailing \0
  * and always null terminate.
  */
+static
 copyn(des, src, count)
        register char *des, *src;
        register count;
 copyn(des, src, count)
        register char *des, *src;
        register count;
@@ -151,29 +143,30 @@ copyn(des, src, count)
        *des = '\0';
 }
 
        *des = '\0';
 }
 
-/*
- * For qsort()
- */
-static
-fcompare(file1, file2)
-       char **file1, **file2;
-{
-
-        return (strcmp(*file1, *file2));
-}
-
 static char
 filetype(dir, file)
        char *dir, *file;
 {
 static char
 filetype(dir, file)
        char *dir, *file;
 {
-       char path[512];
+       char path[MAXPATHLEN];
        struct stat statb;
 
        struct stat statb;
 
-       if (dir) {
-               catn(strcpy(path, dir), file, sizeof path);
-               if (stat(path, &statb) >= 0) {
-                       if (statb.st_mode & S_IFDIR)
-                               return ('/');
+       catn(strcpy(path, dir), file, sizeof path);
+       if (lstat(path, &statb) == 0) {
+               switch(statb.st_mode & S_IFMT) {
+                   case S_IFDIR:
+                       return ('/');
+
+                   case S_IFLNK:
+                       if (stat(path, &statb) == 0 &&      /* follow it out */
+                          (statb.st_mode & S_IFMT) == S_IFDIR)
+                               return ('>');
+                       else
+                               return ('@');
+
+                   case S_IFSOCK:
+                       return ('=');
+
+                   default:
                        if (statb.st_mode & 0111)
                                return ('*');
                }
                        if (statb.st_mode & 0111)
                                return ('*');
                }
@@ -181,6 +174,8 @@ filetype(dir, file)
        return (' ');
 }
 
        return (' ');
 }
 
+static struct winsize win;
+
 /*
  * Print sorted down columns
  */
 /*
  * Print sorted down columns
  */
@@ -190,10 +185,14 @@ print_by_column(dir, items, count)
 {
        register int i, rows, r, c, maxwidth = 0, columns;
 
 {
        register int i, rows, r, c, maxwidth = 0, columns;
 
+       if (ioctl(SHOUT, TIOCGWINSZ, (char *)&win) < 0 || win.ws_col == 0)
+               win.ws_col = 80;
        for (i = 0; i < count; i++)
        for (i = 0; i < count; i++)
-               maxwidth = max(maxwidth, strlen(items[i]));
+               maxwidth = maxwidth > (r = strlen(items[i])) ? maxwidth : r;
        maxwidth += 2;                  /* for the file tag and space */
        maxwidth += 2;                  /* for the file tag and space */
-       columns = 78 / maxwidth;
+       columns = win.ws_col / maxwidth;
+       if (columns == 0)
+               columns = 1;
        rows = (count + (columns - 1)) / columns;
        for (r = 0; r < rows; r++) {
                for (c = 0; c < columns; c++) {
        rows = (count + (columns - 1)) / columns;
        for (r = 0; r < rows; r++) {
                for (c = 0; c < columns; c++) {
@@ -202,15 +201,15 @@ print_by_column(dir, items, count)
                                register int w;
 
                                printf("%s", items[i]);
                                register int w;
 
                                printf("%s", items[i]);
-                               putchar(filetype(dir, items[i]));
+                               cshputchar(dir ? filetype(dir, items[i]) : ' ');
                                if (c < columns - 1) {  /* last column? */
                                        w = strlen(items[i]) + 1;
                                        for (; w < maxwidth; w++)
                                if (c < columns - 1) {  /* last column? */
                                        w = strlen(items[i]) + 1;
                                        for (; w < maxwidth; w++)
-                                               printf(" ");
+                                               cshputchar(' ');
                                }
                        }
                }
                                }
                        }
                }
-               printf ("\n");
+               cshputchar('\n');
        }
 }
 
        }
 }
 
@@ -220,7 +219,7 @@ print_by_column(dir, items, count)
  * expands to
  *     home_directory_of_person/mumble
  */
  * expands to
  *     home_directory_of_person/mumble
  */
-char *
+static char *
 tilde(new, old)
        char *new, *old;
 {
 tilde(new, old)
        char *new, *old;
 {
@@ -261,7 +260,8 @@ static
 beep()
 {
 
 beep()
 {
 
-       (void) write(SHOUT, "\07", 1);
+       if (adrof("nobeep") == 0)
+               (void) write(SHOUT, "\007", 1);
 }
 
 /*
 }
 
 /*
@@ -311,7 +311,7 @@ extract_dir_and_name(path, dir, name)
        }
 }
 
        }
 }
 
-char *
+static char *
 getentry(dir_fd, looking_for_lognames)
        DIR *dir_fd;
 {
 getentry(dir_fd, looking_for_lognames)
        DIR *dir_fd;
 {
@@ -319,7 +319,7 @@ getentry(dir_fd, looking_for_lognames)
        register struct direct *dirp;
 
        if (looking_for_lognames) {
        register struct direct *dirp;
 
        if (looking_for_lognames) {
-               if ((pw = getpwent ()) == NULL)
+               if ((pw = getpwent()) == NULL)
                        return (NULL);
                return (pw->pw_name);
        }
                        return (NULL);
                return (pw->pw_name);
        }
@@ -340,7 +340,7 @@ free_items(items)
 }
 
 #define FREE_ITEMS(items) { \
 }
 
 #define FREE_ITEMS(items) { \
-       int omask;\
+       long omask;\
 \
        omask = sigblock(sigmask(SIGINT));\
        free_items(items);\
 \
        omask = sigblock(sigmask(SIGINT));\
        free_items(items);\
@@ -358,7 +358,8 @@ search(word, command, max_word_length)
 {
        static char **items = NULL;
        register DIR *dir_fd;
 {
        static char **items = NULL;
        register DIR *dir_fd;
-       register numitems, name_length, looking_for_lognames;
+       register numitems = 0, ignoring = TRUE, nignored = 0;
+       register name_length, looking_for_lognames;
        char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1];
        char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN+1];
        char *entry;
        char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1];
        char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN+1];
        char *entry;
@@ -379,6 +380,8 @@ search(word, command, max_word_length)
                if (dir_fd == NULL)
                        return (0);
        }
                if (dir_fd == NULL)
                        return (0);
        }
+
+again: /* search for matches */
        name_length = strlen(name);
        for (numitems = 0; entry = getentry(dir_fd, looking_for_lognames); ) {
                if (!is_prefix(name, entry))
        name_length = strlen(name);
        for (numitems = 0; entry = getentry(dir_fd, looking_for_lognames); ) {
                if (!is_prefix(name, entry))
@@ -400,16 +403,31 @@ search(word, command, max_word_length)
                        items[numitems] = xalloc((unsigned)strlen(entry) + 1);
                        copyn(items[numitems], entry, MAXNAMLEN);
                        numitems++;
                        items[numitems] = xalloc((unsigned)strlen(entry) + 1);
                        copyn(items[numitems], entry, MAXNAMLEN);
                        numitems++;
-               } else                          /* RECOGNIZE command */
-                       if (recognize(extended_name, entry, name_length,
-                           ++numitems))
+               } else {                        /* RECOGNIZE command */
+                       if (ignoring && ignored(entry))
+                               nignored++;
+                       else if (recognize(extended_name,
+                           entry, name_length, ++numitems))
                                break;
                                break;
+               }
        }
        }
+       if (ignoring && numitems == 0 && nignored > 0) {
+               ignoring = FALSE;
+               nignored = 0;
+               if (looking_for_lognames)
+                       (void) setpwent();
+               else
+                       rewinddir(dir_fd);
+               goto again;
+       }
+
        if (looking_for_lognames)
                (void) endpwent();
        else
                closedir(dir_fd);
        if (looking_for_lognames)
                (void) endpwent();
        else
                closedir(dir_fd);
-       if (command == RECOGNIZE && numitems > 0) {
+       if (numitems == 0)
+               return (0);
+       if (command == RECOGNIZE) {
                if (looking_for_lognames)
                         copyn(word, "~", 1);
                else
                if (looking_for_lognames)
                         copyn(word, "~", 1);
                else
@@ -419,8 +437,8 @@ search(word, command, max_word_length)
                catn(word, extended_name, max_word_length);
                return (numitems);
        }
                catn(word, extended_name, max_word_length);
                return (numitems);
        }
-       if (command == LIST) {
-               qsort((char *)items, numitems, sizeof(items[1]), fcompare);
+       else {                          /* LIST */
+               qsort((char *)items, numitems, sizeof(items[1]), sortscmp);
                print_by_column(looking_for_lognames ? NULL : tilded_dir,
                    items, numitems);
                if (items != NULL)
                print_by_column(looking_for_lognames ? NULL : tilded_dir,
                    items, numitems);
                if (items != NULL)
@@ -437,13 +455,14 @@ search(word, command, max_word_length)
  * character mismatch between extended_name and entry.
  * If we shorten it back to the prefix length, stop searching.
  */
  * character mismatch between extended_name and entry.
  * If we shorten it back to the prefix length, stop searching.
  */
+static
 recognize(extended_name, entry, name_length, numitems)
        char *extended_name, *entry;
 {
 
 recognize(extended_name, entry, name_length, numitems)
        char *extended_name, *entry;
 {
 
-       if (numitems == 1)                              /* 1st match */
+       if (numitems == 1)                      /* 1st match */
                copyn(extended_name, entry, MAXNAMLEN);
                copyn(extended_name, entry, MAXNAMLEN);
-       else {                                  /* 2nd and subsequent matches */
+       else {                                  /* 2nd & subsequent matches */
                register char *x, *ent;
                register int len = 0;
 
                register char *x, *ent;
                register int len = 0;
 
@@ -458,25 +477,44 @@ recognize(extended_name, entry, name_length, numitems)
 }
 
 /*
 }
 
 /*
- * Return true if check items initial chars in template
+ * Return true if check matches initial chars in template.
  * This differs from PWB imatch in that if check is null
  * This differs from PWB imatch in that if check is null
- * it items anything
+ * it matches anything.
  */
 static
 is_prefix(check, template)
  */
 static
 is_prefix(check, template)
-       char *check, *template;
+       register char *check, *template;
 {
 {
-       register char *check_char, *template_char;
 
 
-       check_char = check;
-       template_char = template;
        do
        do
-               if (*check_char == 0)
+               if (*check == 0)
                        return (TRUE);
                        return (TRUE);
-       while (*check_char++ == *template_char++);
+       while (*check++ == *template++);
        return (FALSE);
 }
 
        return (FALSE);
 }
 
+/*
+ *  Return true if the chars in template appear at the
+ *  end of check, I.e., are it's suffix.
+ */
+static
+is_suffix(check, template)
+       char *check, *template;
+{
+       register char *c, *t;
+
+       for (c = check; *c++;)
+               ;
+       for (t = template; *t++;)
+               ;
+       for (;;) {
+               if (t == template)
+                       return 1;
+               if (c == check || *--t != *--c)
+                       return 0;
+       }
+}
+
 tenex(inputline, inputline_size)
        char *inputline;
        int inputline_size;
 tenex(inputline, inputline_size)
        char *inputline;
        int inputline_size;
@@ -484,7 +522,7 @@ tenex(inputline, inputline_size)
        register int numitems, num_read;
 
        setup_tty(ON);
        register int numitems, num_read;
 
        setup_tty(ON);
-       while ((num_read = read (SHIN, inputline, inputline_size)) > 0) {
+       while ((num_read = read(SHIN, inputline, inputline_size)) > 0) {
                static char *delims = " '\"\t;&<>()|^%";
                register char *str_end, *word_start, last_char, should_retype;
                register int space_left;
                static char *delims = " '\"\t;&<>()|^%";
                register char *str_end, *word_start, last_char, should_retype;
                register int space_left;
@@ -496,7 +534,7 @@ tenex(inputline, inputline_size)
                        break;
                command = (last_char == ESC) ? RECOGNIZE : LIST;
                if (command == LIST)
                        break;
                command = (last_char == ESC) ? RECOGNIZE : LIST;
                if (command == LIST)
-                       putchar ('\n');
+                       cshputchar('\n');
                str_end = &inputline[num_read];
                if (last_char == ESC)
                        --str_end;              /* wipeout trailing cmd char */
                str_end = &inputline[num_read];
                if (last_char == ESC)
                        --str_end;              /* wipeout trailing cmd char */
@@ -515,7 +553,7 @@ tenex(inputline, inputline_size)
                        /* print from str_end on */
                        print_recognized_stuff(str_end);
                        if (numitems != 1)      /* Beep = No match/ambiguous */
                        /* print from str_end on */
                        print_recognized_stuff(str_end);
                        if (numitems != 1)      /* Beep = No match/ambiguous */
-                               beep ();
+                               beep();
                }
 
                /*
                }
 
                /*
@@ -535,9 +573,24 @@ tenex(inputline, inputline_size)
                        printprompt();
                pushback(inputline);
                if (should_retype)
                        printprompt();
                pushback(inputline);
                if (should_retype)
-                       retype ();
+                       retype();
        }
        }
-       setup_tty (OFF);
+       setup_tty(OFF);
        return (num_read);
 }
        return (num_read);
 }
+
+static
+ignored(entry)
+       register char *entry;
+{
+       struct varent *vp;
+       register char **cp;
+
+       if ((vp = adrof("fignore")) == NULL || (cp = vp->vec) == NULL)
+               return (FALSE);
+       for (; *cp != NULL; cp++)
+               if (is_suffix(entry, *cp))
+                       return (TRUE);
+       return (FALSE);
+}
 #endif FILEC
 #endif FILEC