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
1_00_stable_10817
Eric Andersen 2001-01-30 18:03:11 +00:00
parent 201dc0d5a9
commit e57d54b456
3 changed files with 58 additions and 42 deletions

View File

@ -41,9 +41,12 @@
* 1. requires lstat (BSD) - how do you do it without? * 1. requires lstat (BSD) - how do you do it without?
*/ */
static const int TERMINAL_WIDTH = 80; /* use 79 if your terminal has linefold bug */ enum {
static const int COLUMN_WIDTH = 14; /* default if AUTOWIDTH not defined */ TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
static const int COLUMN_GAP = 2; /* includes the file type char, if present */ COLUMN_WIDTH = 14, /* default if AUTOWIDTH not defined */
COLUMN_GAP = 2, /* includes the file type char */
};
/************************************************************************/ /************************************************************************/
@ -173,9 +176,9 @@ static unsigned int follow_links=FALSE;
static unsigned short column = 0; static unsigned short column = 0;
#ifdef BB_FEATURE_AUTOWIDTH #ifdef BB_FEATURE_AUTOWIDTH
static unsigned short terminal_width; static unsigned short terminal_width = TERMINAL_WIDTH;
static unsigned short column_width; static unsigned short column_width = COLUMN_WIDTH;
static unsigned short tabstops; static unsigned short tabstops = COLUMN_GAP;
#else #else
static unsigned short column_width = COLUMN_WIDTH; static unsigned short column_width = COLUMN_WIDTH;
#endif #endif
@ -434,9 +437,15 @@ void showfiles(struct dnode **dn, int nfiles)
((list_fmt & LIST_INO) ? 8 : 0) + ((list_fmt & LIST_INO) ? 8 : 0) +
((list_fmt & LIST_BLOCKS) ? 5 : 0) ((list_fmt & LIST_BLOCKS) ? 5 : 0)
; ;
if (column_width < len) column_width= len; if (column_width < len)
column_width= len;
}
if (column_width >= 6)
ncols = (int)(terminal_width / (column_width + COLUMN_GAP));
else {
ncols = 1;
column_width = COLUMN_WIDTH;
} }
ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
#else #else
ncols= TERMINAL_WIDTH; ncols= TERMINAL_WIDTH;
#endif #endif
@ -447,7 +456,12 @@ void showfiles(struct dnode **dn, int nfiles)
break; break;
} }
nrows= nfiles / ncols; if (ncols > 1) {
nrows = nfiles / ncols;
} else {
nrows = nfiles;
ncols = 1;
}
if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */ if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */
if (nrows > nfiles) nrows= nfiles; if (nrows > nfiles) nrows= nfiles;
@ -617,15 +631,9 @@ int list_single(struct dnode *dn)
case LIST_ID_NAME: case LIST_ID_NAME:
#ifdef BB_FEATURE_LS_USERNAME #ifdef BB_FEATURE_LS_USERNAME
my_getpwuid(scratch, dn->dstat.st_uid); my_getpwuid(scratch, dn->dstat.st_uid);
if (*scratch) printf("%-8.8s ", scratch);
printf("%-8.8s ", scratch);
else
printf("%-8d ", dn->dstat.st_uid);
my_getgrgid(scratch, dn->dstat.st_gid); my_getgrgid(scratch, dn->dstat.st_gid);
if (*scratch) printf("%-8.8s", scratch);
printf("%-8.8s", scratch);
else
printf("%-8d", dn->dstat.st_gid);
column += 17; column += 17;
break; break;
#endif #endif

42
ls.c
View File

