1f08c3bdfSopenharmony_ci#ifndef COMMON_H
2f08c3bdfSopenharmony_ci#define COMMON_H
3f08c3bdfSopenharmony_ci
4f08c3bdfSopenharmony_ci#define UNUSED __attribute__ ((unused))
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci#define OPT_MISSING(prog, opt) do {				\
7f08c3bdfSopenharmony_ci	fprintf(stderr, "%s: option -%c ", prog, opt);		\
8f08c3bdfSopenharmony_ci	fprintf(stderr, "requires an argument\n");		\
9f08c3bdfSopenharmony_ci	usage(prog, 1);						\
10f08c3bdfSopenharmony_ci} while (0)
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define OPT_COLLIDING(prog, opt1, opt2) do {			\
13f08c3bdfSopenharmony_ci	fprintf(stderr,						\
14f08c3bdfSopenharmony_ci		"%s: option -%c collides with option -%c\n",	\
15f08c3bdfSopenharmony_ci		prog, opt1, opt2);				\
16f08c3bdfSopenharmony_ci	usage(prog, 1);						\
17f08c3bdfSopenharmony_ci} while (0)
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#define ARG_WRONG(prog, opt, arg) do {				\
20f08c3bdfSopenharmony_ci	fprintf(stderr, "%s: Wrong argument -%c %s\n",		\
21f08c3bdfSopenharmony_ci		prog, opt, arg);				\
22f08c3bdfSopenharmony_ci	usage(prog, 1);						\
23f08c3bdfSopenharmony_ci} while (0)
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#define USECS_PER_SEC	1000000
26f08c3bdfSopenharmony_ci#define USECS_PER_MSEC	1000
27f08c3bdfSopenharmony_ci#define NSECS_PER_MSEC	1000000
28f08c3bdfSopenharmony_ci#define MSECS_PER_SEC	1000
29f08c3bdfSopenharmony_ci#define NSECS_PER_USEC	1000
30f08c3bdfSopenharmony_ci#define NSECS_PER_SEC	(USECS_PER_SEC * NSECS_PER_USEC)
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#define DECIMAL		10
33f08c3bdfSopenharmony_ci#define BUFFSIZE	512
34f08c3bdfSopenharmony_ci#define DEFBITMASKSIZE	64
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#ifndef PATH_MAX
37f08c3bdfSopenharmony_ci# define PATH_MAX	1024
38f08c3bdfSopenharmony_ci#endif
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci#define while_each_childdir(basepath, p_relpath, c_relpath, c_pathlen)	\
41f08c3bdfSopenharmony_ci{									\
42f08c3bdfSopenharmony_ci	struct dirent *direntp;						\
43f08c3bdfSopenharmony_ci	DIR *dp;							\
44f08c3bdfSopenharmony_ci	struct stat st;							\
45f08c3bdfSopenharmony_ci	char fullpath[PATH_MAX];					\
46f08c3bdfSopenharmony_ci	int pathlen;							\
47f08c3bdfSopenharmony_ci	int start = 0;							\
48f08c3bdfSopenharmony_ci									\
49f08c3bdfSopenharmony_ci	if (basepath[strlen(basepath) - 1] == '/'			\
50f08c3bdfSopenharmony_ci		&& p_relpath[0] == '/')					\
51f08c3bdfSopenharmony_ci		start = 1;						\
52f08c3bdfSopenharmony_ci									\
53f08c3bdfSopenharmony_ci	snprintf(fullpath, sizeof(fullpath), "%s%s", basepath,		\
54f08c3bdfSopenharmony_ci		 &p_relpath[start]);					\
55f08c3bdfSopenharmony_ci	pathlen = strlen(fullpath);					\
56f08c3bdfSopenharmony_ci									\
57f08c3bdfSopenharmony_ci	if ((dp = opendir(fullpath)) == NULL)				\
58f08c3bdfSopenharmony_ci		return -1;						\
59f08c3bdfSopenharmony_ci									\
60f08c3bdfSopenharmony_ci	while ((direntp = readdir(dp)) != NULL) {			\
61f08c3bdfSopenharmony_ci		if (!strcmp(direntp->d_name, ".")			\
62f08c3bdfSopenharmony_ci			|| !strcmp(direntp->d_name, ".."))		\
63f08c3bdfSopenharmony_ci			continue;					\
64f08c3bdfSopenharmony_ci		if (fullpath[pathlen - 1] == '/') {			\
65f08c3bdfSopenharmony_ci			fullpath[pathlen - 1] = '\0';			\
66f08c3bdfSopenharmony_ci			pathlen--;					\
67f08c3bdfSopenharmony_ci		}							\
68f08c3bdfSopenharmony_ci		sprintf(fullpath + pathlen, "/%s", direntp->d_name);	\
69f08c3bdfSopenharmony_ci		stat(fullpath, &st);					\
70f08c3bdfSopenharmony_ci		if (S_ISDIR(st.st_mode)) {				\
71f08c3bdfSopenharmony_ci			start = strlen(basepath);			\
72f08c3bdfSopenharmony_ci			if (basepath[start - 1] == '/')			\
73f08c3bdfSopenharmony_ci				start--;				\
74f08c3bdfSopenharmony_ci			snprintf(c_relpath, c_pathlen, "%s",		\
75f08c3bdfSopenharmony_ci					fullpath + start);
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci#define	end_while_each_childdir						\
78f08c3bdfSopenharmony_ci		}							\
79f08c3bdfSopenharmony_ci	}								\
80f08c3bdfSopenharmony_ci									\
81f08c3bdfSopenharmony_ci	closedir(dp);							\
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci#endif
85