changeset 1372:d46baa9adb5f

Bugfixes for cleaned up makedevs. Newline after table = <stdin>, move start/increment/count loop around entire device creation if/else staircase including chmod/chown code and use ptr to record node vs toybuf.
author Rob Landley <rob@landley.net>
date Mon, 30 Jun 2014 04:58:37 -0500
parents 0a87952492ae
children a3ee4f91462a
files toys/pending/makedevs.c
diffstat 1 files changed, 26 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/makedevs.c	Sat Jun 28 22:47:40 2014 -0500
+++ b/toys/pending/makedevs.c	Mon Jun 30 04:58:37 2014 -0500
@@ -49,11 +49,11 @@
   if (toys.optflags & FLAG_d && strcmp(TT.fname, "-")) {
     fd = xopen(TT.fname, O_RDONLY);
     xprintf("table = %s\n", TT.fname);
-  } else xprintf("table = <stdin>");
+  } else xprintf("table = <stdin>\n");
   xchdir(*toys.optargs);
 
   for (line_no = 0; (line = get_line(fd)); free(line)) {
-    char type=0, str[64], user[64], group[64], *node = str, *ptr = line;
+    char type=0, user[64], group[64], *node, *ptr = line;
     unsigned int mode = 0755, major = 0, minor = 0, cnt = 0, incr = 0, 
                  st_val = 0;
     uid_t uid;
@@ -63,9 +63,10 @@
     line_no++;
     while (isspace(*ptr)) ptr++;
     if (!*ptr || *ptr == '#') continue;
+    node = ptr;
 
     while (*ptr && !isspace(*ptr)) ptr++;
-    if (*ptr) *ptr++ = 0;
+    if (*ptr) *(ptr++) = 0;
     *user = *group = 0;
     sscanf(ptr, "%c %o %63s %63s %u %u %u %u %u", &type, &mode,
            user, group, &major, &minor, &st_val, &incr, &cnt);
@@ -100,28 +101,31 @@
     } else gid = getgid();
 
     while (*node == '/') node++; // using relative path
-    if (type == 'd') {
-      if (mkpathat(AT_FDCWD, node, mode, 3))  {
-        perror_msg("can't create directory '%s'", node);
+
+    for (i = 0; (!cnt && !i) || i < cnt; i++) {
+      if (cnt) {
+        snprintf(toybuf, sizeof(toybuf), "%s%u", node, st_val + i);
+        ptr = toybuf;
+      } else ptr = node;
+
+      if (type == 'd') {
+        if (mkpathat(AT_FDCWD, ptr, mode, 3))  {
+          perror_msg("can't create directory '%s'", ptr);
+          continue;
+        }
+      } else if (type == 'f') {
+        if (stat(ptr, &st) || !S_ISREG(st.st_mode)) {
+          perror_msg("line %d: file '%s' does not exist", line_no, ptr);
+          continue;
+        }
+      } else if (mknod(ptr, mode, makedev(major, minor + i*incr))) {
+        perror_msg("line %d: can't create node '%s'", line_no, ptr);
         continue;
       }
-    } else if (type == 'f') {
-      if (stat(node, &st) || !S_ISREG(st.st_mode)) {
-        perror_msg("line %d: regular file '%s' does not exist", line_no, node);
-        continue;
-      }
-    } else {
-      if (cnt) --cnt;
-      for (i = 0; i <= cnt; i++) {
-        sprintf(toybuf, cnt ? "%s%u" : "%s", node, st_val + i);
-        if (mknod(toybuf, mode, makedev(major, minor + i*incr))) {
-          perror_msg("line %d: can't create node '%s'", line_no, toybuf);
-          continue;
-        }
-      }
+
+      if (chown(ptr, uid, gid) || chmod(ptr, mode)) 
+        perror_msg("line %d: can't chown/chmod '%s'", line_no, ptr);
     }
-    if (chown(toybuf, uid, gid) || chmod(toybuf, mode)) 
-      perror_msg("line %d: can't chown/chmod '%s'", line_no, toybuf);
   }
   xclose(fd);
 }