From 4de267e394674ba5058646771f6fd1846275c658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dejean?= Date: Sat, 12 Jun 2004 12:48:37 +0000 Subject: Removed, replaced by ISO strtoull. (fstype_to_string): Cleaned. * mountlist.c: (xatoi) Removed, replaced by ISO strtoull. (fstype_to_string): Cleaned. (read_filesystem_list) : Changed prototype. Cleaned and secured. Replaced broken free by g_free. (ignore_mount_entry): Added. (glibtop_get_mountlist_s): Cleaned. Better allocation algorithm. @all_fs has now a real meaning, see the documentation. --- sysdeps/common/ChangeLog | 10 +++ sysdeps/common/mountlist.c | 188 +++++++++++++++++++++------------------------ 2 files changed, 96 insertions(+), 102 deletions(-) diff --git a/sysdeps/common/ChangeLog b/sysdeps/common/ChangeLog index 5a04f4ad..85412352 100644 --- a/sysdeps/common/ChangeLog +++ b/sysdeps/common/ChangeLog @@ -1,3 +1,13 @@ +2004-06-12 Benoît Dejean + + * mountlist.c: (xatoi) Removed, replaced by ISO strtoull. + (fstype_to_string): Cleaned. + (read_filesystem_list) : Changed prototype. Cleaned and secured. + Replaced broken free by g_free. + (ignore_mount_entry): Added. + (glibtop_get_mountlist_s): Cleaned. Better allocation algorithm. + @all_fs has now a real meaning, see the documentation. + 2004-06-06 Benoît Dejean * gnuslib.c: (connect_to_internet_server): Cleaned. diff --git a/sysdeps/common/mountlist.c b/sysdeps/common/mountlist.c index 7f2b898e..a5ea88cb 100644 --- a/sysdeps/common/mountlist.c +++ b/sysdeps/common/mountlist.c @@ -24,11 +24,6 @@ #include #include "mountlist.h" -#ifdef STDC_HEADERS -#include -#else -void free (); -#endif #if defined(STDC_HEADERS) || defined(HAVE_STRING_H) #include #else @@ -38,7 +33,7 @@ void free (); #include #include -static struct mount_entry *read_filesystem_list (int need_fs_type, int all_fs); +static struct mount_entry *read_filesystem_list (gboolean need_fs_type); #ifdef HAVE_SYS_PARAM_H #include @@ -99,38 +94,10 @@ static struct mount_entry *read_filesystem_list (int need_fs_type, int all_fs); #define MOUNTED_GETMNTTBL #endif -#ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ -/* Return the value of the hexadecimal number represented by CP. - No prefix (like '0x') or suffix (like 'h') is expected to be - part of CP. */ - -static int -xatoi (cp) - char *cp; -{ - int val; - - val = 0; - while (*cp) - { - if (*cp >= 'a' && *cp <= 'f') - val = val * 16 + *cp - 'a' + 10; - else if (*cp >= 'A' && *cp <= 'F') - val = val * 16 + *cp - 'A' + 10; - else if (*cp >= '0' && *cp <= '9') - val = val * 16 + *cp - '0'; - else - break; - cp++; - } - return val; -} -#endif /* MOUNTED_GETMNTENT1. */ #if defined (MOUNTED_GETMNTINFO) && !defined (__NetBSD__) && !defined (__OpenBSD__) static char * -fstype_to_string (t) - short t; +fstype_to_string (short t) { switch (t) { @@ -226,8 +193,7 @@ fstype_to_string (t) #ifdef MOUNTED_VMOUNT /* AIX. */ static char * -fstype_to_string (t) - int t; +fstype_to_string (int t) { struct vfs_ent *e; @@ -243,19 +209,17 @@ fstype_to_string (t) Add each entry to the tail of the list so that they stay in order. If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in the returned list are valid. Otherwise, they might not be. - If ALL_FS is zero, do not return entries for filesystems that - are automounter (dummy) entries. */ +*/ static struct mount_entry * -read_filesystem_list (need_fs_type, all_fs) - int need_fs_type, all_fs; +read_filesystem_list (gboolean need_fs_type) { struct mount_entry *mount_list; struct mount_entry *me; struct mount_entry *mtail; /* Start the list off with a dummy entry. */ - me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); + me = g_new (struct mount_entry, 1); me->me_next = NULL; mount_list = mtail = me; @@ -289,37 +253,28 @@ read_filesystem_list (need_fs_type, all_fs) } #endif -#ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ +#ifdef MOUNTED_GETMNTENT1 /* Linux, 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ { - struct mntent *mnt; - char *table = MOUNTED; + const struct mntent *mnt; FILE *fp; - char *devopt; + const char *devopt; - fp = setmntent (table, "r"); + fp = setmntent (MOUNTED, "r"); if (fp == NULL) return NULL; while ((mnt = getmntent (fp))) { - if (!all_fs && (!strcmp (mnt->mnt_type, "ignore") - || !strcmp (mnt->mnt_type, "auto"))) - continue; - - me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); + me = g_new(struct mount_entry, 1); me->me_devname = g_strdup (mnt->mnt_fsname); me->me_mountdir = g_strdup (mnt->mnt_dir); me->me_type = g_strdup (mnt->mnt_type); devopt = strstr (mnt->mnt_opts, "dev="); if (devopt) - { - if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X')) - me->me_dev = xatoi (devopt + 6); - else - me->me_dev = xatoi (devopt + 4); - } + me->me_dev = (dev_t) strtoull( devopt + 4, NULL, 0); else - me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ + me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ + me->me_next = NULL; /* Add to the linked list. */ @@ -401,7 +356,7 @@ read_filesystem_list (need_fs_type, all_fs) if (numsys < 0) { - free (stats); + g_free (stats); return (NULL); } @@ -419,17 +374,15 @@ read_filesystem_list (need_fs_type, all_fs) mtail = me; } - free (stats); + g_free (stats); } #endif /* MOUNTED_GETFSSTAT */ #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */ { struct mnttab mnt; - char *table = "/etc/mnttab"; FILE *fp; - - fp = fopen (table, "r"); + fp = fopen ("/etc/mnttab", "r"); if (fp == NULL) return NULL; @@ -490,11 +443,10 @@ read_filesystem_list (need_fs_type, all_fs) #ifdef MOUNTED_GETMNTENT2 /* SVR4. */ { struct mnttab mnt; - char *table = MNTTAB; FILE *fp; int ret; - fp = fopen (table, "r"); + fp = fopen (MNTTAB, "r"); if (fp == NULL) return NULL; @@ -539,12 +491,11 @@ read_filesystem_list (need_fs_type, all_fs) me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry)); if (vmp->vmt_flags & MNT_REMOTE) { - char *host, *path; - /* Prepend the remote pathname. */ - host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off; - path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off; - me->me_devname = g_strdup_printf("%s:%s", host, path); + me->me_devname = \ + g_strdup_printf("%s:%s", + thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off + thisent + vmp->vmt_data[VMT_OBJECT ].vmt_off); } else { @@ -560,23 +511,49 @@ read_filesystem_list (need_fs_type, all_fs) mtail->me_next = me; mtail = me; } - free (entries); + g_free (entries); } #endif /* MOUNTED_VMOUNT. */ /* Free the dummy head. */ me = mount_list; mount_list = mount_list->me_next; - free (me); + g_free (me); return mount_list; } + +static gboolean ignore_mount_entry(const struct mount_entry *me) +{ + static const char ignored[][12] = { + "proc", + "procfs", + "autofs", + "sysfs", + "none", + "devpts", + "usbdevfs", + "binfmt_misc", + "supermount" + }; + + const char (*i)[12] = &ignored[0]; + + while(i != (&ignored[0] + G_N_ELEMENTS(ignored))) { + if(strcmp(*i, me->me_type) == 0) + return TRUE; + ++i; + } + + return FALSE; +} + + glibtop_mountentry * glibtop_get_mountlist_s (glibtop *server, glibtop_mountlist *buf, int all_fs) { - struct mount_entry *me, *tmp, *next; - glibtop_mountentry *mount_list; - int count; + struct mount_entry *entries, *cur, *next; + glibtop_mountentry *mount_list, *e; glibtop_init_r (&server, 0, 0); @@ -584,44 +561,51 @@ glibtop_get_mountlist_s (glibtop *server, glibtop_mountlist *buf, int all_fs) /* Read filesystem list. */ - me = read_filesystem_list (1, all_fs); + entries = read_filesystem_list (TRUE); - if (me == NULL) + if (entries == NULL) return NULL; - /* Count entries. */ - for (count = 0, tmp = me; tmp; count++, tmp = tmp->me_next) - ; + buf->number = 0; - buf->size = sizeof (glibtop_mountentry); - buf->number = count; + gsize allocated = 16; /* magic */ + mount_list = g_new(glibtop_mountentry, allocated); - buf->total = buf->number * buf->size; + for (cur = &entries[0]; cur != NULL; cur = next) { - mount_list = g_malloc (buf->total); + if(all_fs || !ignore_mount_entry(cur)) { + /* add a new glibtop_mountentry, + resize mount_list if needed */ - /* Write data into mount_list and free memory. */ + if(buf->number == allocated) { + allocated *= 2; + mount_list = g_renew(glibtop_mountentry, + mount_list, allocated); + } - for (count = 0, tmp = me; tmp; count++, tmp = next) { + e = &mount_list[buf->number]; - g_strlcpy (mount_list [count].devname, tmp->me_devname, - GLIBTOP_MOUNTENTRY_LEN); - g_strlcpy (mount_list [count].mountdir, tmp->me_mountdir, - GLIBTOP_MOUNTENTRY_LEN); - g_strlcpy (mount_list [count].type, tmp->me_type, - GLIBTOP_MOUNTENTRY_LEN); - mount_list [count].devname [GLIBTOP_MOUNTENTRY_LEN] = 0; - mount_list [count].mountdir [GLIBTOP_MOUNTENTRY_LEN] = 0; - mount_list [count].type [GLIBTOP_MOUNTENTRY_LEN] = 0; - mount_list [count].dev = tmp->me_dev; + g_strlcpy(e->devname, cur->me_devname, sizeof e->devname); + g_strlcpy(e->mountdir, cur->me_mountdir, sizeof e->mountdir); + g_strlcpy(e->type, cur->me_type, sizeof e->type); + e->dev = cur->me_dev; - next = tmp->me_next; - g_free (tmp->me_devname); - g_free (tmp->me_mountdir); - g_free (tmp->me_type); - g_free (tmp); + buf->number++; + } + + /* free current mount_entry and move to the next */ + next = cur->me_next; + g_free(cur->me_devname); + g_free(cur->me_mountdir); + g_free(cur->me_type); + g_free(cur); } + buf->size = sizeof (glibtop_mountentry); + buf->total = buf->number * buf->size; + /* trim mount_list */ + mount_list = g_renew(glibtop_mountentry, mount_list, buf->number); + return mount_list; } -- cgit v1.2.1