1
0
This commit is contained in:
dtb 2024-02-18 00:51:08 -07:00
parent b26f7dbf93
commit 149f255428
2 changed files with 61 additions and 42 deletions

1
walk/Makefile Normal file
View File

@ -0,0 +1 @@
walk: walk.c

View File

@ -1,25 +1,49 @@
// Copyright 2019 Google LLC /*
// * Copyright (c) 2024 DTB <trinity@trinity.moe>
// Licensed under the Apache License, Version 2.0 (the "License"); * SPDX-License-Identifier: AGPL-3.0-or-later
// you may not use this file except in compliance with the License. *
// You may obtain a copy of the License at * This program is free software: you can redistribute it and/or modify it under
// * the terms of the GNU Affero General Public License as published by the Free
// https://www.apache.org/licenses/LICENSE-2.0 * Software Foundation, either version 3 of the License, or (at your option) any
// * later version.
// Unless required by applicable law or agreed to in writing, software *
// distributed under the License is distributed on an "AS IS" BASIS, * This program is distributed in the hope that it will be useful, but WITHOUT
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// See the License for the specific language governing permissions and * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// limitations under the License. * details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* This file incorporates work covered by the following copyright and permission
* notice:
*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
* struct dirent */
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */ #include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */
#include <stdlib.h> /* realloc(3) */ #include <stdlib.h> /* realloc(3) */
#include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */ #include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */
#include <sysexits.h> /* EX_OK, EX_INVALID, EX_OSERR, EX_USAGE */ #if !defined EX_OK || !defined EX_INVALID || !defined EX_OSERR \
|| !defined EX_USAGE
# include <sysexits.h>
#endif
#include <unistd.h> /* getopt(3) */ #include <unistd.h> /* getopt(3) */
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
* struct dirent */
static char *dot[] = {".", NULL}; /* default (argc<2) */ static char *dot[] = {".", NULL}; /* default (argc<2) */
@ -36,16 +60,14 @@ fnprint(char *fn){
return fprintf(stdout, "%s%s", fn, terminator); return fprintf(stdout, "%s%s", fn, terminator);
} }
static char *argv0;
/* Walks the directory named dirname, printing the names of all files it /* Walks the directory named dirname, printing the names of all files it
* contains (but not the name of the directory itself). * contains (but not the name of the directory itself).
* Returns something other than EX_OK with errno set if an error occurs. */ * Returns something other than EX_OK with errno set if an error occurs. */
static int static int
walk(char *dirname){ walk(char *dirname, char *argv0){
DIR dir; DIR *dir;
struct dirent *f; struct dirent *f;
static struct { size_t a; size_t s; char *p; } filename = {0, 0, NULL}; static struct { size_t a; char *s; } filename = { 0, NULL };
size_t l; size_t l;
char *np; char *np;
int retval; int retval;
@ -56,50 +78,46 @@ walk(char *dirname){
return EX_NOINPUT; return EX_NOINPUT;
errno = 0; errno = 0;
while((f = readdir(dir)) != NULL){ while((f = readdir(dir)) != NULL){
if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0) if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0)
continue; continue;
if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1) if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1) > filename.a){
> filename.a){ if((np = realloc(filename.s, l)) == NULL)
if((np = realloc(filename.p, l)) == NULL)
return EX_OSERR; return EX_OSERR;
else{
filename.a = l; filename.a = l;
filename.p = np; /* would you look at that */ filename.s = np;
} }
} stpcpy(stpcpy(stpcpy(filename.s, dirname), "/"), f->d_name);
stpcpy(stpcpy(stpcpy(filename.p, dirname), "/"), f->d_name);
/* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */ /* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */
fnprint(filename.p);
/* Walk the file if we can successfully open it as a /* Walk the file if we can successfully open it as a
* directory. */ * directory. */
if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){ if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
if((retval = walk(filename.p)) != EX_OK){ if((retval = walk(filename.s, argv0)) != EX_OK){
if(retval == EX_OSERR) if(retval == EX_OSERR)
return retval; return retval;
else{ else if(retval != EX_NOINPUT)
fnprint(filename.p);
fprintf(stderr, "%s: %s: %s\n", fprintf(stderr, "%s: %s: %s\n",
argv0, filename.p, argv0, filename.s, strerror(errno));
strerror(errno));
}
}
}else }else
fnprint(filename.p); fnprint(filename.s);
}else
fnprint(filename.s);
} }
if(errno != 0 || closedir(dir) != NULL) if(errno != 0 || closedir(dir) != 0)
fprintf(stderr, "%s: %s: %s\n", fprintf(stderr, "%s: %s: %s\n", argv0, dirname, strerror(errno));
argv0, dirname, strerror(errno));
return EX_OK; return EX_OK;
} }
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
char *argv0;
int c; int c;
int retval; int retval;
argv0 = argv[0]; argv0 = argv[0];
terminator = asv_terminator;
if(argc > 0){ if(argc > 0){
while((c = getopt(argc, argv, "0d:l:n")) != -1) while((c = getopt(argc, argv, "0d:l:n")) != -1)
@ -124,7 +142,7 @@ int main(int argc, char *argv[]){
argv = dot; argv = dot;
while(*argv != NULL) while(*argv != NULL)
if((retval = walk(*(argv++))) != EX_OK) if((retval = walk(*(argv++), argv0)) != EX_OK)
switch(retval){ switch(retval){
case EX_OSERR: perror(argv0); return retval; case EX_OSERR: perror(argv0); return retval;
case EX_NOINPUT: case EX_NOINPUT: