savedir.c revision 56230
153469Sobrien/* savedir.c -- save the list of files in a directory in a string
256230Sru   Copyright (C) 1990, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
353469Sobrien
453469Sobrien   This program is free software; you can redistribute it and/or modify
553469Sobrien   it under the terms of the GNU General Public License as published by
653469Sobrien   the Free Software Foundation; either version 2, or (at your option)
753469Sobrien   any later version.
853469Sobrien
953469Sobrien   This program is distributed in the hope that it will be useful,
1053469Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1153469Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1253469Sobrien   GNU General Public License for more details.
1353469Sobrien
1453469Sobrien   You should have received a copy of the GNU General Public License
1553469Sobrien   along with this program; if not, write to the Free Software Foundation,
1653469Sobrien   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
1753469Sobrien
1853469Sobrien/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
1953469Sobrien
2053469Sobrien#if HAVE_CONFIG_H
2153469Sobrien# include <config.h>
2253469Sobrien#endif
2353469Sobrien
2453469Sobrien#include <sys/types.h>
2553469Sobrien
2653469Sobrien#if HAVE_UNISTD_H
2753469Sobrien# include <unistd.h>
2853469Sobrien#endif
2953469Sobrien
3053469Sobrien#if HAVE_DIRENT_H
3153469Sobrien# include <dirent.h>
3253469Sobrien# define NAMLEN(dirent) strlen((dirent)->d_name)
3353469Sobrien#else
3453469Sobrien# define dirent direct
3553469Sobrien# define NAMLEN(dirent) (dirent)->d_namlen
3653469Sobrien# if HAVE_SYS_NDIR_H
3753469Sobrien#  include <sys/ndir.h>
3853469Sobrien# endif
3953469Sobrien# if HAVE_SYS_DIR_H
4053469Sobrien#  include <sys/dir.h>
4153469Sobrien# endif
4253469Sobrien# if HAVE_NDIR_H
4353469Sobrien#  include <ndir.h>
4453469Sobrien# endif
4553469Sobrien#endif
4653469Sobrien
4753469Sobrien#ifdef CLOSEDIR_VOID
4853469Sobrien/* Fake a return value. */
4953469Sobrien# define CLOSEDIR(d) (closedir (d), 0)
5053469Sobrien#else
5153469Sobrien# define CLOSEDIR(d) closedir (d)
5253469Sobrien#endif
5353469Sobrien
5453469Sobrien#ifdef STDC_HEADERS
5553469Sobrien# include <stdlib.h>
5653469Sobrien# include <string.h>
5753469Sobrien#else
5853469Sobrienchar *malloc ();
5953469Sobrienchar *realloc ();
6053469Sobrien#endif
6153469Sobrien#ifndef NULL
6253469Sobrien# define NULL 0
6353469Sobrien#endif
6453469Sobrien
6553469Sobrien#ifndef stpcpy
6653469Sobrienchar *stpcpy ();
6753469Sobrien#endif
6853469Sobrien
6953469Sobrien#include "savedir.h"
7053469Sobrien
7153469Sobrien/* Return a freshly allocated string containing the filenames
7253469Sobrien   in directory DIR, separated by '\0' characters;
7353469Sobrien   the end is marked by two '\0' characters in a row.
7453469Sobrien   NAME_SIZE is the number of bytes to initially allocate
7553469Sobrien   for the string; it will be enlarged as needed.
7653469Sobrien   Return NULL if DIR cannot be opened or if out of memory. */
7753469Sobrien
7853469Sobrienchar *
7953469Sobriensavedir (dir, name_size)
8053469Sobrien     const char *dir;
8153469Sobrien     unsigned int name_size;
8253469Sobrien{
8353469Sobrien  DIR *dirp;
8453469Sobrien  struct dirent *dp;
8553469Sobrien  char *name_space;
8653469Sobrien  char *namep;
8753469Sobrien
8853469Sobrien  dirp = opendir (dir);
8953469Sobrien  if (dirp == NULL)
9053469Sobrien    return NULL;
9153469Sobrien
9256230Sru  /* Be sure name_size is at least `1' so there's room for
9356230Sru     the final NUL byte.  */
9456230Sru  name_size += !name_size;
9556230Sru
9653469Sobrien  name_space = (char *) malloc (name_size);
9753469Sobrien  if (name_space == NULL)
9853469Sobrien    {
9953469Sobrien      closedir (dirp);
10053469Sobrien      return NULL;
10153469Sobrien    }
10253469Sobrien  namep = name_space;
10353469Sobrien
10453469Sobrien  while ((dp = readdir (dirp)) != NULL)
10553469Sobrien    {
10653469Sobrien      /* Skip "." and ".." (some NFS filesystems' directories lack them). */
10753469Sobrien      if (dp->d_name[0] != '.'
10853469Sobrien	  || (dp->d_name[1] != '\0'
10953469Sobrien	      && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
11053469Sobrien	{
11153469Sobrien	  unsigned size_needed = (namep - name_space) + NAMLEN (dp) + 2;
11253469Sobrien
11353469Sobrien	  if (size_needed > name_size)
11453469Sobrien	    {
11553469Sobrien	      char *new_name_space;
11653469Sobrien
11753469Sobrien	      while (size_needed > name_size)
11853469Sobrien		name_size += 1024;
11953469Sobrien
12053469Sobrien	      new_name_space = realloc (name_space, name_size);
12153469Sobrien	      if (new_name_space == NULL)
12253469Sobrien		{
12353469Sobrien		  closedir (dirp);
12453469Sobrien		  return NULL;
12553469Sobrien		}
12653469Sobrien	      namep += new_name_space - name_space;
12753469Sobrien	      name_space = new_name_space;
12853469Sobrien	    }
12953469Sobrien	  namep = stpcpy (namep, dp->d_name) + 1;
13053469Sobrien	}
13153469Sobrien    }
13253469Sobrien  *namep = '\0';
13353469Sobrien  if (CLOSEDIR (dirp))
13453469Sobrien    {
13553469Sobrien      free (name_space);
13653469Sobrien      return NULL;
13753469Sobrien    }
13853469Sobrien  return name_space;
13953469Sobrien}
140