home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / find12as.zip / SAVEDIR.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  4KB  |  150 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/find/RCS/savedir.c 1.2.0.3 90/09/23 16:09:55 tho Exp $
  30.  */
  31.  
  32. #include <sys/types.h>
  33.  
  34. #ifdef MSDOS
  35. #include <stdio.h>
  36. extern char *savedir (char *dir, unsigned int name_size);
  37. extern char *stpcpy (char *dest, char *source);
  38. #endif /* MSDOS */
  39.  
  40. #ifdef DIRENT
  41. #include <dirent.h>
  42. #define direct dirent
  43. #define NLENGTH(direct) (strlen((direct)->d_name))
  44. #else
  45. #define NLENGTH(direct) ((direct)->d_namlen)
  46. #ifdef USG
  47. #ifdef SYSNDIR
  48. #include <sys/ndir.h>
  49. #else
  50. #include <ndir.h>
  51. #endif
  52. #else
  53. #include <sys/dir.h>
  54. #endif
  55. #endif
  56.  
  57. #ifdef STDC_HEADERS
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #else
  61. char *malloc ();
  62. char *realloc ();
  63. int strlen ();
  64. #ifndef NULL
  65. #define NULL 0
  66. #endif
  67. #endif
  68.  
  69. char *stpcpy ();
  70.  
  71. /* Return a freshly allocated string containing the filenames
  72.    in directory DIR, separated by '\0' characters;
  73.    the end is marked by two '\0' characters in a row.
  74.    NAME_SIZE is the number of bytes to initially allocate
  75.    for the string; it will be enlarged as needed.
  76.    Return NULL if DIR cannot be opened or if out of memory. */
  77.  
  78. char *
  79. savedir (dir, name_size)
  80.      char *dir;
  81.      unsigned name_size;
  82. {
  83.   DIR *dirp;
  84.   struct direct *dp;
  85.   char *name_space;
  86.   char *namep;
  87.  
  88.   dirp = opendir (dir);
  89.   if (dirp == NULL)
  90.     return NULL;
  91.  
  92. #ifdef MSDOS            /* We have to stat() it ourselves ... */
  93.   name_size = 0L;
  94.   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
  95.     name_size += strlen (dp->d_name) + 1;
  96.   seekdir (dirp, 0L);
  97. #endif /* MSDOS */
  98.  
  99.   name_space = (char *) malloc (name_size);
  100.   if (name_space == NULL)
  101.     return NULL;
  102.   namep = name_space;
  103.  
  104.   while ((dp = readdir (dirp)) != NULL)
  105.     {
  106.       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
  107.       if (dp->d_name[0] != '.'
  108.       || (dp->d_name[1] != '\0'
  109.           && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
  110.     {
  111.       unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
  112.  
  113.       if (size_needed > name_size)
  114.         {
  115.           char *new_name_space;
  116.  
  117.           while (size_needed > name_size)
  118.         name_size += 1024;
  119.  
  120.           new_name_space = realloc (name_space, name_size);
  121.           if (new_name_space == NULL)
  122.         {
  123.           closedir (dirp);
  124.           return NULL;
  125.         }
  126.           namep += new_name_space - name_space;
  127.           name_space = new_name_space;
  128.         }
  129.       namep = stpcpy (namep, dp->d_name) + 1;
  130.     }
  131.     }
  132.   *namep = '\0';
  133.   closedir (dirp);
  134.   return name_space;
  135. }
  136.  
  137. /* Copy SOURCE into DEST, stopping after copying the first '\0', and
  138.    return a pointer to the '\0' at the end of DEST;
  139.    in other words, return DEST + strlen (SOURCE). */
  140.  
  141. char *
  142. stpcpy (dest, source)
  143.      char *dest;
  144.      char *source;
  145. {
  146.   while ((*dest++ = *source++) != '\0')
  147.     /* Do nothing. */ ;
  148.   return dest - 1;
  149. }
  150.