X-Git-Url: http://sjero.net/git/?p=wget;a=blobdiff_plain;f=src%2Futils.c;h=8168fb38719d8880d08ef53608a0a31afe86720c;hp=8817283d2ac4628b9eec50b24b9aa7557a8f8f41;hb=HEAD;hpb=e976d4f3dc0c97ae659d1aa4a472be74d2188ba1 diff --git a/src/utils.c b/src/utils.c index 8817283d..8168fb38 100644 --- a/src/utils.c +++ b/src/utils.c @@ -484,9 +484,12 @@ fork_to_background (void) /* child: give up the privileges and keep running. */ setsid (); - freopen ("/dev/null", "r", stdin); - freopen ("/dev/null", "w", stdout); - freopen ("/dev/null", "w", stderr); + if (freopen ("/dev/null", "r", stdin) == NULL) + DEBUGP (("Failed to redirect stdin to /dev/null.\n")); + if (freopen ("/dev/null", "w", stdout) == NULL) + DEBUGP (("Failed to redirect stdout to /dev/null.\n")); + if (freopen ("/dev/null", "w", stderr) == NULL) + DEBUGP (("Failed to redirect stderr to /dev/null.\n")); } #endif /* !WINDOWS && !MSDOS */ @@ -703,7 +706,7 @@ unique_create (const char *name, bool binary, char **opened_name) xfree (uname); uname = unique_name (name, false); } - if (opened_name && fp != NULL) + if (opened_name) { if (fp) *opened_name = uname; @@ -957,16 +960,16 @@ subdir_p (const char *d1, const char *d2) first element that matches DIR, through wildcards or front comparison (as appropriate). */ static bool -dir_matches_p (char **dirlist, const char *dir) +dir_matches_p (const char **dirlist, const char *dir) { - char **x; + const char **x; int (*matcher) (const char *, const char *, int) = opt.ignore_case ? fnmatch_nocase : fnmatch; for (x = dirlist; *x; x++) { /* Remove leading '/' */ - char *p = *x + (**x == '/'); + const char *p = *x + (**x == '/'); if (has_wildcards_p (p)) { if (matcher (p, dir, FNM_PATHNAME) == 0) @@ -1024,9 +1027,9 @@ match_tail (const char *string, const char *tail, bool fold_case) return false; /* tail is longer than string. */ if (!fold_case) - return strcmp (string + pos, tail); + return !strcmp (string + pos, tail); else - return strcasecmp (string + pos, tail); + return !strcasecmp (string + pos, tail); } /* Checks whether string S matches each element of ACCEPTS. A list @@ -1118,56 +1121,6 @@ has_html_suffix_p (const char *fname) return false; } -/* Read a line from FP and return the pointer to freshly allocated - storage. The storage space is obtained through malloc() and should - be freed with free() when it is no longer needed. - - The length of the line is not limited, except by available memory. - The newline character at the end of line is retained. The line is - terminated with a zero character. - - After end-of-file is encountered without anything being read, NULL - is returned. NULL is also returned on error. To distinguish - between these two cases, use the stdio function ferror(). */ - -char * -read_whole_line (FILE *fp) -{ - int length = 0; - int bufsize = 82; - char *line = xmalloc (bufsize); - - while (fgets (line + length, bufsize - length, fp)) - { - length += strlen (line + length); - if (length == 0) - /* Possible for example when reading from a binary file where - a line begins with \0. */ - continue; - - if (line[length - 1] == '\n') - break; - - /* fgets() guarantees to read the whole line, or to use up the - space we've given it. We can double the buffer - unconditionally. */ - bufsize <<= 1; - line = xrealloc (line, bufsize); - } - if (length == 0 || ferror (fp)) - { - xfree (line); - return NULL; - } - if (length + 1 < bufsize) - /* Relieve the memory from our exponential greediness. We say - `length + 1' because the terminating \0 is not included in - LENGTH. We don't need to zero-terminate the string ourselves, - though, because fgets() does that. */ - line = xrealloc (line, length + 1); - return line; -} - /* Read FILE into memory. A pointer to `struct file_memory' are returned; use struct element `content' to access file contents, and the element `length' to know the file length. `content' is *not* @@ -1570,7 +1523,7 @@ with_thousand_seps (wgint n) some detail. */ char * -human_readable (HR_NUMTYPE n) +human_readable (HR_NUMTYPE n, const int acc, const int decimals) { /* These suffixes are compatible with those of GNU `ls -lh'. */ static char powers[] = @@ -1603,10 +1556,10 @@ human_readable (HR_NUMTYPE n) if ((n / 1024) < 1024 || i == countof (powers) - 1) { double val = n / 1024.0; - /* Print values smaller than 10 with one decimal digits, and - others without any decimals. */ + /* Print values smaller than the accuracy level (acc) with (decimal) + * decimal digits, and others without any decimals. */ snprintf (buf, sizeof (buf), "%.*f%c", - val < 10 ? 1 : 0, val, powers[i]); + val < acc ? decimals : 0, val, powers[i]); return buf; } n /= 1024; @@ -2152,8 +2105,8 @@ xsleep (double seconds) This implementation does not emit newlines after 76 characters of base64 data. */ -int -base64_encode (const void *data, int length, char *dest) +size_t +base64_encode (const void *data, size_t length, char *dest) { /* Conversion table. */ static const char tbl[64] = { @@ -2220,7 +2173,7 @@ base64_encode (const void *data, int length, char *dest) This function originates from Free Recode. */ -int +ssize_t base64_decode (const char *base64, void *dest) { /* Table of base64 values for first 128 characters. Note that this @@ -2333,7 +2286,7 @@ compile_posix_regex (const char *str) int errcode = regcomp ((regex_t *) regex, str, REG_EXTENDED | REG_NOSUB); if (errcode != 0) { - int errbuf_size = regerror (errcode, (regex_t *) regex, NULL, 0); + size_t errbuf_size = regerror (errcode, (regex_t *) regex, NULL, 0); char *errbuf = xmalloc (errbuf_size); regerror (errcode, (regex_t *) regex, errbuf, errbuf_size); fprintf (stderr, _("Invalid regular expression %s, %s\n"), @@ -2351,10 +2304,10 @@ compile_posix_regex (const char *str) bool match_pcre_regex (const void *regex, const char *str) { - int l = strlen (str); + size_t l = strlen (str); int ovector[OVECCOUNT]; - int rc = pcre_exec ((pcre *) regex, 0, str, l, 0, 0, ovector, OVECCOUNT); + int rc = pcre_exec ((pcre *) regex, 0, str, (int) l, 0, 0, ovector, OVECCOUNT); if (rc == PCRE_ERROR_NOMATCH) return false; else if (rc < 0) @@ -2380,7 +2333,7 @@ match_posix_regex (const void *regex, const char *str) return true; else { - int errbuf_size = regerror (rc, opt.acceptregex, NULL, 0); + size_t errbuf_size = regerror (rc, opt.acceptregex, NULL, 0); char *errbuf = xmalloc (errbuf_size); regerror (rc, opt.acceptregex, errbuf, errbuf_size); logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"), @@ -2476,7 +2429,7 @@ print_decimal (double number) /* Get the maximum name length for the given path. */ /* Return 0 if length is unknown. */ -size_t +long get_max_length (const char *path, int length, int name) { long ret; @@ -2531,9 +2484,9 @@ get_max_length (const char *path, int length, int name) #ifdef TESTING const char * -test_subdir_p() +test_subdir_p(void) { - static struct { + static const struct { const char *d1; const char *d2; bool result; @@ -2556,7 +2509,7 @@ test_subdir_p() } const char * -test_dir_matches_p() +test_dir_matches_p(void) { static struct { const char *dirlist[3];