From 149f25542875da356534294eb67329214a1a3b16 Mon Sep 17 00:00:00 2001 From: DTB Date: Sun, 18 Feb 2024 00:51:08 -0700 Subject: [PATCH] moar --- walk/Makefile | 1 + walk/walk.c | 102 +++++++++++++++++++++++++++++--------------------- 2 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 walk/Makefile diff --git a/walk/Makefile b/walk/Makefile new file mode 100644 index 0000000..edb9445 --- /dev/null +++ b/walk/Makefile @@ -0,0 +1 @@ +walk: walk.c diff --git a/walk/walk.c b/walk/walk.c index f85dc83..301cf9e 100644 --- a/walk/walk.c +++ b/walk/walk.c @@ -1,25 +1,49 @@ -// 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. +/* + * Copyright (c) 2024 DTB + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * 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 + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * 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 /* closedir(3), opendir(3), readdir(3), DIR, - * struct dirent */ #include /* errno */ #include /* fprintf(3), perror(3), stderr, stdout */ #include /* realloc(3) */ #include /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */ -#include /* EX_OK, EX_INVALID, EX_OSERR, EX_USAGE */ +#if !defined EX_OK || !defined EX_INVALID || !defined EX_OSERR \ + || !defined EX_USAGE +# include +#endif #include /* getopt(3) */ +#include /* closedir(3), opendir(3), readdir(3), DIR, + * struct dirent */ static char *dot[] = {".", NULL}; /* default (argc<2) */ @@ -36,16 +60,14 @@ fnprint(char *fn){ return fprintf(stdout, "%s%s", fn, terminator); } -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 something other than EX_OK with errno set if an error occurs. */ static int -walk(char *dirname){ - DIR dir; +walk(char *dirname, char *argv0){ + DIR *dir; 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; char *np; int retval; @@ -56,50 +78,46 @@ walk(char *dirname){ return EX_NOINPUT; errno = 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) + if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1) > filename.a){ + if((np = realloc(filename.s, l)) == NULL) return EX_OSERR; - else{ - filename.a = l; - filename.p = np; /* would you look at that */ - } + filename.a = l; + filename.s = np; } - stpcpy(stpcpy(stpcpy(filename.p, dirname), "/"), f->d_name); + stpcpy(stpcpy(stpcpy(filename.s, 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. */ 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) return retval; - else{ - fnprint(filename.p); + else if(retval != EX_NOINPUT) fprintf(stderr, "%s: %s: %s\n", - argv0, filename.p, - strerror(errno)); - } - } + argv0, filename.s, strerror(errno)); + }else + fnprint(filename.s); }else - fnprint(filename.p); + fnprint(filename.s); } - if(errno != 0 || closedir(dir) != NULL) - fprintf(stderr, "%s: %s: %s\n", - argv0, dirname, strerror(errno)); + if(errno != 0 || closedir(dir) != 0) + fprintf(stderr, "%s: %s: %s\n", argv0, dirname, strerror(errno)); return EX_OK; } int main(int argc, char *argv[]){ + char *argv0; int c; int retval; argv0 = argv[0]; + terminator = asv_terminator; if(argc > 0){ while((c = getopt(argc, argv, "0d:l:n")) != -1) @@ -124,7 +142,7 @@ int main(int argc, char *argv[]){ argv = dot; while(*argv != NULL) - if((retval = walk(*(argv++))) != EX_OK) + if((retval = walk(*(argv++), argv0)) != EX_OK) switch(retval){ case EX_OSERR: perror(argv0); return retval; case EX_NOINPUT: