]> arthur.barton.de Git - netatalk.git/blob - bin/psorder/pa.c
Initial revision
[netatalk.git] / bin / psorder / pa.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 /* This is used with pa.h */
25
26 #include <stdlib.h>
27
28 #include "pa.h"
29
30 pa_buf_t *pa_init( fd )
31         int fd;
32 {
33         pa_buf_t *h;
34         int rc;
35
36         h = (pa_buf_t *) malloc( sizeof( pa_buf_t ));
37         h->buf = (char *) malloc( PA_BUFBLK * 2 );
38         h->bufsz = PA_BUFBLK * 2;
39
40         if (( rc = read( fd, h->buf, PA_BUFBLK )) < 0 ) {
41                 return( 0 );
42         }
43
44         h->cur = h->buf - 1;
45         h->end = h->buf + rc - 1;
46         h->state = PA_NORMAL;
47         h->fd = fd;
48
49         return( h );
50 }
51
52 char *pa_gettok( h )
53         pa_buf_t *h;
54 {
55         h->state = PA_NORMAL;
56         h->tmp = *(h->cur);
57         *(h->cur) = 0;
58         return( h->mark );
59 }
60
61 char _pa_fixbuf( h )
62         pa_buf_t *h;
63 {
64         int rc;
65         char *t;
66
67         if ( h->state == PA_NORMAL ) {
68                 *(h->buf) = *(h->cur);
69                 h->cur = h->buf;
70         } else {
71                 bcopy( h->mark, h->buf, h->end - h->mark + 1 );
72                 h->cur = h->buf + ( h->cur - h->mark );
73                 h->end = h->buf + ( h->end - h->mark );
74                 h->mark = h->buf;
75         }
76
77         if ( h->bufsz - (( h->cur - h->buf ) + 1 ) < PA_BUFBLK ) {
78                 t = h->buf;
79                 h->buf = (char *) realloc( h->buf, h->bufsz + PA_BUFBLK );
80                 h->bufsz += PA_BUFBLK;
81                 h->mark = h->buf + ( h->mark - t );
82                 h->cur = h->buf + ( h->cur - t );
83                 h->end = h->buf + ( h->end - t );
84         }
85
86         if (( rc = read( h->fd, h->cur + 1, PA_BUFBLK )) <= 0 ) {
87                 return( (char) 0 );
88         }
89
90         h->end = h->cur + rc;
91         return( *(++(h->cur)) );
92 }