4 * Simple I/O functions for the parsing of /etc/exports and /etc/nfsclients.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/fcntl.h>
25 xfopen(char *fname, char *type)
30 if (!(fp = fopen(fname, type)))
32 xfp = (XFILE *) xmalloc(sizeof(*xfp));
53 xflock(char *fname, char *type)
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 };
62 fd = open(fname, O_RDONLY);
64 fd = open(fname, (O_RDWR|O_CREAT), mode);
66 xlog(L_WARNING, "could not open %s for locking", fname);
70 sa.sa_handler = doalarm;
72 sigemptyset(&sa.sa_mask);
73 sigaction(SIGALRM, &sa, &oldsa);
75 if (fcntl(fd, F_SETLKW, &fl) < 0) {
77 xlog(L_WARNING, "failed to lock %s", fname);
83 sigaction(SIGALRM, &oldsa, NULL);
94 #define isoctal(x) (isdigit(x) && ((x)<'8'))
96 xgettok(XFILE *xfp, char sepa, char *tok, int len)
102 while (i < len && (c = xgetc(xfp)) != EOF &&
103 (quoted || (c != sepa && !isspace(c)))) {
115 (c = strtol(tok+i-3,NULL, 8)) < 256)) {
124 if (i >= len || (sepa && c != sepa))
133 int c = getc(xfp->x_fp);
138 if ((c = getc(xfp->x_fp)) != '\n') {
139 ungetc(c, xfp->x_fp);
143 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
144 ungetc(c, xfp->x_fp);
153 xungetc(int c, XFILE *xfp)
158 ungetc(c, xfp->x_fp);
164 xskip(XFILE *xfp, char *str)
168 while ((c = xgetc(xfp)) != EOF) {
170 c = xskipcomment(xfp);
171 if (strchr(str, c) == NULL)
178 xskipcomment(XFILE *xfp)
182 while ((c = getc(xfp->x_fp)) != EOF && c != '\n');