]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/gettok.c
Merge master
[netatalk.git] / etc / afpd / gettok.c
1 /*
2  * $Id: gettok.c,v 1.6 2009-10-13 22:55:37 didg 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 #include <string.h>
14 #include <ctype.h>
15 #include <pwd.h>
16
17 #include <atalk/globals.h>
18
19 static char     *l_curr;
20 static char     *l_end;
21
22 void initline( int len, char *line)
23 {
24     l_curr = line;
25     l_end = line + len;
26 }
27
28 #define ST_QUOTE        0
29 #define ST_WORD         1
30 #define ST_BEGIN        2
31
32 int
33 parseline(int len, char *token)
34 {
35     char        *p, *e;
36     int         state;
37
38     state = ST_BEGIN;
39     p = token;
40     e = token + len;
41
42     for (;;) {
43         if ( l_curr > l_end ) {                 /* end of line */
44             *token = '\0';
45             return( -1 );
46         }
47
48         switch ( *l_curr ) {
49         case '"' :
50             if ( state == ST_QUOTE ) {
51                 state = ST_WORD;
52             } else {
53                 state = ST_QUOTE;
54             }
55             break;
56
57         case '\0' :
58         case '\t' :
59         case '\n' :
60         case ' ' :
61             if ( state == ST_WORD ) {
62                 *p = '\0';
63                 return( p - token );
64             }
65             if ( state != ST_QUOTE ) {
66                 break;
67             }
68             /* FALL THROUGH */
69
70         default :
71             if ( state == ST_BEGIN ) {
72                 state = ST_WORD;
73             }
74             if ( p > e ) {                      /* end of token */
75                 *token = '\0';
76                 return( -1 );
77             }
78             *p++ = *l_curr;
79             break;
80         }
81
82         l_curr++;
83     }
84 }
85
86 #ifdef notdef
87 void parseline(char *token, char *user)
88 {
89     char                *p = pos, *t = token, *u, *q, buf[ MAXPATHLEN ];
90     struct passwd       *pwent;
91     int                 quoted = 0;
92
93     while ( isspace( *p )) {
94         p++;
95     }
96
97     /*
98      * If we've reached the end of the line, or a comment,
99      * don't return any more tokens.
100      */
101     if ( *p == '\0' || *p == '#' ) {
102         *token = '\0';
103         return;
104     }
105
106     if ( *p == '"' ) {
107         p++;
108         quoted = 1;
109     }
110     while ( *p != '\0' && ( quoted || !isspace( *p ))) {
111         if ( *p == '"' ) {
112             if ( quoted ) {
113                 *t = '\0';
114                 break;
115             }
116             quoted = 1;
117             p++;
118         } else {
119             *t++ = *p++;
120         }
121     }
122     pos = p;
123     *t = '\0';
124
125     /*
126      * We got to the end of the line without closing an open quote
127      */
128     if ( *p == '\0' && quoted ) {
129         *token = '\0';
130         return;
131     }
132
133     t = token;
134     if ( *t == '~' ) {
135         t++;
136         if ( *t == '\0' || *t == '/' ) {
137             u = user;
138             if ( *t == '/' ) {
139                 t++;
140             }
141         } else {
142             u = t;
143             if (( q = strchr( t, '/' )) == NULL ) {
144                 t = "";
145             } else {
146                 *q = '\0';
147                 t = q + 1;
148             }
149         }
150         if ( u == NULL || ( pwent = getpwnam( u )) == NULL ) {
151             *token = '\0';
152             return;
153         }
154         strcpy( buf, pwent->pw_dir );
155         if ( *t != '\0' ) {
156             strcat( buf, "/" );
157             strcat( buf, t );
158         }
159         strcpy( token, buf );
160     }
161     return;
162 }
163 #endif /* notdef */