]>
git.saurik.com Git - apple/file_cmds.git/blob - pathchk/pathchk.c
2 * Copyright (c) 2002 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
28 * pathchk -- check pathnames
30 * Check whether files could be created with the names specified on the
31 * command line. If -p is specified, check whether the pathname is portable
32 * to all POSIX systems.
35 #include <sys/cdefs.h>
37 /* Commenting __FBSDID, as it is not needed n OSX ......
38 __FBSDID("$FreeBSD: src/usr.bin/pathchk/pathchk.c,v 1.4 2002/12/15 00:40:47 tjr Exp $");
41 #include <sys/types.h>
52 static int check(const char *);
53 static int portable(const char *);
54 static void usage(void);
56 static int pflag
; /* Perform portability checks */
59 main(int argc
, char *argv
[])
64 while ((ch
= getopt(argc
, argv
, "p")) > 0) {
81 while ((arg
= *argv
++) != NULL
)
91 fprintf(stderr
, "usage: pathchk [-p] pathname...\n");
96 check(const char *path
)
99 long complen
, namemax
, pathmax
, svnamemax
;
101 char *end
, *p
, *pathd
;
103 if ((pathd
= strdup(path
)) == NULL
)
110 namemax
= pathconf(*p
== '/' ? "/" : ".", _PC_NAME_MAX
);
111 if (namemax
== -1 && errno
!= 0)
114 namemax
= _POSIX_NAME_MAX
;
118 complen
= (long)strcspn(p
, "/");
123 if (namemax
!= -1 && complen
> namemax
) {
124 warnx("%s: %s: component too long (limit %ld)", path
,
129 if (!pflag
&& stat(pathd
, &sb
) == -1 && errno
!= ENOENT
) {
130 warn("%s: %.*s", path
, (int)(strlen(pathd
) -
131 complen
- 1), pathd
);
135 if (pflag
&& (badch
= portable(p
)) >= 0) {
136 warnx("%s: %s: component contains non-portable "
137 "character `%c'", path
, p
, badch
);
147 namemax
= pathconf(pathd
, _PC_NAME_MAX
);
148 if (namemax
== -1 && errno
!= 0)
158 pathmax
= pathconf(path
, _PC_PATH_MAX
);
159 if (pathmax
== -1 && errno
!= 0)
162 pathmax
= _POSIX_PATH_MAX
;
163 if (pathmax
!= -1 && strlen(path
) >= (size_t)pathmax
) {
164 warnx("%s: path too long (limit %ld)", path
, pathmax
- 1);
176 * Check whether a path component contains only portable characters. Return
177 * the first non-portable character found.
180 portable(const char *path
)
182 static const char charset
[] =
183 "abcdefghijklmnopqrstuvwxyz"
184 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
191 s
= strspn(path
, charset
);