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>
26 xfopen(char *fname, char *type)
31 if (!(fp = fopen(fname, type)))
33 xfp = (XFILE *) xmalloc(sizeof(*xfp));
54 xflock(char *fname, char *type)
56 struct sigaction sa, oldsa;
57 int readonly = !strcmp(type, "r");
58 struct flock fl = { readonly? F_RDLCK : F_WRLCK, SEEK_SET, 0, 0, 0 };
62 fd = open(fname, (O_RDONLY|O_CREAT), 0600);
64 fd = open(fname, (O_RDWR|O_CREAT), 0600);
66 xlog(L_WARNING, "could not open %s for locking: errno %d (%s)",
67 fname, errno, strerror(errno));
71 sa.sa_handler = doalarm;
73 sigemptyset(&sa.sa_mask);
74 sigaction(SIGALRM, &sa, &oldsa);
76 if (fcntl(fd, F_SETLKW, &fl) < 0) {
78 xlog(L_WARNING, "failed to lock %s: errno %d (%s)",
79 fname, errno, strerror(errno));
85 sigaction(SIGALRM, &oldsa, NULL);
96 #define isoctal(x) (isdigit(x) && ((x)<'8'))
98 xgettok(XFILE *xfp, char sepa, char *tok, int len)
104 while (i < len && (c = xgetc(xfp)) != EOF &&
105 (quoted || (c != sepa && !isspace(c)))) {
117 (c = strtol(tok+i-3,NULL, 8)) < 256)) {
126 if (i >= len || (sepa && c != sepa))
135 int c = getc(xfp->x_fp);
140 if ((c = getc(xfp->x_fp)) != '\n') {
141 ungetc(c, xfp->x_fp);
145 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
146 ungetc(c, xfp->x_fp);
155 xungetc(int c, XFILE *xfp)
160 ungetc(c, xfp->x_fp);
166 xskip(XFILE *xfp, char *str)
170 while ((c = xgetc(xfp)) != EOF) {
172 c = xskipcomment(xfp);
173 if (strchr(str, c) == NULL)
180 xskipcomment(XFILE *xfp)
184 while ((c = getc(xfp->x_fp)) != EOF && c != '\n');