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>
11 #include <sys/fcntl.h>
23 xfopen(char *fname, char *type)
28 if (!(fp = fopen(fname, type)))
30 xfp = (XFILE *) xmalloc(sizeof(*xfp));
51 xflock(char *fname, char *type)
53 struct sigaction sa, oldsa;
54 int readonly = !strcmp(type, "r");
55 struct flock fl = { readonly? F_RDLCK : F_WRLCK, SEEK_SET, 0, 0, 0 };
58 if ((fd = open(fname, readonly? O_RDONLY : (O_RDWR|O_CREAT))) < 0) {
59 xlog(L_WARNING, "could not open %s for locking", fname);
62 sa.sa_handler = doalarm;
64 sigemptyset(&sa.sa_mask);
65 sigaction(SIGALRM, &sa, &oldsa);
67 if (fcntl(fd, F_SETLKW, &fl) < 0) {
69 xlog(L_WARNING, "failed to lock %s", fname);
75 sigaction(SIGALRM, &oldsa, NULL);
86 #define isoctal(x) (isdigit(x) && ((x)<'8'))
88 xgettok(XFILE *xfp, char sepa, char *tok, int len)
94 while (i < len && (c = xgetc(xfp)) != EOF &&
95 (quoted || (c != sepa && !isspace(c)))) {
107 (c = strtol(tok+i-3,NULL, 8)) < 256)) {
116 if (i >= len || (sepa && c != sepa))
125 int c = getc(xfp->x_fp);
130 if ((c = getc(xfp->x_fp)) != '\n') {
131 ungetc(c, xfp->x_fp);
135 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
136 ungetc(c, xfp->x_fp);
140 c = xskipcomment(xfp);
147 xungetc(int c, XFILE *xfp)
152 ungetc(c, xfp->x_fp);
158 xskip(XFILE *xfp, char *str)
162 while ((c = xgetc(xfp)) != EOF && strchr(str, c));
167 xskipcomment(XFILE *xfp)
171 while ((c = getc(xfp->x_fp)) != EOF && c != '\n');