drm/linux/timer.h: No need to protect a callout struct from MP accesses
[dragonfly.git] / usr.bin / pathchk / pathchk.c
blob164f20833bcb83b1a13817ff5a3e579b6abf4851
1 /*-
2 * Copyright (c) 2002 Tim J. Robbins.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: head/usr.bin/pathchk/pathchk.c 256800 2013-10-20 20:10:31Z jilles $
30 * pathchk -- check pathnames
32 * Check whether files could be created with the names specified on the
33 * command line. If -p is specified, check whether the pathname is portable
34 * to all POSIX systems.
37 #include <sys/types.h>
38 #include <sys/stat.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 static int check(const char *);
49 static int portable(const char *);
50 static void usage(void);
52 static int pflag; /* Perform portability checks */
53 static int Pflag; /* Check for empty paths, leading '-' */
55 int
56 main(int argc, char *argv[])
58 int ch, rval;
59 const char *arg;
61 while ((ch = getopt(argc, argv, "pP")) > 0) {
62 switch (ch) {
63 case 'p':
64 pflag = 1;
65 break;
66 case 'P':
67 Pflag = 1;
68 break;
69 default:
70 usage();
71 /*NOTREACHED*/
74 argc -= optind;
75 argv += optind;
77 if (argc == 0)
78 usage();
80 rval = 0;
81 while ((arg = *argv++) != NULL)
82 rval |= check(arg);
84 exit(rval);
87 static void
88 usage(void)
91 fprintf(stderr, "usage: pathchk [-pP] pathname ...\n");
92 exit(1);
95 static int
96 check(const char *path)
98 struct stat sb;
99 long complen, namemax, pathmax, svnamemax;
100 int last;
101 char *end, *p, *pathd;
103 if ((pathd = strdup(path)) == NULL)
104 err(1, "strdup");
106 p = pathd;
108 if (Pflag && *p == '\0') {
109 warnx("%s: empty pathname", path);
110 goto bad;
112 if ((Pflag || pflag) && (*p == '-' || strstr(p, "/-") != NULL)) {
113 warnx("%s: contains a component starting with '-'", path);
114 goto bad;
117 if (!pflag) {
118 errno = 0;
119 namemax = pathconf(*p == '/' ? "/" : ".", _PC_NAME_MAX);
120 if (namemax == -1 && errno != 0)
121 namemax = NAME_MAX;
122 } else
123 namemax = _POSIX_NAME_MAX;
125 for (;;) {
126 p += strspn(p, "/");
127 complen = (long)strcspn(p, "/");
128 end = p + complen;
129 last = *end == '\0';
130 *end = '\0';
132 if (namemax != -1 && complen > namemax) {
133 warnx("%s: %s: component too long (limit %ld)", path,
134 p, namemax);
135 goto bad;
138 if (!pflag && stat(pathd, &sb) == -1 && errno != ENOENT) {
139 warn("%s: %.*s", path, (int)(strlen(pathd) -
140 complen - 1), pathd);
141 goto bad;
144 if (pflag && !portable(p)) {
145 warnx("%s: %s: component contains non-portable "
146 "character", path, p);
147 goto bad;
150 if (last)
151 break;
153 if (!pflag) {
154 errno = 0;
155 svnamemax = namemax;
156 namemax = pathconf(pathd, _PC_NAME_MAX);
157 if (namemax == -1 && errno != 0)
158 namemax = svnamemax;
161 *end = '/';
162 p = end + 1;
165 if (!pflag) {
166 errno = 0;
167 pathmax = pathconf(path, _PC_PATH_MAX);
168 if (pathmax == -1 && errno != 0)
169 pathmax = PATH_MAX;
170 } else
171 pathmax = _POSIX_PATH_MAX;
172 if (pathmax != -1 && strlen(path) >= (size_t)pathmax) {
173 warnx("%s: path too long (limit %ld)", path, pathmax - 1);
174 goto bad;
177 free(pathd);
178 return (0);
180 bad: free(pathd);
181 return (1);
185 * Check whether a path component contains only portable characters.
187 static int
188 portable(const char *path)
190 static const char charset[] =
191 "abcdefghijklmnopqrstuvwxyz"
192 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
193 "0123456789._-";
194 long s;
196 s = strspn(path, charset);
197 if (path[s] != '\0')
198 return (0);
200 return (1);