2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Tim J. Robbins.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
51 static int check(const char *);
52 static int portable(const char *);
53 static void usage(void);
55 static int pflag; /* Perform portability checks */
56 static int Pflag; /* Check for empty paths, leading '-' */
59 main(int argc, char *argv[])
64 while ((ch = getopt(argc, argv, "pP")) > 0) {
84 while ((arg = *argv++) != NULL)
94 fprintf(stderr, "usage: pathchk [-Pp] pathname ...\n");
99 check(const char *path)
102 long complen, namemax, pathmax, svnamemax;
104 char *end, *p, *pathd;
106 if ((pathd = strdup(path)) == NULL)
111 if (Pflag && *p == '\0') {
112 warnx("%s: empty pathname", path);
115 if ((Pflag || pflag) && (*p == '-' || strstr(p, "/-") != NULL)) {
116 warnx("%s: contains a component starting with '-'", path);
122 namemax = pathconf(*p == '/' ? "/" : ".", _PC_NAME_MAX);
123 if (namemax == -1 && errno != 0)
126 namemax = _POSIX_NAME_MAX;
130 complen = (long)strcspn(p, "/");
135 if (namemax != -1 && complen > namemax) {
136 warnx("%s: %s: component too long (limit %ld)", path,
141 if (!pflag && stat(pathd, &sb) == -1 && errno != ENOENT) {
142 warn("%s: %.*s", path, (int)(strlen(pathd) -
143 complen - 1), pathd);
147 if (pflag && !portable(p)) {
148 warnx("%s: %s: component contains non-portable "
149 "character", path, p);
159 namemax = pathconf(pathd, _PC_NAME_MAX);
160 if (namemax == -1 && errno != 0)
170 pathmax = pathconf(path, _PC_PATH_MAX);
171 if (pathmax == -1 && errno != 0)
174 pathmax = _POSIX_PATH_MAX;
175 if (pathmax != -1 && strlen(path) >= (size_t)pathmax) {
176 warnx("%s: path too long (limit %ld)", path, pathmax - 1);
188 * Check whether a path component contains only portable characters.
191 portable(const char *path)
193 static const char charset[] =
194 "abcdefghijklmnopqrstuvwxyz"
195 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
199 s = strspn(path, charset);