--- eject/eject.c +++ eject/eject.c @@ -72,6 +72,8 @@ #include #include +#include + /* Used by the ToggleTray() function. If ejecting the tray takes this * time or less, the tray was probably already ejected, so we close it * again. @@ -851,40 +853,34 @@ static int GetMajorMinor(const char *name, int *maj, int *min) static int MountedDevice(const char *name, char **mountName, char **deviceName) { FILE *fp; - char line[1024]; - char s1[1024]; - char s2[1024]; - int rc; + struct mntent* mntentry; int maj; int min; GetMajorMinor(name, &maj, &min); - fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab"), "r"); + fp = setmntent((p_option ? "/proc/mounts" : "/etc/mtab"), "r"); if (fp == NULL) { fprintf(stderr, _("unable to open %s: %s\n"), (p_option ? "/proc/mounts" : "/etc/mtab"), strerror(errno)); exit(1); } - while (fgets(line, sizeof(line), fp) != 0) { - rc = sscanf(line, "%1023s %1023s", s1, s2); - if (rc >= 2) { - int mtabmaj, mtabmin; - GetMajorMinor(s1, &mtabmaj, &mtabmin); - if (((strcmp(s1, name) == 0) || (strcmp(s2, name) == 0)) || - ((maj != -1) && (maj == mtabmaj) && (min == mtabmin))) { - FCLOSE(fp); - *deviceName = strdup(s1); - *mountName = strdup(s2); + for (mntentry = getmntent(fp); mntentry; mntentry = getmntent(fp)) { + int mtabmaj, mtabmin; + GetMajorMinor(mntentry->mnt_fsname, &mtabmaj, &mtabmin); + if (((strcmp(mntentry->mnt_fsname, name) == 0) || (strcmp(mntentry->mnt_dir, name) == 0)) || + ((maj != -1) && (maj == mtabmaj) && (min == mtabmin))) { + endmntent(fp); + *deviceName = strdup(mntentry->mnt_fsname); + *mountName = strdup(mntentry->mnt_dir); return 1; } - } } + endmntent(fp); *deviceName = 0; *mountName = 0; - FCLOSE(fp); return 0; } @@ -897,12 +893,9 @@ static int MountedDevice(const char *name, char **mountName, char **deviceName) static int MountableDevice(const char *name, char **mountName, char **deviceName) { FILE *fp; - char line[1024]; - char s1[1024]; - char s2[1024]; - int rc; + struct mntent* mntentry; - fp = fopen("/etc/fstab", "r"); + fp = setmntent("/etc/fstab", "r"); if (fp == NULL) { /* * /etc/fstab may be unreadable in some situations due to passwords in the @@ -916,16 +909,15 @@ static int MountableDevice(const char *name, char **mountName, char **deviceName return -1; } - while (fgets(line, sizeof(line), fp) != 0) { - rc = sscanf(line, "%1023s %1023s", s1, s2); - if (rc >= 2 && s1[0] != '#' && strcmp(s2, name) == 0) { - FCLOSE(fp); - *deviceName = strdup(s1); - *mountName = strdup(s2); + for (mntentry = getmntent(fp); mntentry; mntentry = getmntent(fp)) { + if (strcmp(mntentry->mnt_fsname,"#") && strcmp(mntentry->mnt_dir, name) == 0) { + endmntent(fp); + *deviceName = strdup(mntentry->mnt_fsname); + *mountName = strdup(mntentry->mnt_dir); return 1; } } - FCLOSE(fp); + endmntent(fp); return 0; } @@ -938,9 +930,7 @@ static void UnmountDevices(const char *pattern) { regex_t preg; FILE *fp; - char s1[1024]; - char s2[1024]; - char line[1024]; + struct mntent* mntentry; int status; if (regcomp(&preg, pattern, REG_EXTENDED)!=0) { @@ -948,26 +938,25 @@ static void UnmountDevices(const char *pattern) exit(1); } - fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab"), "r"); + fp = setmntent((p_option ? "/proc/mounts" : "/etc/mtab"), "r"); if (fp == NULL) { fprintf(stderr, _("unable to open %s: %s\n"),(p_option ? "/proc/mounts" : "/etc/mtab"), strerror(errno)); exit(1); } - while (fgets(line, sizeof(line), fp) != 0) { - status = sscanf(line, "%1023s %1023s", s1, s2); - if (status >= 2) { - status = regexec(&preg, s1, 0, 0, 0); - if (status == 0) { - if (v_option) - printf(_("%s: unmounting `%s'\n"), programName, s2); - Unmount(s2); - } + for (mntentry = getmntent(fp); mntentry; mntentry = getmntent(fp)) + { + status = regexec(&preg, mntentry->mnt_fsname, 0, 0, 0); + if (status == 0) + { + if (v_option) + printf(_("%s: unmounting `%s'\n"), programName, mntentry->mnt_fsname); + Unmount(mntentry->mnt_fsname); } } regfree(&preg); - FCLOSE(fp); + endmntent(fp); }