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), 0644)) < 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);
147 xungetc(int c, XFILE *xfp)
152 ungetc(c, xfp->x_fp);
158 xskip(XFILE *xfp, char *str)
162 while ((c = xgetc(xfp)) != EOF) {
164 c = xskipcomment(xfp);
165 if (strchr(str, c) == NULL)
172 xskipcomment(XFILE *xfp)
176 while ((c = getc(xfp->x_fp)) != EOF && c != '\n');