#include "dump.h"
static struct pmntent {
struct mntent *pm_mnt;
struct pmntent *pm_next;
} *mnttable;
#define mntstrdup(s) ((s) ? strdup((s)) : "")
#ifdef __STDC__
static struct mntent *mygetmntent(FILE *, char *);
static struct pmntent *addmtab(char *, struct pmntent *);
static struct mntent *allocmntent(struct mntent *);
#else
static struct pmntent *addmtab();
static struct mntent *mygetmntent();
static struct mntent *allocmntent();
static int idatesort();
#endif
static struct mntent *
mygetmntent(f, name)
FILE *f;
char *name;
{
static struct mntent mt;
int status;
if ((status = getmntent(f, &mt)) == 0)
return (&mt);
switch (status) {
case EOF: break;
case MNT_TOOLONG:
msg(gettext("%s has a line that is too long\n"), name);
break;
case MNT_TOOMANY:
msg(gettext("%s has a line with too many entries\n"), name);
break;
case MNT_TOOFEW:
msg(gettext("%s has a line with too few entries\n"), name);
break;
default:
msg(gettext(
"Unknown return code, %d, from getmntent() on %s\n"),
status, name);
break;
}
return (NULL);
}
static struct pmntent *
addvfstab(tablename, pm)
char *tablename;
struct pmntent *pm;
{
struct mnttab *mnt;
struct vfstab vfs;
FILE *tp;
int status;
assert(((mnttable == NULL) && (pm == NULL)) || (pm != NULL));
tp = fopen(tablename, "r");
if (tp == (FILE *)0) {
msg(gettext("Cannot open %s for dump table information.\n"),
tablename);
return ((struct pmntent *)0);
}
while ((status = getvfsent(tp, &vfs)) == 0) {
if (vfs.vfs_fstype == (char *)0 ||
strcmp(vfs.vfs_fstype, MNTTYPE_42) != 0)
continue;
mnt = (struct mnttab *)xmalloc(sizeof (*mnt));
mnt->mnt_fsname = mntstrdup(vfs.vfs_special);
mnt->mnt_dir = mntstrdup(vfs.vfs_mountp);
mnt->mnt_type = mntstrdup(vfs.vfs_fstype);
mnt->mnt_opts = mntstrdup(vfs.vfs_mntopts);
if (mnttable == (struct pmntent *)0)
mnttable = pm = (struct pmntent *)xmalloc(sizeof (*pm));
else {
pm->pm_next = (struct pmntent *)xmalloc(sizeof (*pm));
pm = pm->pm_next;
}
pm->pm_mnt = mnt;
pm->pm_next = (struct pmntent *)0;
}
switch (status) {
case EOF: break;
case VFS_TOOLONG:
msg(gettext("%s has a line that is too long\n"), tablename);
break;
case VFS_TOOMANY:
msg(gettext("%s has a line with too many entries\n"),
tablename);
break;
case VFS_TOOFEW:
msg(gettext("%s has a line with too few entries\n"), tablename);
break;
default:
msg(gettext(
"Unknown return code, %d, from getvfsent() on %s\n"),
status, tablename);
break;
}
(void) fclose(tp);
return (pm);
}
static struct mntent *
allocmntent(mnt)
struct mntent *mnt;
{
struct mntent *new;
new = (struct mntent *)xmalloc(sizeof (*mnt));
new->mnt_fsname = mntstrdup(mnt->mnt_fsname);
new->mnt_dir = mntstrdup(mnt->mnt_dir);
new->mnt_type = mntstrdup(mnt->mnt_type);
new->mnt_opts = mntstrdup(mnt->mnt_opts);
return (new);
}
void
mnttabread()
{
struct pmntent *pm = (struct pmntent *)0;
if (mnttable != (struct pmntent *)0)
return;
pm = addvfstab(VFSTAB, pm);
(void) addmtab(MOUNTED, pm);
}
static struct pmntent *
addmtab(tablename, pm)
char *tablename;
struct pmntent *pm;
{
struct mntent *mnt;
FILE *tp;
tp = setmntent(tablename, "r");
if (tp == (FILE *)0) {
msg(gettext("Cannot open %s for dump table information.\n"),
tablename);
return ((struct pmntent *)0);
}
while (mnt = mygetmntent(tp, tablename)) {
if (mnt->mnt_type == (char *)0 ||
strcmp(mnt->mnt_type, MNTTYPE_42) != 0)
continue;
mnt = allocmntent(mnt);
if (mnttable == (struct pmntent *)0)
mnttable = pm = (struct pmntent *)xmalloc(sizeof (*pm));
else {
pm->pm_next = (struct pmntent *)xmalloc(sizeof (*pm));
pm = pm->pm_next;
}
pm->pm_mnt = mnt;
pm->pm_next = (struct pmntent *)0;
}
(void) endmntent(tp);
return (pm);
}
struct mntent *
mnttabsearch(key, mounted)
char *key;
int mounted;
{
struct pmntent *pm;
struct mntent *mnt;
struct mntent *first = (struct mntent *)0;
char *s;
char *gotreal;
char path[MAXPATHLEN];
for (pm = mnttable; pm; pm = pm->pm_next) {
s = NULL;
mnt = pm->pm_mnt;
if (strcmp(mnt->mnt_dir, key) == 0)
goto found;
if (strcmp(mnt->mnt_fsname, key) == 0)
goto found;
if ((s = rawname(mnt->mnt_fsname)) != NULL &&
strcmp(s, key) == 0)
goto found;
gotreal = realpath(mnt->mnt_dir, path);
if (gotreal && strcmp(path, key) == 0)
goto found;
if (key[0] != '/') {
if (*mnt->mnt_fsname == '/' &&
strcmp(mnt->mnt_fsname + 1, key) == 0)
goto found;
if (*mnt->mnt_dir == '/' &&
strcmp(mnt->mnt_dir + 1, key) == 0)
goto found;
if (gotreal && *path == '/' &&
strcmp(path + 1, key) == 0)
goto found;
}
if (s != NULL && s != mnt->mnt_fsname)
free(s);
continue;
found:
if (s != NULL && s != mnt->mnt_fsname)
free(s);
if (lf_ismounted(mnt->mnt_fsname, mnt->mnt_dir) > 0)
return (mnt);
else if (first == (struct mntent *)0)
first = mnt;
}
if (mounted)
return ((struct mntent *)0);
return (first);
}
static struct pmntent *current;
static int set;
void
#ifdef __STDC__
setmnttab(void)
#else
setmnttab()
#endif
{
current = mnttable;
set = 1;
}
struct mntent *
#ifdef __STDC__
getmnttab(void)
#else
getmnttab()
#endif
{
struct pmntent *pm;
if (!set)
setmnttab();
pm = current;
if (current) {
current = current->pm_next;
return (pm->pm_mnt);
}
return ((struct mntent *)0);
}