From 624427e2c458ec94fe6ef33e9efeb7876042aa21 Mon Sep 17 00:00:00 2001 From: luochunsheng Date: Tue, 9 Apr 2019 20:18:39 +0800 Subject: [PATCH] paths to targets and facilities normalized, bug #1485304 --- alternatives.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/alternatives.c b/alternatives.c index c1a68db..cdfec6d 100644 --- a/alternatives.c +++ b/alternatives.c @@ -85,6 +85,21 @@ static int usage(int rc) { exit(rc); } +const char *normalize_path(const char *s) { + if (s) { + const char *src = s; + char *dst = (char *)s; + while ((*dst = *src) != '\0') { + do { + src++; + } while (*dst == '/' && *src == '/'); + dst++; + } + } + return (const char *)s; +} + + int streq(const char *a, const char *b) { if (a && b) return strcmp(a, b) ? 0 : 1; @@ -147,7 +162,7 @@ static void setupDoubleArg(enum programModes * mode, const char *** nextArgPtr, nextArg++; if (!*nextArg) usage(2); - *target = strdup(*nextArg); + *target = strdup(normalize_path(*nextArg)); *nextArgPtr = nextArg + 1; } @@ -155,7 +170,7 @@ static void setupLinkSet(struct linkSet * set, const char *** nextArgPtr) { const char ** nextArg = *nextArgPtr; if (!*nextArg || **nextArg != '/') usage(2); - set->facility = strdup(*nextArg); + set->facility = strdup(normalize_path(*nextArg)); nextArg++; if (!*nextArg || **nextArg == '/') usage(2); @@ -163,7 +178,7 @@ static void setupLinkSet(struct linkSet * set, const char *** nextArgPtr) { nextArg++; if (!*nextArg || **nextArg != '/') usage(2); - set->target = strdup(*nextArg); + set->target = strdup(normalize_path(*nextArg)); *nextArgPtr = nextArg + 1; } @@ -295,7 +310,7 @@ static int readConfig(struct alternativeSet * set, const char * title, return 1; } - set->alts[set->numAlts].master.facility = strdup(groups[0].facility); + set->alts[set->numAlts].master.facility = strdup(normalize_path(groups[0].facility)); set->alts[set->numAlts].master.title = strdup(groups[0].title); set->alts[set->numAlts].master.target = line; set->alts[set->numAlts].numSlaves = numGroups - 1; @@ -352,7 +367,7 @@ static int readConfig(struct alternativeSet * set, const char * title, set->alts[set->numAlts].slaves[i - 1].title = strdup(groups[i].title); set->alts[set->numAlts].slaves[i - 1].facility = - strdup(groups[i].facility); + strdup(normalize_path(groups[i].facility)); set->alts[set->numAlts].slaves[i - 1].target = (line && strlen(line)) ? line : NULL; } @@ -396,7 +411,7 @@ static int readConfig(struct alternativeSet * set, const char * title, set->current = i; } - set->currentLink = strdup(linkBuf); + set->currentLink = strdup(normalize_path(linkBuf)); return 0; } @@ -1060,12 +1075,12 @@ int main(int argc, const char ** argv) { } else if (!strcmp(*nextArg, "--altdir")) { nextArg++; if (!*nextArg) usage(2); - altDir = strdup(*nextArg); + altDir = strdup(normalize_path(*nextArg)); nextArg++; } else if (!strcmp(*nextArg, "--admindir")) { nextArg++; if (!*nextArg) usage(2); - stateDir = strdup(*nextArg); + stateDir = strdup(normalize_path(*nextArg)); nextArg++; } else if (!strcmp(*nextArg, "--list")) { if (mode != MODE_UNKNOWN) usage(2); -- 1.8.3.1