get window size, break up printdir into three functions
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Tue, 27 Jun 1989 07:32:10 +0000 (23:32 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Tue, 27 Jun 1989 07:32:10 +0000 (23:32 -0800)
SCCS-vsn: bin/ls/ls.c 5.21

usr/src/bin/ls/ls.c

index de660a3..12b4e7a 100644 (file)
@@ -25,11 +25,12 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ls.c       5.20 (Berkeley) %G%";
+static char sccsid[] = "@(#)ls.c       5.21 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
+#include <sys/ioctl.h>
 #include <dirent.h>
 #include <strings.h>
 #include <errno.h>
 #include <dirent.h>
 #include <strings.h>
 #include <errno.h>
@@ -43,7 +44,6 @@ int   qflg, Aflg, Cflg, Fflg, Lflg, Rflg, Sflg;
 
 /* flags */
 int f_accesstime;              /* use time of last access */
 
 /* flags */
 int f_accesstime;              /* use time of last access */
-int f_firsttime = 1;           /* to control recursion */
 int f_group;                   /* show group ownership of a file */
 int f_ignorelink;              /* indirect through symbolic link operands */
 int f_inode;                   /* print inode */
 int f_group;                   /* show group ownership of a file */
 int f_ignorelink;              /* indirect through symbolic link operands */
 int f_inode;                   /* print inode */
@@ -66,9 +66,13 @@ main(argc, argv)
        char **argv;
 {
        extern int optind, stat();
        char **argv;
 {
        extern int optind, stat();
+       struct sgttyb sgbuf;
+       struct winsize win;
        int ch;
        int ch;
+       char *p, *getenv();
        int namecmp(), revnamecmp(), acccmp(), revacccmp();
        int modcmp(), revmodcmp(), statcmp(), revstatcmp();
        int namecmp(), revnamecmp(), acccmp(), revacccmp();
        int modcmp(), revmodcmp(), statcmp(), revstatcmp();
+       int printcol(), printlong(), printscol();
 
        /*
         * terminal defaults to -C -q
 
        /*
         * terminal defaults to -C -q
@@ -77,6 +81,11 @@ main(argc, argv)
        if (isatty(1)) {
                f_nonprint = 1;
                lengthfcn = prablelen;
        if (isatty(1)) {
                f_nonprint = 1;
                lengthfcn = prablelen;
+               (void)ioctl(1, TIOCGETP, &sgbuf);
+               if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col)
+                       termwidth = (p = getenv("COLUMNS")) ? atoi(p) : 80;
+               else
+                       termwidth = win.ws_col;
        } else
                f_singlecol = 1;
 
        } else
                f_singlecol = 1;
 
@@ -195,6 +204,14 @@ main(argc, argv)
                        sortfcn = modcmp;
        }
 
                        sortfcn = modcmp;
        }
 
+       /* select a print function */
+       if (f_singlecol)
+               printfcn = printscol;
+       else if (f_longform)
+               printfcn = printlong;
+       else
+               printfcn = printcol;
+
        if (argc)
                doargs(argc, argv);
        else
        if (argc)
                doargs(argc, argv);
        else
@@ -312,7 +329,7 @@ displaydir(stats, num)
        if (num > 1 && !f_specialdir)
                qsort((char *)stats, num, sizeof(LS), sortfcn);
 
        if (num > 1 && !f_specialdir)
                qsort((char *)stats, num, sizeof(LS), sortfcn);
 
-       printdir(stats, num);
+       printfcn(stats, num);
 
        if (f_recursive) {
                savedpath = endofpath;
 
        if (f_recursive) {
                savedpath = endofpath;