Mercurial > pt1.oyama
annotate recpt1/mkpath.c @ 75:1d1d616cde98
build fix
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Sat, 28 Nov 2009 02:06:16 +0900 |
parents | f1553492e8bb |
children |
rev | line source |
---|---|
61
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
1 /* mkpath is originally written by Jonathan Leffler. |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
2 * see "http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux" for detail. |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
3 * copyright: (C) JLSS 1990-91,1997-98,2001,2005,2008 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
4 */ |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
5 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
6 #include <sys/stat.h> |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
7 #include <unistd.h> |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
8 #include <stdlib.h> |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
9 #include <string.h> |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
10 #include <errno.h> |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
11 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
12 static int |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
13 do_mkdir(const char *path, mode_t mode) |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
14 { |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
15 struct stat st; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
16 int status = 0; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
17 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
18 if (stat(path, &st) != 0) { |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
19 /* Directory does not exist */ |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
20 if (mkdir(path, mode) != 0) |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
21 status = -1; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
22 } |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
23 else if (!S_ISDIR(st.st_mode)) { |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
24 errno = ENOTDIR; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
25 status = -1; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
26 } |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
27 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
28 return(status); |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
29 } |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
30 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
31 int |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
32 mkpath(const char *path, mode_t mode) |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
33 { |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
34 char *pp; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
35 char *sp; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
36 int status; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
37 char *copypath = strdup(path); |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
38 |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
39 status = 0; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
40 pp = copypath; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
41 while (status == 0 && (sp = strchr(pp, '/')) != 0) { |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
42 if (sp != pp) { |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
43 /* Neither root nor double slash in path */ |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
44 *sp = '\0'; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
45 status = do_mkdir(copypath, mode); |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
46 *sp = '/'; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
47 } |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
48 pp = sp + 1; |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
49 } |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
50 if (status == 0) |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
51 status = do_mkdir(path, mode); |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
52 free(copypath); |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
53 return (status); |
f1553492e8bb
ensure path of destination file exists. imported Jonathan Leffler's mkpath.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff
changeset
|
54 } |