Patch from Matt Kraai to fix an infinate loop with ls -aR

This commit is contained in:
Eric Andersen 2000-11-29 21:52:06 +00:00
parent 4bfb6b7b67
commit cf1189f5a7
2 changed files with 64 additions and 12 deletions

View File

@ -127,6 +127,7 @@
#define SPLIT_DIR 0 #define SPLIT_DIR 0
#define SPLIT_FILE 1 #define SPLIT_FILE 1
#define SPLIT_SUBDIR 2
#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
@ -236,11 +237,16 @@ static void nexttabstop( void )
} }
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
static int is_subdir(struct dnode *dn)
{
return (S_ISDIR(dn->dstat.st_mode) && strcmp(dn->name, ".") != 0 &&
strcmp(dn->name, "..") != 0);
}
int countdirs(struct dnode **dn, int nfiles) int countdirs(struct dnode **dn, int nfiles)
{ {
int i, dirs; int i, dirs;
/* count how many dirs and regular files there are */
if (dn==NULL || nfiles < 1) return(0); if (dn==NULL || nfiles < 1) return(0);
dirs= 0; dirs= 0;
for (i=0; i<nfiles; i++) { for (i=0; i<nfiles; i++) {
@ -249,6 +255,18 @@ int countdirs(struct dnode **dn, int nfiles)
return(dirs); return(dirs);
} }
int countsubdirs(struct dnode **dn, int nfiles)
{
int i, subdirs;
if (dn == NULL || nfiles < 1) return 0;
subdirs = 0;
for (i = 0; i < nfiles; i++)
if (is_subdir(dn[i]))
subdirs++;
return subdirs;
}
int countfiles(struct dnode **dnp) int countfiles(struct dnode **dnp)
{ {
int nfiles; int nfiles;
@ -296,9 +314,13 @@ struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
if (dn==NULL || nfiles < 1) return(NULL); if (dn==NULL || nfiles < 1) return(NULL);
/* count how many dirs and regular files there are */ /* count how many dirs and regular files there are */
dncnt= countdirs(dn, nfiles); /* assume we are looking for dirs */ if (which == SPLIT_SUBDIR)
if (which != SPLIT_DIR) dncnt = countsubdirs(dn, nfiles);
dncnt= nfiles - dncnt; /* looking for files */ else {
dncnt= countdirs(dn, nfiles); /* assume we are looking for dirs */
if (which == SPLIT_FILE)
dncnt= nfiles - dncnt; /* looking for files */
}
/* allocate a file array and a dir array */ /* allocate a file array and a dir array */
dnp= dnalloc(dncnt); dnp= dnalloc(dncnt);
@ -309,6 +331,10 @@ struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
if (S_ISDIR(dn[i]->dstat.st_mode)) { if (S_ISDIR(dn[i]->dstat.st_mode)) {
dnp[d++]= dn[i]; dnp[d++]= dn[i];
} /* else skip the file */ } /* else skip the file */
} else if (which == SPLIT_SUBDIR) {
if (is_subdir(dn[i])) {
dnp[d++]= dn[i];
} /* else skip the file or dir */
} else { } else {
if (!(S_ISDIR(dn[i]->dstat.st_mode))) { if (!(S_ISDIR(dn[i]->dstat.st_mode))) {
dnp[d++]= dn[i]; dnp[d++]= dn[i];
@ -455,8 +481,8 @@ void showdirs(struct dnode **dn, int ndirs)
#ifdef BB_FEATURE_LS_RECURSIVE #ifdef BB_FEATURE_LS_RECURSIVE
if (disp_opts & DISP_RECURSIVE) { if (disp_opts & DISP_RECURSIVE) {
/* recursive- list the sub-dirs */ /* recursive- list the sub-dirs */
dnd= splitdnarray(subdnp, nfiles, SPLIT_DIR); dnd= splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
dndirs= countdirs(subdnp, nfiles); dndirs= countsubdirs(subdnp, nfiles);
if (dndirs > 0) { if (dndirs > 0) {
#ifdef BB_FEATURE_LS_SORTFILES #ifdef BB_FEATURE_LS_SORTFILES
shellsort(dnd, dndirs); shellsort(dnd, dndirs);

38
ls.c
View File

@ -127,6 +127,7 @@
#define SPLIT_DIR 0 #define SPLIT_DIR 0
#define SPLIT_FILE 1 #define SPLIT_FILE 1
#define SPLIT_SUBDIR 2
#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
@ -236,11 +237,16 @@ static void nexttabstop( void )
} }
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
static int is_subdir(struct dnode *dn)
{
return (S_ISDIR(dn->dstat.st_mode) && strcmp(dn->name, ".") != 0 &&
strcmp(dn->name, "..") != 0);
}
int countdirs(struct dnode **dn, int nfiles) int countdirs(struct dnode **dn, int nfiles)
{ {
int i, dirs; int i, dirs;
/* count how many dirs and regular files there are */
if (dn==NULL || nfiles < 1) return(0); if (dn==NULL || nfiles < 1) return(0);
dirs= 0; dirs= 0;
for (i=0; i<nfiles; i++) { for (i=0; i<nfiles; i++) {
@ -249,6 +255,18 @@ int countdirs(struct dnode **dn, int nfiles)
return(dirs); return(dirs);
} }
int countsubdirs(struct dnode **dn, int nfiles)
{
int i, subdirs;
if (dn == NULL || nfiles < 1) return 0;
subdirs = 0;
for (i = 0; i < nfiles; i++)
if (is_subdir(dn[i]))
subdirs++;
return subdirs;
}
int countfiles(struct dnode **dnp) int countfiles(struct dnode **dnp)
{ {
int nfiles; int nfiles;
@ -296,9 +314,13 @@ struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
if (dn==NULL || nfiles < 1) return(NULL); if (dn==NULL || nfiles < 1) return(NULL);
/* count how many dirs and regular files there are */ /* count how many dirs and regular files there are */
dncnt= countdirs(dn, nfiles); /* assume we are looking for dirs */ if (which == SPLIT_SUBDIR)
if (which != SPLIT_DIR) dncnt = countsubdirs(dn, nfiles);
dncnt= nfiles - dncnt; /* looking for files */ else {
dncnt= countdirs(dn, nfiles); /* assume we are looking for dirs */
if (which == SPLIT_FILE)
dncnt= nfiles - dncnt; /* looking for files */
}
/* allocate a file array and a dir array */ /* allocate a file array and a dir array */
dnp= dnalloc(dncnt); dnp= dnalloc(dncnt);
@ -309,6 +331,10 @@ struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
if (S_ISDIR(dn[i]->dstat.st_mode)) { if (S_ISDIR(dn[i]->dstat.st_mode)) {
dnp[d++]= dn[i]; dnp[d++]= dn[i];
} /* else skip the file */ } /* else skip the file */
} else if (which == SPLIT_SUBDIR) {
if (is_subdir(dn[i])) {
dnp[d++]= dn[i];
} /* else skip the file or dir */
} else { } else {
if (!(S_ISDIR(dn[i]->dstat.st_mode))) { if (!(S_ISDIR(dn[i]->dstat.st_mode))) {
dnp[d++]= dn[i]; dnp[d++]= dn[i];
@ -455,8 +481,8 @@ void showdirs(struct dnode **dn, int ndirs)
#ifdef BB_FEATURE_LS_RECURSIVE #ifdef BB_FEATURE_LS_RECURSIVE
if (disp_opts & DISP_RECURSIVE) { if (disp_opts & DISP_RECURSIVE) {
/* recursive- list the sub-dirs */ /* recursive- list the sub-dirs */
dnd= splitdnarray(subdnp, nfiles, SPLIT_DIR); dnd= splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
dndirs= countdirs(subdnp, nfiles); dndirs= countsubdirs(subdnp, nfiles);
if (dndirs > 0) { if (dndirs > 0) {
#ifdef BB_FEATURE_LS_SORTFILES #ifdef BB_FEATURE_LS_SORTFILES
shellsort(dnd, dndirs); shellsort(dnd, dndirs);