[comp.os.minix] Multicolumn support for ls

bishop@ecsvax.UUCP (05/07/87)

Here's a quick fix for ls so that it uses multiple columns.

Use "ls -c" to force single-column output.  It doesn't check
stdout to see what it is, so shell scripts using this may have
to be modified.

oldls.c is the standard ls.  newls.c is the revised version.
Let me know about any problems this causes.

*** oldls.c	Wed May  6 17:54:12 1987
--- newls.c	Wed May  6 17:53:52 1987
***************
*** 202,211 ****
  char *dirname;
  {
  /*  If an entry is a file, print it; if a directory, process it. */
  
!   int k, m, nrf;
    struct file *fp;
  
    nrf = nrfiles;
    for (k = index; k < index + count; k++) {
  	fp = &file[sort_index[k]];
--- 202,213 ----
  char *dirname;
  {
  /*  If an entry is a file, print it; if a directory, process it. */
+ /*  Modified AGB 04/25/87 to add five column support */
  
!   int k, m, nrf, prntcount;
    struct file *fp;
  
+   prntcount = 0;
    nrf = nrfiles;
    for (k = index; k < index + count; k++) {
  	fp = &file[sort_index[k]];
***************
*** 217,225 ****
  	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);
--- 219,232 ----
  	if (present('f')) m = I_DIRECTORY;
  	if (m != I_DIRECTORY || present('d') || expand == 0) {
  		/* List a single line. */
! 		print_line(fp, prntcount++, count);
  	} else {
  		/* Expand and print directory. */
+ 		if (!((prntcount%5 == 0) || present('l') ||
+ 			present('i') || present('s') || present('c'))) {
+ 			fputc('\n', stdout);
+ 		}
+ 		prntcount = 0; /* Directories start file over again */
  		exp_dir(fp);
  		sort(nrf, nrfiles - nrf, 0);
  		if (topfiles > 1) fprintf(stdout, "\n%s:\n", fp->name);
***************
*** 307,314 ****
  
  
  
! print_line(fp)
  struct file *fp;
  {
    int blks, m, prot, s;
    char *p1, *p2, *p3, c;
--- 314,324 ----
  
  
  
! print_line(fp,prntcount,count)
  struct file *fp;
+ int prntcount, count;
+ 
+ /* Modified AGB 04/25/87 to add five column support */
  {
    int blks, m, prot, s;
    char *p1, *p2, *p3, c;
***************
*** 356,362 ****
    }
  
  	/* Print file name. */
! 	fprintf(stdout, "%s\n",fp->name);
  }
  
  
--- 366,377 ----
    }
  
  	/* Print file name. */
! 	if (present('l') || present('i') || present('s') || present('c')) {
! 		fprintf(stdout, "%s\n",fp->name);
! 	} else {
! 		fprintf(stdout, "%-15s%c",fp->name,(prntcount%5==4 ||
! 			prntcount==count-1) ? '\n' : ' ');
! 	}
  }