diff options
| author | Peter Eisentraut | 2013-10-19 01:28:15 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2013-10-19 01:28:15 +0000 |
| commit | ba7c5975adea74c6f17bdb0e0427ad85962092a2 (patch) | |
| tree | 650de1234670c0ec325204ec6762b70bcd7d05a3 /src/common | |
| parent | cab5dc5daf2f6f5da0ce79deb399633b4bb443b5 (diff) | |
Move pgfnames() from libpgport to libpgcommon
It requires pstrdup() from libpgcommon.
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Makefile | 2 | ||||
| -rw-r--r-- | src/common/pgfnames.c | 109 |
2 files changed, 110 insertions, 1 deletions
diff --git a/src/common/Makefile b/src/common/Makefile index c5b98ab27bb..575a48a94de 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -23,7 +23,7 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -DFRONTEND $(CPPFLAGS) LIBS += $(PTHREAD_LIBS) -OBJS_COMMON = exec.o relpath.o wait_error.o +OBJS_COMMON = exec.o pgfnames.o relpath.o wait_error.o OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o diff --git a/src/common/pgfnames.c b/src/common/pgfnames.c new file mode 100644 index 00000000000..d3c7bbc7c6d --- /dev/null +++ b/src/common/pgfnames.c @@ -0,0 +1,109 @@ +/*------------------------------------------------------------------------- + * + * pgfnames.c + * directory handling functions + * + * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/common/pgfnames.c + * + *------------------------------------------------------------------------- + */ + +#ifndef FRONTEND +#include "postgres.h" +#else +#include "postgres_fe.h" +#endif + +#include <dirent.h> + +/* + * pgfnames + * + * return a list of the names of objects in the argument directory. Caller + * must call pgfnames_cleanup later to free the memory allocated by this + * function. + */ +char ** +pgfnames(const char *path) +{ + DIR *dir; + struct dirent *file; + char **filenames; + int numnames = 0; + int fnsize = 200; /* enough for many small dbs */ + + dir = opendir(path); + if (dir == NULL) + { +#ifndef FRONTEND + elog(WARNING, "could not open directory \"%s\": %m", path); +#else + fprintf(stderr, _("could not open directory \"%s\": %s\n"), + path, strerror(errno)); +#endif + return NULL; + } + + filenames = (char **) palloc(fnsize * sizeof(char *)); + + errno = 0; + while ((file = readdir(dir)) != NULL) + { + if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) + { + if (numnames + 1 >= fnsize) + { + fnsize *= 2; + filenames = (char **) repalloc(filenames, + fnsize * sizeof(char *)); + } + filenames[numnames++] = pstrdup(file->d_name); + } + errno = 0; + } +#ifdef WIN32 + + /* + * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in + * released version + */ + if (GetLastError() == ERROR_NO_MORE_FILES) + errno = 0; +#endif + if (errno) + { +#ifndef FRONTEND + elog(WARNING, "could not read directory \"%s\": %m", path); +#else + fprintf(stderr, _("could not read directory \"%s\": %s\n"), + path, strerror(errno)); +#endif + } + + filenames[numnames] = NULL; + + closedir(dir); + + return filenames; +} + + +/* + * pgfnames_cleanup + * + * deallocate memory used for filenames + */ +void +pgfnames_cleanup(char **filenames) +{ + char **fn; + + for (fn = filenames; *fn; fn++) + pfree(*fn); + + pfree(filenames); +} |
