xref: /titanic_41/usr/src/lib/libast/common/path/pathgetlink.c (revision dd49f125507979bb2ab505a8daf2a46d1be27051)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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 
28 #include "univlib.h"
29 
30 #ifdef UNIV_MAX
31 
32 #include <ctype.h>
33 
34 #endif
35 
36 /*
37  * return external representation for symbolic link text of name in buf
38  * the link text string length is returned
39  */
40 
41 int
42 pathgetlink(const char* name, char* buf, int siz)
43 {
44 	int	n;
45 
46 	if ((n = readlink(name, buf, siz)) < 0) return(-1);
47 	if (n >= siz)
48 	{
49 		errno = EINVAL;
50 		return(-1);
51 	}
52 	buf[n] = 0;
53 #ifdef UNIV_MAX
54 	if (isspace(*buf))
55 	{
56 		register char*	s;
57 		register char*	t;
58 		register char*	u;
59 		register char*	v;
60 		int		match = 0;
61 		char		tmp[PATH_MAX];
62 
63 		s = buf;
64 		t = tmp;
65 		while (isalnum(*++s) || *s == '_' || *s == '.');
66 		if (*s++)
67 		{
68 			for (;;)
69 			{
70 				if (!*s || isspace(*s))
71 				{
72 					if (match)
73 					{
74 						*t = 0;
75 						n = t - tmp;
76 						strcpy(buf, tmp);
77 					}
78 					break;
79 				}
80 				if (t >= &tmp[sizeof(tmp)]) break;
81 				*t++ = *s++;
82 				if (!match && t < &tmp[sizeof(tmp) - univ_size + 1]) for (n = 0; n < UNIV_MAX; n++)
83 				{
84 					if (*(v = s - 1) == *(u = univ_name[n]))
85 					{
86 						while (*u && *v++ == *u) u++;
87 						if (!*u)
88 						{
89 							match = 1;
90 							strcpy(t - 1, univ_cond);
91 							t += univ_size - 1;
92 							s = v;
93 							break;
94 						}
95 					}
96 				}
97 			}
98 		}
99 	}
100 #endif
101 	return(n);
102 }
103