aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-01-30 18:03:11 +0000
committerEric Andersen <andersen@codepoet.org>2001-01-30 18:03:11 +0000
commite57d54b456bf091aded43fc95bee9b05e7461dd0 (patch)
treeab02892c3929a64304d457f6857a68e01efea145
parent201dc0d5a99b8ef765f2b79e6a492a70772a0774 (diff)
downloadbusybox-w32-e57d54b456bf091aded43fc95bee9b05e7461dd0.tar.gz
busybox-w32-e57d54b456bf091aded43fc95bee9b05e7461dd0.tar.bz2
busybox-w32-e57d54b456bf091aded43fc95bee9b05e7461dd0.zip
Fix ls behavior for broken or very narrow terminals. Fix my_*
functions so they comply with the original interface (i.e. don't exit on error, stringify uids and gids when no amtching name found). -Erik
-rw-r--r--coreutils/ls.c42
-rw-r--r--ls.c42
-rw-r--r--utility.c16
3 files changed, 58 insertions, 42 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index affa48c12..2ed5fe165 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -41,9 +41,12 @@
41 * 1. requires lstat (BSD) - how do you do it without? 41 * 1. requires lstat (BSD) - how do you do it without?
42 */ 42 */
43 43
44static const int TERMINAL_WIDTH = 80; /* use 79 if your terminal has linefold bug */ 44enum {
45static const int COLUMN_WIDTH = 14; /* default if AUTOWIDTH not defined */ 45 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
46static const int COLUMN_GAP = 2; /* includes the file type char, if present */ 46 COLUMN_WIDTH = 14, /* default if AUTOWIDTH not defined */
47 COLUMN_GAP = 2, /* includes the file type char */
48};
49
47 50
48/************************************************************************/ 51/************************************************************************/
49 52
@@ -173,9 +176,9 @@ static unsigned int follow_links=FALSE;
173 176
174static unsigned short column = 0; 177static unsigned short column = 0;
175#ifdef BB_FEATURE_AUTOWIDTH 178#ifdef BB_FEATURE_AUTOWIDTH
176static unsigned short terminal_width; 179static unsigned short terminal_width = TERMINAL_WIDTH;
177static unsigned short column_width; 180static unsigned short column_width = COLUMN_WIDTH;
178static unsigned short tabstops; 181static unsigned short tabstops = COLUMN_GAP;
179#else 182#else
180static unsigned short column_width = COLUMN_WIDTH; 183static unsigned short column_width = COLUMN_WIDTH;
181#endif 184#endif
@@ -434,9 +437,15 @@ void showfiles(struct dnode **dn, int nfiles)
434 ((list_fmt & LIST_INO) ? 8 : 0) + 437 ((list_fmt & LIST_INO) ? 8 : 0) +
435 ((list_fmt & LIST_BLOCKS) ? 5 : 0) 438 ((list_fmt & LIST_BLOCKS) ? 5 : 0)
436 ; 439 ;
437 if (column_width < len) column_width= len; 440 if (column_width < len)
441 column_width= len;
442 }
443 if (column_width >= 6)
444 ncols = (int)(terminal_width / (column_width + COLUMN_GAP));
445 else {
446 ncols = 1;
447 column_width = COLUMN_WIDTH;
438 } 448 }
439 ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
440#else 449#else
441 ncols= TERMINAL_WIDTH; 450 ncols= TERMINAL_WIDTH;
442#endif 451#endif
@@ -447,7 +456,12 @@ void showfiles(struct dnode **dn, int nfiles)
447 break; 456 break;
448 } 457 }
449 458
450 nrows= nfiles / ncols; 459 if (ncols > 1) {
460 nrows = nfiles / ncols;
461 } else {
462 nrows = nfiles;
463 ncols = 1;
464 }
451 if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */ 465 if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */
452 466
453 if (nrows > nfiles) nrows= nfiles; 467 if (nrows > nfiles) nrows= nfiles;
@@ -617,15 +631,9 @@ int list_single(struct dnode *dn)
617 case LIST_ID_NAME: 631 case LIST_ID_NAME:
618#ifdef BB_FEATURE_LS_USERNAME 632#ifdef BB_FEATURE_LS_USERNAME
619 my_getpwuid(scratch, dn->dstat.st_uid); 633 my_getpwuid(scratch, dn->dstat.st_uid);
620 if (*scratch) 634 printf("%-8.8s ", scratch);
621 printf("%-8.8s ", scratch);
622 else
623 printf("%-8d ", dn->dstat.st_uid);
624 my_getgrgid(scratch, dn->dstat.st_gid); 635 my_getgrgid(scratch, dn->dstat.st_gid);
625 if (*scratch) 636 printf("%-8.8s", scratch);
626 printf("%-8.8s", scratch);
627 else
628 printf("%-8d", dn->dstat.st_gid);
629 column += 17; 637 column += 17;
630 break; 638 break;
631#endif 639#endif
diff --git a/ls.c b/ls.c
index affa48c12..2ed5fe165 100644
--- a/ls.c
+++ b/ls.c
@@ -41,9 +41,12 @@
41 * 1. requires lstat (BSD) - how do you do it without? 41 * 1. requires lstat (BSD) - how do you do it without?
42 */ 42 */
43 43
44static const int TERMINAL_WIDTH = 80; /* use 79 if your terminal has linefold bug */ 44enum {
45static const int COLUMN_WIDTH = 14; /* default if AUTOWIDTH not defined */ 45 TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
46static const int COLUMN_GAP = 2; /* includes the file type char, if present */ 46 COLUMN_WIDTH = 14, /* default if AUTOWIDTH not defined */
47 COLUMN_GAP = 2, /* includes the file type char */
48};
49
47 50
48/************************************************************************/ 51/************************************************************************/
49 52
@@ -173,9 +176,9 @@ static unsigned int follow_links=FALSE;
173 176
174static unsigned short column = 0; 177static unsigned short column = 0;
175#ifdef BB_FEATURE_AUTOWIDTH 178#ifdef BB_FEATURE_AUTOWIDTH
176static unsigned short terminal_width; 179static unsigned short terminal_width = TERMINAL_WIDTH;
177static unsigned short column_width; 180static unsigned short column_width = COLUMN_WIDTH;
178static unsigned short tabstops; 181static unsigned short tabstops = COLUMN_GAP;
179#else 182#else
180static unsigned short column_width = COLUMN_WIDTH; 183static unsigned short column_width = COLUMN_WIDTH;
181#endif 184#endif
@@ -434,9 +437,15 @@ void showfiles(struct dnode **dn, int nfiles)
434 ((list_fmt & LIST_INO) ? 8 : 0) + 437 ((list_fmt & LIST_INO) ? 8 : 0) +
435 ((list_fmt & LIST_BLOCKS) ? 5 : 0) 438 ((list_fmt & LIST_BLOCKS) ? 5 : 0)
436 ; 439 ;
437 if (column_width < len) column_width= len; 440 if (column_width < len)
441 column_width= len;
442 }
443 if (column_width >= 6)
444 ncols = (int)(terminal_width / (column_width + COLUMN_GAP));
445 else {
446 ncols = 1;
447 column_width = COLUMN_WIDTH;
438 } 448 }
439 ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
440#else 449#else
441 ncols= TERMINAL_WIDTH; 450 ncols= TERMINAL_WIDTH;
442#endif 451#endif
@@ -447,7 +456,12 @@ void showfiles(struct dnode **dn, int nfiles)
447 break; 456 break;
448 } 457 }
449 458
450 nrows= nfiles / ncols; 459 if (ncols > 1) {
460 nrows = nfiles / ncols;
461 } else {
462 nrows = nfiles;
463 ncols = 1;
464 }
451 if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */ 465 if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */
452 466
453 if (nrows > nfiles) nrows= nfiles; 467 if (nrows > nfiles) nrows= nfiles;
@@ -617,15 +631,9 @@ int list_single(struct dnode *dn)
617 case LIST_ID_NAME: 631 case LIST_ID_NAME:
618#ifdef BB_FEATURE_LS_USERNAME 632#ifdef BB_FEATURE_LS_USERNAME
619 my_getpwuid(scratch, dn->dstat.st_uid); 633 my_getpwuid(scratch, dn->dstat.st_uid);
620 if (*scratch) 634 printf("%-8.8s ", scratch);
621 printf("%-8.8s ", scratch);
622 else
623 printf("%-8d ", dn->dstat.st_uid);
624 my_getgrgid(scratch, dn->dstat.st_gid); 635 my_getgrgid(scratch, dn->dstat.st_gid);
625 if (*scratch) 636 printf("%-8.8s", scratch);
626 printf("%-8.8s", scratch);
627 else
628 printf("%-8d", dn->dstat.st_gid);
629 column += 17; 637 column += 17;
630 break; 638 break;
631#endif 639#endif
diff --git a/utility.c b/utility.c
index bf4284c94..568b5f218 100644
--- a/utility.c
+++ b/utility.c
@@ -871,7 +871,7 @@ long my_getpwnam(char *name)
871 871
872 myuser = getpwnam(name); 872 myuser = getpwnam(name);
873 if (myuser==NULL) 873 if (myuser==NULL)
874 error_msg_and_die( "unknown username: %s\n", name); 874 return(-1);
875 875
876 return myuser->pw_uid; 876 return myuser->pw_uid;
877} 877}
@@ -883,7 +883,7 @@ long my_getgrnam(char *name)
883 883
884 mygroup = getgrnam(name); 884 mygroup = getgrnam(name);
885 if (mygroup==NULL) 885 if (mygroup==NULL)
886 error_msg_and_die( "unknown group: %s\n", name); 886 return(-1);
887 887
888 return (mygroup->gr_gid); 888 return (mygroup->gr_gid);
889} 889}
@@ -895,9 +895,9 @@ void my_getpwuid(char *name, long uid)
895 895
896 myuser = getpwuid(uid); 896 myuser = getpwuid(uid);
897 if (myuser==NULL) 897 if (myuser==NULL)
898 error_msg_and_die( "unknown uid %ld\n", (long)uid); 898 sprintf(name, "%-8ld ", (long)uid);
899 899 else
900 strcpy(name, myuser->pw_name); 900 strcpy(name, myuser->pw_name);
901} 901}
902 902
903/* gets a groupname given a gid */ 903/* gets a groupname given a gid */
@@ -907,9 +907,9 @@ void my_getgrgid(char *group, long gid)
907 907
908 mygroup = getgrgid(gid); 908 mygroup = getgrgid(gid);
909 if (mygroup==NULL) 909 if (mygroup==NULL)
910 error_msg_and_die( "unknown gid %ld\n", (long)gid); 910 sprintf(group, "%-8ld ", (long)gid);
911 911 else
912 strcpy(group, mygroup->gr_name); 912 strcpy(group, mygroup->gr_name);
913} 913}
914 914
915#if defined BB_ID 915#if defined BB_ID