bool
acceptable (const char *s)
{
- int l = strlen (s);
+ const char *p;
if (opt.output_document && strcmp (s, opt.output_document) == 0)
return true;
- while (l && s[l] != '/')
- --l;
- if (s[l] == '/')
- s += (l + 1);
+ if ((p = strrchr (s, '/')))
+ s = p + 1;
+
if (opt.accepts)
{
if (opt.rejects)
}
else if (opt.rejects)
return !in_acclist ((const char *const *)opt.rejects, s, true);
+
return true;
}
bool
match_tail (const char *string, const char *tail, bool fold_case)
{
- int i, j;
+ int pos = strlen (string) - strlen (tail);
- /* We want this to be fast, so we code two loops, one with
- case-folding, one without. */
+ if (pos < 0)
+ return false; /* tail is longer than string. */
if (!fold_case)
- {
- for (i = strlen (string), j = strlen (tail); i >= 0 && j >= 0; i--, j--)
- if (string[i] != tail[j])
- break;
- }
- else
- {
- for (i = strlen (string), j = strlen (tail); i >= 0 && j >= 0; i--, j--)
- if (c_tolower (string[i]) != c_tolower (tail[j]))
- break;
- }
-
- /* If the tail was exhausted, the match was succesful. */
- if (j == -1)
- return true;
+ return strcmp (string + pos, tail);
else
- return false;
+ return strcasecmp (string + pos, tail);
}
/* Checks whether string S matches each element of ACCEPTS. A list
char *
suffix (const char *str)
{
- int i;
+ char *p;
- for (i = strlen (str); i && str[i] != '/' && str[i] != '.'; i--)
- ;
+ if ((p = strrchr (str, '.')) && !strchr (p + 1, '/'))
+ return p + 1;
- if (str[i++] == '.')
- return (char *)str + i;
- else
- return NULL;
+ return NULL;
}
/* Return true if S contains globbing wildcards (`*', `?', `[' or
bool
has_wildcards_p (const char *s)
{
- for (; *s; s++)
- if (*s == '*' || *s == '?' || *s == '[' || *s == ']')
- return true;
- return false;
+ return !!strpbrk (s, "*?[]");
}
/* Return true if FNAME ends with a typical HTML suffix. The
return buf;
}
-/* Converts the byte to bits format if --bits option is enabled
+/* Converts the byte to bits format if --report-bps option is enabled
*/
wgint
convert_to_bits (wgint num)
{
- if (opt.bits_fmt)
+ if (opt.report_bps)
return num * 8;
return num;
}
{
int errbuf_size = regerror (errcode, (regex_t *) regex, NULL, 0);
char *errbuf = xmalloc (errbuf_size);
- errbuf_size = regerror (errcode, (regex_t *) regex, errbuf, errbuf_size);
+ regerror (errcode, (regex_t *) regex, errbuf, errbuf_size);
fprintf (stderr, _("Invalid regular expression %s, %s\n"),
quote (str), errbuf);
xfree (errbuf);
{
int errbuf_size = regerror (rc, opt.acceptregex, NULL, 0);
char *errbuf = xmalloc (errbuf_size);
- errbuf_size = regerror (rc, opt.acceptregex, errbuf, errbuf_size);
+ regerror (rc, opt.acceptregex, errbuf, errbuf_size);
logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"),
quote (str), rc);
xfree (errbuf);
return buf;
}
+/* Get the maximum name length for the given path. */
+/* Return 0 if length is unknown. */
+size_t
+get_max_length (const char *path, int length, int name)
+{
+ long ret;
+ char *p, *d;
+
+ /* Make a copy of the path that we can modify. */
+ p = path ? strdupdelim (path, path + length) : strdup ("");
+
+ for (;;)
+ {
+ errno = 0;
+ /* For an empty path query the current directory. */
+#if HAVE_PATHCONF
+ ret = pathconf (*p ? p : ".", name);
+ if (!(ret < 0 && errno == ENOENT))
+ break;
+#else
+ ret = PATH_MAX;
+#endif
+
+ /* The path does not exist yet, but may be created. */
+ /* Already at current or root directory, give up. */
+ if (!*p || strcmp (p, "/") == 0)
+ break;
+
+ /* Remove one directory level and try again. */
+ d = strrchr (p, '/');
+ if (d == p)
+ p[1] = '\0'; /* check root directory */
+ else if (d)
+ *d = '\0'; /* remove last directory part */
+ else
+ *p = '\0'; /* check current directory */
+ }
+
+ xfree (p);
+
+ if (ret < 0)
+ {
+ /* pathconf() has a message for us. */
+ if (errno != 0)
+ perror ("pathconf");
+
+ /* If (errno == 0) then there is no max length.
+ Even on error return 0 so the caller can continue. */
+ return 0;
+ }
+
+ return ret;
+}
+
#ifdef TESTING
const char *
test_subdir_p()
{
- int i;
- struct {
- char *d1;
- char *d2;
+ static struct {
+ const char *d1;
+ const char *d2;
bool result;
} test_array[] = {
{ "/somedir", "/somedir", true },
{ "/somedir", "/somedir/d2", true },
{ "/somedir/d1", "/somedir", false },
};
+ unsigned i;
for (i = 0; i < countof(test_array); ++i)
{
const char *
test_dir_matches_p()
{
- int i;
- struct {
- char *dirlist[3];
- char *dir;
+ static struct {
+ const char *dirlist[3];
+ const char *dir;
bool result;
} test_array[] = {
{ { "/somedir", "/someotherdir", NULL }, "somedir", true },
{ { "/Tmp/has", NULL, NULL }, "/Tmp/has space", false },
{ { "/Tmp/has", NULL, NULL }, "/Tmp/has,comma", false },
};
+ unsigned i;
for (i = 0; i < countof(test_array); ++i)
{