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