Headers and library sources from all versions of Lightspeed C and THINK C
1
2/*
3 * stdio.c
4 *
5 * Copyright (c) 1991 Symantec Corporation. All rights reserved.
6 *
7 */
8
9#include "stdio.h"
10#include "stdarg.h"
11#include "string.h"
12#include "limits.h"
13#include "ansi_private.h"
14
15static void initfp(FILE *, char *, size_t);
16static int nullio(FILE *, int);
17
18char __copyright[] = "THINK C Libraries � 1991 Symantec Corp.";
19
20
21/*
22 * __checkfile - open std stream if necessary
23 *
24 * If the stream is an unopened std stream, it (as well as the other std
25 * streams) is opened, thus providing the illusion that it was open all
26 * along.
27 *
28 * If the stream is not open, we try to make it look enough like an open
29 * stream so that we won't crash. Reads will return EOF, and writes will
30 * go to the bit-bucket.
31 *
32 */
33
34FILE *
35__checkfile(FILE *fp)
36{
37#ifndef _NOCONSOLE_
38 if (fp->std)
39 __open_std();
40#endif
41 if (!fp->refnum) {
42 fp->ptr = (unsigned char *) (fp->buf = &fp->one);
43 fp->size = 1;
44 fp->proc = nullio;
45 }
46 return(fp);
47}
48
49
50/*
51 * __c2p - convert string from C to pascal format
52 *
53 */
54
55unsigned char *
56__c2p(register const char *s, register char *t)
57{
58 asm {
59 movea.l t,a0 ; A0 ==> start of p-string
60 st d1 ; D1.B = length
61@1 move.b d0,(t)+
62 addq.b #1,d1
63 move.b (s)+,d0
64 bne.s @1
65 move.b d1,(a0)
66 move.l a0,d0 ; return p-string
67 }
68}
69
70
71/*
72 * __strin - set up an input stream for sscanf
73 *
74 */
75
76FILE *
77__strin(FILE *fp, const char *s)
78{
79 initfp(fp, (char *) s, strlen(s));
80 return(fp);
81}
82
83
84/*
85 * __strout - set up an output stream for sprintf
86 *
87 */
88
89FILE *
90__strout(FILE *fp, char *s)
91{
92 initfp(fp, s, ULONG_MAX);
93 fp->dirty = 1;
94 return(fp);
95}
96
97
98/*
99 * initfp - initialize a file pointer to dummy values
100 *
101 */
102
103static void
104initfp(FILE *fp, char *s, size_t cnt)
105{
106 memset(fp, 0, sizeof(FILE));
107 fp->refnum = -1;
108 fp->ptr = (unsigned char *) s;
109 fp->cnt = cnt;
110 fp->proc = nullio;
111}
112
113
114/*
115 * nullio - dummy I/O proc
116 *
117 */
118
119static int
120nullio(FILE *fp, int i)
121{
122 return(EOF);
123}
124
125
126/* ---------- sprintf/sscanf ---------- */
127
128
129int
130sprintf(char *s, const char *fmt, ...)
131{
132 return(vsprintf(s, fmt, __va(fmt)));
133}
134
135
136int
137vsprintf(char *s, const char *fmt, void *p)
138{
139 FILE f;
140 int n;
141
142 if ((n = vfprintf(__strout(&f, s), fmt, p)) >= 0)
143 s[n] = 0;
144 return(n);
145}
146
147
148int
149sscanf(const char *s, const char *fmt, ...)
150{
151 return(_vsscanf(s, fmt, __va(fmt)));
152}
153
154
155int
156_vsscanf(const char *s, const char *fmt, void *p)
157{
158 FILE f;
159
160 return(_vfscanf(__strin(&f, s), fmt, p));
161}
162
163
164/* ---------- vectored interface ---------- */
165
166
167int
168__read(FILE *fp)
169{
170 return((*fp->proc)(fp, 0));
171}
172
173
174int
175__write(FILE *fp)
176{
177 return((*fp->proc)(fp, 1));
178}
179
180
181int
182__close(FILE *fp)
183{
184 return((*fp->proc)(fp, 2));
185}