X-Git-Url: http://sjero.net/git/?a=blobdiff_plain;f=src%2Futils.c;h=faae62e1b334b93422dd0fa2f5fb8f1880b99e79;hb=b1838bdfd197ec970d834ce5042995df92516841;hp=fb3ccd451671350360213f05a575963626f986c6;hpb=321b5dce853e856df342cc8bddea0dab1f7193b2;p=wget diff --git a/src/utils.c b/src/utils.c index fb3ccd45..faae62e1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1844,12 +1844,12 @@ number_to_static_string (wgint number) 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; } @@ -2494,6 +2494,60 @@ print_decimal (double number) 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 *