[comp.os.minix] LS enhancement to print in columns

hall@cod.NOSC.MIL (Robert R. Hall) (05/19/89)

It was stated in a previous article that the Minix ls doesn't
print in columns.  Chris Wee posted a patch for version 1.2
which  had the -w option to print in 4 coloumns
       and the -x option to print "/" after directory names.

These options did not get included into the version 1.3 update
and thus got dropped.
Here is a patch to add these options to version 1.3 LS again.


--------------------        ls.cdiff     ------------------------------
*** /usr/v1.3d/commands/ls.c	Fri May 12 10:30:31 1989
--- ls.c	Thu May 18 13:11:02 1989
***************
*** 1,19 ****
  /* ls - list files and directories 	Author: Andy Tanenbaum */
  /* Version: Minix 1.3 */
  
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <minix/const.h>
  #include <minix/type.h>
! #include <fs/const.h>
! #include <fs/type.h>
  
  #define DIRNAMELEN        14	/* # chars in a directory entry name */
  #define NFILE            256	/* max files in arg list to ls */
  #define MAXPATHLEN       256	/* max chars in a path name */
  #define NDIRBLOCKS        16	/* max length of a directory */
! #define LEGAL      0x0E0969L	/* legal flags to ls */
  
  struct file {
    char *name;
--- 1,27 ----
  /* ls - list files and directories 	Author: Andy Tanenbaum */
  /* Version: Minix 1.3 */
+ /* modified:	Chris Wee
+  * date:	7 May 1988
+  */
  
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <minix/const.h>
  #include <minix/type.h>
! #include "../fs/const.h"
! #include "../fs/type.h"
  
  #define DIRNAMELEN        14	/* # chars in a directory entry name */
  #define NFILE            256	/* max files in arg list to ls */
  #define MAXPATHLEN       256	/* max chars in a path name */
  #define NDIRBLOCKS        16	/* max length of a directory */
! #define LEGAL      0xDE096DL	/* legal flags to ls */
! 				/* xw_utsr_____l__i_gf_dc_a */
! #define N_COLUMNS	4
! #define	COLWIDTH	20	/* enuf for 4 cols on 80-col display */
! 
! int	column = 0;		/* column # */
  
  struct file {
    char *name;
***************
*** 83,88 ****
--- 91,98 ----
    }
    if (topfiles == 0) print_total(0, nrfiles);
    print(0, nrfiles, expand_flag, "");
+   if (column)		/* if not in first column, then another newline */
+ 	fprintf(stdout,"\n");
    fflush(stdout);
    exit(0);
  }
***************
*** 99,106 ****
  
    k = argc - topfiles;
    statflag = (topfiles == 0 ? 0 : 1);
!   if (present('c') || present('t') || present('u')) statflag = 1;
!   if (present('s') || present('l')) statflag = 1;
    while (k < argc) fill_file("", argv[k++], statflag,1);
  }
  
--- 109,117 ----
  
    k = argc - topfiles;
    statflag = (topfiles == 0 ? 0 : 1);
!   if (present('c') || present('t') || present('u') ||
!       present('s') || present('l') || present('x'))
! 	 statflag = 1;
    while (k < argc) fill_file("", argv[k++], statflag,1);
  }
  
***************
*** 265,273 ****
  	fprintf(stdout, "Directory %s too long\n", fp->name);
  	return;
    }
!   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) {
--- 276,284 ----
  	fprintf(stdout, "Directory %s too long\n", fp->name);
  	return;
    }
! 
!   statflag = (	present('c') || present('t') || present('u') ||
! 		present('s') || present('l') || present('x') ) ? 1 : 0;
  
    for (k = 0; k < klim; k++) {
  	if (dir[k].inum != 0) {
***************
*** 319,325 ****
  struct file *fp;
  int  is_path;
  {
!   int blks, m, prot, s;
    char *p1, *p2, *p3, c;
  
    if (present('i')) fprintf(stdout, "%5d ", fp->inumber);
--- 330,336 ----
  struct file *fp;
  int  is_path;
  {
!   int blks, m, lenght, prot, s;
    char *p1, *p2, *p3, c;
  
    if (present('i')) fprintf(stdout, "%5d ", fp->inumber);
***************
*** 359,392 ****
  		s = (short) fp->size;
  		fprintf(stdout, "%3d, %3d ", (s>>8)&0377, s&0377);
  	} else {
! 		fprintf(stdout, "%8D ", fp->size);
  	}
  	date(fp->modtime);
    }
  
  	/* Print file name. */
! 	m = 0;
! 	pfname(fp->name,fp->is_path);
! 	fprintf(stdout, "\n");
! }
! 
! pfname(ptr,pathflag)
! char *ptr;
! int  pathflag;
! {
! 	int  m;
! 	if (pathflag) {
! 		fprintf(stdout,"%s",ptr);
! 	} else {
! 		/* dirname entry is null terminated if length < 14 */
! 		m = 0;
! 		while (*ptr && m < DIRNAMELEN) {
! 			putc(*ptr++, stdout);
! 			++m;
! 		}
  	}
  }
  
  
  
  owngrp(fp)
--- 370,423 ----
  		s = (short) fp->size;
  		fprintf(stdout, "%3d, %3d ", (s>>8)&0377, s&0377);
  	} else {
! 		fprintf(stdout, "%7D ", fp->size);
  	}
  	date(fp->modtime);
    }
  
  	/* Print file name. */
! 	lenght = len(fp->name);
! 	m = 0;
! 	pfname(fp->name,fp->is_path);
! 
! 	/*
! 	 * print tailing file indicator symbol if -F flag is present
! 	 */
! 	if (present('x') && ((fp->mode & I_TYPE) == I_DIRECTORY)) {
! 		fprintf(stdout,"/");
! 		lenght++;
! 	}
! 
! 	/*
! 	 * print filenames in columns as default
! 	 */
! 	if (present('l') || !present('w') || (++column == N_COLUMNS)) {
! 		column = 0;		/* reset column # */
! 		fprintf(stdout, "\n");
! 	}
! 	else {
! 		for (lenght = COLWIDTH - lenght; lenght > 0 ; lenght--)
! 			fprintf(stdout," ");
  	}
  }
  
+ pfname(ptr,pathflag)
+ char *ptr;
+ int  pathflag;
+ {
+ 	int  m;
+ 	if (pathflag) {
+ 		fprintf(stdout,"%s",ptr);
+ 	} else {
+ 		/* dirname entry is null terminated if length < 14 */
+ 		m = 0;
+ 		while (*ptr && m < DIRNAMELEN) {
+ 			putc(*ptr++, stdout);
+ 			++m;
+ 		}
+ 	}
+ }
+ 
  
  
  owngrp(fp)
***************
*** 402,408 ****
    }
    buf = getuidgid(xid);
    if (buf != 0)
! 	fprintf(stdout, "%6s ",buf);
    else
  	fprintf(stdout, "%6d ",xid);
  }
--- 433,440 ----
    }
    buf = getuidgid(xid);
    if (buf != 0)
! 	/* Assuming the number 8 is not a good idea. */
! 	fprintf(stdout, "%-8.8s ",buf);
    else
  	fprintf(stdout, "%6d ",xid);
  }