]> git.sesse.net Git - rdpsrv/blob - Xserver/config/util/lndir.c
Support RDP5 logon packets.
[rdpsrv] / Xserver / config / util / lndir.c
1 /* $XConsortium: lndir.c /main/16 1996/09/28 16:16:40 rws $ */
2 /* $XFree86: xc/config/util/lndir.c,v 3.6 1997/01/18 06:51:01 dawes Exp $ */
3 /* Create shadow link tree (after X11R4 script of the same name)
4    Mark Reinhold (mbr@lcs.mit.edu)/3 January 1990 */
5
6 /* 
7 Copyright (c) 1990,  X Consortium
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
22 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 Except as contained in this notice, the name of the X Consortium shall not be
27 used in advertising or otherwise to promote the sale, use or other dealings
28 in this Software without prior written authorization from the X Consortium.
29
30 */
31
32 /* From the original /bin/sh script:
33
34   Used to create a copy of the a directory tree that has links for all
35   non-directories (except those named RCS, SCCS or CVS.adm).  If you are
36   building the distribution on more than one machine, you should use
37   this technique.
38
39   If your master sources are located in /usr/local/src/X and you would like
40   your link tree to be in /usr/local/src/new-X, do the following:
41
42         %  mkdir /usr/local/src/new-X
43         %  cd /usr/local/src/new-X
44         %  lndir ../X
45 */
46
47 #include <X11/Xos.h>
48 #include <X11/Xfuncproto.h>
49 #include <stdio.h>
50 #include <sys/stat.h>
51 #if !defined(MINIX) && !defined(Lynx)
52 #include <sys/param.h>
53 #endif
54 #include <errno.h>
55
56 #ifndef X_NOT_POSIX
57 #include <dirent.h>
58 #else
59 #ifdef SYSV
60 #include <dirent.h>
61 #else
62 #ifdef USG
63 #include <dirent.h>
64 #else
65 #include <sys/dir.h>
66 #ifndef dirent
67 #define dirent direct
68 #endif
69 #endif
70 #endif
71 #endif
72 #ifndef MAXPATHLEN
73 #define MAXPATHLEN 2048
74 #endif
75
76 #if NeedVarargsPrototypes
77 #include <stdarg.h>
78 #endif
79
80 #ifdef X_NOT_STDC_ENV
81 extern int errno;
82 #endif
83 int silent = 0;                 /* -silent */
84 int ignore_links = 0;           /* -ignorelinks */
85
86 char *rcurdir;
87 char *curdir;
88
89 void
90 quit (
91 #if NeedVarargsPrototypes
92     int code, char * fmt, ...)
93 #else
94     code, fmt, a1, a2, a3)
95     char *fmt;
96 #endif
97 {
98 #if NeedVarargsPrototypes
99     va_list args;
100     va_start(args, fmt);
101     vfprintf (stderr, fmt, args);
102     va_end(args);
103 #else
104     fprintf (stderr, fmt, a1, a2, a3);
105 #endif
106     putc ('\n', stderr);
107     exit (code);
108 }
109
110 void
111 quiterr (code, s)
112     char *s;
113 {
114     perror (s);
115     exit (code);
116 }
117
118 void
119 msg (
120 #if NeedVarargsPrototypes
121     char * fmt, ...)
122 #else
123     fmt, a1, a2, a3)
124     char *fmt;
125 #endif
126 {
127 #if NeedVarargsPrototypes
128     va_list args;
129 #endif
130     if (curdir) {
131         fprintf (stderr, "%s:\n", curdir);
132         curdir = 0;
133     }
134 #if NeedVarargsPrototypes
135     va_start(args, fmt);
136     vfprintf (stderr, fmt, args);
137     va_end(args);
138 #else
139     fprintf (stderr, fmt, a1, a2, a3);
140 #endif
141     putc ('\n', stderr);
142 }
143
144 void
145 mperror (s)
146     char *s;
147 {
148     if (curdir) {
149         fprintf (stderr, "%s:\n", curdir);
150         curdir = 0;
151     }
152     perror (s);
153 }
154
155
156 int equivalent(lname, rname)
157     char *lname;
158     char *rname;
159 {
160     char *s;
161
162     if (!strcmp(lname, rname))
163         return 1;
164     for (s = lname; *s && (s = strchr(s, '/')); s++) {
165         while (s[1] == '/')
166             strcpy(s+1, s+2);
167     }
168     return !strcmp(lname, rname);
169 }
170
171
172 /* Recursively create symbolic links from the current directory to the "from"
173    directory.  Assumes that files described by fs and ts are directories. */
174
175 dodir (fn, fs, ts, rel)
176 char *fn;                       /* name of "from" directory, either absolute or
177                                    relative to cwd */
178 struct stat *fs, *ts;           /* stats for the "from" directory and cwd */
179 int rel;                        /* if true, prepend "../" to fn before using */
180 {
181     DIR *df;
182     struct dirent *dp;
183     char buf[MAXPATHLEN + 1], *p;
184     char symbuf[MAXPATHLEN + 1];
185     char basesym[MAXPATHLEN + 1];
186     struct stat sb, sc;
187     int n_dirs;
188     int symlen;
189     int basesymlen = -1;
190     char *ocurdir;
191
192     if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) {
193         msg ("%s: From and to directories are identical!", fn);
194         return 1;
195     }
196
197     if (rel)
198         strcpy (buf, "../");
199     else
200         buf[0] = '\0';
201     strcat (buf, fn);
202     
203     if (!(df = opendir (buf))) {
204         msg ("%s: Cannot opendir", buf);
205         return 1;
206     }
207
208     p = buf + strlen (buf);
209     *p++ = '/';
210     n_dirs = fs->st_nlink;
211     while (dp = readdir (df)) {
212         if (dp->d_name[strlen(dp->d_name) - 1] == '~')
213             continue;
214         strcpy (p, dp->d_name);
215
216         if (n_dirs > 0) {
217             if (stat (buf, &sb) < 0) {
218                 mperror (buf);
219                 continue;
220             }
221
222 #ifdef S_ISDIR
223             if(S_ISDIR(sb.st_mode))
224 #else
225             if (sb.st_mode & S_IFDIR) 
226 #endif
227             {
228                 /* directory */
229                 n_dirs--;
230                 if (dp->d_name[0] == '.' &&
231                     (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
232                                                dp->d_name[2] == '\0')))
233                     continue;
234                 if (!strcmp (dp->d_name, "RCS"))
235                     continue;
236                 if (!strcmp (dp->d_name, "SCCS"))
237                     continue;
238                 if (!strcmp (dp->d_name, "CVS"))
239                     continue;
240                 if (!strcmp (dp->d_name, "CVS.adm"))
241                     continue;
242                 ocurdir = rcurdir;
243                 rcurdir = buf;
244                 curdir = silent ? buf : (char *)0;
245                 if (!silent)
246                     printf ("%s:\n", buf);
247                 if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
248                     if (mkdir (dp->d_name, 0777) < 0 ||
249                         stat (dp->d_name, &sc) < 0) {
250                         mperror (dp->d_name);
251                         curdir = rcurdir = ocurdir;
252                         continue;
253                     }
254                 }
255                 if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
256                     msg ("%s: is a link instead of a directory", dp->d_name);
257                     curdir = rcurdir = ocurdir;
258                     continue;
259                 }
260                 if (chdir (dp->d_name) < 0) {
261                     mperror (dp->d_name);
262                     curdir = rcurdir = ocurdir;
263                     continue;
264                 }
265                 dodir (buf, &sb, &sc, (buf[0] != '/'));
266                 if (chdir ("..") < 0)
267                     quiterr (1, "..");
268                 curdir = rcurdir = ocurdir;
269                 continue;
270             }
271         }
272
273         /* non-directory */
274         symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1);
275         if (symlen >= 0)
276             symbuf[symlen] = '\0';
277
278         /* The option to ignore links exists mostly because
279            checking for them slows us down by 10-20%.
280            But it is off by default because this really is a useful check. */
281         if (!ignore_links) {
282             /* see if the file in the base tree was a symlink */
283             basesymlen = readlink(buf, basesym, sizeof(basesym) - 1);
284             if (basesymlen >= 0)
285                 basesym[basesymlen] = '\0';
286         }
287
288         if (symlen >= 0) {
289             /* Link exists in new tree.  Print message if it doesn't match. */
290             if (!equivalent (basesymlen>=0 ? basesym : buf, symbuf))
291                 msg ("%s: %s", dp->d_name, symbuf);
292         } else {
293             if (symlink (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
294                 mperror (dp->d_name);
295         }
296     }
297
298     closedir (df);
299     return 0;
300 }
301
302
303 main (ac, av)
304 int ac;
305 char **av;
306 {
307     char *prog_name = av[0];
308     char *fn, *tn;
309     struct stat fs, ts;
310
311     while (++av, --ac) {
312         if (strcmp(*av, "-silent") == 0)
313             silent = 1;
314         else if (strcmp(*av, "-ignorelinks") == 0)
315             ignore_links = 1;
316         else if (strcmp(*av, "--") == 0) {
317             ++av, --ac;
318             break;
319         }
320         else
321             break;
322     }
323
324     if (ac < 1 || ac > 2)
325         quit (1, "usage: %s [-silent] [-ignorelinks] fromdir [todir]",
326               prog_name);
327
328     fn = av[0];
329     if (ac == 2)
330         tn = av[1];
331     else
332         tn = ".";
333
334     /* to directory */
335     if (stat (tn, &ts) < 0)
336         quiterr (1, tn);
337 #ifdef S_ISDIR
338     if (!(S_ISDIR(ts.st_mode)))
339 #else
340     if (!(ts.st_mode & S_IFDIR))
341 #endif
342         quit (2, "%s: Not a directory", tn);
343     if (chdir (tn) < 0)
344         quiterr (1, tn);
345
346     /* from directory */
347     if (stat (fn, &fs) < 0)
348         quiterr (1, fn);
349 #ifdef S_ISDIR
350     if (!(S_ISDIR(fs.st_mode)))
351 #else
352     if (!(fs.st_mode & S_IFDIR))
353 #endif
354         quit (2, "%s: Not a directory", fn);
355
356     exit (dodir (fn, &fs, &ts, 0));
357 }