Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!hc!beta!hgm From: hgm@beta.UUCP (Harry McGavran) Newsgroups: comp.os.minix Subject: Diffs for more ls enhancements Message-ID: <5194@beta.UUCP> Date: Fri, 8-May-87 17:37:36 EDT Article-I.D.: beta.5194 Posted: Fri May 8 17:37:36 1987 Date-Received: Sat, 9-May-87 21:37:15 EDT Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 375 Here are the diffs for an improved ls. This one prints multiple columns sorted DOWN rather than across. I think it reasonably reliable detects whether the output is going to the tty or not, in spite of the fact that isatty(stdout) doesn't seem to work. What I did here is real hoaky, but until fstat can get fixed or someone finds a better way, it is what I came up with. When it detects that stdout is not the tty, it defaults to a single column. By default it also appends a "*" to the executables and a "/" to the directories. I have added to options to disable the above features. "-1" causes single column output, and "-m" cause multiple column output when stdout is not to the tty. "-n" will turn off the addition of "*" and "/". I make no claims as to the suitability of these changes for anything. I extend my appreciation to Ed Nather, whose "ls" for MS-DOS gave me the ideas and some of the code for this. I believe he doesn't mind redistribution of it, as long as he gets credit. Hope you find this useful. To install it, move your ORIGINAL ls source to some system with the latest version of "patch". Cut the diffs below out into a file called "ls.pat". Then type "patch -l ls.c size == -1L) /* -1 means stat not done */ ! if (stat_file(dirname, fp) < 0) continue; ! m = fp->mode & I_TYPE; /* 'm' may be junk if 'expand' = 0 */ ! if (present('f')) m = I_DIRECTORY; ! if (m != I_DIRECTORY || present('d') || expand == 0) { ! /* List a single line. */ ! print_line(fp); ! } else { ! /* Expand and print directory. */ ! exp_dir(fp); ! sort(nrf, nrfiles - nrf, 0); ! if (topfiles > 1) fprintf(stdout, "\n%s:\n", fp->name); ! print_total(nrf, nrfiles - nrf); ! print(nrf, nrfiles - nrf, 0, fp->name); /* recursion ! */ ! nrfiles = nrf; } } } --- 216,254 ---- { /* If an entry is a file, print it; if a directory, process it. */ ! int k, l, m, n, nrf; struct file *fp; + if(present('1') || present('i') || present('s') || present ('l')) + n = count; /* set for 1-column listing */ + else + n = (count + 4)/5; /* or 5-column */ + nrf = nrfiles; ! for (k = index; k < (index+n); k++) { ! for(l = 0; (k+l) < (index+count); l += n) { ! fp = &file[sort_index[k+l]]; ! if (present('l') || present('s') || present('i')) ! if (fp->size == -1L) /* -1 means stat not done */ ! if (stat_file(dirname, fp) < 0) continue; ! m = fp->mode & I_TYPE; /* 'm' may be junk if 'expand' = 0 */ ! if (present('f')) m = I_DIRECTORY; ! if (m != I_DIRECTORY || present('d') || expand == 0) { ! /* List a single line. */ ! print_line(fp); ! } else { ! /* Expand and print directory. */ ! exp_dir(fp); ! sort(nrf, nrfiles - nrf, 0); ! if (topfiles > 1) fprintf(stdout, "\n%s:\n", fp->name); ! print_total(nrf, nrfiles - nrf); ! print(nrf, nrfiles - nrf, 0, fp->name); /* recursion ! */ ! nrfiles = nrf; ! } } + cc = 0; + fputc('\n',stdout); } } *************** *** 260,266 **** } statflag = 0; if (present('c') || present('t') || present('u')) statflag = 1; ! if (present('s') || present('l')) statflag = 1; for (k = 0; k < klim; k++) { if (dir[k].inum != 0) { --- 282,288 ---- } statflag = 0; if (present('c') || present('t') || present('u')) statflag = 1; ! if (present('s') || present('l') || !present('n')) statflag = 1; for (k = 0; k < klim; k++) { if (dir[k].inum != 0) { *************** *** 355,362 **** date(fp->modtime); } ! /* Print file name. */ ! fprintf(stdout, "%s\n",fp->name); } --- 377,404 ---- date(fp->modtime); } ! /* Print file name. */ ! while(cc % COLSIZ) { /* pad to columnarize */ ! fputc(' ', stdout); ! cc++; ! } ! ! m = 0; ! while((c = (fp->name)[m++]) != 0) { ! fputc(c, stdout); ! cc++; ! } ! ! if (!present('n')) { /* add entry type */ ! if ((fp->mode & I_TYPE) == I_DIRECTORY) { ! fputc('/', stdout); ! cc++; ! } else if (fp->mode & 0111) { ! fputc('*', stdout); ! cc++; ! } ! } ! } *************** *** 452,457 **** --- 494,504 ---- ptr++; while (*ptr != 0) { + if (*ptr == '1') { + multcol = 0; + ptr++; + continue; + } k = *ptr - 'a'; t = 1L << k; if (*ptr < 'a' || *ptr > 'z' || (t|LEGAL) != LEGAL) { *************** *** 470,475 **** --- 517,524 ---- present(let) char let; { + if (let == '1') + return (!multcol); return (flags >> (let - 'a')) & 01; } *************** *** 580,590 **** char lbuf[100], *ptr, *ptr1; int bin; if (usrid == lastuid) return(lastname); lseek(passwd, 0L, 0); /* rewind the file */ linenext = 0; linelimit = 0; ! /* Scan the file. */ while (1) { ptr = lbuf; --- 629,641 ---- char lbuf[100], *ptr, *ptr1; int bin; + extern long lseek(); + if (usrid == lastuid) return(lastname); lseek(passwd, 0L, 0); /* rewind the file */ linenext = 0; linelimit = 0; ! /* Scan the file. */ while (1) { ptr = lbuf; *************** *** 639,642 **** --- 690,717 ---- blocks += (fileb + NR_INDIRECTS - 1)/NR_INDIRECTS; return(blocks); } + + + + + + istty() + { + /* see if stdout is to a tty, since isatty doesn't work on stdout */ + + long cur_pos, end_file; + + extern long lseek(); + + if ((cur_pos = lseek(1, 0L, 1)) > 0L) { + if ((end_file = lseek(1, 0L, 2)) == 0L) + return (1); + if (end_file > 0L) + lseek(1, cur_pos, 0); + } + + return (0); + + } + -----------------------------Cut here--------------------------------- Harry McGavran hgm@LANL.GOV Los Alamos National Laboratory MS-B294, Group C-8 Los Alamos, New Mexico 87545 505/667-4050