]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/xio.c
nfs-utils: Fix source code character encoding
[nfs-utils.git] / support / nfs / xio.c
1 /*
2  * support/nfs/xio.c
3  * 
4  * Simple I/O functions for the parsing of /etc/exports and /etc/nfsclients.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <sys/fcntl.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <signal.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include "xmalloc.h"
22 #include "xlog.h"
23 #include "xio.h"
24
25 XFILE *
26 xfopen(char *fname, char *type)
27 {
28         XFILE   *xfp;
29         FILE    *fp;
30
31         if (!(fp = fopen(fname, type)))
32                 return NULL;
33         xfp = (XFILE *) xmalloc(sizeof(*xfp));
34         xfp->x_fp = fp;
35         xfp->x_line = 1;
36
37         return xfp;
38 }
39
40 void
41 xfclose(XFILE *xfp)
42 {
43         fclose(xfp->x_fp);
44         xfree(xfp);
45 }
46
47 int
48 xflock(char *fname, char *type)
49 {
50         int             readonly = !strcmp(type, "r");
51         struct flock    fl = { readonly? F_RDLCK : F_WRLCK, SEEK_SET, 0, 0, 0 };
52         int             fd;
53
54         if (readonly)
55                 fd = open(fname, (O_RDONLY|O_CREAT), 0600);
56         else
57                 fd = open(fname, (O_RDWR|O_CREAT), 0600);
58         if (fd < 0) {
59                 xlog(L_WARNING, "could not open %s for locking: errno %d (%s)",
60                                 fname, errno, strerror(errno));
61                 return -1;
62         }
63
64         if (fcntl(fd, F_SETLKW, &fl) < 0) {
65                 xlog(L_WARNING, "failed to lock %s: errno %d (%s)",
66                                 fname, errno, strerror(errno));
67                 close(fd);
68                 fd = -1;
69         }
70
71         return fd;
72 }
73
74 void
75 xfunlock(int fd)
76 {
77         close(fd);
78 }
79
80 #define isoctal(x) (isdigit(x) && ((x)<'8'))
81 int
82 xgettok(XFILE *xfp, char sepa, char *tok, int len)
83 {
84         int     i = 0;
85         int     c = 0;
86         int     quoted=0;
87
88         while (i < len && (c = xgetc(xfp)) != EOF &&
89                (quoted || (c != sepa && !isspace(c)))) {
90                 if (c == '"') {
91                         quoted = !quoted;
92                         continue;
93                 }
94                 tok[i++] = c;
95                 if (i >= 4 &&
96                     tok[i-4] == '\\' &&
97                     isoctal(tok[i-3]) &&
98                     isoctal(tok[i-2]) &&
99                     isoctal(tok[i-1]) &&
100                     ((tok[i]=0),
101                      (c = strtol(tok+i-3,NULL, 8)) < 256)) {
102                         i -= 4;
103                         tok[i++] = c;
104                 }
105         }       
106         if (c == '\n')
107                 xungetc(c, xfp);
108         if (!i)
109                 return 0;
110         if (i >= len || (sepa && c != sepa))
111                 return -1;
112         tok[i] = '\0';
113         return 1;
114 }
115
116 int
117 xgetc(XFILE *xfp)
118 {
119         int     c = getc(xfp->x_fp);
120
121         if (c == EOF)
122                 return c;
123         if (c == '\\') {
124                 if ((c = getc(xfp->x_fp)) != '\n') {
125                         ungetc(c, xfp->x_fp);
126                         return '\\';
127                 }
128                 xfp->x_line++;
129                 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
130                 ungetc(c, xfp->x_fp);
131                 return ' ';
132         }
133         if (c == '\n')
134                 xfp->x_line++;
135         return c;
136 }
137
138 void
139 xungetc(int c, XFILE *xfp)
140 {
141         if (c == EOF)
142                 return;
143
144         ungetc(c, xfp->x_fp);
145         if (c == '\n')
146                 xfp->x_line--;
147 }
148
149 void
150 xskip(XFILE *xfp, char *str)
151 {
152         int     c;
153
154         while ((c = xgetc(xfp)) != EOF) {
155                 if (c == '#')
156                         c = xskipcomment(xfp);
157                 if (strchr(str, c) == NULL)
158                         break;
159         }
160         xungetc(c, xfp);
161 }
162
163 char
164 xskipcomment(XFILE *xfp)
165 {
166         int     c;
167
168         while ((c = getc(xfp->x_fp)) != EOF && c != '\n');
169         return c;
170 }