Fix erroneous -Werror=missing-braces on old GCC.
[pgsql.git] / src / common / pgfnames.c
blob9d2fe9d6592a34baa6702dc375e64c21036aef90
1 /*-------------------------------------------------------------------------
3 * pgfnames.c
4 * directory handling functions
6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * IDENTIFICATION
10 * src/common/pgfnames.c
12 *-------------------------------------------------------------------------
15 #ifndef FRONTEND
16 #include "postgres.h"
17 #else
18 #include "postgres_fe.h"
19 #endif
21 #include <dirent.h>
23 #ifndef FRONTEND
24 #define pg_log_warning(...) elog(WARNING, __VA_ARGS__)
25 #else
26 #include "common/logging.h"
27 #endif
30 * pgfnames
32 * return a list of the names of objects in the argument directory. Caller
33 * must call pgfnames_cleanup later to free the memory allocated by this
34 * function.
36 char **
37 pgfnames(const char *path)
39 DIR *dir;
40 struct dirent *file;
41 char **filenames;
42 int numnames = 0;
43 int fnsize = 200; /* enough for many small dbs */
45 dir = opendir(path);
46 if (dir == NULL)
48 pg_log_warning("could not open directory \"%s\": %m", path);
49 return NULL;
52 filenames = (char **) palloc(fnsize * sizeof(char *));
54 while (errno = 0, (file = readdir(dir)) != NULL)
56 if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
58 if (numnames + 1 >= fnsize)
60 fnsize *= 2;
61 filenames = (char **) repalloc(filenames,
62 fnsize * sizeof(char *));
64 filenames[numnames++] = pstrdup(file->d_name);
68 if (errno)
69 pg_log_warning("could not read directory \"%s\": %m", path);
71 filenames[numnames] = NULL;
73 if (closedir(dir))
74 pg_log_warning("could not close directory \"%s\": %m", path);
76 return filenames;
81 * pgfnames_cleanup
83 * deallocate memory used for filenames
85 void
86 pgfnames_cleanup(char **filenames)
88 char **fn;
90 for (fn = filenames; *fn; fn++)
91 pfree(*fn);
93 pfree(filenames);