]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rmtab.c
Initial revision
[nfs-utils.git] / support / nfs / rmtab.c
1 /*
2  * support/nfs/rmtab.c
3  *
4  * Handling for rmtab.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include "config.h"
10
11 #include <sys/fcntl.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include "nfslib.h"
19
20 static FILE     *rmfp = NULL;
21
22 int
23 setrmtabent(char *type)
24 {
25         if (rmfp)
26                 fclose(rmfp);
27         rmfp = fsetrmtabent(_PATH_RMTAB, type);
28         return (rmfp != NULL);
29 }
30
31 FILE *
32 fsetrmtabent(char *fname, char *type)
33 {
34         int     readonly = !strcmp(type, "r");
35         FILE    *fp;
36
37         if (!fname)
38                 return NULL;
39         if ((fp = fopen(fname, type)) == NULL) {
40                 xlog(L_ERROR, "can't open %s for %sing", fname,
41                                 readonly ? "read" : "writ");
42                 return NULL;
43         }
44         return fp;
45 }
46
47 struct rmtabent *
48 getrmtabent(int log)
49 {
50         return fgetrmtabent(rmfp, log);
51 }
52
53 struct rmtabent *
54 fgetrmtabent(FILE *fp, int log)
55 {
56         static struct rmtabent  re;
57         char    buf[2048], *sp;
58
59         errno = 0;
60         if (!fp)
61                 return NULL;
62         do {
63                 if (fgets(buf, sizeof(buf)-1, fp) == NULL)
64                         return NULL;
65                 if ((sp = strchr(buf, '\n')) != NULL)
66                         *sp = '\0';
67                 if (!(sp = strchr(buf, ':'))) {
68                         if (log)
69                                 xlog(L_ERROR, "malformed entry in rmtab file");
70                         errno = EINVAL;
71                         return NULL;
72                 }
73                 *sp++ = '\0';
74         } while (0);
75         strncpy(re.r_client, buf, sizeof (re.r_client) - 1);
76         re.r_client[sizeof (re.r_client) - 1] = '\0';
77         strncpy(re.r_path, sp, sizeof (re.r_path) - 1);
78         re.r_path[sizeof (re.r_path) - 1] = '\0';
79         return &re;
80 }
81
82 void
83 putrmtabent(struct rmtabent *rep)
84 {
85         fputrmtabent(rmfp, rep);
86 }
87
88 void
89 fputrmtabent(FILE *fp, struct rmtabent *rep)
90 {
91         if (!fp)
92                 return;
93         fprintf(fp, "%s:%s\n", rep->r_client, rep->r_path);
94 }
95
96 void
97 endrmtabent(void)
98 {
99         fendrmtabent(rmfp);
100         rmfp = NULL;
101 }
102
103 void
104 fendrmtabent(FILE *fp)
105 {
106         if (fp)
107                 fclose(fp);
108 }
109
110 void
111 rewindrmtabent(void)
112 {
113         if (rmfp)
114                 rewind(rmfp);
115 }
116
117 void
118 frewindrmtabent(FILE *fp)
119 {
120         if (fp)
121                 rewind (fp);
122 }