From 0e54d613ae73baae389f82279958af959c746290 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 29 Nov 2005 18:47:35 +0000 Subject: [PATCH] * modules/savedir (Depends-on): Add openat. * lib/savedir.h (fdsavedir): New decl. * lib/savedir.c (fdsavedir, savedirstream): New functions; the latter contains most of the former guts of savedir. (savedir): Use savedirstream. Include "openat.h". --- ChangeLog | 4 ++++ lib/ChangeLog | 8 ++++++++ lib/savedir.c | 40 +++++++++++++++++++++++++++++++--------- lib/savedir.h | 5 +++-- modules/savedir | 1 + 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0a864f95f..e8294a001 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2005-11-29 Paul Eggert + + * modules/savedir (Depends-on): Add openat. + 2005-11-25 Paul Eggert * modules/obstack (Files): Add m4/ulonglong.m4. diff --git a/lib/ChangeLog b/lib/ChangeLog index 5c8eb7a29..94df670f3 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,11 @@ +2005-11-29 Paul Eggert + + * savedir.h (fdsavedir): New decl. + * savedir.c (fdsavedir, savedirstream): New functions; the latter + contains most of the former guts of savedir. + (savedir): Use savedirstream. + Include "openat.h". + 2005-11-15 Paul Eggert * xstrtod.c: Don't bother with #pragma STDC FENV_ACCESS ON, as diff --git a/lib/savedir.c b/lib/savedir.c index 86930eb56..b08f32b79 100644 --- a/lib/savedir.c +++ b/lib/savedir.c @@ -55,27 +55,27 @@ #include #include +#include "openat.h" #include "xalloc.h" -/* Return a freshly allocated string containing the file names - in directory DIR, separated by '\0' characters; - the end is marked by two '\0' characters in a row. - Return NULL (setting errno) if DIR cannot be opened, read, or closed. */ - #ifndef NAME_SIZE_DEFAULT # define NAME_SIZE_DEFAULT 512 #endif -char * -savedir (const char *dir) +/* Return a freshly allocated string containing the file names + in directory DIRP, separated by '\0' characters; + the end is marked by two '\0' characters in a row. + Return NULL (setting errno) if DIRP cannot be read or closed. + If DIRP is NULL, return NULL without affecting errno. */ + +static char * +savedirstream (DIR *dirp) { - DIR *dirp; char *name_space; size_t allocated = NAME_SIZE_DEFAULT; size_t used = 0; int save_errno; - dirp = opendir (dir); if (dirp == NULL) return NULL; @@ -127,3 +127,25 @@ savedir (const char *dir) } return name_space; } + +/* Return a freshly allocated string containing the file names + in directory DIR, separated by '\0' characters; + the end is marked by two '\0' characters in a row. + Return NULL (setting errno) if DIR cannot be opened, read, or closed. */ + +char * +savedir (char const *dir) +{ + return savedirstream (opendir (dir)); +} + +/* Return a freshly allocated string containing the file names + in directory FD, separated by '\0' characters; + the end is marked by two '\0' characters in a row. + Return NULL (setting errno) if FD cannot be read or closed. */ + +char * +fdsavedir (int fd) +{ + return savedirstream (fdopendir (fd)); +} diff --git a/lib/savedir.h b/lib/savedir.h index 4828b1bec..7809d2392 100644 --- a/lib/savedir.h +++ b/lib/savedir.h @@ -1,6 +1,6 @@ /* Save the list of files in a directory in a string. - Copyright 1997, 1999, 2001, 2003 Free Software Foundation, Inc. + Copyright 1997, 1999, 2001, 2003, 2005 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,6 +21,7 @@ #if !defined SAVEDIR_H_ # define SAVEDIR_H_ -char *savedir (const char *dir); +char *savedir (char const *dir); +char *fdsavedir (int fd); #endif diff --git a/modules/savedir b/modules/savedir index fe9a027fd..e781af76b 100644 --- a/modules/savedir +++ b/modules/savedir @@ -7,6 +7,7 @@ lib/savedir.c m4/savedir.m4 Depends-on: +openat xalloc configure.ac: -- 2.11.0