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);
87 xgettok(XFILE *xfp, char sepa, char *tok, int len)
92 while (i < len && (c = xgetc(xfp)) != EOF && c != sepa && !isspace(c))
98 if (i >= len || (sepa && c != sepa))
107 int c = getc(xfp->x_fp);
112 if ((c = getc(xfp->x_fp)) != '\n') {
113 ungetc(c, xfp->x_fp);
117 while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
118 ungetc(c, xfp->x_fp);
122 c = xskipcomment(xfp);
129 xungetc(int c, XFILE *xfp)
134 ungetc(c, xfp->x_fp);
140 xskip(XFILE *xfp, char *str)
144 while ((c = xgetc(xfp)) != EOF && strchr(str, c));
145 ungetc(c, xfp->x_fp);
149 xskipcomment(XFILE *xfp)
153 while ((c = getc(xfp->x_fp)) != EOF && c != '\n');