source: trunk/minix/lib/other/mtab.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 4.9 KB
Line 
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:
5 *
6 * special mounted_on version rw_flag
7 *
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
13 *
14 * An example /etc/mtab:
15 *
16 * /dev/ram / 2 rw
17 * /dev/hd1 /usr 2 rw
18 * /dev/fd0 /user 1 ro
19 *
20 *
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.
23 *
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
28 *
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.
33 */
34
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>
43
44#define BUF_SIZE 512 /* size of the /etc/mtab buffer */
45
46char *etc_mtab = "/etc/mtab"; /* name of the /etc/mtab file */
47static char mtab_in[BUF_SIZE+1]; /* holds /etc/mtab when it is read in */
48static char mtab_out[BUF_SIZE+1]; /* buf to build /etc/mtab for output later */
49static char *iptr = mtab_in; /* pointer to next line to feed out. */
50static char *optr = mtab_out; /* pointer to place where next line goes */
51
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 ));
59
60
61int load_mtab(prog_name)
62char *prog_name;
63{
64/* Read in /etc/mtab and store it in /etc/mtab. */
65
66 int fd, n;
67 char *ptr;
68
69 /* Open the file. */
70 fd = open(etc_mtab, O_RDONLY);
71 if (fd < 0) {
72 err(prog_name, ": cannot open ");
73 return(-1);
74 }
75
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);
82 }
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);
89 }
90
91 close(fd);
92
93 /* Replace all the whitespace by '\0'. */
94 ptr = mtab_in;
95 while (*ptr != '\0') {
96 if (isspace(*ptr)) *ptr = '\0';
97 ptr++;
98 }
99 return(0);
100}
101
102
103int rewrite_mtab(prog_name)
104char *prog_name;
105{
106/* Write mtab_out to /etc/mtab. */
107
108 int fd, n;
109
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);
115 }
116
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);
123 }
124
125 close(fd);
126 return(0);
127}
128
129
130int get_mtab_entry(special, mounted_on, version, rw_flag)
131char *special;
132char *mounted_on;
133char *version;
134char *rw_flag;
135{
136/* Return the next entry from mtab_in. */
137
138 if (iptr >= &mtab_in[BUF_SIZE]) {
139 special[0] = '\0';
140 return(-1);
141 }
142
143 strcpy(special, iptr);
144 while (isprint(*iptr)) iptr++;
145 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
146
147 strcpy(mounted_on, iptr);
148 while (isprint(*iptr)) iptr++;
149 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
150
151 strcpy(version, iptr);
152 while (isprint(*iptr)) iptr++;
153 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
154
155 strcpy(rw_flag, iptr);
156 while (isprint(*iptr)) iptr++;
157 while (*iptr == '\0'&& iptr < &mtab_in[BUF_SIZE]) iptr++;
158 return(0);
159}
160
161
162int put_mtab_entry(special, mounted_on, version, rw_flag)
163char *special;
164char *mounted_on;
165char *version;
166char *rw_flag;
167{
168/* Append an entry to the mtab_out buffer. */
169
170 int n1, n2, n3, n4;
171
172 n1 = strlen(special);
173 n2 = strlen(mounted_on);
174 n3 = strlen(version);
175 n4 = strlen(rw_flag);
176
177 if (optr + n1 + n2 + n3 + n4 + 5 >= &mtab_out[BUF_SIZE]) return(-1);
178 strcpy(optr, special);
179 optr += n1;
180 *optr++ = ' ';
181
182 strcpy(optr, mounted_on);
183 optr += n2;
184 *optr++ = ' ';
185
186 strcpy(optr, version);
187 optr += n3;
188 *optr++ = ' ';
189
190 strcpy(optr, rw_flag);
191 optr += n4;
192 *optr++ = '\n';
193 return(0);
194}
195
196
197void
198err(prog_name, str)
199char *prog_name, *str;
200{
201 std_err(prog_name);
202 std_err(str);
203 std_err(etc_mtab);
204 perror(" ");
205}
Note: See TracBrowser for help on using the repository browser.