summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew G. Morgan <morgan@kernel.org>2020-05-16 15:03:10 -0700
committerAndrew G. Morgan <morgan@kernel.org>2020-05-16 15:03:10 -0700
commit2e0db2fc58a2da02f318ef41bb481d58f4c4268f (patch)
tree45800c600e25400ac85cdc6693b7892d9840349d
parent370c16a5e0ee04ca255a089aa7aad697984abfc2 (diff)
downloadlibcap2-2e0db2fc58a2da02f318ef41bb481d58f4c4268f.tar.gz
Slight refactoring of _makenames.c.
I'm generally in favor of using zero'd memory. Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
-rw-r--r--libcap/_makenames.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/libcap/_makenames.c b/libcap/_makenames.c
index 079e78c..02ebbdf 100644
--- a/libcap/_makenames.c
+++ b/libcap/_makenames.c
@@ -21,25 +21,34 @@ struct {
{NULL, -1}
};
+/*
+ * recalloc uses realloc to grow some memory but it resets the
+ * indicated extended empty space.
+ */
+static void *recalloc(void *p, int was, int is) {
+ void *n = realloc(p, is);
+ if (!n) {
+ fputs("out of memory", stderr);
+ exit(1);
+ }
+ memset(n+was, 0, is-was);
+ return n;
+}
+
int main(void)
{
int i, maxcaps=0, maxlength=0;
const char **pointers = NULL, **pointers_tmp;
int pointers_avail = 0;
-
for ( i=0; list[i].index >= 0 && list[i].name; ++i ) {
if (maxcaps <= list[i].index) {
maxcaps = list[i].index + 1;
}
if (list[i].index >= pointers_avail) {
- pointers_avail = 2 * list[i].index + 1;
- pointers_tmp = realloc(pointers, pointers_avail * sizeof(char *));
- if (!pointers_tmp) {
- fputs("out of memory", stderr);
- exit(1);
- }
- pointers = pointers_tmp;
+ int was = pointers_avail * sizeof(char *);
+ pointers_avail = 2 * list[i].index + 1;
+ pointers = recalloc(pointers, was, pointers_avail * sizeof(char *));
}
pointers[list[i].index] = list[i].name;
int n = strlen(list[i].name);
@@ -60,10 +69,11 @@ int main(void)
"#define LIBCAP_CAP_NAMES { \\\n", maxcaps, maxlength+1);
for (i=0; i<maxcaps; ++i) {
- if (pointers[i])
+ if (pointers[i]) {
printf(" /* %d */\t\"%s\", \\\n", i, pointers[i]);
- else
+ } else {
printf(" /* %d */\tNULL,\t\t/* - presently unused */ \\\n", i);
+ }
}
printf(" }\n"