Minor cleanup for pathchk(1)
This commit is contained in:
parent
b445614f70
commit
efabd56535
35
pathchk.c
35
pathchk.c
|
@ -8,28 +8,13 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "arg.h"
|
|
||||||
|
|
||||||
#define PORTABLE_CHARACTER_SET "0123456789._-qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
|
#define PORTABLE_CHARACTER_SET "0123456789._-qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
|
||||||
/* If your system supports more other characters, but not all non-NUL characters, define SYSTEM_CHARACTER_SET. */
|
/* If your system supports more other characters, but not all non-NUL characters, define SYSTEM_CHARACTER_SET. */
|
||||||
|
|
||||||
#ifndef PATH_MAX
|
|
||||||
# define PATH_MAX SIZE_MAX
|
|
||||||
#endif
|
|
||||||
#ifndef NAME_MAX
|
|
||||||
# define NAME_MAX SIZE_MAX
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int most = 0;
|
static int most = 0;
|
||||||
static int extra = 0;
|
static int extra = 0;
|
||||||
|
|
||||||
static void
|
|
||||||
usage(void)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "usage: %s [-pP] filename...\n", argv0);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
pathchk(char *filename)
|
pathchk(char *filename)
|
||||||
{
|
{
|
||||||
|
@ -40,13 +25,13 @@ pathchk(char *filename)
|
||||||
|
|
||||||
/* Empty? */
|
/* Empty? */
|
||||||
if (extra && !*filename) {
|
if (extra && !*filename) {
|
||||||
fprintf(stderr, "%s: empty filename\n", argv0);
|
weprintf("%s: empty filename\n", argv0);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Leading hyphen? */
|
/* Leading hyphen? */
|
||||||
if (extra && ((*filename == '-') || strstr(filename, "/-"))) {
|
if (extra && ((*filename == '-') || strstr(filename, "/-"))) {
|
||||||
fprintf(stderr, "%s: %s: leading '-' in component of filename\n", argv0, filename);
|
weprintf("%s: %s: leading '-' in component of filename\n", argv0, filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,9 +45,9 @@ pathchk(char *filename)
|
||||||
character_set = "/"PORTABLE_CHARACTER_SET;
|
character_set = "/"PORTABLE_CHARACTER_SET;
|
||||||
if (character_set && *(invalid = filename + strspn(filename, character_set))) {
|
if (character_set && *(invalid = filename + strspn(filename, character_set))) {
|
||||||
for (invalid_end = invalid + 1; *invalid_end & 0x80; invalid_end++);
|
for (invalid_end = invalid + 1; *invalid_end & 0x80; invalid_end++);
|
||||||
fprintf(stderr, "%s: %s: ", argv0, filename);
|
weprintf("%s: %s: ", argv0, filename);
|
||||||
*invalid_end = 0;
|
*invalid_end = 0;
|
||||||
fprintf(stderr, "nonportable character '%s'\n", invalid);
|
weprintf("nonportable character '%s'\n", invalid);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,14 +55,14 @@ pathchk(char *filename)
|
||||||
if (lstat(filename, &_attr) && errno != ENOENT) {
|
if (lstat(filename, &_attr) && errno != ENOENT) {
|
||||||
/* lstat rather than stat, so that if filename is a bad symlink, but
|
/* lstat rather than stat, so that if filename is a bad symlink, but
|
||||||
* all parents are OK, no error will be detected. */
|
* all parents are OK, no error will be detected. */
|
||||||
fprintf(stderr, "%s: %s: %s\n", argv0, filename, strerror(errno));
|
weprintf("%s: %s:", argv0, filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Too long pathname? */
|
/* Too long pathname? */
|
||||||
maxlen = most ? _POSIX_PATH_MAX : PATH_MAX;
|
maxlen = most ? _POSIX_PATH_MAX : PATH_MAX;
|
||||||
if (strlen(filename) >= maxlen) {
|
if (strlen(filename) >= maxlen) {
|
||||||
fprintf(stderr, "%s: %s: is longer than %zu bytes\n",
|
weprintf("%s: %s: is longer than %zu bytes\n",
|
||||||
argv0, filename, maxlen);
|
argv0, filename, maxlen);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +73,7 @@ pathchk(char *filename)
|
||||||
q = strchr(p, '/');
|
q = strchr(p, '/');
|
||||||
len = q ? (size_t)(q++ - p) : strlen(p);
|
len = q ? (size_t)(q++ - p) : strlen(p);
|
||||||
if (len > maxlen) {
|
if (len > maxlen) {
|
||||||
fprintf(stderr, "%s: %s: includes component longer than %zu bytes\n",
|
weprintf("%s: %s: includes component longer than %zu bytes\n",
|
||||||
argv0, filename, maxlen);
|
argv0, filename, maxlen);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -97,6 +82,12 @@ pathchk(char *filename)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(void)
|
||||||
|
{
|
||||||
|
eprintf("usage: %s [-pP] filename...\n", argv0);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user