1 static const char CVSID
[] = "$Id: utils.c,v 1.10 2001/12/13 13:27:01 amai Exp $";
2 /*******************************************************************************
4 * utils.c -- miscellaneous non-GUI routines *
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 *
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.* *
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 *
21 *******************************************************************************/
28 #include <lib$routines.h>
31 #include "../util/VMSparam.h"
32 #include "../util/VMSutils.h"
34 #include <sys/types.h>
35 #include <sys/utsname.h>
39 #include <sys/param.h>
42 /* just to get 'Boolean' types defined: */
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");
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. */
70 static char homedir
[MAXPATHLEN
]="";
71 struct passwd
*passwdEntry
;
79 passwdEntry
= getpwuid(getuid());
80 if (passwdEntry
&& *(passwdEntry
->pw_dir
)) {
81 ptr
= passwdEntry
->pw_dir
;
83 /* This is really serious, so just exit. */
84 perror("NEdit/nc: getpwuid() failed ");
88 strncpy(homedir
, ptr
, sizeof(homedir
)-1);
89 homedir
[sizeof(homedir
)-1]='\0';
90 /* Fix trailing slash */
92 if (len
>1 && homedir
[len
-1]=='/') {
99 ** Return a pointer to the username of the current user in a statically
106 return cuserid(NULL
);
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
;
121 passwdEntry
= getpwuid(getuid());
123 /* This is really serious, so just exit. */
124 perror("NEdit/nc: getpwuid() failed ");
127 userName
=malloc(strlen(passwdEntry
->pw_name
)+1);
128 strcpy(userName
, passwdEntry
->pw_name
);
135 ** Writes the hostname of the current system in string "hostname".
140 static char hostname
[MAXNODENAMELEN
+1];
141 static int hostnameFound
= False
;
143 if (!hostnameFound
) {
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::". */
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
,
158 if (syi_status
!= SS$_NORMAL
) {
159 fprintf(stderr
, "Error return from lib$getsyi: %d", syi_status
);
160 strcpy(hostname
, "VMS");
162 hostname
[hostnameLen
] = '\0';
163 FreeStrDesc(hostnameDesc
);
165 struct utsname nameStruct
;
166 int rc
= uname(&nameStruct
);
168 /* Shouldn't ever happen, so we better exit() here */
169 perror("NEdit/nc: uname() failed ");
172 strcpy(hostname
, nameStruct
.nodename
);
174 hostnameFound
= True
;
181 ** Create a path: $HOME/filename
182 ** Return "" if it doesn't fit into the buffer
185 *PrependHome(const char *filename
, char *buf
, int buflen
)
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
) {
197 strcpy(buf
, homedir
);
199 strcat(buf
, filename
);
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
)
218 return i2
<= i3
? i2
: i3
;
221 extern int Max3(int i1
, int i2
, int i3
)
223 if (i1
>= i2
&& i1
>= i3
)
225 return i2
>= i3
? i2
: i3
;