tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Patch: Use fstab different to /etc/fstab in fstab.h


in a case where it would be useful to read a fstab that is not the system's
own one (sysinst), i.e. lies not /etc/fstab, you can't do that with fstab.h.

The solution would be to have a function setfstab(const char*) do set the
fstab you want to read. The code is about 20 lines of additional code + some
Rewriting that for a userland program would need either copying nearly the
whole fstab.c or changing the interface, which is not wanted.

Appended are patches (fstab.h.diff, fstab.c.diff, fstab.3.diff) and a test
program (fstab-test.file, fstab-test.c, Makefile). To test it locally, just
copy over fstab.c, apply the patch and change the include to a local include.
Then run make and execute fstab-test. It will show the mount point of a
directory according to your and the delivered fstab.

So far, the test was run on two systems, one 5.99.55, one 5.0.2 (thanks to

I fully understand objections that it's unnecessary bloat and only used in
that single case.
Then I would rather copy fstab.c and modify that code to fit my needs.

Regards, Julian

Attachment: Makefile
Description: Binary data

Index: lib/libc/gen/fstab.c
RCS file: /cvsroot/src/lib/libc/gen/fstab.c,v
retrieving revision 1.28
diff -u -r1.28 fstab.c
--- lib/libc/gen/fstab.c        12 Aug 2006 23:49:54 -0000      1.28
+++ lib/libc/gen/fstab.c        9 Sep 2011 10:29:24 -0000
@@ -55,6 +55,7 @@
@@ -209,6 +210,21 @@
+setfstab(const char *path)
+       _fs_lineno = 0;
+       if (_fs_fp) {
+               (void)fclose(_fs_fp);
+               _fs_fp = NULL;
+       }
+       if ((_fs_fp = fopen(path, "r")) == NULL) {
+               warn("Cannot open `%s'", path);
+               return 0;
+       }
+       return 1;
        _fs_lineno = 0;
Index: include/fstab.h
RCS file: /cvsroot/src/include/fstab.h,v
retrieving revision 1.11
diff -u -r1.11 fstab.h
--- include/fstab.h     3 Feb 2005 04:39:32 -0000       1.11
+++ include/fstab.h     9 Sep 2011 10:29:48 -0000
@@ -71,6 +71,7 @@
 struct fstab *getfsent(void);
 struct fstab *getfsspec(const char *);
 struct fstab *getfsfile(const char *);
+int setfstab(const char *);
 int setfsent(void);
 void endfsent(void);
#include <stdio.h>
#include <stdlib.h>
#include <fstab.h>

main(int argc, char *argv[]) {
        struct fstab *file;

        if (argc != 2)
                errx(EXIT_FAILURE, "fstab-test <mount-path>");

        file = getfsfile(argv[1]);
        if (file)
                printf("Your fstab: %s\n", file->fs_spec);
                printf("No entry found in your fstab.\n");

        file = getfsfile(argv[1]);
        if (file)
                printf("This fstab: %s\n", file->fs_spec);
                printf("No entry found in this fstab.\n");

        return 0;

Attachment: fstab-test.file
Description: Binary data

Index: ./lib/libc/gen/getfsent.3
RCS file: /cvsroot/src/lib/libc/gen/getfsent.3,v
retrieving revision 1.11
diff -u -r1.11 getfsent.3
--- ./lib/libc/gen/getfsent.3   22 Mar 2010 19:30:53 -0000      1.11
+++ ./lib/libc/gen/getfsent.3   5 Sep 2011 22:01:00 -0000
@@ -50,6 +50,8 @@
 .Ft struct fstab *
 .Fn getfsfile "const char *file"
 .Ft int
+.Fn setfstab "const char *path"
+.Ft int
 .Fn setfsent void
 .Ft void
 .Fn endfsent void
@@ -82,8 +84,7 @@
 .Fn setfsent
-opens the file (closing any previously opened file) or rewinds it
-if it is already open.
+opens the file or rewinds it if it is already open.
 .Fn endfsent
@@ -96,7 +97,22 @@
 .Fn getfsfile
 search the entire file (opening it if necessary) for a matching special
-file name or file system file name.
+file name
+.Ar spec
+or file system file name
+.Ar file .
+.Fn setfstab
+you can specify to read the fstab file from
+.Ar path
+instead of the generic
+.Pa /etc/fstab
+as is stored in the constant
+You have to call
+.Fn endfsent
+to close the file and open up the right fstab again.
 For programs wishing to read the entire database,
 .Fn getfsent
@@ -117,8 +133,10 @@
 or error.
 .Fn setfsent
-returns 0 on failure, 1 on success.
+.Fn setfstab
+return 0 on failure, 1 on success.
 .Fn endfsent
@@ -142,6 +160,10 @@
 .Fn setfsent
 functions appeared in
 .Bx 4.3 .
+.Fn setfstab
+function appeared in
+.Nx 6.0 .
 These functions use static data storage;
 if the data is needed for future use, it should be

Attachment: signature.asc
Description: PGP signature

Home | Main Index | Thread Index | Old Index