44ac155b305c9ed0a169d23137a76183e9785b00
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / lib / libast / sfio / sfpeek.c
1 /* $XConsortium: sfpeek.c /main/3 1995/11/01 18:32:14 rswiston $ */
2 /***************************************************************
3 *                                                              *
4 *                      AT&T - PROPRIETARY                      *
5 *                                                              *
6 *         THIS IS PROPRIETARY SOURCE CODE LICENSED BY          *
7 *                          AT&T CORP.                          *
8 *                                                              *
9 *                Copyright (c) 1995 AT&T Corp.                 *
10 *                     All Rights Reserved                      *
11 *                                                              *
12 *           This software is licensed by AT&T Corp.            *
13 *       under the terms and conditions of the license in       *
14 *       http://www.research.att.com/orgs/ssr/book/reuse        *
15 *                                                              *
16 *               This software was created by the               *
17 *           Software Engineering Research Department           *
18 *                    AT&T Bell Laboratories                    *
19 *                                                              *
20 *               For further information contact                *
21 *                     gsf@research.att.com                     *
22 *                                                              *
23 ***************************************************************/
24 #include        "sfhdr.h"
25
26 /*      Safe access to the internal stream buffer.
27 **      This function is obsolete. sfreserve() should be used.
28 **
29 **      Written by Kiem-Phong Vo (06/27/90).
30 */
31
32 #if __STD_C
33 sfpeek(reg Sfio_t* f, Void_t** bp, reg int size)
34 #else
35 sfpeek(f,bp,size)
36 reg Sfio_t*     f;      /* file to peek */
37 Void_t**        bp;     /* start of data area */
38 reg int         size;   /* size of peek */
39 #endif
40 {       reg int n;
41
42         /* query for the extent of the remainder of the buffer */
43         if(!bp || size == 0)
44         {       if(f->mode&SF_INIT)
45                         (void)_sfmode(f,0,0);
46
47                 if((f->flags&SF_RDWRSTR) == SF_RDWRSTR)
48                 {       SFSTRSIZE(f);
49                         n = (f->data+f->here) - f->next;
50                 }
51                 else    n = f->endb - f->next;
52
53                 if(!bp)
54                         return n;
55                 else if(n > 0)  /* size == 0 */
56                 {       *bp = (Void_t*)f->next;
57                         return 0;
58                 }
59                 /* else fall down and fill buffer */
60         }
61
62         if(!(n = f->flags&SF_READ) )
63                 n = SF_WRITE;
64         if(f->mode != n && _sfmode(f,n,0) < 0)
65                 return -1;
66
67         *bp = sfreserve(f, size <= 0 ? 0 : size > f->size ? f->size : size, 0);
68
69         if(*bp && size >= 0)
70                 return size;
71
72         if((n = sfslen()) > 0)
73         {       *bp = (Void_t*)f->next;
74                 if(size < 0)
75                 {       f->mode |= SF_PEEK;
76                         f->endr = f->endw = f->data;
77                 }
78                 else
79                 {       if(size > n)
80                                 size = n;
81                         f->next += size;
82                 }
83         }
84
85         return (size >= 0 && n >= size) ? size : n;
86 }