Remove Unixware and openserver support
[oweals/cde.git] / cde / lib / csa / lutil.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23
24 /* $XConsortium: lutil.c /main/4 1996/11/21 20:00:35 drk $ */
25 /*
26  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
27  *  (c) Copyright 1993, 1994 International Business Machines Corp.
28  *  (c) Copyright 1993, 1994 Novell, Inc.
29  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
30  */
31
32 #include <sys/param.h>
33 #include <EUSCompat.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <rpc/rpc.h>
38 #include <unistd.h>
39 #if defined(sun)
40 #include <netdb.h>
41 #include <sys/systeminfo.h>
42 #endif
43 #define X_INCLUDE_PWD_H
44 #define XOS_USE_XT_LOCKING
45 #if defined(linux)
46 #undef SVR4
47 #endif
48 #include <X11/Xos_r.h>
49 #if defined(linux)
50 #define SVR4
51 #endif
52
53 #if !defined(linux)
54 extern char * strdup(const char *);
55 #endif
56
57 extern char *
58 _DtCmGetPrefix(char *str, char sep)
59 {
60         char buf[BUFSIZ];
61         char *ptr;
62
63         if (str == NULL)
64                 return(NULL);
65
66         ptr = buf;
67         while (*str && *str != sep)
68                 *ptr++ = *str++;
69         if (ptr == buf)
70                 return(NULL);
71         else {
72                 *ptr = '\0';
73                 return(strdup(buf));
74         }
75 }
76
77 extern char *
78 _DtCmGetLocalHost()
79 {
80         static char *host = NULL;
81
82         if (host == NULL) {
83                 host = (char *)malloc(MAXHOSTNAMELEN+1);
84 #if defined(sun)
85                 (void)sysinfo(SI_HOSTNAME, host, MAXHOSTNAMELEN);
86 #else
87                 (void)gethostname(host, MAXHOSTNAMELEN);
88 #endif /* sun */
89         }
90
91         return (host);
92 }
93
94 extern char *
95 _DtCmGetLocalDomain(char *hostname)
96 {
97         static char     *domain = NULL;
98         char            buf[BUFSIZ], *ptr;
99         CLIENT          *cl;
100
101         if (domain == NULL) {
102                 domain = (char *)malloc(BUFSIZ);
103 #if defined(sun)
104                 sysinfo(SI_SRPC_DOMAIN, domain, BUFSIZ - 1);
105 #else
106                 getdomainname(domain, BUFSIZ - 1);
107 #endif /* sun */
108
109                 /* check domain name */
110                 /* this is a hack to find out the domain name that
111                  * is acceptable to the rpc interface, e.g.
112                  * DGDO.Eng.Sun.COM is returned by sysinfo but
113                  * this name is not acceptable to the rpc interface
114                  * hence we need to stripe out the first component
115                  */
116                 ptr = domain;
117                 if (hostname == NULL) hostname = _DtCmGetLocalHost();
118                 while (1) {
119                         snprintf(buf, sizeof buf, "%s.%s", hostname, ptr);
120                         if ((cl = clnt_create(buf, 100068, 5, "udp")) == NULL) {
121                                 ptr = strchr(ptr, '.');
122                                 if (ptr)
123                                         ptr++;
124                                 else
125                                         break;
126                         } else {
127                                 clnt_destroy(cl);
128                                 break;
129                         }
130                 }
131                 if (ptr && ptr != domain)
132                         domain = ptr;
133         }
134
135         return (domain);
136 }
137
138 extern char *
139 _DtCmGetHostAtDomain()
140 {
141         static char     *hostname = NULL;
142         char            *host;
143
144         if (hostname == NULL) {
145                 hostname = malloc(BUFSIZ);
146
147                 host = _DtCmGetLocalHost();
148                 if (strchr(host, '.') == NULL)
149                         snprintf(hostname, BUFSIZ, "%s.%s", host,
150                                 _DtCmGetLocalDomain(host));
151                 else
152                         /* XXX strcpy unsafe here */
153                         strcpy(hostname, host);
154         }
155
156         return (hostname);
157 }
158
159 extern char *
160 _DtCmGetUserName()
161 {
162         static char *name = NULL;
163         _Xgetpwparams   pwd_buf;
164         struct passwd * pwd_ret;
165
166         if (name == NULL) {
167           name = malloc(BUFSIZ);
168
169           if ((pwd_ret = _XGetpwuid(geteuid(), pwd_buf)) == NULL)
170             strcpy(name, "nobody");
171           else
172             strcpy(name, pwd_ret->pw_name);
173         }
174
175         return name;
176 }
177
178 /*
179  * this routine checks whether the given name is a valid user name
180  */
181 extern boolean_t
182 _DtCmIsUserName(char *user)
183 {
184         _Xgetpwparams   pwd_buf;
185         struct passwd * pwd_ret;
186
187         pwd_ret = _XGetpwnam(user, pwd_buf);
188         if (pwd_ret == NULL)
189                 return (B_FALSE);
190         else
191                 return (B_TRUE);
192 }
193