we've only got one tty discipline now.
[unix-history] / usr / src / bin / csh / file.c
index f94fdec..447fcc2 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char *sccsid = "@(#)file.c      5.3 (Berkeley) %G%";
+static char *sccsid = "@(#)file.c      5.8 (Berkeley) %G%";
 #endif
 
 #ifdef FILEC
 #endif
 
 #ifdef FILEC
@@ -30,7 +30,7 @@ static char *sccsid = "@(#)file.c     5.3 (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
@@ -44,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)) {
@@ -67,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);
 }
 
 /*
 }
 
 /*
@@ -77,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);
@@ -98,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);
@@ -117,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;
@@ -130,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;
@@ -152,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 ('*');
                }
@@ -182,6 +174,8 @@ filetype(dir, file)
        return (' ');
 }
 
        return (' ');
 }
 
+static struct winsize win;
+
 /*
  * Print sorted down columns
  */
 /*
  * Print sorted down columns
  */
@@ -191,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++) {
@@ -203,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');
        }
 }
 
        }
 }
 
@@ -221,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;
 {
@@ -313,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;
 {
@@ -321,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);
        }
@@ -342,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);\
@@ -417,7 +415,7 @@ again:      /* search for matches */
                ignoring = FALSE;
                nignored = 0;
                if (looking_for_lognames)
                ignoring = FALSE;
                nignored = 0;
                if (looking_for_lognames)
-                       (void)setpwent();
+                       (void) setpwent();
                else
                        rewinddir(dir_fd);
                goto again;
                else
                        rewinddir(dir_fd);
                goto again;
@@ -427,7 +425,9 @@ again:      /* search for matches */
                (void) endpwent();
        else
                closedir(dir_fd);
                (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
@@ -437,8 +437,8 @@ again:      /* search for matches */
                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)
@@ -455,13 +455,14 @@ again:    /* search for matches */
  * 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;
 
@@ -476,9 +477,9 @@ 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)
@@ -533,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 */