]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/gettok.c
Make dot files visible by default with usedots, add invisibledots for the old behavio...
[netatalk.git] / etc / afpd / gettok.c
1 /*
2  * $Id: gettok.c,v 1.5 2001-12-03 05:03:38 jmarcus Exp $
3  *
4  * Copyright (c) 1990,1994 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <sys/param.h>
13
14 /* STDC check */
15 #if STDC_HEADERS
16 #include <string.h>
17 #else /* STDC_HEADERS */
18 #ifndef HAVE_STRCHR
19 #define strchr index
20 #define strrchr index
21 #endif /* HAVE_STRCHR */
22 char *strchr (), *strrchr ();
23 #ifndef HAVE_MEMCPY
24 #define memcpy(d,s,n) bcopy ((s), (d), (n))
25 #define memmove(d,s,n) bcopy ((s), (d), (n))
26 #endif /* ! HAVE_MEMCPY */
27 #endif /* STDC_HEADERS */
28
29 #include <ctype.h>
30 #include <pwd.h>
31
32 #include "globals.h"
33
34 static char     *l_curr;
35 static char     *l_end;
36
37 void initline( len, line )
38 int             len;
39 char    *line;
40 {
41     l_curr = line;
42     l_end = line + len;
43 }
44
45 #define ST_QUOTE        0
46 #define ST_WORD         1
47 #define ST_BEGIN        2
48
49 int
50 parseline( len, token )
51 int             len;
52 char    *token;
53 {
54     char        *p, *e;
55     int         state;
56
57     state = ST_BEGIN;
58     p = token;
59     e = token + len;
60
61     for (;;) {
62         if ( l_curr > l_end ) {                 /* end of line */
63             *token = '\0';
64             return( -1 );
65         }
66
67         switch ( *l_curr ) {
68         case '"' :
69             if ( state == ST_QUOTE ) {
70                 state = ST_WORD;
71             } else {
72                 state = ST_QUOTE;
73             }
74             break;
75
76         case '\0' :
77         case '\t' :
78         case '\n' :
79         case ' ' :
80             if ( state == ST_WORD ) {
81                 *p = '\0';
82                 return( p - token );
83             }
84             if ( state != ST_QUOTE ) {
85                 break;
86             }
87             /* FALL THROUGH */
88
89         default :
90             if ( state == ST_BEGIN ) {
91                 state = ST_WORD;
92             }
93             if ( p > e ) {                      /* end of token */
94                 *token = '\0';
95                 return( -1 );
96             }
97             *p++ = *l_curr;
98             break;
99         }
100
101         l_curr++;
102     }
103 }
104
105 #ifdef notdef
106 void parseline( token, user )
107 char    *token, *user;
108 {
109     char                *p = pos, *t = token, *u, *q, buf[ MAXPATHLEN ];
110     struct passwd       *pwent;
111     int                 quoted = 0;
112
113     while ( isspace( *p )) {
114         p++;
115     }
116
117     /*
118      * If we've reached the end of the line, or a comment,
119      * don't return any more tokens.
120      */
121     if ( *p == '\0' || *p == '#' ) {
122         *token = '\0';
123         return;
124     }
125
126     if ( *p == '"' ) {
127         p++;
128         quoted = 1;
129     }
130     while ( *p != '\0' && ( quoted || !isspace( *p ))) {
131         if ( *p == '"' ) {
132             if ( quoted ) {
133                 *t = '\0';
134                 break;
135             }
136             quoted = 1;
137             p++;
138         } else {
139             *t++ = *p++;
140         }
141     }
142     pos = p;
143     *t = '\0';
144
145     /*
146      * We got to the end of the line without closing an open quote
147      */
148     if ( *p == '\0' && quoted ) {
149         *token = '\0';
150         return;
151     }
152
153     t = token;
154     if ( *t == '~' ) {
155         t++;
156         if ( *t == '\0' || *t == '/' ) {
157             u = user;
158             if ( *t == '/' ) {
159                 t++;
160             }
161         } else {
162             u = t;
163             if (( q = strchr( t, '/' )) == NULL ) {
164                 t = "";
165             } else {
166                 *q = '\0';
167                 t = q + 1;
168             }
169         }
170         if ( u == NULL || ( pwent = getpwnam( u )) == NULL ) {
171             *token = '\0';
172             return;
173         }
174         strcpy( buf, pwent->pw_dir );
175         if ( *t != '\0' ) {
176             strcat( buf, "/" );
177             strcat( buf, t );
178         }
179         strcpy( token, buf );
180     }
181     return;
182 }
183 #endif /* notdef */