Provides functionality to create directories recursively.
- Copyright
- (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
- (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
- License
- This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
- Author
- R. Soliday
Definition in file mkdir.c.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
Go to the source code of this file.
|
| int | makedir (char *newdir) |
| | Creates a directory and all necessary parent directories.
|
| |
◆ makedir()
| int makedir |
( |
char * | newdir | ) |
|
Creates a directory and all necessary parent directories.
This function attempts to create the directory specified by newdir. It handles the creation of parent directories recursively. It aborts if an ENOENT error is encountered, but ignores other errors such as when the directory already exists.
- Parameters
-
| newdir | The path of the directory to create. |
- Returns
- int Returns 1 if the directory was created successfully, 0 on error.
Definition at line 47 of file mkdir.c.
47 {
48 char *buffer = strdup(newdir);
49 char *p;
50 int len = strlen(buffer);
51
52 if (len <= 0) {
53 free(buffer);
54 return 0;
55 }
56 if (buffer[len - 1] == '/') {
57 buffer[len - 1] = '\0';
58 }
59 if (mkdir(buffer, 0755) == 0) {
60 free(buffer);
61 return 1;
62 }
63
64 p = buffer + 1;
65 while (1) {
66 char hold;
67
68 while (*p && *p != '\\' && *p != '/')
69 p++;
70 hold = *p;
71 *p = 0;
72 if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT)) {
73 fprintf(stderr, "Couldn't create directory %s\n", buffer);
74 free(buffer);
75 return 0;
76 }
77 if (hold == 0)
78 break;
79 *p++ = hold;
80 }
81 free(buffer);
82 return 1;
83}