Skip to content

Commit

Permalink
- Add setfstab() and getfstab().
Browse files Browse the repository at this point in the history
- Use the environment variable 'PATH_FSTAB' if set rather than the
  hardcoded '/etc/fstab' (fstab.h:_PATH_FSTAB)
  • Loading branch information
Matthew N. Dodd authored and Matthew N. Dodd committed Apr 7, 2003
1 parent 947193d commit 134dbc4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/fstab.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ struct fstab *getfsspec(const char *);
struct fstab *getfsfile(const char *);
int setfsent(void);
void endfsent(void);
void setfstab(const char *);
const char *getfstab(void);
__END_DECLS

#endif /* !_FSTAB_H_ */
3 changes: 2 additions & 1 deletion lib/libc/gen/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ MLINKS+=getcwd.3 getwd.3
MLINKS+=getcontext.3 setcontext.3
MLINKS+=getdomainname.3 setdomainname.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 \
getgrent.3 setgrent.3 getgrent.3 setgroupent.3
MLINKS+=gethostname.3 sethostname.3
Expand Down
41 changes: 39 additions & 2 deletions lib/libc/gen/fstab.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,40 @@ __FBSDID("$FreeBSD$");
static FILE *_fs_fp;
static struct fstab _fs_fstab;
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 fixfsfile(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
fixfsfile()
{
Expand Down Expand Up @@ -226,7 +255,13 @@ setfsent()
LineNo = 0;
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;
return(1);
}
Expand All @@ -241,6 +276,8 @@ endfsent()
(void)fclose(_fs_fp);
_fs_fp = NULL;
}

fsp_set = 0;
}

static void
Expand All @@ -251,7 +288,7 @@ error(err)
char num[30];

(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);
sprintf(num, "%d: ", LineNo);
(void)_write(STDERR_FILENO, num, strlen(num));
Expand Down
31 changes: 30 additions & 1 deletion lib/libc/gen/getfsent.3
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
.Fn setfsent void
.Ft void
.Fn endfsent void
.Ft void
.Fn setfstab "const char *file"
.Ft const char *
.Fn getfstab void
.Sh DESCRIPTION
The
.Fn getfsent ,
Expand Down Expand Up @@ -94,6 +98,18 @@ function
closes the file.
.Pp
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
and
.Fn getfsfile
Expand Down Expand Up @@ -128,6 +144,13 @@ The
.Fn endfsent
function
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
.Bl -tag -width /etc/fstab -compact
.It Pa /etc/fstab
Expand All @@ -146,7 +169,13 @@ the
and
.Fn setfsent
functions appeared in
.Bx 4.3 .
.Bx 4.3 ;
the
.Fn setfstab
and
.Fn getfstab
functions appeared in
.Fx 5.1.
.Sh BUGS
These functions use static data storage;
if the data is needed for future use, it should be
Expand Down

0 comments on commit 134dbc4

Please sign in to comment.