From: Mark Wooding Date: Tue, 16 Jul 2024 13:10:34 +0000 (+0100) Subject: checkpath.c: Hoist the `buf' used for link targets for reuse. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/checkpath/commitdiff_plain/de1bcaacf00696ba09688b51c6c0bbfd8b60d778 checkpath.c: Hoist the `buf' used for link targets for reuse. And also because I seriously dislike variables declared in inner scopes now. --- diff --git a/checkpath.c b/checkpath.c index 12038dd..d874246 100644 --- a/checkpath.c +++ b/checkpath.c @@ -352,6 +352,7 @@ unsigned checkpath(const char *p, const struct checkpath *cp) struct elt *e, *ee; struct stat st; unsigned bad = 0; + dstr buf = DSTR_INIT; /* --- 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)) { - dstr buf = DSTR_INIT; 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"); @@ -455,7 +456,6 @@ unsigned checkpath(const char *p, const struct checkpath *cp) else pop(); ee = splitpath(buf.buf, ee); - dstr_destroy(&buf); continue; } @@ -488,6 +488,7 @@ unsigned checkpath(const char *p, const struct checkpath *cp) } popall(); + dstr_destroy(&buf); return (bad); }