From 63d42cd686fdabe77e29d92888496075ffc8af71 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 21 Sep 2015 23:56:37 +0200 Subject: [PATCH] Add fstab(3). --- libc/Makefile | 6 +++ libc/fstab/endfsent.cpp | 34 +++++++++++++ libc/fstab/getfsent.cpp | 48 +++++++++++++++++++ libc/fstab/getfsfile.cpp | 37 ++++++++++++++ libc/fstab/getfsspec.cpp | 37 ++++++++++++++ libc/fstab/scanfsent.cpp | 101 +++++++++++++++++++++++++++++++++++++++ libc/fstab/setfsent.cpp | 37 ++++++++++++++ libc/include/fstab.h | 71 +++++++++++++++++++++++++++ 8 files changed, 371 insertions(+) create mode 100644 libc/fstab/endfsent.cpp create mode 100644 libc/fstab/getfsent.cpp create mode 100644 libc/fstab/getfsfile.cpp create mode 100644 libc/fstab/getfsspec.cpp create mode 100644 libc/fstab/scanfsent.cpp create mode 100644 libc/fstab/setfsent.cpp create mode 100644 libc/include/fstab.h diff --git a/libc/Makefile b/libc/Makefile index 5f9129da..719f0ba9 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -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 \ diff --git a/libc/fstab/endfsent.cpp b/libc/fstab/endfsent.cpp new file mode 100644 index 00000000..85667a46 --- /dev/null +++ b/libc/fstab/endfsent.cpp @@ -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 . + + fstab/endfsent.cpp + Closes the filesystem table. + +*******************************************************************************/ + +#include +#include + +extern "C" void endfsent(void) +{ + if ( !__fstab_file ) + return; + fclose(__fstab_file); + __fstab_file = NULL; +} diff --git a/libc/fstab/getfsent.cpp b/libc/fstab/getfsent.cpp new file mode 100644 index 00000000..a8e107bf --- /dev/null +++ b/libc/fstab/getfsent.cpp @@ -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 . + + fstab/getfsent.cpp + Read filesystem table entry. + +*******************************************************************************/ + +#include +#include +#include + +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; +} diff --git a/libc/fstab/getfsfile.cpp b/libc/fstab/getfsfile.cpp new file mode 100644 index 00000000..0e03da64 --- /dev/null +++ b/libc/fstab/getfsfile.cpp @@ -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 . + + fstab/getfsfile.cpp + Lookup filesystem table by mount point. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libc/fstab/getfsspec.cpp b/libc/fstab/getfsspec.cpp new file mode 100644 index 00000000..9cea2375 --- /dev/null +++ b/libc/fstab/getfsspec.cpp @@ -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 . + + fstab/getfsspec.cpp + Lookup filesystem table by special file. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libc/fstab/scanfsent.cpp b/libc/fstab/scanfsent.cpp new file mode 100644 index 00000000..858c3f6d --- /dev/null +++ b/libc/fstab/scanfsent.cpp @@ -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 . + + fstab/scanfsent.cpp + Parse filesystem table entry. + +*******************************************************************************/ + +#include +#include +#include +#include + +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; +} diff --git a/libc/fstab/setfsent.cpp b/libc/fstab/setfsent.cpp new file mode 100644 index 00000000..b2e80365 --- /dev/null +++ b/libc/fstab/setfsent.cpp @@ -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 . + + fstab/setfsent.cpp + Open filesystem table. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libc/include/fstab.h b/libc/include/fstab.h new file mode 100644 index 00000000..0f841769 --- /dev/null +++ b/libc/include/fstab.h @@ -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 . + + fstab.h + Filesystem table. + +*******************************************************************************/ + +#ifndef INCLUDE_FSTAB_H +#define INCLUDE_FSTAB_H + +#include + +#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