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 struct flock fl = { readonly? F_RDLCK : F_WRLCK, SEEK_SET, 0, 0, 0 };
60 if ((fd = open(fname, readonly? O_RDONLY : (O_RDWR|O_CREAT))) < 0) {
61 xlog(L_WARNING, "could not open %s for locking", fname);
64 sa.sa_handler = doalarm;
66 sigemptyset(&sa.sa_mask);
67 sigaction(SIGALRM, &sa, &oldsa);
69 if (fcntl(fd, F_SETLKW, &fl) < 0) {
71 xlog(L_WARNING, "failed to lock %s", fname);
77 sigaction(SIGALRM, &oldsa, NULL);
88 #define isoctal(x) (isdigit(x) && ((x)<'8'))
90 xgettok(XFILE *xfp, char sepa, char *tok, int len)
96 while (i < len && (c = xgetc(xfp)) != EOF &&
97 (quoted || (c != sepa && !isspace(c)))) {
109 (c = strtol(tok+i-3,NULL, 8)) < 256)) {
118 if (i >= len || (sepa && c != sepa))
127 int c = getc(xfp->x_fp);
132 if ((c = getc(xfp->x_fp)) != '\n') {
133 ungetc(c, xfp->x_fp);
137 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
138 ungetc(c, xfp->x_fp);
142 c = xskipcomment(xfp);
149 xungetc(int c, XFILE *xfp)
154 ungetc(c, xfp->x_fp);
160 xskip(XFILE *xfp, char *str)
164 while ((c = xgetc(xfp)) != EOF && strchr(str, c));
169 xskipcomment(XFILE *xfp)
173 while ((c = getc(xfp->x_fp)) != EOF && c != '\n');