Whamcloud - gitweb
po: update sr.po (from translationproject.org)
[tools/e2fsprogs.git] / misc / e2initrd_helper.c
index 243aab4..436aab8 100644 (file)
@@ -9,9 +9,12 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <unistd.h>
+#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
+#endif
 #include <ctype.h>
 #include <string.h>
 #include <time.h>
 #include <utime.h>
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
-#else 
+#else
 extern int optind;
 extern char *optarg;
 #endif
 
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
-#include "e2p/e2p.h"
 #include "blkid/blkid.h"
+#include "support/nls-enable.h"
 
 #include "../version.h"
-#include "nls-enable.h"
 
-const char * program_name = "get_fstab";
-char * device_name;
+static const char * program_name = "e2initrd_helper";
+static char * device_name;
 static int open_flag;
 static int root_type;
 static blkid_cache cache = NULL;
@@ -67,13 +69,12 @@ static void usage(void)
        exit (1);
 }
 
-errcode_t get_file(ext2_filsys fs, const char * filename, 
+static errcode_t get_file(ext2_filsys fs, const char * filename,
                   struct mem_file *ret_file)
 {
        errcode_t       retval;
        char            *buf;
-       ext2_file_t     e2_file;
-       int             nbytes;
+       ext2_file_t     e2_file = NULL;
        unsigned int    got;
        struct ext2_inode inode;
        ext2_ino_t      ino;
@@ -82,7 +83,7 @@ errcode_t get_file(ext2_filsys fs, const char * filename,
        ret_file->size = 0;
        ret_file->ptr = 0;
 
-       retval = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 
+       retval = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
                              filename, &ino);
        if (retval)
                return retval;
@@ -101,25 +102,28 @@ errcode_t get_file(ext2_filsys fs, const char * filename,
 
        retval = ext2fs_file_open(fs, ino, 0, &e2_file);
        if (retval)
-               return retval;
+               goto errout;
 
        retval = ext2fs_file_read(e2_file, buf, inode.i_size, &got);
-       if (retval) 
+       if (retval)
                goto errout;
 
        retval = ext2fs_file_close(e2_file);
        if (retval)
-               return retval;
+               goto errout;
 
        ret_file->buf = buf;
        ret_file->size = (int) got;
+       return 0;
 
 errout:
-       ext2fs_file_close(e2_file);
+       free(buf);
+       if (e2_file)
+               ext2fs_file_close(e2_file);
        return retval;
 }
 
-char *get_line(struct mem_file *file)
+static char *get_line(struct mem_file *file)
 {
        char    *cp, *ret;
        int     s = 0;
@@ -142,7 +146,7 @@ char *get_line(struct mem_file *file)
        return ret;
 }
 
-int mem_file_eof(struct mem_file *file)
+static int mem_file_eof(struct mem_file *file)
 {
        return (file->ptr >= file->size);
 }
@@ -247,7 +251,7 @@ static int parse_fstab_line(char *line, struct fs_info *fs)
 
        if (!device)
                return -1;      /* Allow blank lines */
-       
+
        if (!mntpnt || !type)
                return -1;
 
@@ -274,13 +278,12 @@ static int parse_fstab_line(char *line, struct fs_info *fs)
        fs->flags = 0;
        fs->next = NULL;
 
-       if (dev)
-               free(dev);
-          
+       free(dev);
+
        return 0;
 }
 
-void free_fstab_line(struct fs_info *fs)
+static void free_fstab_line(struct fs_info *fs)
 {
        if (fs->device)
                fs->device = 0;
@@ -297,15 +300,13 @@ void free_fstab_line(struct fs_info *fs)
 static void PRS(int argc, char **argv)
 {
        int c;
-       char * tmp;
-       struct group * gr;
-       struct passwd * pw;
 
 #ifdef ENABLE_NLS
        setlocale(LC_MESSAGES, "");
        setlocale(LC_CTYPE, "");
        bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
        textdomain(NLS_CAT_NAME);
+       set_com_err_gettext(gettext);
 #endif
 
        while ((c = getopt(argc, argv, "rv")) != EOF) {
@@ -315,7 +316,7 @@ static void PRS(int argc, char **argv)
                        break;
 
                case 'v':
-                       printf("%s %s (%s)\n", program_name, 
+                       printf("%s %s (%s)\n", program_name,
                               E2FSPROGS_VERSION, E2FSPROGS_DATE);
                        break;
                default:
@@ -326,13 +327,13 @@ static void PRS(int argc, char **argv)
                usage();
        device_name = blkid_get_devname(NULL, argv[optind], NULL);
        if (!device_name) {
-               com_err("tune2fs", 0, _("Unable to resolve '%s'"), 
+               com_err(program_name, 0, _("Unable to resolve '%s'"),
                        argv[optind]);
                exit(1);
        }
 }
 
-void get_root_type(ext2_filsys fs)
+static void get_root_type(ext2_filsys fs)
 {
        errcode_t retval;
        struct mem_file file;
@@ -341,14 +342,18 @@ void get_root_type(ext2_filsys fs)
        int             ret;
 
        retval = get_file(fs, "/etc/fstab", &file);
+       if (retval) {
+               com_err(program_name, retval, "couldn't open /etc/fstab");
+               exit(1);
+       }
 
        while (!mem_file_eof(&file)) {
                buf = get_line(&file);
                if (!buf)
                        continue;
-               
+
                ret = parse_fstab_line(buf, &fs_info);
-               if (ret < 0) 
+               if (ret < 0)
                        goto next_line;
 
                if (!strcmp(fs_info.mountpt, "/"))
@@ -366,27 +371,27 @@ int main (int argc, char ** argv)
 {
        errcode_t retval;
        ext2_filsys fs;
-       struct ext2_super_block *sb;
        io_manager io_ptr;
-       int             got, ret;
 
-       initialize_ext2_error_table();
+       add_error_table(&et_ext2_error_table);
 
        blkid_get_cache(&cache, NULL);
        PRS(argc, argv);
-       
+
 #ifdef CONFIG_TESTIO_DEBUG
-       io_ptr = test_io_manager;
-       test_io_backing_manager = unix_io_manager;
-#else
-       io_ptr = unix_io_manager;
+       if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
+               io_ptr = test_io_manager;
+               test_io_backing_manager = unix_io_manager;
+       } else
 #endif
+               io_ptr = unix_io_manager;
        retval = ext2fs_open (device_name, open_flag, 0, 0, io_ptr, &fs);
         if (retval)
                exit(1);
 
-       if (root_type) 
+       if (root_type)
                get_root_type(fs);
 
+       remove_error_table(&et_ext2_error_table);
        return (ext2fs_close (fs) ? 1 : 0);
 }