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