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);
97 for(i=0;i<strlen(optarg);i++)
109 struct passwd *pw = getpwnam (optarg);
113 fprintf (stderr, "Can't find user %s\n", optarg);
120 uid=(uid_t)atol(optarg);
127 gid = (gid_t)strtol (optarg + 1, &term, 0);
128 if (term == optarg + 1)
130 fprintf (stderr, "Can't parse numeric gid %s\n", optarg);
134 struct group *gr = getgrnam (optarg);
138 fprintf (stderr, "Can't find group %s\n", optarg);
174 char *fname = argv[optind];
175 int rc = follow ? stat64 (fname, &buf) : lstat64 (fname, &buf);
179 if (!(absent && errno == ENOENT))
182 printf ("Can't %sstat %s: %s\n",
184 fname, strerror (errno));
189 printf ("%s: absent OK\n", fname);
196 printf ("%s exists\n", fname);
202 if (!strcmp (type, "d") ||
203 !strcmp (type, "dir"))
205 if (!S_ISDIR (buf.st_mode))
208 printf ("%s is not a directory\n",
213 else if (!strcmp (type, "f") ||
214 !strcmp (type, "file"))
216 if (!S_ISREG (buf.st_mode))
219 printf ("%s is not a regular file\n",
224 else if (!strcmp (type, "l") ||
225 !strcmp (type, "link"))
227 if (!S_ISLNK (buf.st_mode))
230 printf ("%s is not a link\n",
237 fprintf (stderr, "Can't parse file type %s\n",
243 printf ("%s has type %s OK\n", fname, type);
248 if ((buf.st_mode & ~S_IFMT) != perms)
251 printf ("%s has perms 0%o, not 0%o\n",
252 fname, (buf.st_mode & ~S_IFMT),
258 printf ("%s has perms 0%o OK\n",
264 if (buf.st_size != size)
267 printf ("%s has size %Ld, not %Ld\n",
268 fname, (long long)buf.st_size,
274 printf ("%s has size %Ld OK\n", fname, size);
277 if (checklink != NULL)
279 static char lname[4<<10];
281 rc = readlink (fname, lname, sizeof (lname) - 1);
286 printf ("%s: can't read link: %s\n",
287 fname, strerror (errno));
292 if (strcmp (checklink, lname))
295 printf ("%s is a link to %s and not %s\n",
296 fname, lname, checklink);
301 printf ("%s links to %s OK\n", fname, checklink);
304 if (uid != (uid_t)-1)
306 if (buf.st_uid != uid)
309 printf ("%s is owned by user #%ld and not #%ld\n",
310 fname, (long)buf.st_uid, (long)uid);
315 printf ("%s is owned by user #%ld OK\n",
319 if (gid != (gid_t)-1)
321 if (buf.st_gid != gid)
324 printf ("%s is owned by group #%ld and not #%ld\n",
325 fname, (long)buf.st_gid, (long)gid);
330 printf ("%s is owned by group #%ld OK\n",
333 } while (++optind < argc);