1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /* No user fns here.  Pesch 15apr92. */
18 
19 #define _DEFAULT_SOURCE
20 #include <_ansi.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <sys/unistd.h>
25 #include "local.h"
26 
27 /*
28  * Small standard I/O/seek/close functions.
29  * These maintain the `known seek offset' for seek optimisation.
30  */
31 
32 ssize_t
__sread(void * cookie,char * buf,size_t n)33 __sread (
34        void *cookie,
35        char *buf,
36        size_t n)
37 {
38   register FILE *fp = (FILE *) cookie;
39   register ssize_t ret;
40 
41 #ifdef __SCLE
42   int oldmode = 0;
43   if (fp->_flags & __SCLE)
44     oldmode = setmode (fp->_file, O_BINARY);
45 #endif
46 
47   ret = read (fp->_file, buf, n);
48 
49 #ifdef __SCLE
50   if (oldmode)
51     setmode (fp->_file, oldmode);
52 #endif
53 
54   /* If the read succeeded, update the current offset.  */
55 
56   if (ret >= 0)
57     fp->_offset += ret;
58   else
59     fp->_flags &= ~__SOFF;	/* paranoia */
60   return ret;
61 }
62 
63 /* Dummy function used in sscanf/swscanf. */
64 ssize_t
__seofread(void * cookie,char * buf,size_t len)65 __seofread (
66        void *cookie,
67        char *buf,
68        size_t len)
69 {
70   (void) cookie;
71   (void) buf;
72   (void) len;
73   return 0;
74 }
75 
76 ssize_t
__swrite(void * cookie,char const * buf,size_t n)77 __swrite (
78        void *cookie,
79        char const *buf,
80        size_t n)
81 {
82   register FILE *fp = (FILE *) cookie;
83   ssize_t w;
84 #ifdef __SCLE
85   int oldmode=0;
86 #endif
87 
88   if (fp->_flags & __SAPP)
89     lseek (fp->_file, (_off_t) 0, SEEK_END);
90   fp->_flags &= ~__SOFF;	/* in case O_APPEND mode is set */
91 
92 #ifdef __SCLE
93   if (fp->_flags & __SCLE)
94     oldmode = setmode (fp->_file, O_BINARY);
95 #endif
96 
97   w = write (fp->_file, buf, n);
98 
99 #ifdef __SCLE
100   if (oldmode)
101     setmode (fp->_file, oldmode);
102 #endif
103 
104   return w;
105 }
106 
107 _fpos_t
__sseek(void * cookie,_fpos_t offset,int whence)108 __sseek (
109        void *cookie,
110        _fpos_t offset,
111        int whence)
112 {
113   register FILE *fp = (FILE *) cookie;
114   register _off_t ret;
115 
116   ret = lseek (fp->_file, (_off_t) offset, whence);
117   if (ret == -1L)
118     fp->_flags &= ~__SOFF;
119   else
120     {
121       fp->_flags |= __SOFF;
122       fp->_offset = ret;
123     }
124   return ret;
125 }
126 
127 int
__sclose(void * cookie)128 __sclose (
129        void *cookie)
130 {
131   FILE *fp = (FILE *) cookie;
132 
133   return close (fp->_file);
134 }
135 
136 #ifdef __SCLE
137 int
__stextmode(int fd)138 __stextmode (int fd)
139 {
140 #ifdef __CYGWIN__
141   extern int _cygwin_istext_for_stdio (int);
142   return _cygwin_istext_for_stdio (fd);
143 #else
144   return 0;
145 #endif
146 }
147 #endif
148