- Add setfstab() and getfstab().

- Use the environment variable 'PATH_FSTAB' if set rather than the
  hardcoded '/etc/fstab' (fstab.h:_PATH_FSTAB)
This commit is contained in:
Matthew N. Dodd 2003-04-07 12:55:00 +00:00
parent 947193d93f
commit 134dbc4c32
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=113219
4 changed files with 73 additions and 4 deletions

View File

@ -75,6 +75,8 @@ struct fstab *getfsspec(const char *);
struct fstab *getfsfile(const char *); struct fstab *getfsfile(const char *);
int setfsent(void); int setfsent(void);
void endfsent(void); void endfsent(void);
void setfstab(const char *);
const char *getfstab(void);
__END_DECLS __END_DECLS
#endif /* !_FSTAB_H_ */ #endif /* !_FSTAB_H_ */

View File

@ -84,7 +84,8 @@ MLINKS+=getcwd.3 getwd.3
MLINKS+=getcontext.3 setcontext.3 MLINKS+=getcontext.3 setcontext.3
MLINKS+=getdomainname.3 setdomainname.3 MLINKS+=getdomainname.3 setdomainname.3
MLINKS+=getfsent.3 endfsent.3 getfsent.3 getfsfile.3 getfsent.3 getfsspec.3 \ MLINKS+=getfsent.3 endfsent.3 getfsent.3 getfsfile.3 getfsent.3 getfsspec.3 \
getfsent.3 getfstype.3 getfsent.3 setfsent.3 getfsent.3 getfstype.3 getfsent.3 setfsent.3 \
getfsent.3 setfstab.3 getfsent.3 getfstab.3
MLINKS+=getgrent.3 endgrent.3 getgrent.3 getgrgid.3 getgrent.3 getgrnam.3 \ MLINKS+=getgrent.3 endgrent.3 getgrent.3 getgrgid.3 getgrent.3 getgrnam.3 \
getgrent.3 setgrent.3 getgrent.3 setgroupent.3 getgrent.3 setgrent.3 getgrent.3 setgroupent.3
MLINKS+=gethostname.3 sethostname.3 MLINKS+=gethostname.3 sethostname.3

View File

@ -54,11 +54,40 @@ __FBSDID("$FreeBSD$");
static FILE *_fs_fp; static FILE *_fs_fp;
static struct fstab _fs_fstab; static struct fstab _fs_fstab;
static int LineNo = 0; static int LineNo = 0;
static char *path_fstab;
static char fstab_path[PATH_MAX];
static int fsp_set = 0;
static void error(int); static void error(int);
static void fixfsfile(void); static void fixfsfile(void);
static int fstabscan(void); static int fstabscan(void);
void
setfstab(const char *file)
{
if (file == NULL) {
path_fstab = _PATH_FSTAB;
} else {
strncpy(fstab_path, file, PATH_MAX);
fstab_path[PATH_MAX - 1] = '\0';
path_fstab = fstab_path;
}
fsp_set = 1;
return;
}
const char *
getfstab (void)
{
if (fsp_set)
return (path_fstab);
else
return (_PATH_FSTAB);
}
static void static void
fixfsfile() fixfsfile()
{ {
@ -226,7 +255,13 @@ setfsent()
LineNo = 0; LineNo = 0;
return(1); return(1);
} }
if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) { if (fsp_set == 0) {
if (issetugid())
setfstab(NULL);
else
setfstab(getenv("PATH_FSTAB"));
}
if ((_fs_fp = fopen(path_fstab, "r")) != NULL) {
LineNo = 0; LineNo = 0;
return(1); return(1);
} }
@ -241,6 +276,8 @@ endfsent()
(void)fclose(_fs_fp); (void)fclose(_fs_fp);
_fs_fp = NULL; _fs_fp = NULL;
} }
fsp_set = 0;
} }
static void static void
@ -251,7 +288,7 @@ error(err)
char num[30]; char num[30];
(void)_write(STDERR_FILENO, "fstab: ", 7); (void)_write(STDERR_FILENO, "fstab: ", 7);
(void)_write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
(void)_write(STDERR_FILENO, ":", 1); (void)_write(STDERR_FILENO, ":", 1);
sprintf(num, "%d: ", LineNo); sprintf(num, "%d: ", LineNo);
(void)_write(STDERR_FILENO, num, strlen(num)); (void)_write(STDERR_FILENO, num, strlen(num));

View File

@ -56,6 +56,10 @@
.Fn setfsent void .Fn setfsent void
.Ft void .Ft void
.Fn endfsent void .Fn endfsent void
.Ft void
.Fn setfstab "const char *file"
.Ft const char *
.Fn getfstab void
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn getfsent , .Fn getfsent ,
@ -94,6 +98,18 @@ function
closes the file. closes the file.
.Pp .Pp
The The
.Fn setfstab
function sets the file to be used by subsequent operations.
The value set by
.Fn setfstab
does not persist across calls to
.Fn endfsent
.Pp
The
.Fn getfstab
function returns the name of the file that that will be used.
.Pp
The
.Fn getfsspec .Fn getfsspec
and and
.Fn getfsfile .Fn getfsfile
@ -128,6 +144,13 @@ The
.Fn endfsent .Fn endfsent
function function
returns nothing. returns nothing.
.Sh ENVIRONMENT
.Bl -tag -width PATH_FSTAB
.It Pa PATH_FSTAB
If the environment variable
.Pa PATH_FSTAB
is set all operations are performed against the specified file.
.El
.Sh FILES .Sh FILES
.Bl -tag -width /etc/fstab -compact .Bl -tag -width /etc/fstab -compact
.It Pa /etc/fstab .It Pa /etc/fstab
@ -146,7 +169,13 @@ the
and and
.Fn setfsent .Fn setfsent
functions appeared in functions appeared in
.Bx 4.3 . .Bx 4.3 ;
the
.Fn setfstab
and
.Fn getfstab
functions appeared in
.Fx 5.1.
.Sh BUGS .Sh BUGS
These functions use static data storage; These functions use static data storage;
if the data is needed for future use, it should be if the data is needed for future use, it should be