1
0

more magic

This commit is contained in:
dtb 2024-02-16 21:53:42 -07:00
parent ef590f67a9
commit b26f7dbf93

View File

@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR */
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
* struct dirent */
#include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */
#include <stdlib.h> /* realloc(3) */
#include <string.h> /* strerror(3) */
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
#include <getopt.h>
#include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */
#include <sysexits.h> /* EX_OK, EX_INVALID, EX_OSERR, EX_USAGE */
#include <unistd.h> /* getopt(3) */
static char *dot[] = {".", NULL}; /* default (argc<2) */
@ -38,9 +38,9 @@ fnprint(char *fn){
static char *argv0;
// Walks the directory named dirname, printing the names of all files it
// contains (but not the name of the directory itself). Returns 2 if dirname is
// not a directory and 1 if another error occurs.
/* Walks the directory named dirname, printing the names of all files it
* contains (but not the name of the directory itself).
* Returns something other than EX_OK with errno set if an error occurs. */
static int
walk(char *dirname){
DIR dir;
@ -50,23 +50,20 @@ walk(char *dirname){
char *np;
int retval;
if((dir = opendir(dirname)) == NULL){
fprintf(stdout, "%s: %s: %s\n",
argv0, dirname, strerror(errno));
return 1 + (errno == ENOTDIR);
}
fnprint(dirname);
if((dir = opendir(dirname)) == NULL)
return EX_NOINPUT;
errno = 0;
retval = 0;
while((f = readdir(dir)) != NULL){
if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0)
continue;
if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1)
> filename.a){
if((np = realloc(filename.p, l)) == NULL){
perror(argv0);
if((np = realloc(filename.p, l)) == NULL)
return EX_OSERR;
}else{
else{
filename.a = l;
filename.p = np; /* would you look at that */
}
@ -74,19 +71,28 @@ walk(char *dirname){
stpcpy(stpcpy(stpcpy(filename.p, dirname), "/"), f->d_name);
/* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */
fnprint(filename.p);
/* Walk the file if we can successfully open it as a directory.
* Don't worry about it if it's not one (walk(filename) == 2). */
if ((f->d_type == DT_DIR || f->d_type == DT_UNKNOWN)
&& walk(filename.p) == 1)
retval = 1;
}
if(errno != 0 || closedir(dir) != NULL){
fprintf(stderr, "%s: %s: %s\n",
argv0, dirname, strerror(errno));
retval = 1;
/* Walk the file if we can successfully open it as a
* directory. */
if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
if((retval = walk(filename.p)) != EX_OK){
if(retval == EX_OSERR)
return retval;
else{
fnprint(filename.p);
fprintf(stderr, "%s: %s: %s\n",
argv0, filename.p,
strerror(errno));
}
}
}else
fnprint(filename.p);
}
return retval;
if(errno != 0 || closedir(dir) != NULL)
fprintf(stderr, "%s: %s: %s\n",
argv0, dirname, strerror(errno));
return EX_OK;
}
int main(int argc, char *argv[]){
@ -117,10 +123,15 @@ int main(int argc, char *argv[]){
if(*argv == NULL)
argv = dot;
while(*argv != NULL){
fnprint(*argv);
retval |= walk(*(argv++));
}
while(*argv != NULL)
if((retval = walk(*(argv++))) != EX_OK)
switch(retval){
case EX_OSERR: perror(argv0); return retval;
case EX_NOINPUT:
fprintf(stderr, "%s: %s: %s\n",
argv0, *(argv - 1), strerror(errno));
break;
}
return retval;
return EX_OK;
}