From: Theodore Ts'o Date: Sat, 19 Mar 2005 06:13:22 +0000 (-0500) Subject: Add new functions which convert between a string and os_type: e2p_os2string() X-Git-Tag: E2FSPROGS-1_37~13 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=63253946309651c1015947d522e2ba4b35a807a5;p=thirdparty%2Fe2fsprogs.git Add new functions which convert between a string and os_type: e2p_os2string() and e2p_string2os() in the e2p library. --- diff --git a/lib/e2p/ChangeLog b/lib/e2p/ChangeLog index 243c86af1..a64cd1040 100644 --- a/lib/e2p/ChangeLog +++ b/lib/e2p/ChangeLog @@ -1,3 +1,10 @@ +2005-03-19 Theodore Ts'o + + * 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 * Release of E2fsprogs 1.36 diff --git a/lib/e2p/Makefile.in b/lib/e2p/Makefile.in index c91cd88ea..f77e76966 100644 --- a/lib/e2p/Makefile.in +++ b/lib/e2p/Makefile.in @@ -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 diff --git a/lib/e2p/e2p.h b/lib/e2p/e2p.h index 048014f17..d208b46a8 100644 --- a/lib/e2p/e2p.h +++ b/lib/e2p/e2p.h @@ -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); diff --git a/lib/e2p/ls.c b/lib/e2p/ls.c index 0bd741182..e8d9d482d 100644 --- a/lib/e2p/ls.c +++ b/lib/e2p/ls.c @@ -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 index 000000000..fe6597dd5 --- /dev/null +++ b/lib/e2p/ostype.c @@ -0,0 +1,73 @@ +/* + * getostype.c - Get the Filesystem OS type + * + * Copyright (C) 2004,2005 Theodore Ts'o + * + * This file can be redistributed under the terms of the GNU Library General + * Public License + */ + +#include "e2p.h" +#include + +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 + + diff --git a/misc/ChangeLog b/misc/ChangeLog index e1a6da8ff..1f389b609 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,3 +1,7 @@ +2005-03-19 Theodore Ts'o + + * mke2fs.c (show_stats): Use the new e2p_os2string() function + 2005-03-18 Theodore Ts'o * filefrag.c (frag_report): Automatically detect files that are diff --git a/misc/mke2fs.c b/misc/mke2fs.c index 456a471f9..7211dc55b 100644 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@ -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);