]> git.sesse.net Git - rdpsrv/blob - Xserver/config/util/mkshadow/savedir.c
Support RDP5 logon packets.
[rdpsrv] / Xserver / config / util / mkshadow / savedir.c
1 /* $XConsortium: savedir.c,v 1.1 94/04/13 18:26:54 rws Exp $ */
2 /* savedir.c -- save the list of files in a directory in a string
3    Copyright 1990, 1993 Free Software Foundation, Inc.
4
5    Permission to use, copy, modify, and distribute this program for
6    any purpose and without fee is hereby granted, provided that this
7    copyright and permission notice appear on all copies, and that
8    notice be given that copying and distribution is by permission of
9    the Free Software Foundation.  The Free Software Foundation makes
10    no representations about the suitability of this software for any
11    purpose.  It is provided "as is" without expressed or implied
12    warranty.
13
14    (The FSF has modified its usual distribution terms, for this file,
15    as a courtesy to the X project.)  */
16
17 /* Written by David MacKenzie <djm@ai.mit.edu>.
18    Modified to use <dirent.h> by default.  Per Bothner <bothner@cygnus.com>. */
19
20 #include <sys/types.h>
21 #if !defined(DIRECT) && !defined(BSD)
22 #include <dirent.h>
23 #define NLENGTH(direct) (strlen((direct)->d_name))
24 #else
25 #undef dirent
26 #define dirent direct
27 #define NLENGTH(direct) ((direct)->d_namlen)
28 #ifdef BSD
29 #include <sys/dir.h>
30 #else
31 #ifdef SYSNDIR
32 #include <sys/ndir.h>
33 #else
34 #include <ndir.h>
35 #endif
36 #endif
37 #endif
38
39 #if defined(VOID_CLOSEDIR) || defined(BSD)
40 /* Fake a return value. */
41 #define CLOSEDIR(d) (closedir (d), 0)
42 #else
43 #define CLOSEDIR(d) closedir (d)
44 #endif
45
46 #ifdef STDC_HEADERS
47 #include <stdlib.h>
48 #include <string.h>
49 #else
50 char *malloc ();
51 char *realloc ();
52 int strlen ();
53 #ifndef NULL
54 #define NULL 0
55 #endif
56 #endif
57
58 char *stpcpy ();
59
60 /* Return a freshly allocated string containing the filenames
61    in directory DIR, separated by '\0' characters;
62    the end is marked by two '\0' characters in a row.
63    NAME_SIZE is the number of bytes to initially allocate
64    for the string; it will be enlarged as needed.
65    Return NULL if DIR cannot be opened or if out of memory. */
66
67 char *
68 savedir (dir, name_size)
69      char *dir;
70      unsigned name_size;
71 {
72   DIR *dirp;
73   struct dirent *dp;
74   char *name_space;
75   char *namep;
76
77   dirp = opendir (dir);
78   if (dirp == NULL)
79     return NULL;
80
81   name_space = (char *) malloc (name_size);
82   if (name_space == NULL)
83     {
84       closedir (dirp);
85       return NULL;
86     }
87   namep = name_space;
88
89   while ((dp = readdir (dirp)) != NULL)
90     {
91       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
92       if (dp->d_name[0] != '.'
93           || (dp->d_name[1] != '\0'
94               && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
95         {
96           unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
97
98           if (size_needed > name_size)
99             {
100               char *new_name_space;
101
102               while (size_needed > name_size)
103                 name_size += 1024;
104
105               new_name_space = realloc (name_space, name_size);
106               if (new_name_space == NULL)
107                 {
108                   closedir (dirp);
109                   return NULL;
110                 }
111               namep += new_name_space - name_space;
112               name_space = new_name_space;
113             }
114           strcpy (namep, dp->d_name);
115           namep += strlen (namep) + 1;
116         }
117     }
118   *namep = '\0';
119   if (CLOSEDIR (dirp))
120     {
121       free (name_space);
122       return NULL;
123     }
124   return name_space;
125 }