2016-02-03 14:12:26 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2016-03-29 19:46:25 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
2016-02-03 14:12:26 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define PORTABLE_CHARACTER_SET "0123456789._-qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
|
|
|
|
/* If your system supports more other characters, but not all non-NUL characters, define SYSTEM_CHARACTER_SET. */
|
|
|
|
|
|
|
|
static int most = 0;
|
|
|
|
static int extra = 0;
|
|
|
|
|
|
|
|
static int
|
|
|
|
pathchk(char *filename)
|
|
|
|
{
|
|
|
|
char *invalid, *invalid_end, *p, *q;
|
|
|
|
const char *character_set;
|
|
|
|
size_t len, maxlen;
|
2016-02-11 05:04:54 -05:00
|
|
|
struct stat st;
|
2016-02-03 14:12:26 -05:00
|
|
|
|
|
|
|
/* Empty? */
|
2016-03-29 19:46:25 -04:00
|
|
|
if (extra && !*filename)
|
|
|
|
eprintf("empty filename\n");
|
2016-02-03 14:12:26 -05:00
|
|
|
|
|
|
|
/* Leading hyphen? */
|
2016-03-29 19:46:25 -04:00
|
|
|
if (extra && ((*filename == '-') || strstr(filename, "/-")))
|
|
|
|
eprintf("%s: leading '-' in component of filename\n", filename);
|
2016-02-03 14:12:26 -05:00
|
|
|
|
|
|
|
/* Nonportable character? */
|
|
|
|
#ifdef SYSTEM_CHARACTER_SET
|
|
|
|
character_set = "/"SYSTEM_CHARACTER_SET;
|
|
|
|
#else
|
|
|
|
character_set = 0;
|
|
|
|
#endif
|
|
|
|
if (most)
|
|
|
|
character_set = "/"PORTABLE_CHARACTER_SET;
|
|
|
|
if (character_set && *(invalid = filename + strspn(filename, character_set))) {
|
|
|
|
for (invalid_end = invalid + 1; *invalid_end & 0x80; invalid_end++);
|
2016-03-29 19:46:25 -04:00
|
|
|
p = estrdup(filename);
|
2016-02-03 14:12:26 -05:00
|
|
|
*invalid_end = 0;
|
2016-03-29 19:46:25 -04:00
|
|
|
eprintf("%s: nonportable character '%s'\n", p, invalid);
|
2016-02-03 14:12:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Symlink error? Non-searchable directory? */
|
2016-02-11 05:04:54 -05:00
|
|
|
if (lstat(filename, &st) && errno != ENOENT) {
|
2016-02-03 14:12:26 -05:00
|
|
|
/* lstat rather than stat, so that if filename is a bad symlink, but
|
|
|
|
* all parents are OK, no error will be detected. */
|
2016-03-29 19:46:25 -04:00
|
|
|
eprintf("%s:", filename);
|
2016-02-03 14:12:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Too long pathname? */
|
|
|
|
maxlen = most ? _POSIX_PATH_MAX : PATH_MAX;
|
2016-03-29 19:46:25 -04:00
|
|
|
if (strlen(filename) >= maxlen)
|
|
|
|
eprintf("%s: is longer than %zu bytes\n", filename, maxlen);
|
2016-02-03 14:12:26 -05:00
|
|
|
|
|
|
|
/* Too long component? */
|
|
|
|
maxlen = most ? _POSIX_NAME_MAX : NAME_MAX;
|
|
|
|
for (p = filename; p; p = q) {
|
|
|
|
q = strchr(p, '/');
|
|
|
|
len = q ? (size_t)(q++ - p) : strlen(p);
|
2016-03-29 19:46:25 -04:00
|
|
|
if (len > maxlen)
|
|
|
|
eprintf("%s: includes component longer than %zu bytes\n",
|
|
|
|
filename, maxlen);
|
2016-02-03 14:12:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-11 05:00:00 -05:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-pP] filename...\n", argv0);
|
|
|
|
}
|
|
|
|
|
2016-02-03 14:12:26 -05:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'p':
|
|
|
|
most = 1;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
extra = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (!argc)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for (; argc--; argv++)
|
|
|
|
ret |= pathchk(*argv);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|