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