1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Add fstab(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2015-09-21 23:56:37 +02:00
parent 360eaaf527
commit 63d42cd686
8 changed files with 371 additions and 0 deletions

View file

@ -352,6 +352,12 @@ fcntl/openat.o \
fcntl/open.o \
fsmarshall/fsm_fsbind.o \
fsmarshall/fsm_mountat.o \
fstab/endfsent.o \
fstab/getfsent.o \
fstab/getfsfile.o \
fstab/getfsspec.o \
fstab/scanfsent.o \
fstab/setfsent.o \
getopt/getopt_long.o \
getopt/getopt.o \
grp/endgrent.o \

34
libc/fstab/endfsent.cpp Normal file
View file

@ -0,0 +1,34 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/endfsent.cpp
Closes the filesystem table.
*******************************************************************************/
#include <fstab.h>
#include <stdio.h>
extern "C" void endfsent(void)
{
if ( !__fstab_file )
return;
fclose(__fstab_file);
__fstab_file = NULL;
}

48
libc/fstab/getfsent.cpp Normal file
View file

@ -0,0 +1,48 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/getfsent.cpp
Read filesystem table entry.
*******************************************************************************/
#include <fstab.h>
#include <stdio.h>
#include <stdlib.h>
extern "C" struct fstab* getfsent(void)
{
if ( !__fstab_file && !setfsent() )
return NULL;
static struct fstab fs;
static char* line = NULL;
static size_t line_size = 0;
ssize_t line_length;
while ( 0 <= (line_length = getline(&line, &line_size, __fstab_file)) )
{
if ( line_length && line[line_length - 1] == '\n' )
line[--line_length] = '\0';
if ( scanfsent(line, &fs) )
return &fs;
}
free(line);
line = NULL;
line_size = 0;
return NULL;
}

37
libc/fstab/getfsfile.cpp Normal file
View file

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/getfsfile.cpp
Lookup filesystem table by mount point.
*******************************************************************************/
#include <fstab.h>
#include <string.h>
extern "C" struct fstab* getfsfile(const char* mount_point)
{
if ( !setfsent() )
return NULL;
struct fstab* fs;
while ( (fs = getfsent()) )
if ( !strcmp(fs->fs_file, mount_point) )
return fs;
return NULL;
}

37
libc/fstab/getfsspec.cpp Normal file
View file

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/getfsspec.cpp
Lookup filesystem table by special file.
*******************************************************************************/
#include <fstab.h>
#include <string.h>
extern "C" struct fstab* getfsspec(const char* special_file)
{
if ( !setfsent() )
return NULL;
struct fstab* fs;
while ( (fs = getfsent()) )
if ( !strcmp(fs->fs_spec, special_file) )
return fs;
return NULL;
}

101
libc/fstab/scanfsent.cpp Normal file
View file

@ -0,0 +1,101 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/scanfsent.cpp
Parse filesystem table entry.
*******************************************************************************/
#include <ctype.h>
#include <fstab.h>
#include <stdlib.h>
#include <string.h>
static char* next_fsent_field(char** str_p)
{
char* str = *str_p;
while ( *str && isspace((unsigned char) *str) )
str++;
if ( !*str || *str == '#' )
return NULL;
size_t length = 1;
while ( str[length] &&
!isspace((unsigned char) str[length]) &&
str[length] != '#' )
length++;
if ( str[length] )
{
char c = str[length];
str[length] = '\0';
*str_p = str + length + (c != '#' ? 1 : 0);
}
else
*str_p = str + length;
return str;
}
char* find_fstype(char* str)
{
const char* types[] =
{
FSTAB_RO,
FSTAB_RQ,
FSTAB_RW,
FSTAB_SW,
FSTAB_XX,
};
size_t count = sizeof(types) / sizeof(types[0]);
while ( *str )
{
while ( *str == ',' )
{
str++;
continue;
}
size_t length = strcspn(str, ",");
if ( length == 2 )
{
for ( size_t i = 0; i < count; i++ )
if ( str[0] == types[i][0] && str[1] == types[i][1] )
return (char*) types[i];
}
str += length;
}
return NULL;
}
extern "C" int scanfsent(char* str, struct fstab* fs)
{
char* str_freq;
char* str_passno;
if ( !(fs->fs_spec = next_fsent_field(&str)) ||
!(fs->fs_file = next_fsent_field(&str)) ||
!(fs->fs_vfstype = next_fsent_field(&str)) ||
!(fs->fs_mntops = next_fsent_field(&str)) ||
!(str_freq = next_fsent_field(&str)) ||
!(str_passno = next_fsent_field(&str)) )
return 0;
if ( !(fs->fs_type = find_fstype(fs->fs_mntops)) )
return 0;
if ( !strcmp(fs->fs_type, "xx") )
return 0;
fs->fs_freq = atoi(str_freq);
fs->fs_passno = atoi(str_passno);
return 1;
}

37
libc/fstab/setfsent.cpp Normal file
View file

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab/setfsent.cpp
Open filesystem table.
*******************************************************************************/
#include <fstab.h>
#include <stdio.h>
extern "C" { FILE* __fstab_file = NULL; }
extern "C" int setfsent(void)
{
if ( __fstab_file )
rewind(__fstab_file);
else if ( !(__fstab_file = fopen("/etc/fstab", "r")) )
return 0;
return 1;
}

71
libc/include/fstab.h Normal file
View file

@ -0,0 +1,71 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
fstab.h
Filesystem table.
*******************************************************************************/
#ifndef INCLUDE_FSTAB_H
#define INCLUDE_FSTAB_H
#include <sys/cdefs.h>
#ifndef __FILE_defined
#define __FILE_defined
typedef struct __FILE FILE;
#endif
struct fstab
{
char* fs_spec;
char* fs_file;
char* fs_vfstype;
char* fs_mntops;
char* fs_type;
int fs_freq;
int fs_passno;
};
#define FSTAB_RO "ro"
#define FSTAB_RQ "rq"
#define FSTAB_RW "rw"
#define FSTAB_SW "sw"
#define FSTAB_XX "xx"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__is_sortix_libc)
extern FILE* __fstab_file;
#endif
void endfsent(void);
struct fstab* getfsent(void);
struct fstab* getfsfile(const char*);
struct fstab* getfsspec(const char*);
int scanfsent(char*, struct fstab*);
int setfsent(void);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif