From 9c7a51080303b9870d9ad40123a00a199ac1945c Mon Sep 17 00:00:00 2001 From: yuqing Date: Mon, 18 Jan 2016 15:32:32 +0800 Subject: [PATCH] do NOT use function xxxmntent in linux --- src/system_info.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/system_info.c b/src/system_info.c index fd52d43..426112c 100644 --- a/src/system_info.c +++ b/src/system_info.c @@ -27,7 +27,6 @@ #ifdef OS_LINUX #include #include -#include #else #ifdef OS_FREEBSD #include @@ -152,26 +151,30 @@ int get_mounted_filesystems(struct fast_statfs *stats, const int size, int *coun #ifdef OS_LINUX const char *filename = "/proc/mounts"; FILE *fp; - struct mntent *mnt; + char *p; + char *mntfromname; + char *mntonname; + char *fstypename; struct statfs buf; + char line[1024]; int result; int i; *count = 0; - fp = setmntent(filename, "r"); + fp = fopen(filename, "r"); if (fp == NULL) { result = errno != 0 ? errno : ENOENT; logError("file: "__FILE__", line: %d, " - "call setmntent fail, " + "call fopen %s fail, " "errno: %d, error info: %s", - __LINE__, errno, STRERROR(errno)); + __LINE__, filename, errno, STRERROR(errno)); return result; } memset(stats, 0, sizeof(struct fast_statfs) * size); result = 0; - while ((mnt=getmntent(fp)) != NULL) + while (fgets(line, sizeof(line), fp) != NULL) { if (*count >= size) { @@ -179,12 +182,21 @@ int get_mounted_filesystems(struct fast_statfs *stats, const int size, int *coun break; } - SET_MNT_FIELDS(stats[*count], mnt->mnt_type, - mnt->mnt_fsname, mnt->mnt_dir); + p = line; + mntfromname = strsep(&p, " \t"); + mntonname = strsep(&p, " \t"); + fstypename = strsep(&p, " \t"); + + snprintf(stats[*count].f_mntfromname, + sizeof(stats[*count].f_mntfromname), "%s", mntfromname); + snprintf(stats[*count].f_mntonname, + sizeof(stats[*count].f_mntonname), "%s", mntonname); + snprintf(stats[*count].f_fstypename, + sizeof(stats[*count].f_fstypename), "%s", fstypename); (*count)++; } - endmntent(fp); + fclose(fp); for (i=0; i<*count; i++) {