[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH 5/6] libutil: Convert rootdir to a dynamically allocated arra
From: |
Punit Agrawal |
Subject: |
[RFC PATCH 5/6] libutil: Convert rootdir to a dynamically allocated array |
Date: |
Mon, 23 Jan 2017 22:43:38 +0000 |
In preparation for dropping the use of PATH_MAX, convert rootdir to a
dynamically allocated array. As rootdir is used to cache state across
function calls (iterating over files), free the buffer when it is no
longer being used (find_close).
---
libutil/find.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/libutil/find.c b/libutil/find.c
index 1863df4..14d59d5 100644
--- a/libutil/find.c
+++ b/libutil/find.c
@@ -97,7 +97,7 @@ static regex_t *skip; /**< regex for skipping
units */
static regex_t *suff; /**< regex for suffixes */
static FILE *ip;
static FILE *temp;
-static char rootdir[PATH_MAX];
+static char *rootdir;
static char cwddir[MAXPATHLEN];
static int find_mode;
static int find_eof;
@@ -635,7 +635,7 @@ find_open(const char *start, int explain)
if (!start)
start = "./";
- if (realpath(start, rootdir) == NULL)
+ if ((rootdir = realpath(start, NULL)) == NULL)
die("cannot get real path of '%s'.", trimpath(dir));
/*
* setup stack.
@@ -664,6 +664,7 @@ find_open(const char *start, int explain)
void
find_open_filelist(const char *filename, const char *root, int explain)
{
+ size_t rootdir_len;
assert(find_mode == 0);
find_mode = FILELIST_OPEN;
find_explain = explain;
@@ -689,11 +690,19 @@ find_open_filelist(const char *filename, const char
*root, int explain)
}
/*
* rootdir always ends with '/'.
+ *
+ * rootdir_len is two characters longer than root to be able
+ * to append a "/" if needed and the string terminator
+ * ofcourse.
*/
- if (!strcmp(root+ROOT, "/"))
- strlimcpy(rootdir, root, sizeof(rootdir));
- else
- snprintf(rootdir, sizeof(rootdir), "%s/", root);
+ rootdir_len = strlen(root) + 2;
+ rootdir = malloc(rootdir_len);
+ if (!rootdir)
+ die("short of memory.");
+
+ snprintf(rootdir, rootdir_len, "%s%s", root,
+ strcmp(root+ROOT, "/") ? "/" : "");
+
strlimcpy(cwddir, root, sizeof(cwddir));
}
/**
@@ -909,6 +918,8 @@ find_close(void)
} else {
die("find_close: internal error.");
}
+ if (rootdir)
+ free(rootdir);
if (suff)
regfree(suff);
if (skip)
--
2.11.0
- [RFC PATCH 0/6] Enable building Gnu Global package on Hurd, Punit Agrawal, 2017/01/24
- [RFC PATCH 3/6] libutil: Remove usage of PATH_MAX from getrealpath, Punit Agrawal, 2017/01/24
- [RFC PATCH 1/6] libutil: Drop extraneous function declarations, Punit Agrawal, 2017/01/24
- [RFC PATCH 2/6] libutil: Staticize locally used functions, Punit Agrawal, 2017/01/24
- [RFC PATCH 5/6] libutil: Convert rootdir to a dynamically allocated array,
Punit Agrawal <=
- [RFC PATCH 4/6] libutil: Remove usage of PATH_MAX from has_symlinkloop, Punit Agrawal, 2017/01/24
- [RFC PATCH 6/6] libutil: Drop the requirement for PATH_MAX macro, Punit Agrawal, 2017/01/24
- Re: [RFC PATCH 0/6] Enable building Gnu Global package on Hurd, Shigio YAMAGUCHI, 2017/01/25