File: makepath.c

package info (click to toggle)
libite 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,188 kB
  • sloc: sh: 4,665; ansic: 4,165; makefile: 141
file content (89 lines) | stat: -rw-r--r-- 1,519 bytes parent folder | download | duplicates (2)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <errno.h>
#include <errno.h>
#include <libgen.h>
#include <stdlib.h>
#include "check.h"

#define BASEDIR "/tmp/libite-test"

int checkpath(const char *dir)
{
	char tmp[256];
	struct stat sb;

	snprintf(tmp, sizeof(tmp), "ls -ld %s", dir);
	if (system(tmp))
		perror("system");

	if (!stat(dir, &sb) && S_ISDIR(sb.st_mode))
		return 0;

	errno = ENOTDIR;
	return 1;
}

int test_makepath(const char *dir)
{
	int ret = makepath(dir);

	if (!ret)
		ret = checkpath(dir);
	if (ret)
		perror("Failed");

	return ret;
}

int test_fmkpath(const char *subdir)
{
	int ret;

	ret = fmkpath(0755, "%s/%s", BASEDIR, subdir);
	if (!ret) {
		char tmp[256];

		snprintf(tmp, sizeof(tmp), "%s/%s", BASEDIR, subdir);
		ret = checkpath(tmp);
	} else
		perror("Failed");

	return ret;
}

int main(void)
{
	int i, ret = 0;
	char *list[] = {
		BASEDIR "/tok/",
		BASEDIR "/tok2",
		BASEDIR "/ab",
		BASEDIR "/b",
		BASEDIR "/a/",
		BASEDIR "/a/b",
		BASEDIR "/a/c/",
		NULL
	};

	mkdir(BASEDIR, 0755);

	printf("Pass 1/2: makepath() =========================\n");
	for (i = 0; list[i] && !ret; i++)
		ret |= test_makepath(list[i]);

	printf("Pass 2/2: fmkpath() ==========================\n");
	for (i = 0; list[i] && !ret; i++)
		ret |= test_fmkpath(list[i] + strlen(BASEDIR) + 1);

	printf("Done: Cleaning up ============================\n");
	system("rm -rf " BASEDIR);

	return ret;
}

/**
 * Local Variables:
 *  indent-tabs-mode: t
 *  c-file-style:     "linux"
 *  compile-command:  "make makepath && ./makepath"
 * End:
 */