From 42387e496463f6dbba168b5d36807b858f94fa96 Mon Sep 17 00:00:00 2001 From: Erik Andersen Date: Mon, 21 Feb 2000 17:27:17 +0000 Subject: [PATCH] Forgot to add basename. More fixes to du from Friedrich Vedder . -Erik --- Changelog | 8 ++++---- basename.c | 40 +++++++++++++++++++++++++++++++++++++ coreutils/basename.c | 40 +++++++++++++++++++++++++++++++++++++ coreutils/du.c | 47 ++++++++++++++++++++++++++++++++++++-------- du.c | 47 ++++++++++++++++++++++++++++++++++++-------- 5 files changed, 162 insertions(+), 20 deletions(-) create mode 100644 basename.c create mode 100644 coreutils/basename.c diff --git a/Changelog b/Changelog index a0cc13aa3..ffdca37ec 100644 --- a/Changelog +++ b/Changelog @@ -1,15 +1,15 @@ 0.43 - * Wrote basename + * Wrote basename. * tar wouldn't create directory entries that don't end in '/', - now it does (fix thanks to Avery Pennarun ) + now it does (thanks to Avery Pennarun ) * Several fixes from Pavel Roskin : - When `tail' fails to open a file it now exits. - When `syslogd' is given the `-n' option it should still use fork() for running klogd. * nslookup types are now changed to u_int32_t (instead of uint32_t) changed per a patch from Pascal Bellard - * Fixed "du" so it gives the same answers as GNU "du" (busybox du used to - count hard-linked files more then once). Many thanks to + * Fixed "du" so it gives the same answers as GNU "du" (busybox du used + to count hard-linked files more then once). Many thanks to Friedrich Vedder for the fix. -Erik Andersen diff --git a/basename.c b/basename.c new file mode 100644 index 000000000..1db885f62 --- /dev/null +++ b/basename.c @@ -0,0 +1,40 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini basename implementation for busybox + * + * Copyright (C) 1999 by Lineo, inc. + * Written by Erik Andersen , + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "internal.h" +#include + +extern int basename_main(int argc, char **argv) +{ + char* s; + + if ((argc < 2) || (**(argv + 1) == '-')) { + usage("basename [file ...]\n"); + } + argv++; + + s = strrchr(*argv, '/'); + printf("%s\n", (s)? s + 1 : *argv); + exit(TRUE); +} + diff --git a/coreutils/basename.c b/coreutils/basename.c new file mode 100644 index 000000000..1db885f62 --- /dev/null +++ b/coreutils/basename.c @@ -0,0 +1,40 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini basename implementation for busybox + * + * Copyright (C) 1999 by Lineo, inc. + * Written by Erik Andersen , + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "internal.h" +#include + +extern int basename_main(int argc, char **argv) +{ + char* s; + + if ((argc < 2) || (**(argv + 1) == '-')) { + usage("basename [file ...]\n"); + } + argv++; + + s = strrchr(*argv, '/'); + printf("%s\n", (s)? s + 1 : *argv); + exit(TRUE); +} + diff --git a/coreutils/du.c b/coreutils/du.c index 02d1d9737..4dc7ea13a 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -102,14 +102,30 @@ static void add_inode(const ino_t ino) inode_hash_list[i] = inode; } +/* Clear inode hash list */ +static void reset_inode_list(void) +{ + int i; + INODETYPE *inode; + + for (i = 0; i < HASH_SIZE; i++) { + while (inode_hash_list[i] != NULL) { + inode = inode_hash_list[i]->next; + free(inode_hash_list[i]); + inode_hash_list[i] = inode; + } + } +} + /* tiny recursive du */ static long du(char *filename) { struct stat statbuf; long sum; + int len; if ((lstat(filename, &statbuf)) != 0) { - fprintf(stdout, "du: %s: %s\n", filename, strerror(errno)); + printf("du: %s: %s\n", filename, strerror(errno)); return 0; } @@ -118,7 +134,9 @@ static long du(char *filename) /* Don't add in stuff pointed to by symbolic links */ if (S_ISLNK(statbuf.st_mode)) { - return 0; + sum = 0L; + if (du_depth == 1) + print(sum, filename); } if (S_ISDIR(statbuf.st_mode)) { DIR *dir; @@ -126,8 +144,14 @@ static long du(char *filename) dir = opendir(filename); if (!dir) { + du_depth--; return 0; } + + len = strlen(filename); + if (filename[len - 1] == '/') + filename[--len] = '\0'; + while ((entry = readdir(dir))) { char newfile[PATH_MAX + 1]; char *name = entry->d_name; @@ -137,8 +161,9 @@ static long du(char *filename) continue; } - if (strlen(filename) + strlen(name) + 1 > PATH_MAX) { + if (len + strlen(name) + 1 > PATH_MAX) { fprintf(stderr, name_too_long, "du"); + du_depth--; return 0; } sprintf(newfile, "%s/%s", filename, name); @@ -150,9 +175,14 @@ static long du(char *filename) } else if (statbuf.st_nlink > 1 && !count_hardlinks) { /* Add files with hard links only once */ - if (is_in_list(statbuf.st_ino)) - return 0; - add_inode(statbuf.st_ino); + if (is_in_list(statbuf.st_ino)) { + sum = 0L; + if (du_depth == 1) + print(sum, filename); + } + else { + add_inode(statbuf.st_ino); + } } du_depth--; return sum; @@ -198,13 +228,14 @@ int du_main(int argc, char **argv) for (; i < argc; i++) { sum = du(argv[i]); - if ((sum) && (isDirectory(argv[i], FALSE, NULL))) { + if (sum && isDirectory(argv[i], FALSE, NULL)) { print_normal(sum, argv[i]); } + reset_inode_list(); } } exit(0); } -/* $Id: du.c,v 1.14 2000/02/19 18:16:49 erik Exp $ */ +/* $Id: du.c,v 1.15 2000/02/21 17:27:17 erik Exp $ */ diff --git a/du.c b/du.c index 02d1d9737..4dc7ea13a 100644 --- a/du.c +++ b/du.c @@ -102,14 +102,30 @@ static void add_inode(const ino_t ino) inode_hash_list[i] = inode; } +/* Clear inode hash list */ +static void reset_inode_list(void) +{ + int i; + INODETYPE *inode; + + for (i = 0; i < HASH_SIZE; i++) { + while (inode_hash_list[i] != NULL) { + inode = inode_hash_list[i]->next; + free(inode_hash_list[i]); + inode_hash_list[i] = inode; + } + } +} + /* tiny recursive du */ static long du(char *filename) { struct stat statbuf; long sum; + int len; if ((lstat(filename, &statbuf)) != 0) { - fprintf(stdout, "du: %s: %s\n", filename, strerror(errno)); + printf("du: %s: %s\n", filename, strerror(errno)); return 0; } @@ -118,7 +134,9 @@ static long du(char *filename) /* Don't add in stuff pointed to by symbolic links */ if (S_ISLNK(statbuf.st_mode)) { - return 0; + sum = 0L; + if (du_depth == 1) + print(sum, filename); } if (S_ISDIR(statbuf.st_mode)) { DIR *dir; @@ -126,8 +144,14 @@ static long du(char *filename) dir = opendir(filename); if (!dir) { + du_depth--; return 0; } + + len = strlen(filename); + if (filename[len - 1] == '/') + filename[--len] = '\0'; + while ((entry = readdir(dir))) { char newfile[PATH_MAX + 1]; char *name = entry->d_name; @@ -137,8 +161,9 @@ static long du(char *filename) continue; } - if (strlen(filename) + strlen(name) + 1 > PATH_MAX) { + if (len + strlen(name) + 1 > PATH_MAX) { fprintf(stderr, name_too_long, "du"); + du_depth--; return 0; } sprintf(newfile, "%s/%s", filename, name); @@ -150,9 +175,14 @@ static long du(char *filename) } else if (statbuf.st_nlink > 1 && !count_hardlinks) { /* Add files with hard links only once */ - if (is_in_list(statbuf.st_ino)) - return 0; - add_inode(statbuf.st_ino); + if (is_in_list(statbuf.st_ino)) { + sum = 0L; + if (du_depth == 1) + print(sum, filename); + } + else { + add_inode(statbuf.st_ino); + } } du_depth--; return sum; @@ -198,13 +228,14 @@ int du_main(int argc, char **argv) for (; i < argc; i++) { sum = du(argv[i]); - if ((sum) && (isDirectory(argv[i], FALSE, NULL))) { + if (sum && isDirectory(argv[i], FALSE, NULL)) { print_normal(sum, argv[i]); } + reset_inode_list(); } } exit(0); } -/* $Id: du.c,v 1.14 2000/02/19 18:16:49 erik Exp $ */ +/* $Id: du.c,v 1.15 2000/02/21 17:27:17 erik Exp $ */