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
//
// 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 <trinity@trinity.moe>
* 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 <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> /* 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 <dirent.h> /* 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: