1 /*
2  * Copyright (c) 1998-2018, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <stdio.h>
19 #if !defined(WINNT) && !defined(ST100)
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #endif
23 #include <time.h>
24 #include <errno.h>
25 
26 /* get environ */
27 
28 #if defined(WIN64)
29 char * * * __cdecl __p__environ(void);
30 /*
31  * enclose _fileno within parens to ensure calling the function rather than
32  * the _fileno function macro (if/when it exists).
33  */
34 #define fileno(x) (_fileno)(x)
35 #endif
36 
37 #if   defined(WINNT)
38 #include <stdlib.h>
39 extern char **environ;
40 #elif defined(TARGET_OSX)
41 #include <crt_externs.h>
42 #else
43 extern char **environ;
44 #endif
45 
46 char **
__io_environ()47 __io_environ()
48 {
49 #if defined(TARGET_WIN)
50   return(*__p__environ());
51 #elif !defined(TARGET_OSX)
52   return (environ);
53 #else
54   return (*_NSGetEnviron());
55 #endif
56 }
57 
58 /* get errno value */
59 
60 int
__io_errno()61 __io_errno()
62 {
63   return (errno);
64 }
65 
66 /* set errno value */
67 
68 void
__io_set_errno(int value)69 __io_set_errno(int value)
70 {
71   errno = value;
72 }
73 
74 /* return standard fp's */
75 
76 void *
__io_stdin(void)77 __io_stdin(void)
78 {
79   return ((void *)stdin);
80 }
81 
82 void *
__io_stdout(void)83 __io_stdout(void)
84 {
85   return ((void *)stdout);
86 }
87 
88 void *
__io_stderr(void)89 __io_stderr(void)
90 {
91   return ((void *)stderr);
92 }
93 
94 /* convert macros to routines */
95 
96 #if defined(TARGET_WIN) || defined(WIN32)
97 #include <stdio.h>
98 int
__io_fgetc(FILE * p)99 __io_fgetc(FILE *p)
100 {
101   return _fgetc_nolock(p);
102 }
103 
104 int
__io_ungetc(int x,FILE * p)105 __io_ungetc(int x, FILE *p)
106 {
107   return (_ungetc_nolock(x, (FILE *)p));
108 }
109 
110 int
__io_fputc(int x,FILE * p)111 __io_fputc(int x, FILE *p)
112 {
113   return (_putc_nolock(x, (FILE *)p));
114 }
115 
116 #else
117 
118 int
__io_getc(void * p)119 __io_getc(void *p)
120 {
121   return (getc((FILE *)p));
122 }
123 
124 int
__io_putc(int x,void * p)125 __io_putc(int x, void *p)
126 {
127   return (putc(x, (FILE *)p));
128 }
129 #endif
130 
131 int
__io_getchar(void)132 __io_getchar(void)
133 {
134   return (getchar());
135 }
136 
137 int
__io_putchar(int x)138 __io_putchar(int x)
139 {
140   return (putchar(x));
141 }
142 
143 void
__io_clearerr(void * p)144 __io_clearerr(void *p)
145 {
146   clearerr((FILE *)p);
147 }
148 
149 int
__io_feof(void * p)150 __io_feof(void *p)
151 {
152   return (feof((FILE *)p));
153 }
154 
155 int
__io_ferror(void * p)156 __io_ferror(void *p)
157 {
158   return (ferror((FILE *)p));
159 }
160 
161 /* get fd from fp */
162 
163 int
__io_getfd(void * fp)164 __io_getfd(void *fp)
165 {
166   return (((FILE *)fp)->_fileno);
167 }
168 
169 /* is a tty? */
170 
171 int
__io_isatty(int fd)172 __io_isatty(int fd)
173 {
174   return (isatty(fd));
175 }
176 
177 /* some NT stuff */
178 
179 int
__io_binary_mode(void * fp)180 __io_binary_mode(void *fp)
181 {
182 #if defined(WINNT)
183 #include <fcntl.h>
184 
185 #if defined(WIN64) || defined(WIN32)
186 #define O_BINARY _O_BINARY
187 #endif
188 
189   int mode;
190 
191   mode = setmode(fileno((FILE *)fp), O_BINARY);
192   if (mode == -1) {
193     /* The mode argument is clearly legal, so this should not
194      * happen.  But, in a console app, setmode will fail on
195      * the fd representing stdout.
196      */
197     return 0;
198   }
199   (void)setmode(fileno((FILE *)fp), mode);
200   return (mode & O_BINARY);
201 #else
202   return 1;
203 #endif
204 }
205 
206 int
__io_setmode_binary(void * fp)207 __io_setmode_binary(void *fp)
208 {
209 #if defined(WINNT)
210 #include <fcntl.h>
211 
212 #if defined(WIN64) || defined(WIN32)
213 #define O_BINARY _O_BINARY
214 #endif
215 
216   int mode;
217 
218   return setmode(fileno((FILE *)fp), O_BINARY);
219 #else
220   return 0; /* NOTE: -1 is error */
221 #endif
222 }
223 
224 int
__io_ispipe(void * f)225 __io_ispipe(void *f)
226 {
227 #if !defined(WINNT) && !defined(ST100)
228   struct stat st;
229 
230   fstat(fileno((FILE *)f), &st);
231   if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode))
232     return 1;
233 #endif
234   return 0;
235 }
236 
237 /*
238  * On AT&T SysV R4, Release 1.0, the fwrite function does not correctly
239  * handle line-buffered files.  If the file is line-buffered, then we
240  * just to putc's, else do fwrite directly.
241  *
242  * This is o.k. to be ANSI since pgcc is always used to compile it.
243  */
244 
245 int
__io_fwrite(char * ptr,size_t size,size_t nitems,FILE * stream)246 __io_fwrite(char *ptr, size_t size, size_t nitems, FILE *stream)
247 {
248 #ifdef BROKEN_FWRITE
249   int i, c;
250 
251   if (stream->_base)
252     if (!(stream->_flag & _IOLBF))
253       return fwrite(ptr, size, nitems, stream);
254 
255   /* first time, or line buffered, force putc */
256   /* line buffered */
257   for (i = size * nitems; i > 0; --i)
258     putc(*ptr++, stream);
259   if (ferror(stream))
260     return 0;
261   return nitems;
262 #else
263   return (fwrite(ptr, size, nitems, stream));
264 #endif
265 }
266 
267 #if defined(WINNT) || defined(WIN64) || defined(WIN32)
268 
269 #if   defined(PGI_CRTDLL)
270 extern long *_imp___timezone_dll; /* for crtdll.dll */
271 #define timezone (*_imp___timezone_dll)
272 #elif defined(PGI_CYGNUS)
273 #define timezone _timezone /* cygnus, timezone is usually a function */
274 #endif
275 
276 #elif !defined(DEC) && !defined(IBM) && !defined(ST100_V1_2) &&                !defined(OSX86) /* !defined(WINNT) */
277 extern time_t timezone; /* for the rest */
278 #endif
279 
280 int
__io_timezone(void * tm)281 __io_timezone(void *tm)
282 {
283 #if defined(SUN4) || defined(PPC) || defined(OSX86)
284   return ((struct tm *)tm)->tm_gmtoff;
285 #elif defined(WINNT) || defined(WIN64) || defined(WIN32)
286   return (0);
287 #else
288   return -(timezone - (((struct tm *)tm)->tm_isdst ? 3600 : 0));
289 #endif
290 }
291 
292 #if  (defined(WIN32) || defined(WIN64))
293 /* wrappers for stderr, stdin, stdout : include
294   pgc/port/pgi_iobuf.h after stdio.h
295  */
296 void *
_pgi_get_iob(int xx)297 _pgi_get_iob(int xx) {
298 	 return __acrt_iob_func (xx);
299 }
300 #endif
301