- exit (0);
- }
- /* child: keep running */
-}
-#endif /* not WINDOWS */
-\f
-#if 0
-/* debug */
-char *
-ps (char *orig)
-{
- char *r = xstrdup (orig);
- path_simplify (r);
- return r;
-}
-#endif
-
-/* Canonicalize PATH, and return a new path. The new path differs from PATH
- in that:
- Multple `/'s are collapsed to a single `/'.
- Leading `./'s and trailing `/.'s are removed.
- Trailing `/'s are removed.
- Non-leading `../'s and trailing `..'s are handled by removing
- portions of the path.
-
- E.g. "a/b/c/./../d/.." will yield "a/b". This function originates
- from GNU Bash.
-
- Changes for Wget:
- Always use '/' as stub_char.
- Don't check for local things using canon_stat.
- Change the original string instead of strdup-ing.
- React correctly when beginning with `./' and `../'.
- Don't zip out trailing slashes. */
-int
-path_simplify (char *path)
-{
- register int i, start;
- int changes = 0;
- char stub_char;
-
- if (!*path)
- return 0;
-
- stub_char = '/';
-
- if (path[0] == '/')
- /* Preserve initial '/'. */
- ++path;
-
- /* Nix out leading `.' or `..' with. */
- if ((path[0] == '.' && path[1] == '\0')
- || (path[0] == '.' && path[1] == '.' && path[2] == '\0'))
- {
- path[0] = '\0';
- changes = 1;
- return changes;
- }
-
- /* Walk along PATH looking for things to compact. */
- i = 0;
- while (1)
- {
- if (!path[i])
- break;
-
- while (path[i] && path[i] != '/')
- i++;
-
- start = i++;
-
- /* If we didn't find any slashes, then there is nothing left to do. */
- if (!path[start])
- break;
-
- /* Handle multiple `/'s in a row. */
- while (path[i] == '/')
- i++;
-
- if ((start + 1) != i)
- {
- strcpy (path + start + 1, path + i);
- i = start + 1;
- changes = 1;
- }
-
- /* Check for `../', `./' or trailing `.' by itself. */
- if (path[i] == '.')
- {
- /* Handle trailing `.' by itself. */
- if (!path[i + 1])
- {
- path[--i] = '\0';
- changes = 1;
- break;
- }
-
- /* Handle `./'. */
- if (path[i + 1] == '/')
- {
- strcpy (path + i, path + i + 1);
- i = (start < 0) ? 0 : start;
- changes = 1;
- continue;
- }
-
- /* Handle `../' or trailing `..' by itself. */
- if (path[i + 1] == '.' &&
- (path[i + 2] == '/' || !path[i + 2]))
- {
- while (--start > -1 && path[start] != '/');
- strcpy (path + start + 1, path + i + 2 + (start == -1 && path[i + 2]));
- i = (start < 0) ? 0 : start;
- changes = 1;
- continue;
- }
- } /* path == '.' */
- } /* while */
-
- /* Addition: Remove all `./'-s and `../'-s preceding the string. */
- i = 0;
- while (1)
- {
- if (path[i] == '.' && path[i + 1] == '/')
- i += 2;
- else if (path[i] == '.' && path[i + 1] == '.' && path[i + 2] == '/')
- i += 3;
- else
- break;
- }
- if (i)
- {
- strcpy (path, path + i - 0);
- changes = 1;