Fix if/else scope in yow.c from Rudi
[sxemacs] / lib-src / make-path.c
1 /* Make all the directories along a path.
2    Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs.
5
6 SXEmacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Synched up with: FSF 19.28. */
20
21 /* This program works like mkdir, except that it generates
22    intermediate directories if they don't exist.  This is just like
23    the `mkdir -p' command on most systems; unfortunately, the mkdir
24    command on some of the purer BSD systems (like Mt. Xinu) don't have
25    that option. */
26
27 #ifdef emacs
28 #include <config.h>
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <stdio.h>
34 #include <errno.h>
35
36 char *prog_name;
37
38 static int touchy_mkdir(char *path)
39 {
40         struct stat buf;
41
42         /* If PATH already exists and is a directory, return success.  */
43         if (stat(path, &buf) >= 0 && (buf.st_mode & S_IFMT) == S_IFDIR)
44                 return 0;
45
46         /* Otherwise, try to make it.  If PATH exists but isn't a directory,
47            this will signal an error.  */
48         if (mkdir(path, 0777) < 0) {
49                 fprintf(stderr, "%s: ", prog_name);
50                 perror(path);
51                 return 1;
52         }
53
54         return 0;
55 }
56
57 int main(int argc, char *argv[])
58 {
59         prog_name = *argv;
60
61         for (argc--, argv++; argc > 0; argc--, argv++) {
62                 char *path = *argv;
63                 int i;
64
65                 /* Stop at each slash in path and try to create the directory.
66                    Skip any initial slash.  */
67                 for (i = (path[0] == '/') ? 1 : 0; path[i]; i++)
68                         if (path[i] == '/') {
69                                 path[i] = '\0';
70                                 if (touchy_mkdir(path) < 0)
71                                         goto next_pathname;
72                                 path[i] = '/';
73                         }
74
75                 touchy_mkdir(path);
76
77               next_pathname:
78                 ;
79         }
80
81         return 0;
82 }