xref: /titanic_52/usr/src/contrib/ast/src/lib/libast/path/pathrepl.c (revision 906afcb89d0412cc073b95c2d701a804a8cdb62c)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * in place replace of first occurrence of /match/ with /replace/ in path
28  * end of path returned
29  */
30 
31 #define _AST_API_H	1
32 
33 #include <ast.h>
34 
35 char*
36 pathrepl(char* path, const char* match, const char* replace)
37 {
38 	return pathrepl_20100601(path, PATH_MAX, match, replace);
39 }
40 
41 #undef	_AST_API_H
42 
43 #include <ast_api.h>
44 
45 char*
46 pathrepl_20100601(register char* path, size_t size, const char* match, register const char* replace)
47 {
48 	register const char*	m = match;
49 	register const char*	r;
50 	char*			t;
51 
52 	if (!match)
53 		match = "";
54 	if (!replace)
55 		replace = "";
56 	if (streq(match, replace))
57 		return(path + strlen(path));
58 	if (!size)
59 		size = strlen(path) + 1;
60 	for (;;)
61 	{
62 		while (*path && *path++ != '/');
63 		if (!*path) break;
64 		if (*path == *m)
65 		{
66 			t = path;
67 			while (*m && *m++ == *path) path++;
68 			if (!*m && *path == '/')
69 			{
70 				register char*	p;
71 
72 				p = t;
73 				r = replace;
74 				while (p < path && *r) *p++ = *r++;
75 				if (p < path) while (*p++ = *path++);
76 				else if (*r && p >= path)
77 				{
78 					register char*	u;
79 
80 					t = path + strlen(path);
81 					u = t + strlen(r);
82 					while (t >= path) *u-- = *t--;
83 					while (*r) *p++ = *r++;
84 				}
85 				else p += strlen(p) + 1;
86 				return(p - 1);
87 			}
88 			path = t;
89 			m = match;
90 		}
91 	}
92 	return(path);
93 }
94