xref: /onnv-gate/usr/src/lib/libast/common/path/pathgetlink.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 
284887Schin #include "univlib.h"
294887Schin 
304887Schin #ifdef UNIV_MAX
314887Schin 
324887Schin #include <ctype.h>
334887Schin 
344887Schin #endif
354887Schin 
364887Schin /*
374887Schin  * return external representation for symbolic link text of name in buf
384887Schin  * the link text string length is returned
394887Schin  */
404887Schin 
414887Schin int
pathgetlink(const char * name,char * buf,int siz)424887Schin pathgetlink(const char* name, char* buf, int siz)
434887Schin {
444887Schin 	int	n;
454887Schin 
464887Schin 	if ((n = readlink(name, buf, siz)) < 0) return(-1);
474887Schin 	if (n >= siz)
484887Schin 	{
494887Schin 		errno = EINVAL;
504887Schin 		return(-1);
514887Schin 	}
524887Schin 	buf[n] = 0;
534887Schin #ifdef UNIV_MAX
544887Schin 	if (isspace(*buf))
554887Schin 	{
564887Schin 		register char*	s;
574887Schin 		register char*	t;
584887Schin 		register char*	u;
594887Schin 		register char*	v;
604887Schin 		int		match = 0;
614887Schin 		char		tmp[PATH_MAX];
624887Schin 
634887Schin 		s = buf;
644887Schin 		t = tmp;
654887Schin 		while (isalnum(*++s) || *s == '_' || *s == '.');
664887Schin 		if (*s++)
674887Schin 		{
684887Schin 			for (;;)
694887Schin 			{
704887Schin 				if (!*s || isspace(*s))
714887Schin 				{
724887Schin 					if (match)
734887Schin 					{
744887Schin 						*t = 0;
754887Schin 						n = t - tmp;
764887Schin 						strcpy(buf, tmp);
774887Schin 					}
784887Schin 					break;
794887Schin 				}
804887Schin 				if (t >= &tmp[sizeof(tmp)]) break;
814887Schin 				*t++ = *s++;
824887Schin 				if (!match && t < &tmp[sizeof(tmp) - univ_size + 1]) for (n = 0; n < UNIV_MAX; n++)
834887Schin 				{
844887Schin 					if (*(v = s - 1) == *(u = univ_name[n]))
854887Schin 					{
864887Schin 						while (*u && *v++ == *u) u++;
874887Schin 						if (!*u)
884887Schin 						{
894887Schin 							match = 1;
904887Schin 							strcpy(t - 1, univ_cond);
914887Schin 							t += univ_size - 1;
924887Schin 							s = v;
934887Schin 							break;
944887Schin 						}
954887Schin 					}
964887Schin 				}
974887Schin 			}
984887Schin 		}
994887Schin 	}
1004887Schin #endif
1014887Schin 	return(n);
1024887Schin }
103