1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
* support/nfs/xio.c
*
* Simple I/O functions for the parsing of /etc/exports and /etc/nfsclients.
*
* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include "xmalloc.h"
#include "xlog.h"
#include "xio.h"
XFILE *
xfopen(char *fname, char *type)
{
XFILE *xfp;
FILE *fp;
if (!(fp = fopen(fname, type)))
return NULL;
xfp = (XFILE *) xmalloc(sizeof(*xfp));
xfp->x_fp = fp;
xfp->x_line = 1;
return xfp;
}
void
xfclose(XFILE *xfp)
{
fclose(xfp->x_fp);
xfree(xfp);
}
int
xflock(char *fname, char *type)
{
int readonly = !strcmp(type, "r");
struct flock fl = { readonly? F_RDLCK : F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
if (readonly)
fd = open(fname, (O_RDONLY|O_CREAT), 0600);
else
fd = open(fname, (O_RDWR|O_CREAT), 0600);
if (fd < 0) {
xlog(L_WARNING, "could not open %s for locking: errno %d (%s)",
fname, errno, strerror(errno));
return -1;
}
if (fcntl(fd, F_SETLKW, &fl) < 0) {
xlog(L_WARNING, "failed to lock %s: errno %d (%s)",
fname, errno, strerror(errno));
close(fd);
fd = -1;
}
return fd;
}
void
xfunlock(int fd)
{
close(fd);
}
#define isoctal(x) (isdigit(x) && ((x)<'8'))
int
xgettok(XFILE *xfp, char sepa, char *tok, int len)
{
int i = 0;
int c = 0;
int quoted=0;
while (i < len && (c = xgetc(xfp)) != EOF &&
(quoted || (c != sepa && !isspace(c)))) {
if (c == '"') {
quoted = !quoted;
continue;
}
tok[i++] = c;
if (i >= 4 &&
tok[i-4] == '\\' &&
isoctal(tok[i-3]) &&
isoctal(tok[i-2]) &&
isoctal(tok[i-1]) &&
((tok[i]=0),
(c = strtol(tok+i-3,NULL, 8)) < 256)) {
i -= 4;
tok[i++] = c;
}
}
if (c == '\n')
xungetc(c, xfp);
if (!i)
return 0;
if (i >= len || (sepa && c != sepa))
return -1;
tok[i] = '\0';
return 1;
}
int
xgetc(XFILE *xfp)
{
int c = getc(xfp->x_fp);
if (c == EOF)
return c;
if (c == '\\') {
if ((c = getc(xfp->x_fp)) != '\n') {
ungetc(c, xfp->x_fp);
return '\\';
}
xfp->x_line++;
while ((c = getc(xfp->x_fp)) == ' ' || c == '\t');
ungetc(c, xfp->x_fp);
return ' ';
}
if (c == '\n')
xfp->x_line++;
return c;
}
void
xungetc(int c, XFILE *xfp)
{
if (c == EOF)
return;
ungetc(c, xfp->x_fp);
if (c == '\n')
xfp->x_line--;
}
void
xskip(XFILE *xfp, char *str)
{
int c;
while ((c = xgetc(xfp)) != EOF) {
if (c == '#')
c = xskipcomment(xfp);
if (strchr(str, c) == NULL)
break;
}
xungetc(c, xfp);
}
char
xskipcomment(XFILE *xfp)
{
int c;
while ((c = getc(xfp->x_fp)) != EOF && c != '\n');
return c;
}
|