2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: bufio.c /main/5 1996/11/01 10:11:35 drk $
26 * Copyright 1991 Massachusetts Institute of Technology
28 * Permission to use, copy, modify, distribute, and sell this software and its
29 * documentation for any purpose is hereby granted without fee, provided that
30 * the above copyright notice appear in all copies and that both that
31 * copyright notice and this permission notice appear in supporting
32 * documentation, and that the name of M.I.T. not be used in advertising or
33 * publicity pertaining to distribution of the software without specific,
34 * written prior permission. M.I.T. makes no representations about the
35 * suitability of this software for any purpose. It is provided "as is"
36 * without express or implied warranty.
38 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
40 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
42 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
43 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45 * Author: Keith Packard, MIT X Consortium
49 /* #include "fontmisc.h" */
62 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
65 #define FileDes(f) ((int) (f)->hidden)
66 #define CompressFileDes(f) (((CECompressInfoPtr) (f)->hidden)->fd)
67 #define CompressSize(f) (((CECompressInfoPtr) (f)->hidden)->size)
78 curoff = f->bufp - f->buffer;
79 fileoff = curoff + f->left;
80 if (curoff + count <= fileoff) {
84 todo = count - (fileoff - curoff);
85 if (lseek (FileDes(f), todo, 1) == -1) {
92 fileoff = read (FileDes(f), f->buffer, curoff);
112 cnt = f->bufp - f->buffer;
114 f->left = BUFFILESIZE;
115 if (write (FileDes(f), f->buffer, cnt) != cnt)
121 _DtHelpCeBufFileOpenWr (int fd)
125 f = _DtHelpCeBufFileCreate ((char *) fd, BufFileRawFlush, NULL, _DtHelpCeBufFileFlush);
127 f->left = BUFFILESIZE;
131 #ifdef obsolete_function
132 _DtHelpCeBufFileWrite (
140 if (BufFilePut (*b++, f) == BUFFILEEOF)
148 _DtHelpCeBufFileFlush (BufFilePtr f, int doClose)
150 if (f->bufp != f->buffer)
151 (*f->io) (BUFFILEEOF, f);
154 return (close (FileDes(f)));
159 /*****************************************************************************
161 *****************************************************************************/
162 /*****************************************************************************
163 * Routines working on a File descriptor
164 *****************************************************************************/
166 FdRawRead (BufFilePtr f)
170 left = read (FileDes(f), f->buffer, BUFFILESIZE);
176 f->bufp = f->buffer + 1;
190 /*****************************************************************************
191 * Routines working on a Raw Compressed file
192 *****************************************************************************/
194 CompressRawRead (BufFilePtr f)
199 left = read (CompressFileDes(f), f->buffer,
200 MIN(CompressSize(f),BUFFILESIZE));
206 CompressSize(f) -= left;
208 f->bufp = f->buffer + 1;
218 close (CompressFileDes (f));
223 /*****************************************************************************
224 * Routines working on a Pipe
225 *****************************************************************************/
226 /*****************************************************************************
227 * Function: int RdPipeStream (BufFilePtr f)
233 *****************************************************************************/
235 RdPipeStream (BufFilePtr f)
239 left = fread(f->buffer, 1, BUFFILESIZE, FileStream(f));
247 clearerr(FileStream(f));
249 f->bufp = f->buffer + 1;
253 /*********************************************************************
254 * Procedure: int ClosePipeStream (BufFilePtr f);
260 ********************************************************************/
267 pclose(FileStream(f));
272 /*****************************************************************************
273 * Semi-Public Routines
274 *****************************************************************************/
275 /*****************************************************************************
276 * Function: BufFilePtr _DtHelpCeBufFileCreate (char *hidden,
277 * int (*io)(), int (*skip)(),
280 * Returns: A pointer to malloc'ed memory or NULL.
282 * Purpose: Create a buffered i/o mechanism.
284 *****************************************************************************/
286 _DtHelpCeBufFileCreate (
294 f = (BufFilePtr) malloc (sizeof *f);
306 /*****************************************************************************
307 * Function: BufFilePtr _DtHelpCeBufFileRdWithFd (int fd)
309 * Returns: A pointer to malloc'ed memory or NULL.
311 * Purpose: Create a buffered i/o mechanism using a file descriptor
312 * as private data and attaching a raw read to the i/o
315 *****************************************************************************/
317 _DtHelpCeBufFileRdWithFd (int fd)
319 return _DtHelpCeBufFileCreate ((char *) fd, FdRawRead, BufFileRawSkip, FdClose);
322 /*****************************************************************************
323 * Function: BufFilePtr _DtHelpCeBufFileRdRawZ (CECompressInfoPtr file)
325 * Returns: A pointer to malloc'ed memory or NULL.
327 * Purpose: Create a buffered i/o mechanism using a file descriptor
328 * as private data and attaching a raw read of compressed
329 * data to the i/o routine.
331 *****************************************************************************/
333 _DtHelpCeBufFileRdRawZ (CECompressInfoPtr file)
335 return _DtHelpCeBufFileCreate ((char *) file, CompressRawRead, NULL,
339 /*****************************************************************************
340 * Function: void _DtHelpCeBufFileClose (BufFilePtr file, int doClose)
344 * Purpose: Calls the close routine associated with the pointer.
345 * Frees the BufFile information.
347 *****************************************************************************/
349 _DtHelpCeBufFileClose (
353 (void) (*f->close) (f, doClose);
357 /*****************************************************************************
358 * Function: void _DtHelpCeBufFileRd (BufFilePtr file, int doClose)
362 * Purpose: Calls the close routine associated with the pointer.
363 * Frees the BufFile information.
365 *****************************************************************************/
383 /*****************************************************************************
384 * Function: BufFilePtr _DtHelpCeCreatePipeBufFile (FILE *stream)
386 * Returns: A pointer to malloc'ed memory or NULL.
388 * Purpose: Create a buffered i/o mechanism using a pipe descriptor
389 * as private data and attaching a raw read to the i/o
392 *****************************************************************************/
394 _DtHelpCeCreatePipeBufFile (FILE *stream)
396 return _DtHelpCeBufFileCreate ((char *) stream,
397 RdPipeStream, NULL, ClosePipeStream);