#include <locale.h>
#include <libintl.h>
#include <limits.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
static char portfsset[] = \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-";
#ifndef M_FSDELIM
#define M_FSDELIM(c) ((c) == '/')
#endif
static char *nametoolong = "%s: component too long.\n";
static char *pathtoolong = "%s: pathname too long.\n";
static char *notsrch = "%s: Not searchable.\n";
static char *badchar = "%s: Nonportable character '%c' (%#02X) found.\n";
static char *badbyte = "%s: Nonportable byte %#02X found.\n";
static char *pathconfprob = "pathchk: warning: \
pathconf(\"%s\", %s) returns '%s'. Using %s = %d\n";
static int printWarnings = 1;
static int checkpathname(char *, int);
static void usage(void);
int
main(int argc, char **argv)
{
int c;
int errors;
int pflag = 0;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "pw")) != EOF) {
switch (c) {
case 'p':
pflag = 1;
break;
case 'w':
printWarnings = 0;
break;
default:
usage();
}
}
argv += optind;
if (*argv == 0) {
usage();
}
errors = 0;
while (*argv) {
errors += checkpathname(*argv, pflag);
argv += 1;
}
return (errors);
}
static int
checkPathConf(const char *path, int type, long *valp)
{
errno = 0;
*valp = pathconf(path, type);
if ((*valp == -1) && (errno != 0) && (errno != EACCES)) {
if (type == _PC_PATH_MAX)
*valp = _POSIX_PATH_MAX;
else
*valp = _POSIX_NAME_MAX;
if (printWarnings) {
(void) fprintf(stderr, gettext(pathconfprob), path,
type == _PC_PATH_MAX?"_PC_PATH_MAX" :
"_PC_NAME_MAX", strerror(errno),
type == _PC_PATH_MAX ? "PATH_MAX" : "NAME_MAX",
*valp);
}
}
return ((*valp == -1) && (errno != 0));
}
#define UPDATE_LIMITS(buf)\
{\
if (pflag) {\
nameMax = _POSIX_NAME_MAX;\
pathMax = _POSIX_PATH_MAX;\
} else if (checkPathConf((buf), _PC_PATH_MAX, &pathMax) || \
checkPathConf((buf), _PC_NAME_MAX, &nameMax)) {\
(void) fprintf(stderr, gettext(notsrch), buf);\
return (1);\
}\
}
int
checkpathname(char *path, int pflag)
{
int checkStat;
long nameMax;
long pathMax;
char *scomp;
char *ecomp;
register char *p;
p = path;
checkStat = 1;
if (M_FSDELIM(*p)) {
char buf[2];
buf[0] = *p;
buf[1] = '\0';
UPDATE_LIMITS(buf);
} else {
UPDATE_LIMITS(".");
}
if (pathMax != -1 && strlen(p) > (size_t)pathMax) {
(void) fprintf(stderr, gettext(pathtoolong), path);
return (1);
}
while (*p != '\0') {
while (M_FSDELIM(*p))
p += 1;
if (*p == '\0') {
return (0);
}
scomp = p;
while (*p != '\0' && !M_FSDELIM(*p)) {
if (pflag && (strchr(portfsset, *p) == 0)) {
if (isprint(*p)) {
(void) fprintf(stderr,
gettext(badchar), path, *p, *p);
} else {
(void) fprintf(stderr,
gettext(badbyte), path, *p);
}
return (1);
}
p += 1;
}
ecomp = p;
if ((nameMax != -1) && (ecomp - scomp > nameMax)) {
(void) fprintf(stderr, gettext(nametoolong), scomp);
return (1);
} else if (!pflag && checkStat) {
struct stat sb;
char fsdelim;
fsdelim = *ecomp;
*ecomp = '\0';
if (stat(path, &sb) == -1) {
if ((errno == ENOTDIR && fsdelim != '\0') ||
(errno == EACCES)) {
(void) fprintf(stderr, gettext(notsrch),
path);
return (1);
} else if (errno == ENOENT) {
checkStat = 0;
}
} else if (S_ISDIR(sb.st_mode)) {
if (checkPathConf(path, _PC_NAME_MAX,
&nameMax)) {
(void) fprintf(stderr,
gettext(notsrch), path);
return (1);
}
}
*ecomp = fsdelim;
}
}
return (0);
}
void
usage()
{
(void) fprintf(stderr, gettext("usage: pathchk [-p] pathname ..."));
(void) fprintf(stderr, "\n");
exit(2);
}