Deleted Added
full compact
ungetc.c (22993) ungetc.c (35129)
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 25 unchanged lines hidden (view full) ---

34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38#if 0
39static char sccsid[] = "@(#)ungetc.c 8.2 (Berkeley) 11/3/93";
40#endif
41static const char rcsid[] =
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 25 unchanged lines hidden (view full) ---

34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38#if 0
39static char sccsid[] = "@(#)ungetc.c 8.2 (Berkeley) 11/3/93";
40#endif
41static const char rcsid[] =
42 "$Id$";
42 "$Id: ungetc.c,v 1.5 1997/02/22 15:02:38 peter Exp $";
43#endif /* LIBC_SCCS and not lint */
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include "local.h"
43#endif /* LIBC_SCCS and not lint */
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include "local.h"
49#ifdef _THREAD_SAFE
50#include <pthread.h>
51#include "pthread_private.h"
52#endif
49#include "libc_private.h"
53
54static int __submore __P((FILE *));
55
56/*
57 * Expand the ungetc buffer `in place'. That is, adjust fp->_p when
58 * the buffer moves, so that it points the same distance from the end,
59 * and move the bytes in the buffer around as necessary so that they
60 * are all at the end (stack-style).

--- 35 unchanged lines hidden (view full) ---

96ungetc(c, fp)
97 int c;
98 register FILE *fp;
99{
100 if (c == EOF)
101 return (EOF);
102 if (!__sdidinit)
103 __sinit();
50
51static int __submore __P((FILE *));
52
53/*
54 * Expand the ungetc buffer `in place'. That is, adjust fp->_p when
55 * the buffer moves, so that it points the same distance from the end,
56 * and move the bytes in the buffer around as necessary so that they
57 * are all at the end (stack-style).

--- 35 unchanged lines hidden (view full) ---

93ungetc(c, fp)
94 int c;
95 register FILE *fp;
96{
97 if (c == EOF)
98 return (EOF);
99 if (!__sdidinit)
100 __sinit();
104#ifdef _THREAD_SAFE
105 _thread_flockfile(fp,__FILE__,__LINE__);
106#endif
101 FLOCKFILE(fp);
107 if ((fp->_flags & __SRD) == 0) {
108 /*
109 * Not already reading: no good unless reading-and-writing.
110 * Otherwise, flush any current write stuff.
111 */
112 if ((fp->_flags & __SRW) == 0) {
102 if ((fp->_flags & __SRD) == 0) {
103 /*
104 * Not already reading: no good unless reading-and-writing.
105 * Otherwise, flush any current write stuff.
106 */
107 if ((fp->_flags & __SRW) == 0) {
113#ifdef _THREAD_SAFE
114 _thread_funlockfile(fp);
115#endif
108 FUNLOCKFILE(fp);
116 return (EOF);
117 }
118 if (fp->_flags & __SWR) {
119 if (__sflush(fp)) {
109 return (EOF);
110 }
111 if (fp->_flags & __SWR) {
112 if (__sflush(fp)) {
120#ifdef _THREAD_SAFE
121 _thread_funlockfile(fp);
122#endif
113 FUNLOCKFILE(fp);
123 return (EOF);
124 }
125 fp->_flags &= ~__SWR;
126 fp->_w = 0;
127 fp->_lbfsize = 0;
128 }
129 fp->_flags |= __SRD;
130 }
131 c = (unsigned char)c;
132
133 /*
134 * If we are in the middle of ungetc'ing, just continue.
135 * This may require expanding the current ungetc buffer.
136 */
137 if (HASUB(fp)) {
138 if (fp->_r >= fp->_ub._size && __submore(fp)) {
114 return (EOF);
115 }
116 fp->_flags &= ~__SWR;
117 fp->_w = 0;
118 fp->_lbfsize = 0;
119 }
120 fp->_flags |= __SRD;
121 }
122 c = (unsigned char)c;
123
124 /*
125 * If we are in the middle of ungetc'ing, just continue.
126 * This may require expanding the current ungetc buffer.
127 */
128 if (HASUB(fp)) {
129 if (fp->_r >= fp->_ub._size && __submore(fp)) {
139#ifdef _THREAD_SAFE
140 _thread_funlockfile(fp);
141#endif
130 FUNLOCKFILE(fp);
142 return (EOF);
143 }
144 *--fp->_p = c;
145 fp->_r++;
131 return (EOF);
132 }
133 *--fp->_p = c;
134 fp->_r++;
146#ifdef _THREAD_SAFE
147 _thread_funlockfile(fp);
148#endif
135 FUNLOCKFILE(fp);
149 return (c);
150 }
151 fp->_flags &= ~__SEOF;
152
153 /*
154 * If we can handle this by simply backing up, do so,
155 * but never replace the original character.
156 * (This makes sscanf() work when scanning `const' data.)
157 */
158 if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
159 fp->_p[-1] == c) {
160 fp->_p--;
161 fp->_r++;
136 return (c);
137 }
138 fp->_flags &= ~__SEOF;
139
140 /*
141 * If we can handle this by simply backing up, do so,
142 * but never replace the original character.
143 * (This makes sscanf() work when scanning `const' data.)
144 */
145 if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
146 fp->_p[-1] == c) {
147 fp->_p--;
148 fp->_r++;
162#ifdef _THREAD_SAFE
163 _thread_funlockfile(fp);
164#endif
149 FUNLOCKFILE(fp);
165 return (c);
166 }
167
168 /*
169 * Create an ungetc buffer.
170 * Initially, we will use the `reserve' buffer.
171 */
172 fp->_ur = fp->_r;
173 fp->_up = fp->_p;
174 fp->_ub._base = fp->_ubuf;
175 fp->_ub._size = sizeof(fp->_ubuf);
176 fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
177 fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
178 fp->_r = 1;
150 return (c);
151 }
152
153 /*
154 * Create an ungetc buffer.
155 * Initially, we will use the `reserve' buffer.
156 */
157 fp->_ur = fp->_r;
158 fp->_up = fp->_p;
159 fp->_ub._base = fp->_ubuf;
160 fp->_ub._size = sizeof(fp->_ubuf);
161 fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
162 fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
163 fp->_r = 1;
179#ifdef _THREAD_SAFE
180 _thread_funlockfile(fp);
181#endif
164 FUNLOCKFILE(fp);
182 return (c);
183}
165 return (c);
166}