1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % File:         PXK:PSLEXTRAS.C
5 % Description:  Miscellaneous support routines.
6 % Author:       RAM, HP/FSD
7 % Created:      9-Mar-84
8 % Modified:     21-Mar-85 11:25:52
9 % Mode:         Text
10 % Package:
11 % Status:       Open Source: BSD License
12 %
13 % (c) Copyright 1983, Hewlett-Packard Company, see the file
14 %            HP_disclaimer at the root of the PSL file tree
15 %
16 % Redistribution and use in source and binary forms, with or without
17 % modification, are permitted provided that the following conditions are met:
18 %
19 %    * Redistributions of source code must retain the relevant copyright
20 %      notice, this list of conditions and the following disclaimer.
21 %    * Redistributions in binary form must reproduce the above copyright
22 %      notice, this list of conditions and the following disclaimer in the
23 %      documentation and/or other materials provided with the distribution.
24 %
25 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 % THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 % PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
29 % CONTRIBUTORS
30 % BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 % POSSIBILITY OF SUCH DAMAGE.
37 %
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %
40 % Revisions:
41 %
42 % 05-Apr-88 (Julian Padget)
43 %  Reinstated alarm and ualarm (again)
44 % 29-May-87 (Leigh Stoller & Harold Carr)
45 %  Added external_setenv and friends.
46 % 21-Mar-85 11:09:00 (Scott Marovich)
47 %  Rewrite "timc()" to return time since 1st call, and never cream LISP tag.
48 % 21-Feb-85 09:02:49 (Vicki O'Day)
49 %  Fixed bug in uxwritefloat - it was setting the length field of the printable
50 %  string incorrectly.
51 % 18-Jul-84 11:14:24 (RAM)
52 %  Added external_time.  Put call to expand_file_name in external_stat,
53 %  external_link, and external_unlink.
54 % 10-Jul-84 (Vicki O'Day)
55 %  Added external_stat, external_link and external_unlink.
56 % 29-Jun-84 14:15:53 (RAM)
57 %  Removed hp_quit (obsolete).
58 % 27-Jun-84 (Vicki O'Day)
59 %  Added external_strlen and external_getenv.
60 %
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 */
63 
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 #include <sys/times.h>
71 
external_alarm(sec)72 int external_alarm(sec)
73 unsigned long sec;
74 {
75   alarm(sec);
76 }
77 
external_ualarm(usec,repeat)78 int external_ualarm(usec,repeat)
79 unsigned long usec,repeat;
80 { return( ualarm(usec,repeat));
81 }
82 
83 char *expand_file_name();    /* from unix-io.c */
84 long time(), times();        /* from kernel */
85 
86 /* Tag( external_time )
87  */
external_time(tloc)88 long external_time(tloc)
89 long *tloc;
90 {
91   return (time(tloc));
92 }
93 
94 /* Tag( external_timc )
95  */
96 long
external_timc(buffer)97 external_timc(buffer)
98      struct tms *buffer;
99 {
100   return(times(buffer));
101 }
102 
103 /* Tag( external_stat )
104  */
external_stat(path,buf)105 int external_stat(path, buf)
106 char *path;
107 struct stat *buf;
108 {
109     return stat(expand_file_name(path), buf);
110 }
111 
112 
external_mkdir(name,mode)113 int external_mkdir (name, mode)
114     int mode;
115     char * name;
116  { return mkdir (name, mode); }
117 
external_rmdir(name)118 int external_rmdir (name)
119     char * name;
120  { return rmdir (name); }
121 
122 /* Tag( external_link )
123  */
external_link(path1,path2)124 int external_link (path1, path2)
125 char *path1, *path2;
126 {
127     return link(expand_file_name(path1), expand_file_name(path2));
128 }
129 
130 /* Tag( external_unlink )
131  */
external_unlink(path)132 int external_unlink (path)
133 char *path;
134 {
135     return unlink(expand_file_name(path));
136 }
137 
138 /* Tag( external_strlen )
139  */
external_strlen(s)140 int external_strlen (s)
141      char *s;
142 {
143     return strlen(s);
144 }
145 
146 /* Tag( external_getenv )
147  */
external_getenv(name)148 char *external_getenv (name)
149      char *name;
150 {
151     return getenv(name);
152 }
153 
154 
external_setenv(var,val)155 int external_setenv (var, val)
156     char *var, *val;
157 {
158   int i;
159   extern char **environ;
160   char **envnew;
161   char var_plus_equal_sign[100];
162 
163   /* Look for first empty slot to find number of existing env variables. */
164   for (i = 0 ; environ [i] != NULL ; i++) ;
165 
166   /* Make a new environment array with 2 new slots - 1 for var being set,
167      and 1 extra empty slot. */
168   envnew = (char **) calloc ((i + 2), sizeof(char *));
169 
170   bcopy((char *)environ, (char *)envnew, i * sizeof(char *));
171   environ = envnew;
172   strcpy(var_plus_equal_sign, var);
173   strcat(var_plus_equal_sign, "=");
174   return(setenv (var_plus_equal_sign, val, 1));
175 }
176 
177 /*
178  * sets the value of var to be arg in the Unix environment env.
179  * Var should end with '=' (bindings are of the form "var=value").
180  * This procedure assumes the memory for the first level of environ
181  * was allocated using calloc, with enough extra room at the end so not
182  * to have to do a realloc().
183  */
184 int
setenv(var,value,ov)185 setenv (var, value, ov)
186      const char *var, *value;
187      int ov;
188 {
189     extern char **environ;
190     int index = 0;
191     int len = strlen(var);
192 
193     while (environ [index] != NULL) {
194         if (strncmp (environ [index], var, len) == 0) {
195         /* found it */
196         environ[index] = (char *)malloc (len + strlen (value) + 1);
197         strcpy (environ [index], var);
198         strcat (environ [index], value);
199         return (ov);
200         }
201         index ++;
202     }
203 
204     environ [index] = (char *) malloc (len + strlen (value) + 1);
205     strcpy (environ [index], var);
206     strcat (environ [index], value);
207     environ [++index] = NULL;
208 }
209 
210 void
block_copy(b1,b2,length)211 block_copy (b1, b2, length)
212      char *b1, *b2;
213      int length;
214 {
215   while (length-- > 0)
216     *b2++ = *b1++;
217 }
218 
219 #define LISPEOF  4      /* Lisp uses ctrl-D for end of file */
220 
221 /* Tag( unixreadrecord )
222  */
unixreadrecord(fp,buf)223 int unixreadrecord(fp, buf)
224      FILE *fp;
225      char *buf;
226 {
227   int i;
228   char c;
229   for (i=0, c=' '; ((c != LISPEOF) && (c != '\n')); i++)
230     {
231       c = fgetc(fp);
232       if (c == EOF )
233     c = LISPEOF;
234       *buf++ = c;
235     }
236   return i;
237 }
238 
239 /* Tag( unixwriterecord )
240  */
unixwriterecord(fp,buf,count)241 int unixwriterecord(fp, buf, count)
242      FILE *fp;
243      char *buf;
244 int  count;
245 {
246   int i;
247   for (i=0; i<count; i++, buf++)
248     fputc(*buf, fp);
249 }
250 
251 
252 
253 
254 
255