vapier      15/08/05 07:18:00

  Modified:             uname-test.c
  Log:
  support recursing into directories on the command line so we can quickly 
process the entire cpuinfo package

Revision  Changes    Path
1.11                 src/patchsets/coreutils/uname-test/uname-test.c

file : 
http://sources.gentoo.org/viewvc.cgi/gentoo/src/patchsets/coreutils/uname-test/uname-test.c?rev=1.11&view=markup
plain: 
http://sources.gentoo.org/viewvc.cgi/gentoo/src/patchsets/coreutils/uname-test/uname-test.c?rev=1.11&content-type=text/plain
diff : 
http://sources.gentoo.org/viewvc.cgi/gentoo/src/patchsets/coreutils/uname-test/uname-test.c?r1=1.10&r2=1.11

Index: uname-test.c
===================================================================
RCS file: /var/cvsroot/gentoo/src/patchsets/coreutils/uname-test/uname-test.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- uname-test.c        5 Aug 2015 07:15:25 -0000       1.10
+++ uname-test.c        5 Aug 2015 07:18:00 -0000       1.11
@@ -5,6 +5,7 @@
 
 #define _GNU_SOURCE
 #include <ctype.h>
+#include <dirent.h>
 #include <err.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -12,6 +13,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #ifndef ARRAY_SIZE
 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -205,6 +208,56 @@
        return ret;
 }
 
+static bool process_dir(const char *path)
+{
+       bool ret = true;
+       char *sub_path;
+
+       DIR *dir = opendir(path);
+       if (!dir)
+               err(1, "could not opendir %s", path);
+
+       struct dirent *de;
+       while ((de = readdir(dir)) != NULL) {
+               if (de->d_name[0] == '.')
+                       continue;
+
+               asprintf(&sub_path, "%s/%s", path, de->d_name);
+               switch (de->d_type) {
+               case DT_REG:
+                       if (!process_file(sub_path))
+                               ret = false;
+                       break;
+               case DT_DIR:
+                       if (!process_dir(sub_path))
+                               ret = false;
+                       break;
+               }
+               free(sub_path);
+       }
+
+       closedir(dir);
+
+       return ret;
+}
+
+static bool process_one(const char *path)
+{
+       struct stat st;
+
+       if (stat(path, &st)) {
+               warn("could not stat %s", path);
+               return false;
+       }
+
+       if (S_ISREG(st.st_mode))
+               return process_file(path);
+       else if (S_ISDIR(st.st_mode))
+               return process_dir(path);
+       else
+               return false;
+}
+
 int main(int argc, char *argv[])
 {
        int i, ret;
@@ -216,7 +269,7 @@
                        break;
                default:
  usage:
-                       errx(1, "Usage: uname-test [-v] <file> [files]");
+                       errx(1, "Usage: uname-test [-v] <file|dir> [more 
paths]");
                }
        }
        if (optind == argc)
@@ -224,7 +277,7 @@
 
        ret = 0;
        for (i = optind; i < argc; ++i)
-               if (!process_file(argv[i]))
+               if (!process_one(argv[i]))
                        ret = 1;
 
        return ret;




Reply via email to