chiark / gitweb /
checkpath.c: Hoist the `buf' used for link targets for reuse.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 16 Jul 2024 13:10:34 +0000 (14:10 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 17 Jul 2024 12:23:10 +0000 (13:23 +0100)
And also because I seriously dislike variables declared in inner scopes
now.

checkpath.c

index 12038dd7a266b9165baebb5e96c7226f72b02e55..d874246025f69ed4b69fe14259226569304a88fa 100644 (file)
@@ -352,6 +352,7 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
   struct elt *e, *ee;
   struct stat st;
   unsigned bad = 0;
   struct elt *e, *ee;
   struct stat st;
   unsigned bad = 0;
+  dstr buf = DSTR_INIT;
 
   /* --- Initialize stack pointer and path string --- */
 
 
   /* --- Initialize stack pointer and path string --- */
 
@@ -419,11 +420,11 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
     /* --- Handle symbolic links specially --- */
 
     if (S_ISLNK(st.st_mode)) {
     /* --- Handle symbolic links specially --- */
 
     if (S_ISLNK(st.st_mode)) {
-      dstr buf = DSTR_INIT;
       int i;
 
       /* --- Resolve the link --- */
 
       int i;
 
       /* --- Resolve the link --- */
 
+      dstr_reset(&buf);
       dstr_ensure(&buf, st.st_size + 1);
       if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
        report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
       dstr_ensure(&buf, st.st_size + 1);
       if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
        report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
@@ -455,7 +456,6 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
       else
        pop();
       ee = splitpath(buf.buf, ee);
       else
        pop();
       ee = splitpath(buf.buf, ee);
-      dstr_destroy(&buf);
       continue;
     }
 
       continue;
     }
 
@@ -488,6 +488,7 @@ unsigned checkpath(const char *p, const struct checkpath *cp)
   }
 
   popall();
   }
 
   popall();
+  dstr_destroy(&buf);
   return (bad);
 }
 
   return (bad);
 }