Yet another attempted fix for 434383: menus showing up as blocks.
[nedit.git] / util / utils.c
blobd3d6842faf17af9feca31324e911bec381a2931d
1 static const char CVSID[] = "$Id: utils.c,v 1.10 2001/12/13 13:27:01 amai Exp $";
2 /*******************************************************************************
3 * *
4 * utils.c -- miscellaneous non-GUI routines *
5 * *
6 * *
7 * This is free software; you can redistribute it and/or modify it under the *
8 * terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) any later *
10 * version. *
11 * *
12 * This software is distributed in the hope that it will be useful, but WITHOUT *
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
15 * for more details.* *
16 * *
17 * You should have received a copy of the GNU General Public License along with *
18 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
19 * Place, Suite 330, Boston, MA 02111-1307 USA *
20 * *
21 *******************************************************************************/
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #ifdef VMS
28 #include <lib$routines.h>
29 #include ssdef
30 #include syidef
31 #include "../util/VMSparam.h"
32 #include "../util/VMSutils.h"
33 #endif
34 #include <sys/types.h>
35 #include <sys/utsname.h>
36 #ifdef VMS
37 #include "vmsparam.h"
38 #else
39 #include <sys/param.h>
40 #endif /*VMS*/
41 #include <pwd.h>
42 /* just to get 'Boolean' types defined: */
43 #include <X11/Xlib.h>
45 #include "utils.h"
48 extern const char
49 *GetCurrentDir(void)
50 /* return non-NULL value for the current working directory.
51 If system call fails, provide a fallback value */
53 static char curdir[MAXPATHLEN];
55 if (!getcwd(curdir, MAXPATHLEN)) {
56 perror("NEdit: getcwd() fails");
57 strcpy(curdir, ".");
59 return (curdir);
63 extern const char
64 *GetHomeDir(void)
65 /* return a non-NULL value for the user's home directory,
66 without trailing slash.
67 We try the environment var and the system user database. */
69 const char *ptr;
70 static char homedir[MAXPATHLEN]="";
71 struct passwd *passwdEntry;
72 int len;
74 if (*homedir) {
75 return homedir;
77 ptr=getenv("HOME");
78 if (!ptr) {
79 passwdEntry = getpwuid(getuid());
80 if (passwdEntry && *(passwdEntry->pw_dir)) {
81 ptr= passwdEntry->pw_dir;
82 } else {
83 /* This is really serious, so just exit. */
84 perror("NEdit/nc: getpwuid() failed ");
85 exit(EXIT_FAILURE);
88 strncpy(homedir, ptr, sizeof(homedir)-1);
89 homedir[sizeof(homedir)-1]='\0';
90 /* Fix trailing slash */
91 len=strlen(homedir);
92 if (len>1 && homedir[len-1]=='/') {
93 homedir[len-1]='\0';
95 return homedir;
99 ** Return a pointer to the username of the current user in a statically
100 ** allocated string.
102 const char
103 *GetUserName(void)
105 #ifdef VMS
106 return cuserid(NULL);
107 #else
108 /* cuserid has apparently been dropped from the ansi C standard, and if
109 strict ansi compliance is turned on (on Sun anyhow, maybe others), calls
110 to cuserid fail to compile. Older versions of nedit try to use the
111 getlogin call first, then if that fails, use getpwuid and getuid. This
112 results in the user-name of the original terminal being used, which is
113 not correct when the user uses the su command. Now, getpwuid only: */
115 const struct passwd *passwdEntry;
116 static char *userName=NULL;
118 if (userName)
119 return userName;
121 passwdEntry = getpwuid(getuid());
122 if (!passwdEntry) {
123 /* This is really serious, so just exit. */
124 perror("NEdit/nc: getpwuid() failed ");
125 exit(EXIT_FAILURE);
127 userName=malloc(strlen(passwdEntry->pw_name)+1);
128 strcpy(userName, passwdEntry->pw_name);
129 return userName;
130 #endif /* VMS */
135 ** Writes the hostname of the current system in string "hostname".
137 const char
138 *GetHostName(void)
140 static char hostname[MAXNODENAMELEN+1];
141 static int hostnameFound = False;
143 if (!hostnameFound) {
144 #ifdef VMS
145 /* This should be simple, but uname is not supported in the DEC C RTL and
146 gethostname on VMS depends either on Multinet or UCX. So use uname
147 on Unix, and use LIB$GETSYI on VMS. Note the VMS hostname will
148 be in DECNET format with trailing double colons, e.g. "FNALV1::". */
149 int syi_status;
150 struct dsc$descriptor_s *hostnameDesc;
151 unsigned long int syiItemCode = SYI$_NODENAME; /* get Nodename */
152 unsigned long int unused = 0;
153 unsigned short int hostnameLen = MAXNODENAMELEN+1;
155 hostnameDesc = NulStrWrtDesc(hostname, MAXNODENAMELEN+1);
156 syi_status = lib$getsyi(&syiItemCode, &unused, hostnameDesc, &hostnameLen,
157 0, 0);
158 if (syi_status != SS$_NORMAL) {
159 fprintf(stderr, "Error return from lib$getsyi: %d", syi_status);
160 strcpy(hostname, "VMS");
161 } else
162 hostname[hostnameLen] = '\0';
163 FreeStrDesc(hostnameDesc);
164 #else
165 struct utsname nameStruct;
166 int rc = uname(&nameStruct);
167 if (rc<0) {
168 /* Shouldn't ever happen, so we better exit() here */
169 perror("NEdit/nc: uname() failed ");
170 exit(EXIT_FAILURE);
172 strcpy(hostname, nameStruct.nodename);
173 #endif /* VMS */
174 hostnameFound = True;
176 return hostname;
181 ** Create a path: $HOME/filename
182 ** Return "" if it doesn't fit into the buffer
184 char
185 *PrependHome(const char *filename, char *buf, int buflen)
187 const char *homedir;
188 int home_len, file_len;
190 homedir=GetHomeDir();
191 home_len=strlen(homedir);
192 file_len=strlen(filename);
193 if ( (home_len+1+file_len)>=buflen ) {
194 buf[0]='\0';
196 else {
197 strcpy(buf, homedir);
198 strcat(buf, "/");
199 strcat(buf, filename);
201 return buf;
204 extern int Max(int i1, int i2)
206 return i1 >= i2 ? i1 : i2;
209 extern int Min(int i1, int i2)
211 return i1 <= i2 ? i1 : i2;
214 extern int Min3(int i1, int i2, int i3)
216 if (i1 <= i2 && i1 <= i3)
217 return i1;
218 return i2 <= i3 ? i2 : i3;
221 extern int Max3(int i1, int i2, int i3)
223 if (i1 >= i2 && i1 >= i3)
224 return i1;
225 return i2 >= i3 ? i2 : i3;