Forgot to add basename. More fixes to du from
authorErik Andersen <andersen@codepoet.org>
Mon, 21 Feb 2000 17:27:17 +0000 (17:27 -0000)
committerErik Andersen <andersen@codepoet.org>
Mon, 21 Feb 2000 17:27:17 +0000 (17:27 -0000)
Friedrich Vedder <fwv@myrtle.lahn.de>.
 -Erik

Changelog
basename.c [new file with mode: 0644]
coreutils/basename.c [new file with mode: 0644]
coreutils/du.c
du.c

index a0cc13a..ffdca37 100644 (file)
--- 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 <apenwarr@worldvisions.ca>)
+           now it does (thanks to Avery Pennarun <apenwarr@worldvisions.ca>)
        * Several fixes from Pavel Roskin <pavel_roskin@geocities.com>:
            -  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 <pascal.bellard@ascend.com>
-       * 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 <fwv@myrtle.lahn.de> for the fix.
 
        -Erik Andersen
diff --git a/basename.c b/basename.c
new file mode 100644 (file)
index 0000000..1db885f
--- /dev/null
@@ -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 <andersen@lineo.com>, <andersee@debian.org>
+ *
+ * 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 <stdio.h>
+
+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 (file)
index 0000000..1db885f
--- /dev/null
@@ -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 <andersen@lineo.com>, <andersee@debian.org>
+ *
+ * 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 <stdio.h>
+
+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);
+}
+
index 02d1d97..4dc7ea1 100644 (file)
@@ -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 02d1d97..4dc7ea1 100644 (file)
--- 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 $ */