1 /* Various functions of utilitarian nature.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #else /* not HAVE_STRING_H */
28 #endif /* not HAVE_STRING_H */
30 #include <sys/types.h>
41 #ifdef HAVE_SYS_UTIME_H
42 # include <sys/utime.h>
46 # include <libc.h> /* for access() */
59 /* Croak the fatal memory error and bail out with non-zero exit
62 memfatal (const char *s)
64 /* HACK: expose save_log_p from log.c, so we can turn it off in
65 order to prevent saving the log. Saving the log is dangerous
66 because logprintf() and logputs() can call malloc(), so this
67 could infloop. When logging is turned off, infloop can no longer
69 extern int save_log_p;
72 logprintf (LOG_ALWAYS, _("%s: %s: Not enough memory.\n"), exec_name, s);
76 /* xmalloc, xrealloc and xstrdup exit the program if there is not
77 enough memory. xstrdup also implements strdup on systems that do
91 xrealloc (void *obj, size_t size)
95 /* Not all Un*xes have the feature of realloc() that calling it with
96 a NULL-pointer is the same as malloc(), but it is easy to
99 res = realloc (obj, size);
103 memfatal ("realloc");
108 xstrdup (const char *s)
112 char *s1 = malloc (l + 1);
115 memcpy (s1, s, l + 1);
117 #else /* HAVE_STRDUP */
118 char *s1 = strdup (s);
122 #endif /* HAVE_STRDUP */
125 /* Copy the string formed by two pointers (one on the beginning, other
126 on the char after the last char) to a new, malloc-ed location.
129 strdupdelim (const char *beg, const char *end)
131 char *res = (char *)xmalloc (end - beg + 1);
132 memcpy (res, beg, end - beg);
133 res[end - beg] = '\0';
137 /* Parse a string containing comma-separated elements, and return a
138 vector of char pointers with the elements. Spaces following the
139 commas are ignored. */
141 sepstring (const char *s)
155 res = (char **)xrealloc (res, (i + 2) * sizeof (char *));
156 res[i] = strdupdelim (p, s);
159 /* Skip the blanks following the ','. */
167 res = (char **)xrealloc (res, (i + 2) * sizeof (char *));
168 res[i] = strdupdelim (p, s);
173 /* Return pointer to a static char[] buffer in which zero-terminated
174 string-representation of TM (in form hh:mm:ss) is printed. It is
175 shamelessly non-reentrant, but it doesn't matter, really.
177 If TM is non-NULL, the time_t of the current time will be stored
180 time_str (time_t *tm)
190 ptm = localtime (&tim);
191 sprintf (tms, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
195 /* Returns an error message for ERRNUM. #### This requires more work.
196 This function, as well as the whole error system, is very
199 uerrmsg (uerr_t errnum)
204 return _("Unknown/unsupported protocol");
207 return _("Invalid port specification");
210 return _("Invalid host name");
214 /* $@#@#$ compiler. */
219 /* The Windows versions of the following two functions are defined in
222 /* A cuserid() immitation using getpwuid(), to avoid hassling with
223 utmp. Besides, not all systems have cuesrid(). Under Windows, it
224 is defined in mswindows.c.
226 If WHERE is non-NULL, the username will be stored there.
227 Otherwise, it will be returned as a static buffer (as returned by
228 getpwuid()). In the latter case, the buffer should be copied
229 before calling getpwuid() or pwd_cuserid() again. */
232 pwd_cuserid (char *where)
236 if (!(pwd = getpwuid (getuid ())) || !pwd->pw_name)
240 strcpy (where, pwd->pw_name);
248 fork_to_background (void)
251 /* Whether we arrange our own version of opt.lfilename here. */
256 opt.lfilename = unique_name (DEFAULT_LOGFILE);
268 /* parent, no error */
269 printf (_("Continuing in background.\n"));
271 printf (_("Output will be written to `%s'.\n"), opt.lfilename);
274 /* child: keep running */
276 #endif /* not WINDOWS */
278 /* Canonicalize PATH, and return a new path. The new path differs from PATH
280 Multple `/'s are collapsed to a single `/'.
281 Leading `./'s and trailing `/.'s are removed.
282 Trailing `/'s are removed.
283 Non-leading `../'s and trailing `..'s are handled by removing
284 portions of the path.
286 E.g. "a/b/c/./../d/.." will yield "a/b". This function originates
290 Always use '/' as stub_char.
291 Don't check for local things using canon_stat.
292 Change the original string instead of strdup-ing.
293 React correctly when beginning with `./' and `../'. */
295 path_simplify (char *path)
297 register int i, start, ddot;
303 /*stub_char = (*path == '/') ? '/' : '.';*/
306 /* Addition: Remove all `./'-s preceding the string. If `../'-s
307 precede, put `/' in front and remove them too. */
312 if (path[i] == '.' && path[i + 1] == '/')
314 else if (path[i] == '.' && path[i + 1] == '.' && path[i + 2] == '/')
323 strcpy (path, path + i - ddot);
325 /* Replace single `.' or `..' with `/'. */
326 if ((path[0] == '.' && path[1] == '\0')
327 || (path[0] == '.' && path[1] == '.' && path[2] == '\0'))
333 /* Walk along PATH looking for things to compact. */
340 while (path[i] && path[i] != '/')
345 /* If we didn't find any slashes, then there is nothing left to do. */
349 /* Handle multiple `/'s in a row. */
350 while (path[i] == '/')
353 if ((start + 1) != i)
355 strcpy (path + start + 1, path + i);
359 /* Check for trailing `/'. */
360 if (start && !path[i])
367 /* Check for `../', `./' or trailing `.' by itself. */
370 /* Handle trailing `.' by itself. */
375 if (path[i + 1] == '/')
377 strcpy (path + i, path + i + 1);
378 i = (start < 0) ? 0 : start;
382 /* Handle `../' or trailing `..' by itself. */
383 if (path[i + 1] == '.' &&
384 (path[i + 2] == '/' || !path[i + 2]))
386 while (--start > -1 && path[start] != '/');
387 strcpy (path + start + 1, path + i + 2);
388 i = (start < 0) ? 0 : start;
401 /* "Touch" FILE, i.e. make its atime and mtime equal to the time
402 specified with TM. */
404 touch (const char *file, time_t tm)
406 #ifdef HAVE_STRUCT_UTIMBUF
407 struct utimbuf times;
408 times.actime = times.modtime = tm;
411 times[0] = times[1] = tm;
414 if (utime (file, ×) == -1)
415 logprintf (LOG_NOTQUIET, "utime(%s): %s\n", file, strerror (errno));
418 /* Checks if FILE is a symbolic link, and removes it if it is. Does
419 nothing under MS-Windows. */
421 remove_link (const char *file)
426 if (lstat (file, &st) == 0 && S_ISLNK (st.st_mode))
428 DEBUGP (("Unlinking %s (symlink).\n", file));
431 logprintf (LOG_VERBOSE, _("Failed to unlink symlink `%s': %s\n"),
432 file, strerror (errno));
437 /* Does FILENAME exist? This is quite a lousy implementation, since
438 it supplies no error codes -- only a yes-or-no answer. Thus it
439 will return that a file does not exist if, e.g., the directory is
440 unreadable. I don't mind it too much currently, though. The
441 proper way should, of course, be to have a third, error state,
442 other than true/false, but that would introduce uncalled-for
443 additional complexity to the callers. */
445 file_exists_p (const char *filename)
448 return access (filename, F_OK) >= 0;
451 return stat (filename, &buf) >= 0;
455 /* Returns 0 if PATH is a directory, 1 otherwise (any kind of file).
456 Returns 0 on error. */
458 file_non_directory_p (const char *path)
461 /* Use lstat() rather than stat() so that symbolic links pointing to
462 directories can be identified correctly. */
463 if (lstat (path, &buf) != 0)
465 return S_ISDIR (buf.st_mode) ? 0 : 1;
468 /* Return a unique filename, given a prefix and count */
470 unique_name_1 (const char *fileprefix, int count)
476 filename = (char *)xmalloc (strlen (fileprefix) + numdigit (count) + 2);
477 sprintf (filename, "%s.%d", fileprefix, count);
480 filename = xstrdup (fileprefix);
482 if (!file_exists_p (filename))
491 /* Return a unique file name, based on PREFIX. */
493 unique_name (const char *prefix)
499 file = unique_name_1 (prefix, count++);
503 /* Create DIRECTORY. If some of the pathname components of DIRECTORY
504 are missing, create them first. In case any mkdir() call fails,
505 return its error status. Returns 0 on successful completion.
507 The behaviour of this function should be identical to the behaviour
508 of `mkdir -p' on systems where mkdir supports the `-p' option. */
510 make_directory (const char *directory)
516 /* Make a copy of dir, to be able to write to it. Otherwise, the
517 function is unsafe if called with a read-only char *argument. */
518 STRDUP_ALLOCA (dir, directory);
520 /* If the first character of dir is '/', skip it (and thus enable
521 creation of absolute-pathname directories. */
522 for (i = (*dir == '/'); 1; ++i)
524 for (; dir[i] && dir[i] != '/'; i++)
529 /* Check whether the directory already exists. */
530 if (!file_exists_p (dir))
532 if (mkdir (dir, 0777) < 0)
543 static int in_acclist PARAMS ((const char *const *, const char *, int));
545 /* Determine whether a file is acceptable to be followed, according to
546 lists of patterns to accept/reject. */
548 acceptable (const char *s)
552 while (l && s[l] != '/')
559 return (in_acclist ((const char *const *)opt.accepts, s, 1)
560 && !in_acclist ((const char *const *)opt.rejects, s, 1));
562 return in_acclist ((const char *const *)opt.accepts, s, 1);
564 else if (opt.rejects)
565 return !in_acclist ((const char *const *)opt.rejects, s, 1);
569 /* Compare S1 and S2 frontally; S2 must begin with S1. E.g. if S1 is
570 `/something', frontcmp() will return 1 only if S2 begins with
571 `/something'. Otherwise, 0 is returned. */
573 frontcmp (const char *s1, const char *s2)
575 for (; *s1 && *s2 && (*s1 == *s2); ++s1, ++s2);
579 /* Iterate through STRLIST, and return the first element that matches
580 S, through wildcards or front comparison (as appropriate). */
582 proclist (char **strlist, const char *s, enum accd flags)
586 for (x = strlist; *x; x++)
587 if (has_wildcards_p (*x))
589 if (fnmatch (*x, s, FNM_PATHNAME) == 0)
594 char *p = *x + ((flags & ALLABS) && (**x == '/')); /* Remove '/' */
601 /* Returns whether DIRECTORY is acceptable for download, wrt the
602 include/exclude lists.
604 If FLAGS is ALLABS, the leading `/' is ignored in paths; relative
605 and absolute paths may be freely intermixed. */
607 accdir (const char *directory, enum accd flags)
609 /* Remove starting '/'. */
610 if (flags & ALLABS && *directory == '/')
614 if (!proclist (opt.includes, directory, flags))
619 if (proclist (opt.excludes, directory, flags))
625 /* Match the end of STRING against PATTERN. For instance:
627 match_backwards ("abc", "bc") -> 1
628 match_backwards ("abc", "ab") -> 0
629 match_backwards ("abc", "abc") -> 1 */
631 match_backwards (const char *string, const char *pattern)
635 for (i = strlen (string), j = strlen (pattern); i >= 0 && j >= 0; i--, j--)
636 if (string[i] != pattern[j])
638 /* If the pattern was exhausted, the match was succesful. */
645 /* Checks whether string S matches each element of ACCEPTS. A list
646 element are matched either with fnmatch() or match_backwards(),
647 according to whether the element contains wildcards or not.
649 If the BACKWARD is 0, don't do backward comparison -- just compare
652 in_acclist (const char *const *accepts, const char *s, int backward)
654 for (; *accepts; accepts++)
656 if (has_wildcards_p (*accepts))
658 /* fnmatch returns 0 if the pattern *does* match the
660 if (fnmatch (*accepts, s, 0) == 0)
667 if (match_backwards (s, *accepts))
672 if (!strcmp (s, *accepts))
680 /* Return the malloc-ed suffix of STR. For instance:
681 suffix ("foo.bar") -> "bar"
682 suffix ("foo.bar.baz") -> "baz"
683 suffix ("/foo/bar") -> NULL
684 suffix ("/foo.bar/baz") -> NULL */
686 suffix (const char *str)
690 for (i = strlen (str); i && str[i] != '/' && str[i] != '.'; i--);
692 return xstrdup (str + i);
697 /* Read a line from FP. The function reallocs the storage as needed
698 to accomodate for any length of the line. Reallocs are done
699 storage exponentially, doubling the storage after each overflow to
700 minimize the number of calls to realloc() and fgets(). The newline
701 character at the end of line is retained.
703 After end-of-file is encountered without anything being read, NULL
704 is returned. NULL is also returned on error. To distinguish
705 between these two cases, use the stdio function ferror(). */
708 read_whole_line (FILE *fp)
712 char *line = (char *)xmalloc (bufsize);
714 while (fgets (line + length, bufsize - length, fp))
716 length += strlen (line + length);
718 if (line[length - 1] == '\n')
720 /* fgets() guarantees to read the whole line, or to use up the
721 space we've given it. We can double the buffer
724 line = xrealloc (line, bufsize);
726 if (length == 0 || ferror (fp))
731 if (length + 1 < bufsize)
732 /* Relieve the memory from our exponential greediness. We say
733 `length + 1' because the terminating \0 is not included in
734 LENGTH. We don't need to zero-terminate the string ourselves,
735 though, because fgets() does that. */
736 line = xrealloc (line, length + 1);
740 /* Load file pointed to by FP to memory and return the malloc-ed
741 buffer with the contents. *NREAD will contain the number of read
742 bytes. The file is loaded in chunks, allocated exponentially,
743 starting with FILE_BUFFER_SIZE bytes. */
745 load_file (FILE *fp, char **buf, long *nread)
752 while (!feof (fp) && !ferror (fp))
754 *buf = (char *)xrealloc (*buf, bufsize + *nread);
755 *nread += fread (*buf + *nread, sizeof (char), bufsize, fp);
758 /* #### No indication of encountered error?? */
761 /* Free the pointers in a NULL-terminated vector of pointers, then
762 free the pointer itself. */
764 free_vec (char **vec)
775 /* Append vector V2 to vector V1. The function frees V2 and
776 reallocates V1 (thus you may not use the contents of neither
777 pointer after the call). If V1 is NULL, V2 is returned. */
779 merge_vecs (char **v1, char **v2)
789 /* To avoid j == 0 */
794 for (i = 0; v1[i]; i++);
796 for (j = 0; v2[j]; j++);
798 v1 = (char **)xrealloc (v1, (i + j + 1) * sizeof (char **));
799 memcpy (v1 + i, v2, (j + 1) * sizeof (char *));
804 /* A set of simple-minded routines to store and search for strings in
805 a linked list. You may add a string to the slist, and peek whether
806 it's still in there at any time later. */
808 /* Add an element to the list. If flags is NOSORT, the list will not
811 add_slist (slist *l, const char *s, int flags)
813 slist *t, *old, *beg;
820 t = (slist *)xmalloc (sizeof (slist));
821 t->string = xstrdup (s);
826 /* Find the last element. */
829 t = (slist *)xmalloc (sizeof (slist));
831 t->string = xstrdup (s);
835 /* Empty list or changing the first element. */
836 if (!l || (cmp = strcmp (l->string, s)) > 0)
838 t = (slist *)xmalloc (sizeof (slist));
839 t->string = xstrdup (s);
848 /* Second two one-before-the-last element. */
853 cmp = strcmp (s, l->string);
854 if (cmp == 0) /* no repeating in the list */
858 /* If the next list element is greater than s, put s between the
859 current and the next list element. */
860 t = (slist *)xmalloc (sizeof (slist));
863 t->string = xstrdup (s);
866 t = (slist *)xmalloc (sizeof (slist));
867 t->string = xstrdup (s);
868 /* Insert the new element after the last element. */
874 /* Is there a specific entry in the list? */
876 in_slist (slist *l, const char *s)
882 cmp = strcmp (l->string, s);
885 else if (cmp > 0) /* the list is ordered! */
892 /* Free the whole slist. */
894 free_slist (slist *l)
907 /* Engine for legible and legible_long_long; this function works on
911 legible_1 (const char *repr)
913 static char outbuf[128];
918 /* Reset the pointers. */
921 /* If the number is negative, shift the pointers. */
927 /* How many digits before the first separator? */
928 mod = strlen (inptr) % 3;
930 for (i = 0; i < mod; i++)
931 *outptr++ = inptr[i];
932 /* Now insert the rest of them, putting separator before every
934 for (i1 = i, i = 0; inptr[i1]; i++, i1++)
936 if (i % 3 == 0 && i1 != 0)
938 *outptr++ = inptr[i1];
940 /* Zero-terminate the string. */
945 /* Legible -- return a static pointer to the legibly printed long. */
950 /* Print the number into the buffer. */
951 long_to_string (inbuf, l);
952 return legible_1 (inbuf);
955 /* The same as legible(), but works on VERY_LONG_TYPE. See sysdep.h. */
957 legible_very_long (VERY_LONG_TYPE l)
960 /* Print the number into the buffer. */
961 sprintf (inbuf, VERY_LONG_FORMAT, l);
962 return legible_1 (inbuf);
965 /* Count the digits in a (long) integer. */
970 while ((a /= 10) != 0)
975 /* Print NUMBER to BUFFER. This is equivalent to sprintf(buffer,
976 "%ld", number), only much faster.
978 BUFFER should accept 24 bytes. This should suffice for the longest
979 numbers on 64-bit machines, including the `-' sign and the trailing
982 long_to_string (char *buffer, long number)
984 #if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8)
986 sprintf (buffer, "%ld", number);
987 #else /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
997 #define FROB(figure) do { \
998 if (force || number >= figure) \
999 *p++ = number / figure + '0', number %= figure, force = 1; \
1001 #if SIZEOF_LONG == 8
1002 FROB (1000000000000000000L);
1003 FROB (100000000000000000L);
1004 FROB (10000000000000000L);
1005 FROB (1000000000000000L);
1006 FROB (100000000000000L);
1007 FROB (10000000000000L);
1008 FROB (1000000000000L);
1009 FROB (100000000000L);
1010 FROB (10000000000L);
1011 #endif /* SIZEOF_LONG == 8 */
1022 *p++ = number + '0';
1024 #endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */