home *** CD-ROM | disk | FTP | other *** search
- /* History:
- 5/1/91 DJB baseline
- Originally stolen from Dupuy ofiles.
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "rpcnfs.h" /* XXX: see structmtab.h */
- #include "structmtab.h"
- #include "stattimeout.h"
- #include "mallocfree.h"
- #include "mntops.h"
- #include "confslowstat.h"
- #include "confmntent.h"
- #include "confmntult.h"
-
- #ifdef SLOWSTAT
- #define STAT_TIMEOUT 5 /* time out stat() calls after 5 sec */
- #endif
- #ifndef STAT_TIMEOUT
- #define STAT_TIMEOUT 0
- #endif
-
- #ifdef MNTULT
- static struct fs_data mntdata;
- #else
- static FILE *mnttab;
- #endif
-
- struct statlist *stats = 0;
-
- #ifdef MNTULT
-
- int get_mntlist()
- {
- register struct statlist *statp;
- int mnt = 0;
- int result;
-
- #ifdef NOSTAT_MANY
- while (result = getmnt(&mnt,&mntdata,sizeof(mntdata),NOSTAT_MANY,0))
- #else
- while (result = getmnt(&mnt,&mntdata,sizeof(mntdata),mnt))
- #endif
- {
- if (!(statp = (struct statlist *) malloc(sizeof(struct statlist))))
- break;
- statp->next = stats;
- stats = statp;
-
- (void) strcpy(statp->filename,mntdata.fd_path);
- (void) strcpy(statp->fsname,mntdata.fd_devname);
- statp->device = mntdata.fd_dev;
- }
-
- if (result < 0)
- return -1;
- return 0;
- }
-
- struct mounted *getmntname(name)
- char *name;
- {
- static struct mounted mounted;
- int result;
-
- #ifdef NOSTAT_ONE
- if (!(result = getmnt(0,&mntdata,0,NOSTAT_ONE,name)))
- return 0;
- #else
- int mnt = 0;
-
- while (result = getmnt(&mnt,&mntdata,sizeof(mntdata),mnt))
- {
- if (strcmp(name,mntdata.fd_devname) == 0)
- break;
- if (strcmp(name,mntdata.fd_path) == 0)
- break;
- }
-
- if (mnt == 0)
- return 0;
- #endif
-
- if (result < 0)
- return 0;
-
- mounted.mountpoint = mntdata.fd_path;
- mounted.filesystem = mntdata.fd_devname;
- return &mounted;
- }
-
- struct mounted *getmntfile(filestats)
- struct stat *filestats;
- {
- static struct mounted mounted;
- int context;
-
- while (getmnt(&context,&mntdata,sizeof(mntdata)),context)
- if (filestats->st_dev == mntdata.fd_dev)
- break;
-
- if (!context)
- return 0;
-
- mounted.mountpoint = mntdata.fd_path;
- mounted.filesystem = mntdata.fd_devname;
- return &mounted;
- }
-
- #else
-
- #ifndef MNTENT
-
- struct mtab *getmntent(mntfile)
- FILE *mntfile;
- {
- static struct mtab mtab;
- char devname[MNTMAXSTR];
-
- if (fread((char *) &mtab,sizeof(mtab),1,mntfile) != 1)
- return 0;
- strncpy(devname,mtab.m_dname,MNTMAXSTR);
- strcpy(mtab.m_dname,"/dev/");
- strncat(mtab.m_dname,devname,MNTMAXSTR - 5);
- return &mtab;
- }
-
- #endif
-
- int get_mntlist()
- {
- register struct statlist *statp;
- struct stat status;
- struct mntent *mnt;
-
- #ifdef MNTENT
- mnttab = setmntent(MOUNTED,"r");
- #else
- mnttab = fopen("/etc/mtab","r");
- #endif
-
- if (!mnttab)
- return -1;
-
- while (mnt = getmntent(mnttab))
- {
- if (!(statp = (struct statlist *) malloc(sizeof(struct statlist))))
- break;
- statp->next = stats;
- stats = statp;
-
- (void) strncpy(statp->filename,mnt->mnt_dir,MNTMAXSTR);
- (void) strncpy(statp->fsname,mnt->mnt_fsname,MNTMAXSTR);
- statp->filename[MNTMAXSTR - 1] = '\0';
- statp->fsname[MNTMAXSTR - 1] = '\0';
-
- if (stattimeout(statp->filename,&status,STAT_TIMEOUT))
- { /* find out what it is */
- /*XXX: report error on statp->filename */
- stats = statp->next;
- free((char *) statp);
- continue;
- }
- statp->device = status.st_dev;
- }
- }
-
- struct mounted *getmntname(name)
- char *name;
- {
- static struct mounted mounted;
- register struct mntent *mnt;
-
- rewind(mnttab);
- while ((mnt = getmntent(mnttab)))
- {
- if (strncmp(name,mnt->mnt_fsname,MNTMAXSTR) == 0)
- break;
- if (strncmp(name,mnt->mnt_dir,MNTMAXSTR) == 0)
- break;
- }
-
- if (!mnt)
- return 0;
-
- mounted.mountpoint = mnt->mnt_dir;
- mounted.filesystem = mnt->mnt_fsname;
- return &mounted;
- }
-
- struct mounted *getmntfile(filestats)
- struct stat *filestats;
- {
- static struct mounted mounted;
- register struct mntent *mnt;
- struct stat dirstats;
-
- rewind(mnttab);
- while (mnt = getmntent(mnttab))
- if ((stat(mnt->mnt_dir,&dirstats) >= 0) &&
- (filestats->st_dev == dirstats.st_dev))
- break;
-
- if (!mnt)
- return 0;
-
- mounted.mountpoint = mnt->mnt_dir;
- mounted.filesystem = mnt->mnt_fsname;
- return &mounted;
- }
-
- #endif
-