For /dev/mem, map in memory to be copied to memory's own address space
[minix3.git] / lib / other / mtab.c
blob69b91b8230c75ccf71e01a1808c7692072e35b5e
1 /* This package consists of 4 routines for handling the /etc/mtab file.
2 * The /etc/mtab file contains information about the root and mounted file
3 * systems as a series of lines, each one with exactly four fields separated
4 * by one space as follows:
6 * special mounted_on version rw_flag
8 * where
9 * special is the name of the block special file
10 * mounted_on is the directory on which it is mounted
11 * version is either 1 or 2 for MINIX V1 and V2 file systems
12 * rw_flag is rw or ro for read/write or read only
14 * An example /etc/mtab:
16 * /dev/ram / 2 rw
17 * /dev/hd1 /usr 2 rw
18 * /dev/fd0 /user 1 ro
21 * The four routines for handling /etc/mtab are as follows. They use two
22 * (hidden) internal buffers, mtab_in for input and mtab_out for output.
24 * load_mtab(&prog_name) - read /etc/mtab into mtab_in
25 * get_mtab_entry(&s1, &s2, &s3, &s4) - arrays that are filled in
26 * put_mtab_entry(&s1, &s2, &s3, &s4) - append a line to mtab_out
27 * rewrite_mtab(&prog_name) - write mtab_out to /etc/mtab
29 * If load_mtab and rewrite_mtab work, they return 0. If they fail, they
30 * print their own error messages on stderr and return -1. When get_mtab_entry
31 * runs out of entries to return, it sets the first pointer to NULL and returns
32 * -1 instead of 0. Also, rewrite_mtab returns -1 if it fails.
35 #include <sys/types.h>
36 #include <minix/minlib.h>
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <stdio.h>
44 #define BUF_SIZE 512 /* size of the /etc/mtab buffer */
46 char *etc_mtab = "/etc/mtab"; /* name of the /etc/mtab file */
47 static char mtab_in[BUF_SIZE+1]; /* holds /etc/mtab when it is read in */
48 static char mtab_out[BUF_SIZE+1]; /* buf to build /etc/mtab for output later */
49 static char *iptr = mtab_in; /* pointer to next line to feed out. */
50 static char *optr = mtab_out; /* pointer to place where next line goes */
52 _PROTOTYPE(int load_mtab, (char *prog_name ));
53 _PROTOTYPE(int rewrite_mtab, (char *prog_name ));
54 _PROTOTYPE(int get_mtab_entry, (char *special, char *mounted_on,
55 char *version, char *rw_flag));
56 _PROTOTYPE(int put_mtab_entry, (char *special, char *mounted_on,
57 char *version, char *rw_flag));
58 _PROTOTYPE(void err, (char *prog_name, char *str ));
61 int load_mtab(prog_name)
62 char *prog_name;
64 /* Read in /etc/mtab and store it in /etc/mtab. */
66 int fd, n;
67 char *ptr;
69 /* Open the file. */
70 fd = open(etc_mtab, O_RDONLY);
71 if (fd < 0) {
72 err(prog_name, ": cannot open ");
73 return(-1);
76 /* File opened. Read it in. */
77 n = read(fd, mtab_in, BUF_SIZE);
78 if (n <= 0) {
79 /* Read failed. */
80 err(prog_name, ": cannot read ");
81 return(-1);
83 if (n == BUF_SIZE) {
84 /* Some nut has mounted 50 file systems or something like that. */
85 std_err(prog_name);
86 std_err(": file too large: ");
87 std_err(etc_mtab);
88 return(-1);
91 close(fd);
93 /* Replace all the whitespace by '\0'. */
94 ptr = mtab_in;
95 while (*ptr != '\0') {
96 if (isspace(*ptr)) *ptr = '\0';
97 ptr++;
99 return(0);
103 int rewrite_mtab(prog_name)
104 char *prog_name;
106 /* Write mtab_out to /etc/mtab. */
108 int fd, n;
110 /* Do a creat to truncate the file. */
111 fd = creat(etc_mtab, 0777);
112 if (fd < 0) {
113 err(prog_name, ": cannot overwrite ");
114 return(-1);
117 /* File created. Write it. */
118 n = write(fd, mtab_out, (unsigned int)(optr - mtab_out));
119 if (n <= 0) {
120 /* Write failed. */
121 err(prog_name, " could not write ");
122 return(-1);
125 close(fd);
126 return(0);
130 int get_mtab_entry(special, mounted_on, version, rw_flag)
131 char *special;
132 char *mounted_on;
133 char *version;
134 char *rw_flag;
136 /* Return the next entry from mtab_in. */
138 if (iptr >= &mtab_in[BUF_SIZE]) {
139 special[0] = '\0';
140 return(-1);
143 strcpy(special, iptr);
144 while (isprint(*iptr)) iptr++;
145 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
147 strcpy(mounted_on, iptr);
148 while (isprint(*iptr)) iptr++;
149 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
151 strcpy(version, iptr);
152 while (isprint(*iptr)) iptr++;
153 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
155 strcpy(rw_flag, iptr);
156 while (isprint(*iptr)) iptr++;
157 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
158 return(0);
162 int put_mtab_entry(special, mounted_on, version, rw_flag)
163 char *special;
164 char *mounted_on;
165 char *version;
166 char *rw_flag;
168 /* Append an entry to the mtab_out buffer. */
170 int n1, n2, n3, n4;
172 n1 = strlen(special);
173 n2 = strlen(mounted_on);
174 n3 = strlen(version);
175 n4 = strlen(rw_flag);
177 if (optr + n1 + n2 + n3 + n4 + 5 >= &mtab_out[BUF_SIZE]) return(-1);
178 strcpy(optr, special);
179 optr += n1;
180 *optr++ = ' ';
182 strcpy(optr, mounted_on);
183 optr += n2;
184 *optr++ = ' ';
186 strcpy(optr, version);
187 optr += n3;
188 *optr++ = ' ';
190 strcpy(optr, rw_flag);
191 optr += n4;
192 *optr++ = '\n';
193 return(0);
197 void
198 err(prog_name, str)
199 char *prog_name, *str;
201 std_err(prog_name);
202 std_err(str);
203 std_err(etc_mtab);
204 perror(" ");