src/pathchk.c - coreutils-8.23
Global variables defined
Functions defined
Macros defined
Source code
#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <wchar.h>
#include "system.h"
#include "error.h"
#include "quote.h"
#include "quotearg.h"
#define PROGRAM_NAME "pathchk"
#define AUTHORS \
proper_name ("Paul Eggert"), \
proper_name ("David MacKenzie"), \
proper_name ("Jim Meyering")
#ifndef _POSIX_PATH_MAX
# define _POSIX_PATH_MAX 256
#endif
#ifndef _POSIX_NAME_MAX
# define _POSIX_NAME_MAX 14
#endif
#ifdef _XOPEN_NAME_MAX
# define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
#else
# define NAME_MAX_MINIMUM _POSIX_NAME_MAX
#endif
#ifdef _XOPEN_PATH_MAX
# define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
#else
# define PATH_MAX_MINIMUM _POSIX_PATH_MAX
#endif
#if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
# ifndef _PC_NAME_MAX
# define _PC_NAME_MAX 0
# define _PC_PATH_MAX 1
# endif
# ifndef pathconf
# define pathconf(file, flag) \
(flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
# endif
#endif
static bool validate_file_name (char *, bool, bool);
enum
{
PORTABILITY_OPTION = CHAR_MAX + 1
};
static struct option const longopts[] =
{
{"portability", no_argument, NULL, PORTABILITY_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
emit_try_help ();
else
{
printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
fputs (_("\
Diagnose invalid or unportable file names.\n\
\n\
-p check for most POSIX systems\n\
-P check for empty names and leading \"-\"\n\
--portability check for all POSIX systems (equivalent to -p -P)\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
emit_ancillary_info ();
}
exit (status);
}
int
main (int argc, char **argv)
{
bool ok = true;
bool check_basic_portability = false;
bool check_extra_portability = false;
int optc;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
while ((optc = getopt_long (argc, argv, "+pP", longopts, NULL)) != -1)
{
switch (optc)
{
case PORTABILITY_OPTION:
check_basic_portability = true;
check_extra_portability = true;
break;
case 'p':
check_basic_portability = true;
break;
case 'P':
check_extra_portability = true;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (optind == argc)
{
error (0, 0, _("missing operand"));
usage (EXIT_FAILURE);
}
for (; optind < argc; ++optind)
ok &= validate_file_name (argv[optind],
check_basic_portability, check_extra_portability);
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}
static bool
no_leading_hyphen (char const *file)
{
char const *p;
for (p = file; (p = strchr (p, '-')); p++)
if (p == file || p[-1] == '/')
{
error (0, 0, _("leading '-' in a component of file name %s"),
quote (file));
return false;
}
return true;
}
static bool
portable_chars_only (char const *file, size_t filelen)
{
size_t validlen = strspn (file,
("/"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789._-"));
char const *invalid = file + validlen;
if (*invalid)
{
mbstate_t mbstate = { 0, };
size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
error (0, 0,
_("nonportable character %s in file name %s"),
quotearg_n_style_mem (1, locale_quoting_style, invalid,
(charlen <= MB_LEN_MAX ? charlen : 1)),
quote_n (0, file));
return false;
}
return true;
}
static char * _GL_ATTRIBUTE_PURE
component_start (char *f)
{
while (*f == '/')
f++;
return f;
}
static size_t _GL_ATTRIBUTE_PURE
component_len (char const *f)
{
size_t len;
for (len = 1; f[len] != '/' && f[len]; len++)
continue;
return len;
}
static bool
validate_file_name (char *file, bool check_basic_portability,
bool check_extra_portability)
{
size_t filelen = strlen (file);
char *start;
bool check_component_lengths;
bool file_exists = false;
if (check_extra_portability && ! no_leading_hyphen (file))
return false;
if ((check_basic_portability || check_extra_portability)
&& filelen == 0)
{
error (0, 0, _("empty file name"));
return false;
}
if (check_basic_portability)
{
if (! portable_chars_only (file, filelen))
return false;
}
else
{
struct stat st;
if (lstat (file, &st) == 0)
file_exists = true;
else if (errno != ENOENT || filelen == 0)
{
error (0, errno, "%s", file);
return false;
}
}
if (check_basic_portability
|| (! file_exists && PATH_MAX_MINIMUM <= filelen))
{
size_t maxsize;
if (check_basic_portability)
maxsize = _POSIX_PATH_MAX;
else
{
long int size;
char const *dir = (*file == '/' ? "/" : ".");
errno = 0;
size = pathconf (dir, _PC_PATH_MAX);
if (size < 0 && errno != 0)
{
error (0, errno,
_("%s: unable to determine maximum file name length"),
dir);
return false;
}
maxsize = MIN (size, SSIZE_MAX);
}
if (maxsize <= filelen)
{
unsigned long int len = filelen;
unsigned long int maxlen = maxsize - 1;
error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
maxlen, len, quote (file));
return false;
}
}
check_component_lengths = check_basic_portability;
if (! check_component_lengths && ! file_exists)
{
for (start = file; *(start = component_start (start)); )
{
size_t length = component_len (start);
if (NAME_MAX_MINIMUM < length)
{
check_component_lengths = true;
break;
}
start += length;
}
}
if (check_component_lengths)
{
size_t name_max = NAME_MAX_MINIMUM;
size_t known_name_max = (check_basic_portability ? _POSIX_NAME_MAX : 0);
for (start = file; *(start = component_start (start)); )
{
size_t length;
if (known_name_max)
name_max = known_name_max;
else
{
long int len;
char const *dir = (start == file ? "." : file);
char c = *start;
errno = 0;
*start = '\0';
len = pathconf (dir, _PC_NAME_MAX);
*start = c;
if (0 <= len)
name_max = MIN (len, SSIZE_MAX);
else
switch (errno)
{
case 0:
name_max = SIZE_MAX;
break;
case ENOENT:
known_name_max = name_max;
break;
default:
*start = '\0';
error (0, errno, "%s", dir);
*start = c;
return false;
}
}
length = component_len (start);
if (name_max < length)
{
unsigned long int len = length;
unsigned long int maxlen = name_max;
char c = start[len];
start[len] = '\0';
error (0, 0,
_("limit %lu exceeded by length %lu "
"of file name component %s"),
maxlen, len, quote (start));
start[len] = c;
return false;
}
start += length;
}
}
return true;
}