]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/xio.c
nfs-utils: remove unused function rpc_logcall()
[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 "xmalloc.h"
21 #include "xlog.h"
22 #include "xio.h"
23
24 XFILE *
25 xfopen(char *fname, char *type)
26 {
27         XFILE   *xfp;
28         FILE    *fp;
29
30         if (!(fp = fopen(fname, type)))
31                 return NULL;
32         xfp = (XFILE *) xmalloc(sizeof(*xfp));
33         xfp->x_fp = fp;
34         xfp->x_line = 1;
35
36         return xfp;
37 }
38
39 void
40 xfclose(XFILE *xfp)
41 {
42         fclose(xfp->x_fp);
43         xfree(xfp);
44 }
45
46 static void
47 doalarm(int sig)
48 {
49         return;
50 }
51
52 int
53 xflock(char *fname, char *type)
54 {
55         struct sigaction sa, oldsa;
56         int             readonly = !strcmp(type, "r");
57         mode_t  mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
58         struct flock    fl = { readonly? F_RDLCK : F_WRLCK, SEEK_SET, 0, 0, 0 };
59         int             fd;
60
61         if (readonly)
62                 fd = open(fname, O_RDONLY);
63         else
64                 fd = open(fname, (O_RDWR|O_CREAT), mode);
65         if (fd < 0) {
66                 xlog(L_WARNING, "could not open %s for locking", fname);
67                 return -1;
68         }
69
70         sa.sa_handler = doalarm;
71         sa.sa_flags = 0;
72         sigemptyset(&sa.sa_mask);
73         sigaction(SIGALRM, &sa, &oldsa);
74         alarm(10);
75         if (fcntl(fd, F_SETLKW, &fl) < 0) {
76                 alarm(0);
77                 xlog(L_WARNING, "failed to lock %s", fname);
78                 close(fd);
79                 fd = 0;
80         } else {
81                 alarm(0);
82         }
83         sigaction(SIGALRM, &oldsa, NULL);
84
85         return fd;
86 }
87
88 void
89 xfunlock(int fd)
90 {
91         close(fd);
92 }
93
94 #define isoctal(x) (isdigit(x) && ((x)<'8'))
95 int
96 xgettok(XFILE *xfp, char sepa, char *tok, int len)
97 {
98         int     i = 0;
99         int     c = 0;
100         int     quoted=0;
101
102         while (i < len && (c = xgetc(xfp)) != EOF &&
103                (quoted || (c != sepa && !isspace(c)))) {
104                 if (c == '"') {
105                         quoted = !quoted;
106                         continue;
107                 }
108                 tok[i++] = c;
109                 if (i >= 4 &&
110                     tok[i-4] == '\\' &&
111                     isoctal(tok[i-3]) &&
112                     isoctal(tok[i-2]) &&
113                     isoctal(tok[i-1]) &&
114                     ((tok[i]=0),
115                      (c = strtol(tok+i-3,NULL, 8)) < 256)) {
116                         i -= 4;
117                         tok[i++] = c;
118                 }
119         }       
120         if (c == '\n')
121                 xungetc(c, xfp);
122         if (!i)
123                 return 0;
124         if (i >= len || (sepa && c != sepa))
125                 return -1;
126         tok[i] = '\0';
127         return 1;
128 }
129
130 int
131 xgetc(XFILE *xfp)
132 {
133         int     c = getc(xfp->x_fp);
134
135         if (c == EOF)
136                 return c;
137         if (c == '\\') {
138                 if ((c = getc(xfp->x_fp)) != '\n') {
139                         ungetc(c, xfp->x_fp);
140                         return '\\';
141                 }
142                 xfp->x_line++;
143                 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
144                 ungetc(c, xfp->x_fp);
145                 return ' ';
146         }
147         if (c == '\n')
148                 xfp->x_line++;
149         return c;
150 }
151
152 void
153 xungetc(int c, XFILE *xfp)
154 {
155         if (c == EOF)
156                 return;
157
158         ungetc(c, xfp->x_fp);
159         if (c == '\n')
160                 xfp->x_line--;
161 }
162
163 void
164 xskip(XFILE *xfp, char *str)
165 {
166         int     c;
167
168         while ((c = xgetc(xfp)) != EOF) {
169                 if (c == '#')
170                         c = xskipcomment(xfp);
171                 if (strchr(str, c) == NULL)
172                         break;
173         }
174         xungetc(c, xfp);
175 }
176
177 char
178 xskipcomment(XFILE *xfp)
179 {
180         int     c;
181
182         while ((c = getc(xfp->x_fp)) != EOF && c != '\n');
183         return c;
184 }