home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / SAVEDIR.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  4KB  |  153 lines

  1. /* savedir.c -- save the list of files in a directory in a string
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu>. */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.
  28.  
  29.    $Header: e:/gnu/fileutil/RCS/savedir.c 1.4.0.2 90/09/17 09:28:48 tho Exp $
  30.  */
  31.  
  32. #include <sys/types.h>
  33.  
  34. #ifdef DIRENT
  35. #include <dirent.h>
  36. #ifdef direct
  37. #undef direct
  38. #endif
  39. #define direct dirent
  40. #define NLENGTH(direct) (strlen((direct)->d_name))
  41. #else
  42. #define NLENGTH(direct) ((direct)->d_namlen)
  43. #ifdef USG
  44. #ifdef SYSNDIR
  45. #include <sys/ndir.h>
  46. #else
  47. #include <ndir.h>
  48. #endif
  49. #else
  50. #include <sys/dir.h>
  51. #endif
  52. #endif
  53.  
  54. #ifdef STDC_HEADERS
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #else
  58. char *malloc ();
  59. char *realloc ();
  60. int strlen ();
  61. #ifndef NULL
  62. #define NULL 0
  63. #endif
  64. #endif
  65.  
  66. #ifdef MSDOS
  67. #include <stdio.h>
  68. extern char *savedir (char *dir, unsigned name_size);
  69. static char *stpcpy (char *dest, char *source);
  70. #endif /* MSDOS */
  71.  
  72. static char *stpcpy ();
  73.  
  74. /* Return a freshly allocated string containing the filenames
  75.    in directory DIR, separated by '\0' characters;
  76.    the end is marked by two '\0' characters in a row.
  77.    NAME_SIZE is the number of bytes to initially allocate
  78.    for the string; it will be enlarged as needed.
  79.    Return NULL if DIR cannot be opened or if out of memory. */
  80.  
  81. char *
  82. savedir (dir, name_size)
  83.      char *dir;
  84.      unsigned name_size;
  85. {
  86.   DIR *dirp;
  87.   struct direct *dp;
  88.   char *name_space;
  89.   char *namep;
  90.  
  91.   dirp = opendir (dir);
  92.   if (dirp == NULL)
  93.     return NULL;
  94.  
  95. #ifdef MSDOS                 /* stat () it ourselves ... */
  96.   name_size = 0;
  97.   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
  98.     name_size += strlen(dp->d_name) + 1;
  99.   seekdir(dirp, 0L);
  100. #endif /* MSDOS */
  101.  
  102.   name_space = (char *) malloc (name_size);
  103.   if (name_space == NULL)
  104.     return NULL;
  105.   namep = name_space;
  106.  
  107.   while ((dp = readdir (dirp)) != NULL)
  108.     {
  109.       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
  110.       if (dp->d_name[0] != '.'
  111.       || (dp->d_name[1] != '\0'
  112.           && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
  113.     {
  114.       unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
  115.  
  116.       if (size_needed > name_size)
  117.         {
  118.           char *new_name_space;
  119.  
  120.           while (size_needed > name_size)
  121.         name_size += 1024;
  122.  
  123.           new_name_space = realloc (name_space, name_size);
  124.           if (new_name_space == NULL)
  125.         {
  126.           closedir (dirp);
  127.           return NULL;
  128.         }
  129.           namep += new_name_space - name_space;
  130.           name_space = new_name_space;
  131.         }
  132.       namep = stpcpy (namep, dp->d_name) + 1;
  133.     }
  134.     }
  135.   *namep = '\0';
  136.   closedir (dirp);
  137.   return name_space;
  138. }
  139.  
  140. /* Copy SOURCE into DEST, stopping after copying the first '\0', and
  141.    return a pointer to the '\0' at the end of DEST;
  142.    in other words, return DEST + strlen (SOURCE). */
  143.  
  144. static char *
  145. stpcpy (dest, source)
  146.      char *dest;
  147.      char *source;
  148. {
  149.   while ((*dest++ = *source++) != '\0')
  150.     /* Do nothing. */ ;
  151.   return dest - 1;
  152. }
  153.