summaryrefslogtreecommitdiffstats
path: root/support/nfs/rmtab.c
blob: b9b5ff1c6ae55f1f6e083309f0706acc127757a2 (plain)
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
/*
 * support/nfs/rmtab.c
 *
 * Handling for rmtab.
 *
 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
 */

#include "config.h"

#include <sys/fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "nfslib.h"

static FILE	*rmfp = NULL;

int
setrmtabent(char *type)
{
	if (rmfp)
		fclose(rmfp);
	rmfp = fsetrmtabent(_PATH_RMTAB, type);
	return (rmfp != NULL);
}

FILE *
fsetrmtabent(char *fname, char *type)
{
	int	readonly = !strcmp(type, "r");
	FILE	*fp;

	if (!fname)
		return NULL;
	if ((fp = fopen(fname, type)) == NULL) {
		xlog(L_ERROR, "can't open %s for %sing", fname,
				readonly ? "read" : "writ");
		return NULL;
	}
	return fp;
}

struct rmtabent *
getrmtabent(int log)
{
	return fgetrmtabent(rmfp, log);
}

struct rmtabent *
fgetrmtabent(FILE *fp, int log)
{
	static struct rmtabent	re;
	char	buf[2048], *sp;

	errno = 0;
	if (!fp)
		return NULL;
	do {
		if (fgets(buf, sizeof(buf)-1, fp) == NULL)
			return NULL;
		if ((sp = strchr(buf, '\n')) != NULL)
			*sp = '\0';
		if (!(sp = strchr(buf, ':'))) {
			if (log)
				xlog(L_ERROR, "malformed entry in rmtab file");
			errno = EINVAL;
			return NULL;
		}
		*sp++ = '\0';
	} while (0);
	strncpy(re.r_client, buf, sizeof (re.r_client) - 1);
	re.r_client[sizeof (re.r_client) - 1] = '\0';
	strncpy(re.r_path, sp, sizeof (re.r_path) - 1);
	re.r_path[sizeof (re.r_path) - 1] = '\0';
	return &re;
}

void
putrmtabent(struct rmtabent *rep)
{
	fputrmtabent(rmfp, rep);
}

void
fputrmtabent(FILE *fp, struct rmtabent *rep)
{
	if (!fp)
		return;
	fprintf(fp, "%s:%s\n", rep->r_client, rep->r_path);
}

void
endrmtabent(void)
{
	fendrmtabent(rmfp);
	rmfp = NULL;
}

void
fendrmtabent(FILE *fp)
{
	if (fp)
		fclose(fp);
}

void
rewindrmtabent(void)
{
	if (rmfp)
		rewind(rmfp);
}

void
frewindrmtabent(FILE *fp)
{
	if (fp)
		rewind (fp);
}