4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright (c) 1994 by Sun Microsystems, Inc.
28 * Copyright 1991, 1992 by Mortice Kern Systems Inc. All rights reserved.
30 * Standards Conformance :
34 #pragma ident "%Z%%M% %I% %E% SMI"
36 * Original ident string for reference
37 * ident "$Id: pathchk.c,v 1.29 1994/05/24 15:51:19 mark Exp $"
44 #include <fcntl.h> /* for creat() prototype */
54 * These are the characters in the portable filename character set defined
57 static char portfsset
[] = \
58 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-";
62 #define M_FSDELIM(c) ((c) == '/')
65 static char *nametoolong
= "%s: component too long.\n";
66 static char *pathtoolong
= "%s: pathname too long.\n";
67 static char *notsrch
= "%s: Not searchable.\n";
68 static char *badchar
= "%s: Nonportable character '%c' (%#02X) found.\n";
69 static char *badbyte
= "%s: Nonportable byte %#02X found.\n";
71 static char *pathconfprob
= "pathchk: warning: \
72 pathconf(\"%s\", %s) returns '%s'. Using %s = %d\n";
75 static int printWarnings
= 1;
77 static int checkpathname(char *, int);
78 static void usage(void);
81 * mainline for pathchk
84 main(int argc
, char **argv
)
90 (void) setlocale(LC_ALL
, "");
91 #if !defined(TEXT_DOMAIN)
92 #define TEXT_DOMAIN "SYS_TEST"
94 (void) textdomain(TEXT_DOMAIN
);
97 while ((c
= getopt(argc
, argv
, "pw")) != EOF
) {
104 /* turn off warning messages */
122 errors
+= checkpathname(*argv
, pflag
);
130 * checkPathConf(const char *, int, long *)
132 * Calls pathconf(), and returns 1 if pathconf failed, zero
133 * otherwise. If pathconf() succeeded, then *valp contains the
137 checkPathConf(const char *path
, int type
, long *valp
)
140 *valp
= pathconf(path
, type
);
141 if ((*valp
== -1) && (errno
!= 0) && (errno
!= EACCES
)) {
143 * pathconf() is not supported on some mounted filesystems
144 * (e.g NFS mounts) and pathconf() is known to fail.
145 * So, we print a warning and use the POSIX default values.
147 if (type
== _PC_PATH_MAX
)
148 *valp
= _POSIX_PATH_MAX
;
150 *valp
= _POSIX_NAME_MAX
;
153 (void) fprintf(stderr
, gettext(pathconfprob
), path
,
154 type
== _PC_PATH_MAX
?"_PC_PATH_MAX" :
155 "_PC_NAME_MAX", strerror(errno
),
156 type
== _PC_PATH_MAX
? "PATH_MAX" : "NAME_MAX",
160 return ((*valp
== -1) && (errno
!= 0));
164 #define UPDATE_LIMITS(buf)\
167 nameMax = _POSIX_NAME_MAX;\
168 pathMax = _POSIX_PATH_MAX;\
169 } else if (checkPathConf((buf), _PC_PATH_MAX, &pathMax) || \
170 checkPathConf((buf), _PC_NAME_MAX, &nameMax)) {\
171 (void) fprintf(stderr, gettext(notsrch), buf);\
177 * checkpathname(char *pname)
178 * pathchk a single pathname.
181 checkpathname(char *path
, int pflag
)
194 * Get the initial NAME_MAX and PATH_MAX values
205 * This is a relative pathname, initial values
206 * are relative to the current directory
212 * Check to make sure that the pathname doesn't exceed the
215 if (pathMax
!= -1 && strlen(p
) > (size_t)pathMax
) {
216 (void) fprintf(stderr
, gettext(pathtoolong
), path
);
222 * Now spin around checking all the prefixes of
223 * the pathname, until we hit the end of the
228 * Find the beginning of the next
229 * component. Assume that
230 * M_FSDELIM('\0') == 0
232 while (M_FSDELIM(*p
))
237 * There were trailing fsdelim chars on
238 * the path provided, so we were
239 * finished, we just didn't know it.
247 * Find the end of the current component
248 * and check for valid characters in the component
250 while (*p
!= '\0' && !M_FSDELIM(*p
)) {
252 * for pflag: check for PFCS characters
253 * otherwise assume all characters are valid
255 if (pflag
&& (strchr(portfsset
, *p
) == 0)) {
257 (void) fprintf(stderr
,
258 gettext(badchar
), path
, *p
, *p
);
260 (void) fprintf(stderr
,
261 gettext(badbyte
), path
, *p
);
271 * Make sure that this component does not exceed
272 * NAME_MAX in the current prefix directory
274 if ((nameMax
!= -1) && (ecomp
- scomp
> nameMax
)) {
275 (void) fprintf(stderr
, gettext(nametoolong
), scomp
);
277 } else if (!pflag
&& checkStat
) {
279 * Perform the extra checks that
280 * are required when not just
281 * checking for portability.
289 if (stat(path
, &sb
) == -1) {
292 * intermediate component
295 * directory, or it is an
296 * unsearchable directory.
298 if ((errno
== ENOTDIR
&& fsdelim
!= '\0') ||
300 (void) fprintf(stderr
, gettext(notsrch
),
303 } else if (errno
== ENOENT
) {
306 } else if (S_ISDIR(sb
.st_mode
)) {
308 * If the current prefix is a
309 * directory, then we need to
310 * update the limits for NAME_MAX
311 * for the next component and the suffix.
313 if (checkPathConf(path
, _PC_NAME_MAX
,
315 (void) fprintf(stderr
,
316 gettext(notsrch
), path
);
322 * restore the fsdelim char that we
323 * stomped to produce a prefix.
326 } /* if (we need to stat the path) */
327 } /* while (more of this path to check) */
330 * We successfully traversed the whole pathname
338 (void) fprintf(stderr
, gettext("usage: pathchk [-p] pathname ..."));
339 (void) fprintf(stderr
, "\n");