xref: /titanic_44/usr/src/lib/libast/common/path/pathposix.c (revision bdf0047c9427cca40961a023475891c898579c37)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 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 Research
26  *
27  * convert native path to posix fs representation in <buf,siz>
28  * length of converted path returned
29  * if return length >= siz then buf is indeterminate, but another call
30  * with siz=length+1 would work
31  * if buf==0 then required size is returned
32  */
33 
34 #include <ast.h>
35 
36 #if _UWIN
37 
38 #include <uwin.h>
39 
40 size_t
41 pathposix(const char* path, char* buf, size_t siz)
42 {
43 	return uwin_unpath(path, buf, siz);
44 }
45 
46 #else
47 
48 #if __CYGWIN__
49 
50 extern void	cygwin_conv_to_posix_path(const char*, char*);
51 
52 size_t
53 pathposix(const char* path, char* buf, size_t siz)
54 {
55 	size_t		n;
56 
57 	if (!buf || siz < PATH_MAX)
58 	{
59 		char	tmp[PATH_MAX];
60 
61 		cygwin_conv_to_posix_path(path, tmp);
62 		if ((n = strlen(tmp)) < siz && buf)
63 			memcpy(buf, tmp, n + 1);
64 		return n;
65 	}
66 	cygwin_conv_to_posix_path(path, buf);
67 	return strlen(buf);
68 }
69 
70 #else
71 
72 #if __EMX__ && 0 /* show me the docs */
73 
74 size_t
75 pathposix(const char* path, char* buf, size_t siz)
76 {
77 	char*		s;
78 	size_t		n;
79 
80 	if (!_posixpath(buf, path, siz))
81 	{
82 		for (s = buf; *s; s++)
83 			if (*s == '/')
84 				*s = '\\';
85 	}
86 	else if ((n = strlen(path)) < siz && buf)
87 		memcpy(buf, path, n + 1);
88 	return n;
89 }
90 
91 #else
92 
93 #if __INTERIX
94 
95 #include <interix/interix.h>
96 
97 size_t
98 pathposix(const char* path, char *buf, size_t siz)
99 {
100 	static const char	pfx[] = "/dev/fs";
101 
102 	*buf = 0;
103 	if (!strncasecmp(path, pfx, sizeof(pfx) - 1))
104 		strlcpy(buf, path, siz);
105 	else
106 		winpath2unix(path, PATH_NONSTRICT, buf, siz);
107 	return strlen(buf);
108 }
109 
110 #else
111 
112 size_t
113 pathposix(const char* path, char* buf, size_t siz)
114 {
115 	size_t		n;
116 
117 	if ((n = strlen(path)) < siz && buf)
118 		memcpy(buf, path, n + 1);
119 	return n;
120 }
121 
122 #endif
123 
124 #endif
125 
126 #endif
127 
128 #endif
129