/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Bell Laboratories25*26* in place replace of first occurrence of /match/ with /replace/ in path27* end of path returned28*/2930#define _AST_API_H 13132#include <ast.h>3334char*35pathrepl(char* path, const char* match, const char* replace)36{37return pathrepl_20100601(path, PATH_MAX, match, replace);38}3940#undef _AST_API_H4142#include <ast_api.h>4344char*45pathrepl_20100601(register char* path, size_t size, const char* match, register const char* replace)46{47register const char* m = match;48register const char* r;49char* t;5051if (!match)52match = "";53if (!replace)54replace = "";55if (streq(match, replace))56return(path + strlen(path));57if (!size)58size = strlen(path) + 1;59for (;;)60{61while (*path && *path++ != '/');62if (!*path) break;63if (*path == *m)64{65t = path;66while (*m && *m++ == *path) path++;67if (!*m && *path == '/')68{69register char* p;7071p = t;72r = replace;73while (p < path && *r) *p++ = *r++;74if (p < path) while (*p++ = *path++);75else if (*r && p >= path)76{77register char* u;7879t = path + strlen(path);80u = t + strlen(r);81while (t >= path) *u-- = *t--;82while (*r) *p++ = *r++;83}84else p += strlen(p) + 1;85return(p - 1);86}87path = t;88m = match;89}90}91return(path);92}939495