@ -41,9 +41,12 @@
* 1. requires lstat (BSD) - how do you do it without? * 1. requires lstat (BSD) - how do you do it without?
*/ */
static const int TERMINAL_WIDTH = 80; /* use 79 if your terminal has linefold bug */ enum {
static const int COLUMN_WIDTH = 14; /* default if AUTOWIDTH not defined */ TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */
static const int COLUMN_GAP = 2; /* includes the file type char, if present */ COLUMN_WIDTH = 14, /* default if AUTOWIDTH not defined */
COLUMN_GAP = 2, /* includes the file type char */
};
/************************************************************************/ /************************************************************************/
@ -173,9 +176,9 @@ static unsigned int follow_links=FALSE;
static unsigned short column = 0; static unsigned short column = 0;
#ifdef BB_FEATURE_AUTOWIDTH #ifdef BB_FEATURE_AUTOWIDTH
static unsigned short terminal_width; static unsigned short terminal_width = TERMINAL_WIDTH;
static unsigned short column_width; static unsigned short column_width = COLUMN_WIDTH;
static unsigned short tabstops; static unsigned short tabstops = COLUMN_GAP;
#else #else
static unsigned short column_width = COLUMN_WIDTH; static unsigned short column_width = COLUMN_WIDTH;
#endif #endif
@ -434,9 +437,15 @@ void showfiles(struct dnode **dn, int nfiles)
((list_fmt & LIST_INO) ? 8 : 0) + ((list_fmt & LIST_INO) ? 8 : 0) +
((list_fmt & LIST_BLOCKS) ? 5 : 0) ((list_fmt & LIST_BLOCKS) ? 5 : 0)
; ;
if (column_width < len) column_width= len; if (column_width < len)
column_width= len;
}
if (column_width >= 6)
ncols = (int)(terminal_width / (column_width + COLUMN_GAP));
else {
ncols = 1;
column_width = COLUMN_WIDTH;
} }
ncols= (int)(terminal_width / (column_width + COLUMN_GAP));
#else #else
ncols= TERMINAL_WIDTH; ncols= TERMINAL_WIDTH;
#endif #endif
@ -447,7 +456,12 @@ void showfiles(struct dnode **dn, int nfiles)
break; break;
} }
nrows= nfiles / ncols; if (ncols > 1) {
nrows = nfiles / ncols;
} else {
nrows = nfiles;
ncols = 1;
}
if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */ if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */
if (nrows > nfiles) nrows= nfiles; if (nrows > nfiles) nrows= nfiles;
@ -617,15 +631,9 @@ int list_single(struct dnode *dn)
case LIST_ID_NAME: case LIST_ID_NAME:
#ifdef BB_FEATURE_LS_USERNAME #ifdef BB_FEATURE_LS_USERNAME
my_getpwuid(scratch, dn->dstat.st_uid); my_getpwuid(scratch, dn->dstat.st_uid);
if (*scratch) printf("%-8.8s ", scratch);
printf("%-8.8s ", scratch);
else
printf("%-8d ", dn->dstat.st_uid);
my_getgrgid(scratch, dn->dstat.st_gid); my_getgrgid(scratch, dn->dstat.st_gid);
if (*scratch) printf("%-8.8s", scratch);
printf("%-8.8s", scratch);
else
printf("%-8d", dn->dstat.st_gid);
column += 17; column += 17;
break; break;
#endif #endif

View File

@ -871,7 +871,7 @@ long my_getpwnam(char *name)
myuser = getpwnam(name); myuser = getpwnam(name);
if (myuser==NULL) if (myuser==NULL)
error_msg_and_die( "unknown username: %s\n", name); return(-1);
return myuser->pw_uid; return myuser->pw_uid;
} }
@ -883,7 +883,7 @@ long my_getgrnam(char *name)
mygroup = getgrnam(name); mygroup = getgrnam(name);
if (mygroup==NULL) if (mygroup==NULL)
error_msg_and_die( "unknown group: %s\n", name); return(-1);
return (mygroup->gr_gid); return (mygroup->gr_gid);
} }
@ -895,9 +895,9 @@ void my_getpwuid(char *name, long uid)
myuser = getpwuid(uid); myuser = getpwuid(uid);
if (myuser==NULL) if (myuser==NULL)
error_msg_and_die( "unknown uid %ld\n", (long)uid); sprintf(name, "%-8ld ", (long)uid);
else
strcpy(name, myuser->pw_name); strcpy(name, myuser->pw_name);
} }
/* gets a groupname given a gid */ /* gets a groupname given a gid */
@ -907,9 +907,9 @@ void my_getgrgid(char *group, long gid)
mygroup = getgrgid(gid); mygroup = getgrgid(gid);
if (mygroup==NULL) if (mygroup==NULL)
error_msg_and_die( "unknown gid %ld\n", (long)gid); sprintf(group, "%-8ld ", (long)gid);
else
strcpy(group, mygroup->gr_name); strcpy(group, mygroup->gr_name);
} }
#if defined BB_ID #if defined BB_ID