Jspice3
libfuncs.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: UCB CAD Group
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 #include "spice.h"
9 #include "misc.h"
10 #ifdef HAVE_FCNTL_H
11 #include <fcntl.h>
12 #endif
13 
14 #ifndef HAVE_POPEN
15 static char *tfile;
16 /* This is not reentrant! */
17 
18 /* ARGSUSED */
19 FILE *
20 popen(cmd,mode)
21 
22 const char *cmd, *mode;
23 {
24  char buf[BSIZE_SP];
25  FILE *fp;
26 
27  tfile = smktemp("po");
28  fp = fopen(tfile,"w");
29  if (fp != NULL) {
30  (void)sprintf(buf, "%s > %s",cmd,tfile);
31  (void)system(buf);
32  (void)fclose(fp);
33  fp = fopen(tfile,"r");
34  return (fp);
35  }
36  return (NULL);
37 }
38 
39 
40 int
41 pclose(fp)
42 
43 FILE *fp;
44 {
45  if (fp)
46  (void)fclose(fp);
47  if (tfile) {
48  (void)unlink(tfile);
49  tfree(tfile);
50  }
51  return (0);
52 }
53 #endif
54 
55 
56 #ifndef HAVE_PERROR
57 
58 void
59 perror(str)
60 
61 char *str;
62 {
63  char *m = "error occurred\n";
64  if (str && *str)
65  (void)fprintf(stderr,"%s: %s",str,m);
66  else
67  (void)fprintf(stderr,"%s",m);
68 }
69 #endif
70 
71 
72 #ifndef HAVE_ACCESS
73 /* ARGSUSED */
74 int
75 access(pth,m)
76 char *pth;
77 int m;
78 {
79  return (-1);
80 }
81 #endif
82 
83 #ifndef HAVE_SYSTEM
84 int
85 system(str)
86 char *str;
87 {
88  fprintf(stderr, "Can not execute: %s\n",str);
89  return (-1);
90 }
91 #endif
92 
93 #ifndef HAVE_UNLINK
94 /* ARGSUSED */
95 int
96 unlink(fn)
97 char *fn;
98 {
99  return (-1);
100 }
101 #endif
102 
103 #ifndef HAVE_GETENV
104 /* ARGSUSED */
105 char *
107 char *c;
108 {
109  return (NULL);
110 }
111 #endif
112 
113 #ifndef HAVE_CLEARERR
114 /* ARGSUSED */
115 void
117 FILE *fp;
118 {}
119 #endif
120 
121 #ifndef HAVE_GETCWD
122 /* ARGSUSED */
123 char *
124 getcwd(buf,size)
125 char *buf;
126 int size;
127 {
128  if (buf == NULL)
129  buf = tmalloc(4);
130  *buf = '\0';
131  return (buf);
132 }
133 #endif
134 
135 #ifndef HAVE_GETPID
136 int
138 {
139  return (100);
140 }
141 #endif
142 
143 
144 #ifndef HAVE_DUP2
145 #ifdef HAVE_FCNTL_H
146 
147 int
148 dup2(oldd, newd)
149 
150 int oldd, newd;
151 {
152  close(newd);
153 #ifdef F_DUPFD
154  (void) fcntl(oldd, F_DUPFD, newd);
155 #endif
156  return 0;
157 }
158 #endif
159 #endif
160 
161 
162 /*
163  * A more portable version of the standard "mktemp( )" function
164  */
165 #ifndef TEMPFORMAT
166 #define TEMPFORMAT "temp%s%d"
167 #endif
168 
169 char *
171 
172 char *id;
173 {
174  char rbuf[513];
175  char *nbuf;
176  static int num;
177 
178  if (num == 0) {
179  num = getpid();
180  }
181  else
182  num++;
183 
184  if (!id)
185  id = "sp";
186 
187  sprintf(rbuf, TEMPFORMAT, id, num);
188  nbuf = (char *) malloc(strlen(rbuf) + 1);
189  strcpy(nbuf, rbuf);
190 
191  return (nbuf);
192 }
193 
194 
195 /*
196  * Missing math functions
197  */
198 
199 #ifdef notused
200 double
201 logb(x)
202 
203 double x;
204 {
205  double y = 0.0;
206 
207  if (x != 0.0) {
208  if (x < 0.0)
209  x = - x;
210  while (x > 2.0) {
211  y += 1.0;
212  x /= 2.0;
213  }
214  while (x < 1.0) {
215  y -= 1.0;
216  x *= 2.0;
217  }
218  }
219  else
220  y = 0.0;
221 
222  return (y);
223 }
224 
225 
226 double
227 scalb(x, n)
228 
229 double x;
230 int n;
231 {
232  double y, z = 1.0, k = 2.0;
233 
234  if (n < 0) {
235  n = -n;
236  k = 0.5;
237  }
238 
239  if (x != 0.0)
240  for (y = 1.0; n; n >>= 1) {
241  y *= k;
242  if (n & 1)
243  z *= y;
244  }
245 
246  return (x * z);
247 }
248 #endif
249 
250 
251 #ifndef HAVE_ERFC
252 double
254 double x;
255 {
256 /* Returns the complementary error function with fractional error
257  * everywhere less that 1.2e-7. From Numerical Recipes in C.
258  */
259  double t, z, ans;
260 
261  z = fabs(x);
262  t = 1.0/(1.0 + 0.5*x);
263  ans = t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
264  t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
265  t*(-0.82215223+t*0.17087277)))))))));
266  return (x >= 0 ? ans : 2.0 - ans);
267 }
268 #endif
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
char * strcpy()
int system(char *str)
Definition: libfuncs.c:85
char * malloc()
char * smktemp(char *id)
Definition: libfuncs.c:170
#define TEMPFORMAT
Definition: libfuncs.c:166
char * getcwd(char *buf, int size)
Definition: libfuncs.c:124
static char * tfile
Definition: libfuncs.c:15
char * getenv(char *c)
Definition: libfuncs.c:106
char * tmalloc()
int pclose(FILE *fp)
Definition: libfuncs.c:41
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
int unlink(char *fn)
Definition: libfuncs.c:96
int access(char *pth, int m)
Definition: libfuncs.c:75
Definition: cddefs.h:177
int getpid()
Definition: libfuncs.c:137
void clearerr(FILE *fp)
Definition: libfuncs.c:116
Definition: cddefs.h:192
FILE * popen(char *cmd, char *mode) const
Definition: libfuncs.c:20
Definition: cddefs.h:109
double erfc(double x)
Definition: libfuncs.c:253
void perror(char *str)
Definition: libfuncs.c:59