2 * e2initrd_helper.c - Get the filesystem table
4 * Copyright 2004 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
21 #include <sys/types.h>
32 #include "ext2fs/ext2_fs.h"
33 #include "ext2fs/ext2fs.h"
35 #include "blkid/blkid.h"
37 #include "../version.h"
38 #include "nls-enable.h"
40 const char * program_name = "get_fstab";
44 static blkid_cache cache = NULL;
63 static void usage(void)
66 _("Usage: %s -r device\n"), program_name);
70 static errcode_t get_file(ext2_filsys fs, const char * filename,
71 struct mem_file *ret_file)
77 struct ext2_inode inode;
84 retval = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
89 retval = ext2fs_read_inode(fs, ino, &inode);
93 if (inode.i_size_high || (inode.i_size > 65536))
96 buf = malloc(inode.i_size + 1);
99 memset(buf, 0, inode.i_size+1);
101 retval = ext2fs_file_open(fs, ino, 0, &e2_file);
105 retval = ext2fs_file_read(e2_file, buf, inode.i_size, &got);
109 retval = ext2fs_file_close(e2_file);
114 ret_file->size = (int) got;
117 ext2fs_file_close(e2_file);
121 static char *get_line(struct mem_file *file)
126 cp = file->buf + file->ptr;
127 while (*cp && *cp != '\n') {
135 memcpy(ret, file->buf + file->ptr, s);
136 while (*cp && (*cp == '\n' || *cp == '\r')) {
144 static int mem_file_eof(struct mem_file *file)
146 return (file->ptr >= file->size);
152 static char *string_copy(const char *s)
158 ret = malloc(strlen(s)+1);
164 static char *skip_over_blank(char *cp)
166 while (*cp && isspace(*cp))
171 static char *skip_over_word(char *cp)
173 while (*cp && !isspace(*cp))
178 static char *parse_word(char **buf)
186 word = skip_over_blank(word);
187 next = skip_over_word(word);
194 static void parse_escape(char *word)
202 for (p = word, q = word; *p; p++, q++) {
221 for (i = 0; i < 3; i++, p++) {
224 ac = (ac * 8) + (*p - '0');
232 static int parse_fstab_line(char *line, struct fs_info *fs)
234 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
236 if ((cp = strchr(line, '#')))
237 *cp = 0; /* Ignore everything after the comment char */
240 device = parse_word(&cp);
241 mntpnt = parse_word(&cp);
242 type = parse_word(&cp);
243 opts = parse_word(&cp);
244 freq = parse_word(&cp);
245 passno = parse_word(&cp);
248 return -1; /* Allow blank lines */
250 if (!mntpnt || !type)
253 parse_escape(device);
254 parse_escape(mntpnt);
258 parse_escape(passno);
260 dev = blkid_get_devname(cache, device, NULL);
264 if (strchr(type, ','))
267 fs->device = string_copy(device);
268 fs->mountpt = string_copy(mntpnt);
269 fs->type = string_copy(type);
270 fs->opts = string_copy(opts ? opts : "");
271 fs->freq = freq ? atoi(freq) : -1;
272 fs->passno = passno ? atoi(passno) : -1;
282 static void free_fstab_line(struct fs_info *fs)
292 memset(fs, 0, sizeof(struct fs_info));
296 static void PRS(int argc, char **argv)
301 setlocale(LC_MESSAGES, "");
302 setlocale(LC_CTYPE, "");
303 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
304 textdomain(NLS_CAT_NAME);
307 while ((c = getopt(argc, argv, "rv")) != EOF) {
314 printf("%s %s (%s)\n", program_name,
315 E2FSPROGS_VERSION, E2FSPROGS_DATE);
321 if (optind < argc - 1 || optind == argc)
323 device_name = blkid_get_devname(NULL, argv[optind], NULL);
325 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
331 static void get_root_type(ext2_filsys fs)
334 struct mem_file file;
336 struct fs_info fs_info;
339 retval = get_file(fs, "/etc/fstab", &file);
341 while (!mem_file_eof(&file)) {
342 buf = get_line(&file);
346 ret = parse_fstab_line(buf, &fs_info);
350 if (!strcmp(fs_info.mountpt, "/"))
351 printf("%s\n", fs_info.type);
353 free_fstab_line(&fs_info);
361 int main (int argc, char ** argv)
367 add_error_table(&et_ext2_error_table);
369 blkid_get_cache(&cache, NULL);
372 #ifdef CONFIG_TESTIO_DEBUG
373 io_ptr = test_io_manager;
374 test_io_backing_manager = unix_io_manager;
376 io_ptr = unix_io_manager;
378 retval = ext2fs_open (device_name, open_flag, 0, 0, io_ptr, &fs);
385 remove_error_table(&et_ext2_error_table);
386 return (ext2fs_close (fs) ? 1 : 0);