File: makepath.c

package info (click to toggle)
autopartkit 1.07
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,060 kB
  • ctags: 198
  • sloc: ansic: 2,489; sh: 843; makefile: 103
file content (44 lines) | stat: -rw-r--r-- 664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string.h>
#include <alloca.h>
#include <sys/stat.h>
#include <sys/types.h>

int
make_path(const char *pathname, mode_t mode)
{
    char *dirpath;
    char *slash;

    dirpath = alloca(strlen(pathname) + 1);
    strcpy(dirpath, pathname);

    slash = dirpath;

    while ('/' == *slash)
        ++slash;

    while (1)
    {
	slash = strchr(slash, '/');
	if (slash)
	    *slash = '\0';

	if (0 != mkdir(dirpath, mode))
	{
	    struct stat statbuf;
	    if (stat(dirpath, &statbuf))
	        return -1;
	    if (!S_ISDIR(statbuf.st_mode))
	        return -1;
	}

	if (slash)
	{
	    *slash = '/';
	    slash++;
	}
	else
	    break;
    }
    return 0;
}