Whamcloud - gitweb
Add new functions which convert between a string and os_type: e2p_os2string()
authorTheodore Ts'o <tytso@mit.edu>
Sat, 19 Mar 2005 06:13:22 +0000 (01:13 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 19 Mar 2005 06:13:22 +0000 (01:13 -0500)
and e2p_string2os() in the e2p library.

lib/e2p/ChangeLog
lib/e2p/Makefile.in
lib/e2p/e2p.h
lib/e2p/ls.c
lib/e2p/ostype.c [new file with mode: 0644]
misc/ChangeLog
misc/mke2fs.c

index 243c86a..a64cd10 100644 (file)
@@ -1,3 +1,10 @@
+2005-03-19  Theodore Ts'o  <tytso@mit.edu>
+
+       * ls.c (list_super2): Use the new e2p_os2string() function
+
+       * ostype.c: New file which converts between an integer os_type and
+               a string.
+
 2006-02-05  Theodore Ts'o  <tytso@mit.edu>
 
        * Release of E2fsprogs 1.36
index c91cd88..f77e769 100644 (file)
@@ -18,7 +18,8 @@ all:: e2p.pc
 
 OBJS=          feature.o fgetflags.o fsetflags.o fgetversion.o fsetversion.o \
                getflags.o getversion.o hashstr.o iod.o ls.o mntopts.o \
-               parse_num.o pe.o pf.o ps.o setflags.o setversion.o uuid.o 
+               parse_num.o pe.o pf.o ps.o setflags.o setversion.o uuid.o \
+               ostype.o 
 
 SRCS=          $(srcdir)/feature.c $(srcdir)/fgetflags.c \
                $(srcdir)/fsetflags.c $(srcdir)/fgetversion.c \
@@ -26,8 +27,8 @@ SRCS=         $(srcdir)/feature.c $(srcdir)/fgetflags.c \
                $(srcdir)/getversion.c $(srcdir)/hashstr.c $(srcdir)/iod.c \
                $(srcdir)/ls.c $(srcdir)/mntopts.c $(srcdir)/parse_num.c \
                $(srcdir)/pe.c $(srcdir)/pf.c $(srcdir)/ps.c \
-               $(srcdir)/setflags.c $(srcdir)/setversion.c $(srcdir)/uuid.c
-
+               $(srcdir)/setflags.c $(srcdir)/setversion.c $(srcdir)/uuid.c \
+               $(srcdir)/ostype.c
 HFILES= e2p.h
 
 LIBRARY= libe2p
@@ -63,6 +64,13 @@ e2p.pc: $(srcdir)/e2p.pc.in $(top_builddir)/config.status
        @echo " CONFIG.STATUS $@"
        @cd $(top_builddir); CONFIG_FILES=lib/e2p/e2p.pc ./config.status
 
+tst_ostype: $(srcdir)/ostype.c
+       @echo " LD $@"
+       @$(CC) -DTEST_PROGRAM -o tst_ostype $(srcdir)/ostype.c
+
+check::        tst_ostype
+       ./tst_ostype
+
 installdirs::
        @echo " MKINSTALLDIRS $(libdir) $(includedir)/e2p"
        @$(MKINSTALLDIRS) $(DESTDIR)$(libdir) \
@@ -87,7 +95,8 @@ uninstall::
 
 clean::
        $(RM) -f \#* *.s *.o *.a *~ *.bak core profiled/* checker/*
-       $(RM) -f ../libe2p.a ../libe2p_p.a
+       $(RM) -f ../libe2p.a ../libe2p_p.a tst_ostype
+
 mostlyclean:: clean
 distclean:: clean
        $(RM) -f .depend Makefile e2p.pc \
@@ -132,3 +141,4 @@ setversion.o: $(srcdir)/setversion.c $(srcdir)/e2p.h \
  $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h
 uuid.o: $(srcdir)/uuid.c $(top_builddir)/lib/ext2fs/ext2_types.h \
  $(srcdir)/e2p.h $(top_srcdir)/lib/ext2fs/ext2_fs.h
+ostype.o: $(srcdir)/ostype.c $(srcdir)/e2p.h
index 048014f..d208b46 100644 (file)
@@ -47,3 +47,6 @@ int e2p_string2mntopt(char *string, unsigned int *mask);
 int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok);
 
 unsigned long parse_num_blocks(const char *arg, int log_block_size);
