pathchk: fixes and cleanup

Signed-off-by: Mattias Andrée <maandree@kth.se>
This commit is contained in:
Mattias Andrée 2016-03-30 01:46:25 +02:00 committed by FRIGN
parent e3f497e1f6
commit da04e4cc2a
1 changed files with 17 additions and 27 deletions

View File

@ -1,11 +1,12 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include <stdint.h>
#include <errno.h>
#include <sys/stat.h>
#include "util.h" #include "util.h"
@ -24,16 +25,12 @@ pathchk(char *filename)
struct stat st; struct stat st;
/* Empty? */ /* Empty? */
if (extra && !*filename) { if (extra && !*filename)
weprintf("%s: empty filename\n", argv0); eprintf("empty filename\n");
return 1;
}
/* Leading hyphen? */ /* Leading hyphen? */
if (extra && ((*filename == '-') || strstr(filename, "/-"))) { if (extra && ((*filename == '-') || strstr(filename, "/-")))
weprintf("%s: %s: leading '-' in component of filename\n", argv0, filename); eprintf("%s: leading '-' in component of filename\n", filename);
return 1;
}
/* Nonportable character? */ /* Nonportable character? */
#ifdef SYSTEM_CHARACTER_SET #ifdef SYSTEM_CHARACTER_SET
@ -45,38 +42,31 @@ 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++);
weprintf("%s: %s: ", argv0, filename); p = estrdup(filename);
*invalid_end = 0; *invalid_end = 0;
weprintf("nonportable character '%s'\n", invalid); eprintf("%s: nonportable character '%s'\n", p, invalid);
return 1;
} }
/* Symlink error? Non-searchable directory? */ /* Symlink error? Non-searchable directory? */
if (lstat(filename, &st) && errno != ENOENT) { if (lstat(filename, &st) && 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. */
weprintf("%s: %s:", argv0, filename); eprintf("%s:", filename);
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)
weprintf("%s: %s: is longer than %zu bytes\n", eprintf("%s: is longer than %zu bytes\n", filename, maxlen);
argv0, filename, maxlen);
return 1;
}
/* Too long component? */ /* Too long component? */
maxlen = most ? _POSIX_NAME_MAX : NAME_MAX; maxlen = most ? _POSIX_NAME_MAX : NAME_MAX;
for (p = filename; p; p = q) { for (p = filename; p; p = q) {
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)
weprintf("%s: %s: includes component longer than %zu bytes\n", eprintf("%s: includes component longer than %zu bytes\n",
argv0, filename, maxlen); filename, maxlen);
return 1;
}
} }
return 0; return 0;