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.
This commit is contained in:
Benoît Dejean
2004-06-12 12:48:37 +00:00
parent 10d56bd5fc
commit 4de267e394
2 changed files with 96 additions and 102 deletions

View File

@@ -1,3 +1,13 @@
2004-06-12 Benoît Dejean <tazforever@dlfp.org>
* 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 <tazforever@dlfp.org> 2004-06-06 Benoît Dejean <tazforever@dlfp.org>
* gnuslib.c: (connect_to_internet_server): Cleaned. * gnuslib.c: (connect_to_internet_server): Cleaned.

View File

@@ -24,11 +24,6 @@
#include <sys/types.h> #include <sys/types.h>
#include "mountlist.h" #include "mountlist.h"
#ifdef STDC_HEADERS
#include <stdlib.h>
#else
void free ();
#endif
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h> #include <string.h>
#else #else
@@ -38,7 +33,7 @@ void free ();
#include <glibtop.h> #include <glibtop.h>
#include <glibtop/mountlist.h> #include <glibtop/mountlist.h>
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 #ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> #include <sys/param.h>
@@ -99,38 +94,10 @@ static struct mount_entry *read_filesystem_list (int need_fs_type, int all_fs);
#define MOUNTED_GETMNTTBL #define MOUNTED_GETMNTTBL
#endif #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__) #if defined (MOUNTED_GETMNTINFO) && !defined (__NetBSD__) && !defined (__OpenBSD__)
static char * static char *
fstype_to_string (t) fstype_to_string (short t)
short t;
{ {
switch (t) switch (t)
{ {
@@ -226,8 +193,7 @@ fstype_to_string (t)
#ifdef MOUNTED_VMOUNT /* AIX. */ #ifdef MOUNTED_VMOUNT /* AIX. */
static char * static char *
fstype_to_string (t) fstype_to_string (int t)
int t;
{ {
struct vfs_ent *e; 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. 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 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
the returned list are valid. Otherwise, they might not be. 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 * static struct mount_entry *
read_filesystem_list (need_fs_type, all_fs) read_filesystem_list (gboolean need_fs_type)
int need_fs_type, all_fs;
{ {
struct mount_entry *mount_list; struct mount_entry *mount_list;
struct mount_entry *me; struct mount_entry *me;
struct mount_entry *mtail; struct mount_entry *mtail;
/* Start the list off with a dummy entry. */ /* 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; me->me_next = NULL;
mount_list = mtail = me; mount_list = mtail = me;
@@ -289,37 +253,28 @@ read_filesystem_list (need_fs_type, all_fs)
} }
#endif #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; const struct mntent *mnt;
char *table = MOUNTED;
FILE *fp; FILE *fp;
char *devopt; const char *devopt;
fp = setmntent (table, "r"); fp = setmntent (MOUNTED, "r");
if (fp == NULL) if (fp == NULL)
return NULL; return NULL;
while ((mnt = getmntent (fp))) while ((mnt = getmntent (fp)))
{ {
if (!all_fs && (!strcmp (mnt->mnt_type, "ignore") me = g_new(struct mount_entry, 1);
|| !strcmp (mnt->mnt_type, "auto")))
continue;
me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry));
me->me_devname = g_strdup (mnt->mnt_fsname); me->me_devname = g_strdup (mnt->mnt_fsname);
me->me_mountdir = g_strdup (mnt->mnt_dir); me->me_mountdir = g_strdup (mnt->mnt_dir);
me->me_type = g_strdup (mnt->mnt_type); me->me_type = g_strdup (mnt->mnt_type);
devopt = strstr (mnt->mnt_opts, "dev="); devopt = strstr (mnt->mnt_opts, "dev=");
if (devopt) if (devopt)
{ me->me_dev = (dev_t) strtoull( devopt + 4, NULL, 0);
if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
me->me_dev = xatoi (devopt + 6);
else
me->me_dev = xatoi (devopt + 4);
}
else 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; me->me_next = NULL;
/* Add to the linked list. */ /* Add to the linked list. */
@@ -401,7 +356,7 @@ read_filesystem_list (need_fs_type, all_fs)
if (numsys < 0) if (numsys < 0)
{ {
free (stats); g_free (stats);
return (NULL); return (NULL);
} }
@@ -419,17 +374,15 @@ read_filesystem_list (need_fs_type, all_fs)
mtail = me; mtail = me;
} }
free (stats); g_free (stats);
} }
#endif /* MOUNTED_GETFSSTAT */ #endif /* MOUNTED_GETFSSTAT */
#if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */ #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
{ {
struct mnttab mnt; struct mnttab mnt;
char *table = "/etc/mnttab";
FILE *fp; FILE *fp;
fp = fopen ("/etc/mnttab", "r");
fp = fopen (table, "r");
if (fp == NULL) if (fp == NULL)
return NULL; return NULL;
@@ -490,11 +443,10 @@ read_filesystem_list (need_fs_type, all_fs)
#ifdef MOUNTED_GETMNTENT2 /* SVR4. */ #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
{ {
struct mnttab mnt; struct mnttab mnt;
char *table = MNTTAB;
FILE *fp; FILE *fp;
int ret; int ret;
fp = fopen (table, "r"); fp = fopen (MNTTAB, "r");
if (fp == NULL) if (fp == NULL)
return 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)); me = (struct mount_entry *) g_malloc (sizeof (struct mount_entry));
if (vmp->vmt_flags & MNT_REMOTE) if (vmp->vmt_flags & MNT_REMOTE)
{ {
char *host, *path;
/* Prepend the remote pathname. */ /* Prepend the remote pathname. */
host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off; me->me_devname = \
path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off; g_strdup_printf("%s:%s",
me->me_devname = g_strdup_printf("%s:%s", host, path); thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off
thisent + vmp->vmt_data[VMT_OBJECT ].vmt_off);
} }
else else
{ {
@@ -560,23 +511,49 @@ read_filesystem_list (need_fs_type, all_fs)
mtail->me_next = me; mtail->me_next = me;
mtail = me; mtail = me;
} }
free (entries); g_free (entries);
} }
#endif /* MOUNTED_VMOUNT. */ #endif /* MOUNTED_VMOUNT. */
/* Free the dummy head. */ /* Free the dummy head. */
me = mount_list; me = mount_list;
mount_list = mount_list->me_next; mount_list = mount_list->me_next;
free (me); g_free (me);
return mount_list; 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_mountentry *
glibtop_get_mountlist_s (glibtop *server, glibtop_mountlist *buf, int all_fs) glibtop_get_mountlist_s (glibtop *server, glibtop_mountlist *buf, int all_fs)
{ {
struct mount_entry *me, *tmp, *next; struct mount_entry *entries, *cur, *next;
glibtop_mountentry *mount_list; glibtop_mountentry *mount_list, *e;
int count;
glibtop_init_r (&server, 0, 0); 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. */ /* Read filesystem list. */
me = read_filesystem_list (1, all_fs); entries = read_filesystem_list (TRUE);
if (me == NULL) if (entries == NULL)
return NULL; return NULL;
/* Count entries. */
for (count = 0, tmp = me; tmp; count++, tmp = tmp->me_next) buf->number = 0;
;
gsize allocated = 16; /* magic */
mount_list = g_new(glibtop_mountentry, allocated);
for (cur = &entries[0]; cur != NULL; cur = next) {
if(all_fs || !ignore_mount_entry(cur)) {
/* add a new glibtop_mountentry,
resize mount_list if needed */
if(buf->number == allocated) {
allocated *= 2;
mount_list = g_renew(glibtop_mountentry,
mount_list, allocated);
}
e = &mount_list[buf->number];
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;
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->size = sizeof (glibtop_mountentry);
buf->number = count;
buf->total = buf->number * buf->size; buf->total = buf->number * buf->size;
/* trim mount_list */
mount_list = g_malloc (buf->total); mount_list = g_renew(glibtop_mountentry, mount_list, buf->number);
/* Write data into mount_list and free memory. */
for (count = 0, tmp = me; tmp; count++, tmp = next) {
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;
next = tmp->me_next;
g_free (tmp->me_devname);
g_free (tmp->me_mountdir);
g_free (tmp->me_type);
g_free (tmp);
}
return mount_list; return mount_list;
} }