+
+char *e2p_os2string(int os_type);
+int e2p_string2os(char *str);
index 0bd7411..e8d9d48 100644 (file)
@@ -149,8 +149,7 @@ static void print_mntopts(struct ext2_super_block * s, FILE *f)
 void list_super2(struct ext2_super_block * sb, FILE *f)
 {
        int inode_blocks_per_group;
-       char buf[80];
-       const char *os;
+       char buf[80], *str;
        time_t  tm;
 
        inode_blocks_per_group = (((sb->s_inodes_per_group *
@@ -188,15 +187,9 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
        fprintf(f, "Errors behavior:          ");
        print_fs_errors(f, sb->s_errors);
        fprintf(f, "\n");
-       switch (sb->s_creator_os) {
-           case EXT2_OS_LINUX: os = "Linux"; break;
-           case EXT2_OS_HURD:  os = "GNU/Hurd"; break;
-           case EXT2_OS_MASIX: os = "Masix"; break;
-           case EXT2_OS_FREEBSD: os = "FreeBSD"; break;
-           case EXT2_OS_LITES: os = "Lites"; break;
-           default:            os = "unknown"; break;
-       }
-       fprintf(f, "Filesystem OS type:       %s\n", os);
+       str = e2p_os2string(sb->s_creator_os);
+       fprintf(f, "Filesystem OS type:       %s\n", str);
+       free(str);
        fprintf(f, "Inode count:              %u\n", sb->s_inodes_count);
        fprintf(f, "Block count:              %u\n", sb->s_blocks_count);
        fprintf(f, "Reserved block count:     %u\n", sb->s_r_blocks_count);
diff --git a/lib/e2p/ostype.c b/lib/e2p/ostype.c
new file mode 100644 (file)
index 0000000..fe6597d
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * getostype.c          - Get the Filesystem OS type
+ *
+ * Copyright (C) 2004,2005  Theodore Ts'o <tytso@mit.edu>
+ *
+ * This file can be redistributed under the terms of the GNU Library General
+ * Public License
+ */
+
+#include "e2p.h"
+#include <string.h>
+
+const char *os_tab[] =
+       { "Linux", 
+         "Hurd", 
+         "Masix", 
+         "FreeBSD", 
+         "Lites",
+         0 };
+
+/*
+ * Convert an os_type to a string
+ */
+char *e2p_os2string(int os_type)
+{
+        const char     *os;
+       char            *ret;
+
+       if (os_type <= EXT2_OS_LITES)
+               os = os_tab[os_type];
+       else
+               os = "(unknown os)";
+
+        ret = malloc(strlen(os)+1);
+        strcpy(ret, os);
+        return ret;
+}
+
+/*
+ * Convert an os_type to a string
+ */
+int e2p_string2os(char *str)
+{
+       const char      **cpp;
+       int             i = 0;
+
+       for (cpp = os_tab; *cpp; cpp++, i++) {
+               if (!strcasecmp(str, *cpp))
+                       return i;
+       }
+       return -1;
+}
+
+#ifdef TEST_PROGRAM
+int main(int argc, char **argv)
+{
+       char    *s;
+       int     i, os;
+
+       for (i=0; i <= EXT2_OS_LITES; i++) {
+               s = e2p_os2string(i);
+               os = e2p_string2os(s);
+               printf("%d: %s (%d)\n", i, s, os);
+               if (i != os) {
+                       fprintf(stderr, "Failure!\n");
+                       exit(1);
+               }
+       }
+       exit(0);
+}
+#endif
+
+
index e1a6da8..1f389b6 100644 (file)
@@ -1,3 +1,7 @@
+2005-03-19  Theodore Ts'o  <tytso@mit.edu>
+
+       * mke2fs.c (show_stats): Use the new e2p_os2string() function
+
 2005-03-18  Theodore Ts'o  <tytso@mit.edu>
 
        * filefrag.c (frag_report): Automatically detect files that are
index 456a471..7211dc5 100644 (file)
@@ -686,6 +686,7 @@ static void show_stats(ext2_filsys fs)
 {
        struct ext2_super_block *s = fs->super;
        char                    buf[80];
+        char                    *os;
        blk_t                   group_block;
        dgrp_t                  i;
        int                     need, col_left;
@@ -698,14 +699,9 @@ static void show_stats(ext2_filsys fs)
        strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
        printf(_("Filesystem label=%s\n"), buf);
        fputs(_("OS type: "), stdout);
-       switch (fs->super->s_creator_os) {
-           case EXT2_OS_LINUX: fputs("Linux", stdout); break;
-           case EXT2_OS_HURD:  fputs("GNU/Hurd", stdout);   break;
-           case EXT2_OS_MASIX: fputs ("Masix", stdout); break;
-           case EXT2_OS_FREEBSD: fputs ("FreeBSD", stdout); break;
-           case EXT2_OS_LITES: fputs ("Lites", stdout); break;
-           default:            fputs(_("(unknown os)"), stdout);
-        }
+        os = e2p_os2string(fs->super->s_creator_os);
+       fputs(os, stdout);
+       free(os);
        printf("\n");
        printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
                s->s_log_block_size);