relative.c: Introduce xstrdup(), reindent

This commit is contained in:
Дмитрий Левин 2007-11-18 15:52:46 +00:00
parent c97667fad5
commit 986ef530f3

View File

@ -23,6 +23,27 @@
#include <errno.h>
#include <sys/param.h>
static void *
xmalloc(size_t size)
{
void *r = malloc(size);
if (!r)
error(EXIT_FAILURE, errno, "malloc: allocating %lu bytes",
(unsigned long) size);
return r;
}
static char *
xstrdup(const char *s)
{
size_t len = strlen(s);
char *r = xmalloc(len + 1);
memcpy(r, s, len + 1);
return r;
}
static void __attribute__ ((noreturn))
result(const char *str)
{
@ -82,7 +103,12 @@ lookup_back(const char *str, const char sym, const char *pos)
int
main(int ac, char *av[])
{
unsigned reslen;
const char *orig_what;
char *what_p, *to_p;
char *what = xstrdup(av[1]);
char *to = xstrdup(av[2]);
if (ac < 3)
{
@ -94,32 +120,22 @@ main(int ac, char *av[])
orig_what = normalize(av[1]);
normalize(av[2]);
{
unsigned reslen;
char *what_p, *to_p;
char what[1 + strlen(av[1])], to[1 + strlen(av[2])];
memcpy(what, av[1], sizeof(what));
memcpy(to, av[2], sizeof(to));
what = xstrdup(av[1]);
to = xstrdup(av[2]);
if ('/' != *what)
result(what);
if ('/' != *to)
{
fputs("relative: <to> must be absolute filename\n",
stderr);
return 1;
}
error(EXIT_FAILURE, 0,
"destination must be absolute filename");
reslen = PATH_MAX + strlen(what) + strlen(to);
strip_trailing(what, '/');
strip_trailing(to, '/');
for (what_p = what, to_p = to; *what_p && *to_p;
++what_p, ++to_p)
for (what_p = what, to_p = to; *what_p && *to_p; ++what_p, ++to_p)
if (*what_p != *to_p)
break;
@ -129,8 +145,6 @@ main(int ac, char *av[])
{
char res[reslen];
memset(res, 0, sizeof(res));
if (('/' == *what_p) && !*to_p)
result(orig_what + (++what_p - what));
@ -138,6 +152,8 @@ main(int ac, char *av[])
{
what_p = lookup_back(what, '/', what_p - 1);
strcpy(res, "..");
} else {
res[0] = '\0';
}
for (; *to_p; ++to_p)
@ -154,5 +170,4 @@ main(int ac, char *av[])
strcat(res, orig_what + (what_p - what));
result(res);
}
}
}