xref: /onnv-gate/usr/src/lib/libast/common/path/pathrepl.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Bell Laboratories
264887Schin  *
274887Schin  * in place replace of first occurrence of /match/ with /replace/ in path
284887Schin  * end of path returned
294887Schin  */
304887Schin 
314887Schin #include <ast.h>
324887Schin 
334887Schin char*
pathrepl(register char * path,const char * match,register const char * replace)344887Schin pathrepl(register char* path, const char* match, register const char* replace)
354887Schin {
364887Schin 	register const char*	m = match;
374887Schin 	register const char*	r;
384887Schin 	char*			t;
394887Schin 
404887Schin 	if (!match) match = "";
414887Schin 	if (!replace) replace = "";
424887Schin 	if (streq(match, replace))
434887Schin 		return(path + strlen(path));
444887Schin 	for (;;)
454887Schin 	{
464887Schin 		while (*path && *path++ != '/');
474887Schin 		if (!*path) break;
484887Schin 		if (*path == *m)
494887Schin 		{
504887Schin 			t = path;
514887Schin 			while (*m && *m++ == *path) path++;
524887Schin 			if (!*m && *path == '/')
534887Schin 			{
544887Schin 				register char*	p;
554887Schin 
564887Schin 				p = t;
574887Schin 				r = replace;
584887Schin 				while (p < path && *r) *p++ = *r++;
594887Schin 				if (p < path) while (*p++ = *path++);
604887Schin 				else if (*r && p >= path)
614887Schin 				{
624887Schin 					register char*	u;
634887Schin 
644887Schin 					t = path + strlen(path);
654887Schin 					u = t + strlen(r);
664887Schin 					while (t >= path) *u-- = *t--;
674887Schin 					while (*r) *p++ = *r++;
684887Schin 				}
694887Schin 				else p += strlen(p) + 1;
704887Schin 				return(p - 1);
714887Schin 			}
724887Schin 			path = t;
734887Schin 			m = match;
744887Schin 		}
754887Schin 	}
764887Schin 	return(path);
774887Schin }
78