12 usage (char *argv0, int help)
14 char *progname = strrchr(argv0, '/');
19 fprintf (help ? stdout : stderr,
20 "Usage: %s [flags] file[s]\n",
25 fprintf (stderr, " or try '-h' for help\n");
29 printf ("Check given files have...\n");
30 printf (" -p permission file must have required permissions\n");
31 printf (" -t dir|file|link file must be of the specified type\n");
32 printf (" -l link_name file must be a link to the given name\n");
33 printf (" -s size file must have the given size\n");
34 printf (" -u user file must be owned by given user\n");
35 printf (" -g group file must be owned by given group\n");
36 printf (" -f follow symlinks\n");
37 printf (" -a file must be absent\n");
38 printf (" -v increase verbosity\n");
39 printf (" -h print help\n");
40 printf (" Exit status is 0 on success, 1 on failure\n");
44 main (int argc, char **argv)
49 uid_t uid = (uid_t)-1;
50 gid_t gid = (gid_t)-1;
53 char *checklink = NULL;
59 while ((c = getopt (argc, argv, "p:t:l:s:u:g:avfh")) != -1)
63 perms = (int)strtol (optarg, &term, 0);
66 fprintf (stderr, "Can't parse permission %s\n", optarg);
76 size = strtoll (optarg, &term, 0);
79 fprintf (stderr, "Can't parse size %s\n", optarg);
87 uid = (uid_t)strtol (optarg + 1, &term, 0);
88 if (term == optarg + 1)
90 fprintf (stderr, "Can't parse numeric uid %s\n", optarg);
94 struct passwd *pw = getpwnam (optarg);
98 fprintf (stderr, "Can't find user %s\n", optarg);
108 gid = (gid_t)strtol (optarg + 1, &term, 0);
109 if (term == optarg + 1)
111 fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
115 struct group *gr = getgrnam (optarg);
119 fprintf (stderr, "Can't find group %s\n", optarg);
155 char *fname = argv[optind];
156 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
160 if (!(absent && errno == ENOENT))
163 printf ("Can't %sstat %s: %s\n",
165 fname, strerror (errno));
170 printf ("%s: absent OK\n", fname);
177 printf ("%s exists\n", fname);
183 if (!strcmp (type, "d") ||
184 !strcmp (type, "dir"))
186 if (!S_ISDIR (buf.st_mode))
189 printf ("%s is not a directory\n",
194 else if (!strcmp (type, "f") ||
195 !strcmp (type, "file"))
197 if (!S_ISREG (buf.st_mode))
200 printf ("%s is not a regular file\n",
205 else if (!strcmp (type, "l") ||
206 !strcmp (type, "link"))
208 if (!S_ISLNK (buf.st_mode))
211 printf ("%s is not a link\n",
218 fprintf (stderr, "Can't parse file type %s\n",
224 printf ("%s has type %s OK\n", fname, type);
229 if ((buf.st_mode & ~S_IFMT) != perms)
232 printf ("%s has perms 0%o, not 0%o\n",
233 fname, (buf.st_mode & ~S_IFMT),
239 printf ("%s has perms 0%o OK\n",
245 if (buf.st_size != size)
248 printf ("%s has size %Ld, not %Ld\n",
249 fname, (long long)buf.st_size,
255 printf ("%s has size %Ld OK\n", fname, size);
258 if (checklink != NULL)
260 static char lname[4<<10];
262 rc = readlink (fname, lname, sizeof (lname) - 1);
267 printf ("%s: can't read link: %s\n",
268 fname, strerror (errno));
273 if (strcmp (checklink, lname))
276 printf ("%s is a link to %s and not %s\n",
277 fname, lname, checklink);
282 printf ("%s links to %s OK\n", fname, checklink);
285 if (uid != (uid_t)-1)
287 if (buf.st_uid != uid)
290 printf ("%s is owned by user #%ld and not #%ld\n",
291 fname, (long)buf.st_uid, (long)uid);
296 printf ("%s is owned by user #%ld OK\n",
300 if (gid != (gid_t)-1)
302 if (buf.st_gid != gid)
305 printf ("%s is owned by group #%ld and not #%ld\n",
306 fname, (long)buf.st_gid, (long)gid);
311 printf ("%s is owned by group #%ld OK\n",
314 } while (++optind < argc);