Merge remote-tracking branch 'origin/master' into for-steve
[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 <string.h>
35 #include <errno.h>
36
37 char *prog_name;
38
39 static int touchy_mkdir(char *path)
40 {
41         struct stat buf;
42
43         /* Try to make it.  If PATH exists, EEXIST will be the
44            return. Anyway on error but isn't a directory,
45            this will signal an error.  */
46         if (mkdir(path, 0777) < 0 && errno != EEXIST ) {
47                 int serrno = errno; /* save errno because stat may
48                                        change it... */
49
50                 /* If PATH already exists and is a directory, return success.  */
51                 if (stat(path, &buf) >= 0 && (buf.st_mode & S_IFMT) == S_IFDIR)
52                         return 0;
53
54                 fprintf(stderr, "%s: %s", prog_name, strerror(serrno));
55                 return 1;
56         }
57
58         return 0;
59 }
60
61 int main(int argc, char *argv[])
62 {
63         prog_name = *argv;
64
65         for (argc--, argv++; argc > 0; argc--, argv++) {
66                 char *path = *argv;
67                 int i;
68
69                 /* Stop at each slash in path and try to create the directory.
70                    Skip any initial slash.  */
71                 for (i = (path[0] == '/') ? 1 : 0; path[i]; i++)
72                         if (path[i] == '/') {
73                                 path[i] = '\0';
74                                 if (touchy_mkdir(path) < 0)
75                                         goto next_pathname;
76                                 path[i] = '/';
77                         }
78
79                 touchy_mkdir(path);
80
81               next_pathname:
82                 ;
83         }
84
85         return 0;
86 }