From aa2245e867fa1cce777b5397b2a6f589623ad491 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Fri, 7 Mar 2003 05:58:35 +0000 Subject: [PATCH] . Added expand_tilde() . Added strip_endslash() . wwwroot gets its tilde expanded and trailing slash clipped off --- trunk/darkhttpd.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index 97caf6f..3fd4e5c 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -182,6 +183,48 @@ static void usage(void) +/* --------------------------------------------------------------------------- + * Expands a path beginning with a tilde. The returned string needs to be + * deallocated. + */ +static char *expand_tilde(const char *path) +{ + const char *home; + char *tmp = NULL; + + if (path[0] != '~') return strdup(path); /* do nothing */ + + home = getenv("HOME"); + if (home == NULL) + { + /* no ENV variable, try getpwuid() */ + struct passwd *pw = getpwuid(getuid()); + if (pw) home = pw->pw_dir; + } + + if (home == NULL) errx(1, "can't expand `~'"); + + asprintf(&tmp, "%s%s", home, path+1); + if (tmp == NULL) errx(1, "out of memory in asprintf()"); + return tmp; +} + + + +/* --------------------------------------------------------------------------- + * Strips the ending slash from a string (if there is one) + */ +static void strip_endslash(char **str) +{ + if (strlen(*str) < 1) return; + if ((*str)[strlen(*str)-1] != '/') return; + + (*str)[strlen(*str)-1] = 0; + *str = (char*) realloc(*str, strlen(*str)+1); +} + + + /* --------------------------------------------------------------------------- * Parses commandline options. */ @@ -190,7 +233,9 @@ static void parse_commandline(const int argc, char *argv[]) int i; if (argc < 2) usage(); /* no wwwroot given */ - wwwroot = argv[1]; + wwwroot = expand_tilde( argv[1] ); /* ~/public_html/ */ + strip_endslash(&wwwroot); + debugf("wwwroot = ``%s''\n", wwwroot); /* walk through the remainder of the arguments (if any) */ for (i=2; i