diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Makefile | 228 | ||||
l--------- | core/[.c | 1 | ||||
-rw-r--r-- | core/basename.c | 81 | ||||
-rw-r--r-- | core/cat.c | 95 | ||||
-rw-r--r-- | core/chmod.c | 124 | ||||
-rw-r--r-- | core/chown.c | 72 | ||||
-rw-r--r-- | core/date.c | 60 | ||||
-rw-r--r-- | core/dirname.c | 62 | ||||
-rw-r--r-- | core/echo.c | 42 | ||||
-rw-r--r-- | core/ed.c | 164 | ||||
-rw-r--r-- | core/false.c | 31 | ||||
-rw-r--r-- | core/head.c | 92 | ||||
-rw-r--r-- | core/link.c | 59 | ||||
-rw-r--r-- | core/ln.c | 89 | ||||
-rw-r--r-- | core/ls.c | 270 | ||||
-rw-r--r-- | core/mkdir.c | 77 | ||||
-rw-r--r-- | core/more.c | 121 | ||||
-rw-r--r-- | core/mv.c | 83 | ||||
-rw-r--r-- | core/printf.c | 55 | ||||
-rw-r--r-- | core/rm.c | 74 | ||||
-rw-r--r-- | core/sleep.c | 63 | ||||
-rw-r--r-- | core/tail.c | 98 | ||||
-rw-r--r-- | core/test.c | 128 | ||||
-rw-r--r-- | core/true.c | 31 | ||||
-rw-r--r-- | core/uname.c | 90 | ||||
-rw-r--r-- | core/unlink.c | 57 | ||||
-rw-r--r-- | core/vi.c | 242 |
27 files changed, 0 insertions, 2589 deletions
diff --git a/core/Makefile b/core/Makefile deleted file mode 100644 index 4207ea3..0000000 --- a/core/Makefile +++ /dev/null @@ -1,228 +0,0 @@ -# -# Copyright (C) 2022 Ferass EL HAFIDI -# Copyright (C) 2022 Leah Rowe -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program 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 General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. -# - -include ../config.mk - -# Commands -# ======== - -all: clean $(CORE) - -ver: - echo "#ifndef VERSION_H" > version.h - echo "#define VERSION_H" >> version.h - echo "#define COMPILETIME \"$$(git show --no-patch --pretty=format:%H)\"" >> version.h - echo >> version.h - echo "#endif" >> version.h - -head: head.o - mkdir -p bin - $(CC) $(CFLAGS) head.o -o bin/head - -cat: cat.o - mkdir -p bin - $(CC) $(CFLAGS) cat.o -o bin/cat - -ls: ls.o - mkdir -p bin - $(CC) $(CFLAGS) ls.o -o bin/ls - -date: date.o - mkdir -p bin - $(CC) $(CFLAGS) date.o -o bin/date - -yes: yes.o - mkdir -p bin - $(CC) $(CFLAGS) yes.o -o bin/yes - -mkdir: mkdir.o - mkdir -p bin - $(CC) $(CFLAGS) mkdir.o -o bin/mkdir - -echo: echo.o - mkdir -p bin - $(CC) $(CFLAGS) echo.o -o bin/echo - -true: true.o - mkdir -p bin - $(CC) $(CFLAGS) true.o -o bin/true - -false: false.o - mkdir -p bin - $(CC) $(CFLAGS) false.o -o bin/false - -sleep: sleep.o - mkdir -p bin - $(CC) $(CFLAGS) sleep.o -o bin/sleep - -printf: printf.o - mkdir -p bin - $(CC) $(CFLAGS) printf.o -o bin/printf - -uname: uname.o - mkdir -p bin - $(CC) $(CFLAGS) uname.o -o bin/uname - -link: link.o - mkdir -p bin - $(CC) $(CFLAGS) link.o -o bin/link - -ln: ln.o - mkdir -p bin - $(CC) $(CFLAGS) ln.o -o bin/ln - -chmod: chmod.o - mkdir -p bin - $(CC) $(CFLAGS) chmod.o -o bin/chmod - -basename: basename.o - mkdir -p bin - $(CC) $(CFLAGS) basename.o -o bin/basename - -unlink: unlink.o - mkdir -p bin - $(CC) $(CFLAGS) unlink.o -o bin/unlink - -mv: mv.o - mkdir -p bin - $(CC) $(CFLAGS) mv.o -o bin/mv - -rm: rm.o - mkdir -p bin - $(CC) $(CFLAGS) rm.o -o bin/rm - -more: more.o - mkdir -p bin - $(CC) $(CFLAGS) more.o -o bin/more - -chown: chown.o - mkdir -p bin - $(CC) $(CFLAGS) chown.o -o bin/chown - -tail: tail.o - mkdir -p bin - $(CC) $(CFLAGS) tail.o -o bin/tail - -[: test.o - mkdir -p bin - $(CC) $(CFLAGS) test.o -o bin/\[ - -test: test.o - mkdir -p bin - $(CC) $(CFLAGS) test.o -o bin/test - -ed: ed.o - mkdir -p bin - $(CC) $(CFLAGS) ed.o -o bin/ed - -dirname: dirname.o - mkdir -p bin - $(CC) $(CFLAGS) dirname.o -o bin/dirname - -vi: vi.o - mkdir -p bin - $(CC) $(CFLAGS) vi.o -o bin/vi - -ex: vi.o - mkdir -p bin - $(CC) $(CFLAGS) vi.o -o bin/ex - -clean: - rm -f *.o version.h - rm -Rf bin - -# Utilities -# ========= - -head.o: ver - $(CC) $(CFLAGS) $(NOLINKER) head.c -o head.o - -cat.o: ver - $(CC) $(CFLAGS) $(NOLINKER) cat.c -o cat.o - -ls.o: ver - $(CC) $(CFLAGS) $(NOLINKER) ls.c -o ls.o - -date.o: ver - $(CC) $(CFLAGS) $(NOLINKER) date.c -o date.o - -yes.o: ver - $(CC) $(CFLAGS) $(NOLINKER) yes.c -o yes.o - -mkdir.o: ver - $(CC) $(CFLAGS) $(NOLINKER) mkdir.c -o mkdir.o - -echo.o: ver - $(CC) $(CFLAGS) $(NOLINKER) echo.c -o echo.o - -true.o: ver - $(CC) $(CFLAGS) $(NOLINKER) true.c -o true.o - -false.o: ver - $(CC) $(CFLAGS) $(NOLINKER) false.c -o false.o - -sleep.o: ver - $(CC) $(CFLAGS) $(NOLINKER) sleep.c -o sleep.o - -printf.o: ver - $(CC) $(CFLAGS) $(NOLINKER) printf.c -o printf.o - -uname.o: ver - $(CC) $(CFLAGS) $(NOLINKER) uname.c -o uname.o - -link.o: ver - $(CC) $(CFLAGS) $(NOLINKER) link.c -o link.o - -ln.o: ver - $(CC) $(CFLAGS) $(NOLINKER) ln.c -o ln.o - -chmod.o: ver - $(CC) $(CFLAGS) $(NOLINKER) chmod.c -o chmod.o - -basename.o: ver - $(CC) $(CFLAGS) $(NOLINKER) basename.c -o basename.o - -unlink.o: ver - $(CC) $(CFLAGS) $(NOLINKER) unlink.c -o unlink.o - -mv.o: ver - $(CC) $(CFLAGS) $(NOLINKER) mv.c -o mv.o - -rm.o: ver - $(CC) $(CFLAGS) $(NOLINKER) rm.c -o rm.o - -more.o: ver - $(CC) $(CFLAGS) $(NOLINKER) more.c -o more.o - -chown.o: ver - $(CC) $(CFLAGS) $(NOLINKER) chown.c -o chown.o - -tail.o: ver - $(CC) $(CFLAGS) $(NOLINKER) tail.c -o tail.o - -test.o: ver - $(CC) $(CFLAGS) $(NOLINKER) test.c -o test.o - -ed.o: ver - $(CC) $(CFLAGS) $(NOLINKER) ed.c -o ed.o - -dirname.o: ver - $(CC) $(CFLAGS) $(NOLINKER) dirname.c -o dirname.o - -vi.o: ver - $(CC) $(CFLAGS) $(NOLINKER) vi.c -o vi.o diff --git a/core/[.c b/core/[.c deleted file mode 120000 index aeebb26..0000000 --- a/core/[.c +++ /dev/null @@ -1 +0,0 @@ -test.c
\ No newline at end of file diff --git a/core/basename.c b/core/basename.c deleted file mode 100644 index 6939a0c..0000000 --- a/core/basename.c +++ /dev/null @@ -1,81 +0,0 @@ -/* basename - return non-directory portion of STRING - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <libgen.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *argv[]) { - int argument, status; - long unsigned int i; - char *basenamestr; - - if (argc < 2) { - printUsage(); - return 1; - } - basenamestr = basename(argv[1]); - if (argc == 3) { - for (i = 1; i <= strlen(argv[2]); ++i) { - if (argv[1][strlen(argv[1]) - i] == - argv[2][strlen(argv[2]) - i]) - /* Check */ - status = 0; - else status = 1; - } - - if (status == 0) { - for (i = 1; i <= strlen(argv[2]); ++i) { - if (argv[1][strlen(argv[1]) - i] == - argv[2][strlen(argv[2]) - i]) { - /* Replace with spaces for now ... */ - argv[1][strlen(argv[1]) - i] = ' '; - } // = '\0'; - } - } - } - - printf("%s\n", basenamestr); - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: basename string [suffix]\n\n" - "Return non-directory portion of <string>.\n\n", COMPILETIME); -} diff --git a/core/cat.c b/core/cat.c deleted file mode 100644 index ef0d5d8..0000000 --- a/core/cat.c +++ /dev/null @@ -1,95 +0,0 @@ -/* cat - concatenate a file to stdout - * Copyright (C) 2022 Ferass EL HAFIDI - * Copyright (C) 2022 Leah Rowe - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#define _POSIX_C_SOURCE 200809L -#include <fcntl.h> -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int cat(int flides, int unbuffered); -void printUsage(); - -int main(int argc, char *const argv[]) { - int file, argument, i = 1, unbuffered; - - while ((argument = getopt(argc, argv, "u")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - else if (argument == 'u') - unbuffered = 1; - } - - - if (argc < 2) { - cat(STDIN_FILENO, unbuffered); - } - - for (i = 1; i != argc; i++) { - if (strcmp(argv[i], "-u")) { - if (strcmp(argv[i], "-")) file = open(argv[i], O_RDONLY); - else file = STDIN_FILENO; - if (file == -1) return errno; /* Something went wrong */ - cat(file, unbuffered); - close(file); - } - } - return 0; -} - -int cat(int fildes, int unbuffered) { - char s[4096]; - FILE *filstr; - size_t length; - if (unbuffered) while ((length = read(fildes, s, 4096)) > 0) - write(STDOUT_FILENO, s, length); - else { - filstr = fdopen(fildes, "r"); - while ((length = fread(s, 4096, 1, filstr)) > 0) - fwrite(s, length, 1, stdout); - } - if (errno) return errno; - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: cat [file]\n\n" - "Concatenate <file> to standard output.\n" - "When no file is specified or file is '-', read standard input\n\n" - "\t-u\tPrint unbuffered\n", COMPILETIME); -} diff --git a/core/chmod.c b/core/chmod.c deleted file mode 100644 index f0ca999..0000000 --- a/core/chmod.c +++ /dev/null @@ -1,124 +0,0 @@ -/* chmod - change file modes - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <sys/stat.h> -#include <unistd.h> -#include <stdio.h> -#include <stdlib.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, i = 0; - mode_t modes, owner_modes, group_modes, other_modes; - if (argc == 1) { - printUsage(); - return 1; - } - while ((argument = getopt(argc, argv, "")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - } - /* I know there's a better way of doing it, please let me know if you - * know a better way of doing it - */ - if (argv[1][i] == '0') i++; - switch (argv[1][i]) { - /* Owner modes */ - case '7': - owner_modes = S_IRWXU; - break; - case '4': - owner_modes = S_IRUSR; - break; - case '2': - owner_modes = S_IWUSR; - break; - case '1': - owner_modes = S_IXUSR; - break; - default: - printUsage(); - return 1; - } - i++; - switch (argv[1][i]) { - /* Group modes */ - case '7': - group_modes = S_IRWXG; - break; - case '4': - group_modes = S_IRGRP; - break; - case '2': - group_modes = S_IWGRP; - break; - case '1': - group_modes = S_IXGRP; - break; - default: - printUsage(); - return 1; - } - i++; - switch (argv[1][i]) { - /* Other modes */ - case '7': - other_modes = S_IRWXO; - break; - case '4': - other_modes = S_IROTH; - break; - case '2': - other_modes = S_IWOTH; - break; - case '1': - other_modes = S_IXOTH; - break; - default: - printUsage(); - return 1; - } - - chmod(argv[2], owner_modes | group_modes | other_modes); - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: chmod mode file\n\n" - "Change file modes.\n\n", COMPILETIME); -} diff --git a/core/chown.c b/core/chown.c deleted file mode 100644 index 5acd1fd..0000000 --- a/core/chown.c +++ /dev/null @@ -1,72 +0,0 @@ -/* chown - change the file ownership - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <errno.h> -#include <unistd.h> -#include <stdio.h> -#include <pwd.h> -#include <stdlib.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument; - struct passwd *user; - if (argc == 1) { - printUsage(); - return 1; - } - while ((argument = getopt(argc, argv, "")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - } - if ((user = getpwnam(argv[1])) == NULL && (user = getpwuid(strtol(argv[1], NULL, 10))) == NULL) - return errno; /* User doesn't exist */ - /* User found! */ - else if ((user = getpwnam(argv[1])) != NULL) - chown(argv[2], user->pw_uid, user->pw_gid); - else if ((user = getpwuid(strtol(argv[1], NULL, 10))) != NULL) - chown(argv[2], user->pw_uid, user->pw_gid); - if (errno) return errno; - - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: chown owner file\n\n" - "Change file ownership.\n\n", COMPILETIME); -} diff --git a/core/date.c b/core/date.c deleted file mode 100644 index 6c8af39..0000000 --- a/core/date.c +++ /dev/null @@ -1,60 +0,0 @@ -/* date - Show the date and time. - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <time.h> -#include <stdio.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -void printUsage(); - -int main(int argc, char *argv[]) { - time_t epoch = time(NULL); - struct tm* date = localtime(&epoch); - char date_s[31]; - const char *format; - if (argc == 2 && argv[1][0] == '+') { - argv[1]++; - format = argv[1]; - } - else { - format = "%a %b %e %H:%M:%S %Z %Y"; - } - strftime(date_s, 31, format, date); - printf("%s\n", date_s); - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: date [+format]\n\n" - "Output the date and time.\n\n", COMPILETIME); -} diff --git a/core/dirname.c b/core/dirname.c deleted file mode 100644 index e51f264..0000000 --- a/core/dirname.c +++ /dev/null @@ -1,62 +0,0 @@ -/* dirname - return directory portion of STRING - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <libgen.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *argv[]) { - int argument, status; - long unsigned int i; - char *dirnamestr; - - if (argc != 2) { - printUsage(); - return 1; - } - dirnamestr = dirname(argv[1]); - - printf("%s\n", dirnamestr); - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: dirname string\n\n" - "Return directory portion of <string>.\n\n", COMPILETIME); -} diff --git a/core/echo.c b/core/echo.c deleted file mode 100644 index 919501a..0000000 --- a/core/echo.c +++ /dev/null @@ -1,42 +0,0 @@ -/* echo - write strings to stdout - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> - -int getopt(int argc, char *const argv[], const char *optstring); - -int main(int argc, char *const argv[]) { - int argument, i = 1; - - for (i = 1; i != argc; i++) { - printf("%s ", argv[i]); - } - printf("\n"); - return 0; -} diff --git a/core/ed.c b/core/ed.c deleted file mode 100644 index 7a2b37a..0000000 --- a/core/ed.c +++ /dev/null @@ -1,164 +0,0 @@ -/* ed - edit text - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdio.h> -#include <unistd.h> -#include <fcntl.h> -#include <errno.h> -#include <signal.h> -#include <string.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -void printUsage(); -void print_error(); -size_t c_append(char buffer[4096]); - -int main(int argc, char *argv[]) { - int argument, i = 0, fildes; - char buffer[4096], *edit_pathname, *prompt_string = "", - command_string[4096], *error = ""; - int help_mode = 0; - struct sigaction signal_action; - - while ((argument = getopt(argc, argv, "p:")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - else if (argument == 'p') - prompt_string = optarg; - } - argc -= optind; - argv += optind; - - signal_action.sa_handler = SIG_IGN; - sigemptyset(&signal_action.sa_mask); - sigaction(SIGINT, &signal_action, NULL); - - setvbuf(stdout, NULL, _IONBF, 0); - for (;;) { - write(STDOUT_FILENO, prompt_string, strlen(prompt_string)); - //if (fgets(command_string, 4096, stdin) == NULL) return errno; - if (read(STDIN_FILENO, command_string, 4096) == -1) return errno; - switch (command_string[i]) { /* Addresses */ - case '.': - case '$': - case '\'': - case '+': - case '-': - /* FALLTHROUGH */ - ++i; - print_error((error = "not implemented"), help_mode); - continue; - } - switch (command_string[i]) { /* Commands */ - case 'e': /* Edit Command */ - if (i < 0) { - print_error((error = "unexpected address"), help_mode); - continue; - } - if (strlen(command_string) > 2 && i == 0) { - edit_pathname = command_string + 2; - edit_pathname[strlen(edit_pathname) - 1] = '\0'; - } else if (!edit_pathname) { - print_error((error = "no pathname given"), help_mode); - continue; - } - fildes = open(edit_pathname, O_RDONLY | O_CREAT); - if (errno) return errno; - printf("%ld\n", read(fildes, buffer, 4096)); - if (fildes) close(fildes); - continue; - case 'a': - c_append(buffer); - continue; - case 'w': - if (strtok(command_string, " ") != NULL && strtok(NULL, " ") != NULL - && i == 0) { - edit_pathname = command_string + 2; - edit_pathname[strlen(edit_pathname) - 1] = '\0'; - fildes = open(edit_pathname, O_RDWR | O_CREAT); - } else if (edit_pathname) { - fildes = open(edit_pathname, O_RDWR); - } else { - if (i != 0) - print_error((error = "unexpected address"), help_mode); - else - print_error((error = "no pathname given"), help_mode); - continue; - } - if (errno) return errno; - printf("%ld\n", write(fildes, buffer, strlen(buffer))); - if (fildes) close(fildes); - continue; - case 'q': - if (fildes) close(fildes); - return 0; - case 'H': /* Help mode */ - if (i < 0) { - print_error((error = "unexpected address"), help_mode); - continue; - } - /* If help_mode was equal to 1, set it to 0 and vice-versa. */ - help_mode = !help_mode; - case 'h': /* Help Command */ - /* FALLTHROUGH */ - print_error(error, help_mode + 2); /* Print the last encountered error. */ - continue; - default: /* Invalid/Not implemented command */ - print_error((error = "invalid or not implemented"), help_mode); - } - } -} - -size_t c_append(char buffer[4096]) { - char *tempbuf = NULL; - size_t bufsize = 0, bytes_read = 0; - - while ((bytes_read += getline(&tempbuf, &bufsize, stdin)) > 0) { - if (!strcmp(tempbuf, ".\n")) break; - strcat(buffer, tempbuf); - } - return bytes_read; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: ed [-p prompt_string]\n\n" - "Edit text.\n\n" - "\t-p prompt_string\tAppend a prompt string.\n", COMPILETIME); -} - -void print_error(const char *error, int help_mode) { - if (help_mode <= 1) puts("?"); - if (help_mode) puts(error); -} diff --git a/core/false.c b/core/false.c deleted file mode 100644 index 10f020f..0000000 --- a/core/false.c +++ /dev/null @@ -1,31 +0,0 @@ -/* false - return false value - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -int main() { - return 1; -} diff --git a/core/head.c b/core/head.c deleted file mode 100644 index 236c74d..0000000 --- a/core/head.c +++ /dev/null @@ -1,92 +0,0 @@ -/* head - copy the first part of files - * Copyright (C) 2022 Ferass EL HAFIDI - * Copyright (C) 2022 Leah Rowe - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <fcntl.h> -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include <stdlib.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -/* Functions Prototypes */ -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, i = 1, lines, lines_printed; - FILE *file; - - char s[4096]; - - while ((argument = getopt(argc, argv, "n:")) != -1) { - if (argument == '?' || argument == ':') { - printUsage(); - return 1; - } - else if (argument == 'n') { - lines = strtol(optarg, NULL, 10); - if (errno) return errno; - } - else - lines = 10; - } - if (argc < 2) { - while (read(STDIN_FILENO, s, 4096) > 0) - printf("%s", s); - } - if (!lines) lines = 10; - for (i = 1; i != argc; i++) { - if (strcmp(argv[i], "-n")) { - if (strcmp(argv[i], "-")) file = fopen(argv[i], "r"); - else while (read(STDIN_FILENO, s, 4096) > 0) printf("%s", s); - if (file == NULL) return errno; /* Something went wrong */ - for (lines_printed = 1; lines_printed <= lines; lines_printed++) { - if (fgets(s, 4096, file) != NULL) - printf("%s", s); - else if (errno) return errno; - } - fclose(file); - } - else i++; - } - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: head [-n number] [file]\n\n" - "Copy file to standard output until <number> lines.\n\n" - "\t-n number\tNumber of lines to be copied to standard output\n", - COMPILETIME); -} diff --git a/core/link.c b/core/link.c deleted file mode 100644 index b6fbff2..0000000 --- a/core/link.c +++ /dev/null @@ -1,59 +0,0 @@ -/* link - link 2 files using the link() function - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument; - - if (argc == 1) { - printUsage(); - return 1; - } - if (argc >= 2) { - link(argv[1], argv[2]); - if (errno) return errno; - } - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: link file1 file2\n\n" - "Link <file1> to <file2> using the link() function.\n\n", COMPILETIME); -} diff --git a/core/ln.c b/core/ln.c deleted file mode 100644 index 6852cf6..0000000 --- a/core/ln.c +++ /dev/null @@ -1,89 +0,0 @@ -/* ln - link files - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument; - char param[256], *params = "s", *buffer; - - if (argc == 1) { - printUsage(); - return 1; - } - while ((argument = getopt(argc, argv, params)) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - param[argument] = argument; - } - - for (int i = 1; i < argc - 1; i++) { - if (argv[argc - 1][0] == '-') argc--; - if (argv[i][0] != '-') { - if (param['f']) remove(argv[argc - 1]); - if (param['s']) symlink(argv[i], argv[argc - 1]); - /* The -P option is the default behavior (at least on musl), - * so no if statement. - */ - else if (param['L']) { - readlink(argv[i], buffer, strlen(buffer)); /* Read the link */ - if (errno) return errno; - link(buffer, argv[argc - 1]); - } - else link(argv[i], argv[argc - 1]); - - if (errno) return errno; - } - } - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: ln [-fs] [-P|-L] source_file target_file\n\n" - "Link files.\n\n" - "\t-s\tCreate symbolic links instead of hard links\n" - "\t-f\tIf <target_file>Â exists, override the file\n" - "\t-P\tIf <source_file> names a symbolic link, create a hard link to the " - "symbolic link itself (default)\n" - "\t-L\tIf <source_file> names a symbolic link, create a hard link to the " - "file referenced by the symbolic link\n", COMPILETIME); -} diff --git a/core/ls.c b/core/ls.c deleted file mode 100644 index 11aa31e..0000000 --- a/core/ls.c +++ /dev/null @@ -1,270 +0,0 @@ -/* ls - list files and directories in the given path - * Copyright (C) 2022 Ferass EL HAFIDI - * Copyright (C) 2022 Leah Rowe - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <string.h> -#include <dirent.h> -#include <fcntl.h> -#include <stdio.h> -#include <errno.h> -#include <stdlib.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <pwd.h> -#include <grp.h> -#include <time.h> -#include <sys/ioctl.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -char param[256]; -int getopt(int argc, char *const argv[], const char *optstring); -int ls(char *path); -void printUsage(char *params); - -int main(int argc, char *argv[]) { - int status = 0; - int success = 0; - int argument, i; - char* params = "aACR1imlpgno"; - - for(i=0; i<256; i++) { - param[i]=0; - } - - while ((argument = getopt(argc, argv, params)) != -1) { - if (argument == '?') { - printUsage(params); - return 1; - } - param[argument] = argument; - - if (argument=='C') { - param['1'] = 0; - param['m'] = 0; - } - else if (argument=='1' || argument=='g' || argument=='n') { - param['m'] = 0; - param['C'] = 0; - } - else if (argument=='m') { - param['1'] = 0; - param['C'] = 0; - } - if (argument=='o' || argument=='n' || argument=='g') { - param['l'] = 'l'; - } - } - if (status) { - if(!param['1']) printf("\n"); - return status; - } - - if (!param['C'] && !param['m'] && !param['1']) - param['1'] = '1'; - - if (param['l'] || param['g']) { - param['m'] = 0; - param['C'] = 0; - param['1'] = '1'; - } - - for (i = 1; i < argc; i++) { - if ((success |= (argv[i][0] != '-' ? 1 : 0))) { - if (!strcmp(argv[i],".")) status |= ls("./"); - else status |= ls(argv[i]); - } - } - - i = success ? status : ls("./"); - if(!param['1']) - printf("\n"); - - return i; -} - -void printUsage(char *params) { - printf("Ferass' Base System. (%s)\n\n" - "Usage: " - "ls [-%s] [directory] ...\n\n" - "Print <directory>'s contents to standard output.\n\n" - "\t-a\tInclude names starting with a dot, including '.' and '..'\n" - "\t-A\tSame as `-a` but don't include '.' and '..'\n" - "\t-C\tPrint in columns\n" - "\t-1\tPrint in lines\n" - "\t-R\tRecursively list directories\n" - "\t-i\tFor each file, write its serial number\n" - "\t-m\tList names followed by a comma and space character\n" - "\t-l\tDo not follow symbolic links named as operands and " - "write in long format (unfinished)\n" - "\t-p\tShow '/' after each name if that name is a directory\n" - "\t-g\tEnable the -l option but don't print the file owner's name\n" - "\t-n\tEnable the -l option but print the file owner and group's numeric " - "UID and GID instead of their name\n" - "\t-o\tEnable the -l option but don't print the file group's name\n", - COMPILETIME, params); -} - -int ls(char *path) { - int file, dotname, cwdname, prevdir, dot; - long unsigned int cols_used; - DIR *directory, *subdirectory; - struct stat file_status; - struct dirent *dirtree; - struct winsize columns; - char *name, file_moddate[256]; - char file_modes[] = "----------"; - directory = opendir(path); - - if (param['C']) { - ioctl(STDOUT_FILENO, TIOCGWINSZ, &columns); - cols_used = 0; - } - - if (directory == NULL) { - file = open(path, O_RDONLY); - if (file == -1) return errno; - printf("%s\n", path); - close(file); - return errno; - } - - if (param['R']) { - printf("The previous ls -R implementation had so much " - "flaws that it got removed from the ls codebase.\n"); - return 0; - } - while ((dirtree = readdir(directory)) != NULL) { - name = dirtree->d_name; - - cwdname = strcmp(name, ".") ? 0 : 1; - prevdir = strcmp(name, "..") ? 0 : 1; - dotname = (name[0]=='.' && !cwdname && !prevdir) ? 1 : 0; - dot = dotname | prevdir | cwdname; - - if (dot && !param['a'] && !param['A']) continue; - if ((cwdname || prevdir) && param['A']) continue; - - if (param['i']) printf("%lu ", dirtree->d_ino); - if (param['l']) { - char *fullpath = malloc(strlen(path) + strlen(name) + 2); - if (fullpath) { - strcpy(fullpath, path); - if (path[strlen(path) - 1] != '/') strcat(fullpath, "/"); - strcat(fullpath, name); - } - lstat(fullpath, &file_status); - /* File modes */ - /* File type */ - { - if (S_ISBLK(file_status.st_mode)) file_modes[0] = 'b'; - else if (S_ISCHR(file_status.st_mode)) file_modes[0] = 'c'; - else if (S_ISDIR(file_status.st_mode)) file_modes[0] = 'd'; - else if (S_ISFIFO(file_status.st_mode)) file_modes[0] = 'p'; - else if (S_ISREG(file_status.st_mode)) file_modes[0] = '-'; - else if (S_ISLNK(file_status.st_mode)) file_modes[0] = 'l'; - else file_modes[0] = 's'; - } - /* User */ - { - if (file_status.st_mode & S_IRUSR) file_modes[1] = 'r'; - if (file_status.st_mode & S_IWUSR) file_modes[2] = 'w'; - if (file_status.st_mode & S_IXUSR) file_modes[3] = 'x'; - } - /* Group */ - { - if (file_status.st_mode & S_IRGRP) file_modes[4] = 'r'; - if (file_status.st_mode & S_IWGRP) file_modes[5] = 'w'; - if (file_status.st_mode & S_IXGRP) file_modes[6] = 'x'; - } - /* Others */ - { - if (file_status.st_mode & S_IROTH) file_modes[7] = 'r'; - if (file_status.st_mode & S_IWOTH) file_modes[8] = 'w'; - if (file_status.st_mode & S_IXOTH) file_modes[9] = 'x'; - } - printf("%s ", file_modes); - /* Number of links */ - printf("%lu ", file_status.st_nlink); - /* Owner name/uid */ - if (!param['g']) - /* This recursiveness is needed for whatever reason, - * else it doesn't work... - */ - if (!param['n']) - printf("%s ", getpwuid(file_status.st_uid)->pw_name); - if (param['n']) printf("%u ", file_status.st_uid); - /* Group name/gid */ - if (!param['o']) - if (!param['n']) printf("%s ", getgrgid(file_status.st_gid)->gr_name); - if (param['n']) printf("%u ", file_status.st_gid); - /* Size of file */ - printf("%lu ", file_status.st_size); - /* Date and time */ - int strftime_status = strftime(file_moddate, sizeof(file_moddate), - "%b %e %H:%MM", localtime(&file_status.st_mtime)); - /* It should be st_mtim right? */ - file_moddate[strlen(file_moddate) - 1] = 0; /* Remove newline */ - printf("%s ", strftime_status ? file_moddate : "<strftime() returned 0>"); - free(fullpath); - } - printf("%s", name); - if (param['p']) { - char *fullpath = malloc(strlen(path) + strlen(name) + 2); - if (fullpath) { - strcpy(fullpath, path); - if (path[strlen(path) - 1] != '/') strcat(fullpath, "/"); - strcat(fullpath, name); - } - stat(fullpath, &file_status); - if (S_ISDIR(file_status.st_mode)) printf("/"); - free(fullpath); - } - if (param['C']) { - if (cols_used < (long unsigned int)((columns.ws_col) / 12)) { - for (long unsigned int i = strlen(name); i < 11; i++) printf(" "); - cols_used++; - } - if (cols_used == (long unsigned int)((columns.ws_col) / 12)) { - printf("\n"); - cols_used = 0; - } - } - else if (param['1']) - printf("\n"); - else if (param['m']) - printf(", "); - } - closedir(directory); - - return errno; -} diff --git a/core/mkdir.c b/core/mkdir.c deleted file mode 100644 index 609ddc2..0000000 --- a/core/mkdir.c +++ /dev/null @@ -1,77 +0,0 @@ -/* mkdir - create directories - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <sys/stat.h> -#include <errno.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - char param[256]; - int success, argument, i = 1; - char *basenamestr[256]; - - if (argc == 1) { - printUsage(); - return 1; - } - - while ((argument = getopt(argc, argv, "p")) != -1) { - if (argument == '?') { - printUsage(); - return 0; - } - param[argument] = argument; - } - - for (; i < argc; i++) { - printf("Warning: -p ignored.\n"); - if (argv[i][0] != '-') - success = !mkdir(argv[i], S_IRWXU | S_IRWXG | S_IRWXO) ? 0 : 1; - if (success == 1) { - return errno; - } - - } - - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: mkdir [-p] directory ...\n\n" - "Create a directory.\n\n", COMPILETIME); -} diff --git a/core/more.c b/core/more.c deleted file mode 100644 index 2db41c4..0000000 --- a/core/more.c +++ /dev/null @@ -1,121 +0,0 @@ -/* more - display files in a page-by-page basis. - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include <termios.h> -#include <sys/ioctl.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int i = 0, argument, success, read_file; - long int columns, lines; - char buffer[4096], cmd; - struct winsize w; - FILE *file; - struct termios oldattr, newattr; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - if (getenv("LINES")) lines = strtol(getenv("LINES"), NULL, 10) - 1; - else lines = w.ws_row - 1; - if (getenv("COLUMNS")) columns = strtol(getenv("COLUMNS"), NULL, 10); - else columns = w.ws_col - 1; - - while ((argument = getopt(argc, argv, "")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - } - if (argc != 2) { - printUsage(); return 1; - } - file = fopen(argv[1], "r"); - if (errno) return errno; - success = 0; - while (!success) { - if (!read_file) read_file = 1; /* 1 => read on a page-by-page basis - * 0 => don't read at all - * 2 => read on a line-by-line basis - */ - for (i = 0; i < lines && read_file != 0; i++) { - if (fgets(buffer, 4096, file) != NULL) { - printf("%s", buffer); - } - else if (errno) return errno; - else { - success = 1; - break; - } /* EOF, end while and for loop */ - if (read_file == 2) { - break; - } - } - read_file = 0; - - /* Get a character */ - tcgetattr(0, &oldattr); /* Get previous terminal parameters */ - newattr = oldattr; - newattr.c_lflag &= ~( ICANON | ECHO ); /* Disable ECHO and ICANON */ - tcsetattr( 0, TCSANOW, &newattr); /* Set net parameters */ - cmd = getchar(); - tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */ - - if (errno) return errno; - switch (cmd) { - case 'q': - return 0; - case 'h': - printUsage(); - read_file = 0; - break; - case ' ': - read_file = 1; /* page-by-page */ - break; - default: - read_file = 2; /* line-by-line */ - break; - } - } - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: more file ...\n\n" - "Display a file on a page-by-page basis.\n\n", COMPILETIME); -} diff --git a/core/mv.c b/core/mv.c deleted file mode 100644 index 7124c3f..0000000 --- a/core/mv.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mv - move files. - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <fcntl.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, file; - char cmd, param[256]; - setvbuf(stdout, NULL, _IONBF, 0); - for (int i = 0; i < 256; i++) param[i] = 0; /* Initialise param, - * very important. */ - while ((argument = getopt(argc, argv, "if")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - param[argument] = argument; - if (argument == 'f') param['i'] = 0; - if (argument == 'i') param['f'] = 0; - } - if (!param['f']) param['i'] = 'i'; - if (argc < 3) { - printUsage(); - return 1; - } - if ((file = open(argv[2], O_RDONLY)) != -1 && param['i']) { - printf("File exists, override it? (y/n) "); - read(STDIN_FILENO, &cmd, 3); - if (cmd == 'n' || cmd == 'N') { - close(file); - return 0; - } - } - close(file); /* In case it hasn't been closed */ - - rename(argv[1], argv[2]); /* Technically, moving files == renaming files */ - if (errno) return errno; - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: mv [-if] file1 file2\n\n" - "Move <file1> to <file2>.\n\n" - "\t-i\tIf <file2> exists, ask for confirmation\n" - "\t-f\tNever ask for confirmation\n", COMPILETIME); -} diff --git a/core/printf.c b/core/printf.c deleted file mode 100644 index a82426c..0000000 --- a/core/printf.c +++ /dev/null @@ -1,55 +0,0 @@ -/* printf - write formatted strings to stdout - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, i = 1; - - if (argc == 1) { - printUsage(); - return 1; - } - printf(argv[1], argv[2] ? argv[2] : ""); - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: printf format [string]\n\n" - "Write formatted strings to stdout.\n\n", COMPILETIME); -} diff --git a/core/rm.c b/core/rm.c deleted file mode 100644 index 568f27b..0000000 --- a/core/rm.c +++ /dev/null @@ -1,74 +0,0 @@ -/* rm - remove files. - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, i = 1; - char param[256]; - - while ((argument = getopt(argc, argv, "Rr")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - param[argument] = argument; - } - if (argc <= 1) { - printUsage(); - return 1; - } - - for (; i < argc; i++) { - if (!param['r'] || !param['R']) unlink(argv[i]); - else remove(argv[i]); /* TODO: Actually - * recursively remove - * the directory */ - } - - if (errno) return errno; - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: rm [-Rr] file ...\n\n" - "Remove <file>.\n\n" - "\t-R\tRecursively remove the directory tree\n" - "\t-r\tEnable the -R option\n", COMPILETIME); -} diff --git a/core/sleep.c b/core/sleep.c deleted file mode 100644 index 4f36251..0000000 --- a/core/sleep.c +++ /dev/null @@ -1,63 +0,0 @@ -/* sleep - suspend execution for an interval - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -void printUsage(); - -int main(int argc, char *argv[]) { - long unsigned int seconds; - - if (argc == 2) { - seconds = strtol(argv[1], NULL, 10); - if (!seconds) { - printUsage(); - return 1; - } - sleep(seconds); - } - else if (argc == 1) { - printUsage(); - return 1; - } - - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: sleep seconds\n\n" - "Suspend execution for <seconds> seconds.\n\n", COMPILETIME); -} diff --git a/core/tail.c b/core/tail.c deleted file mode 100644 index 0a0cebc..0000000 --- a/core/tail.c +++ /dev/null @@ -1,98 +0,0 @@ -/* tail - copy the last part of files - * Copyright (C) 2022 Ferass EL HAFIDI - * Copyright (C) 2022 Leah Rowe - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <fcntl.h> -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include <stdlib.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -/* Functions Prototypes & Variables */ -extern char *optarg; -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, i = 1, lines, file_lines; - FILE *file; - - char s[4096]; - - while ((argument = getopt(argc, argv, "n:")) != -1) { - if (argument == '?' || argument == ':') { - printUsage(); - return 1; - } - else if (argument == 'n') { - lines = strtol(optarg, NULL, 10); - if (errno) return errno; - } - else - lines = 10; - } - if (argc < 2) { - while (read(STDIN_FILENO, s, 4096) > 0) - printf("%s", s); - } - if (!lines) lines = 10; - for (i = 1; i != argc; i++) { - if (strcmp(argv[i], "-n")) { - if (strcmp(argv[i], "-")) file = fopen(argv[i], "r"); - else while (read(STDIN_FILENO, s, 4096) > 0) printf("%s", s); - if (file == NULL) return errno; /* Something went wrong */ - while (fgets(s, 4096, file) != NULL) - file_lines++; /* Get number of lines */ - fclose(file); - file_lines = file_lines - lines; - if (strcmp(argv[i], "-")) file = fopen(argv[i], "r"); - while (fgets(s, 4096, file) != NULL) { - if (errno) return errno; - if (file_lines == 0) printf("%s", s); - else file_lines--; - } - } - else i++; - } - - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: head [-n number] [file]\n\n" - "Copy file to standard output until <number> lines.\n\n" - "\t-n number\tNumber of lines to be copied to standard output\n", - COMPILETIME); -} diff --git a/core/test.c b/core/test.c deleted file mode 100644 index af4d072..0000000 --- a/core/test.c +++ /dev/null @@ -1,128 +0,0 @@ -/* [ - evaluate expression - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#define _POSIX_C_SOURCE 200809L -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include <libgen.h> -#include <sys/stat.h> -#include <stdint.h> -#include <string.h> -#include <stdlib.h> - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -/* For readability reasons, define true, false */ -int true = 0; -int false = 1; - -int main(int argc, char *argv[]) { - int argument; - char param[256]; - struct stat file_status; - - for (int i = 0; i < 256; i++) param[i] = 0; - - if (!strcmp(basename(argv[0]), "[")) { - if (strcmp(argv[argc - 1], "]")) { - /* FIXME: Is printing an error message POSIX-compliant? */ - fprintf(stderr, "[: No matching ]\n"); - return 1; - } - argc--; - argv[argc] = 0; - } - - if (argc < 2) return false; - if (!strcmp(argv[1], "!")) { - true = 1; - false = 0; - argc--; - argv++; - } - if (argc == 3) { - param[(uint8_t)argv[1][1]] = argv[1][1]; - /* Files */ - stat(argv[2], &file_status); - if (!errno) { - if (param['b'] && S_ISBLK(file_status.st_mode) != 0) return true; - else if (param['c'] && S_ISCHR(file_status.st_mode) != 0) return true; - else if (param['d'] && S_ISDIR(file_status.st_mode) != 0) return true; - /* Just return true, if -e is used. If it was false, stat() would - * have failed. If it failed, there's a check about which value - * to return. Return false if -e is used, otherwise, return errno - */ - else if (param['e']) return true; - else if (param['f'] && S_ISREG(file_status.st_mode) != 0) return true; - else if (param['g'] && file_status.st_mode & S_ISGID) return true; - else if ((param['h'] || param['L']) - && S_ISLNK(file_status.st_mode) != 0) return true; - else if (param['p'] && S_ISFIFO(file_status.st_mode) != 0) return true; - /* TODO: Better check for -r/-w. */ - else if (param['r'] && (file_status.st_mode & S_IRUSR - || file_status.st_mode & S_IROTH)) return true; - else if (param['S'] && S_ISSOCK(file_status.st_mode) != 0) return true; - else if (param['s'] && file_status.st_size > 0) return true; - else if (param['u'] && S_ISDIR(file_status.st_mode) - && file_status.st_mode & S_ISUID) return true; - else if (param['w'] && (file_status.st_mode & S_IWUSR - || file_status.st_mode & S_IWOTH)) return true; - else if (param['x'] && (file_status.st_mode & S_IXUSR - || file_status.st_mode & S_IXOTH)) return true; - } - else if (errno && param['e']) return false; - if (param['t']) { - if (isatty(strtol(argv[2], NULL, 10))) return true; - else return false; - } - /* Strings */ - if (param['n'] && strlen(argv[2])) return true; - else if (param['z'] && !strlen(argv[2])) return true; - } - - else if (argc == 4) { - if (!strcmp(argv[2], "=") && !strcmp(argv[1], argv[3])) return true; - else if (!strcmp(argv[2], "!=") && strcmp(argv[1], argv[3])) return true; - else if (!strcmp(argv[2], "-eq") && !strcmp(argv[1], argv[3])) return true; - else if (!strcmp(argv[2], "-ne") && strcmp(argv[1], argv[3])) return true; - else if (!strcmp(argv[2], "-gt") && - strtol(argv[1], NULL, 10) > strtol(argv[3], NULL, 10)) return true; - else if (!strcmp(argv[2], "-ge") && - strtol(argv[1], NULL, 10)>= strtol(argv[3], NULL, 10)) return true; - else if (!strcmp(argv[2], "-lt") && - strtol(argv[1], NULL, 10) < strtol(argv[3], NULL, 10)) return true; - else if (!strcmp(argv[2], "-le") && - strtol(argv[1], NULL, 10)<= strtol(argv[3], NULL, 10)) return true; - } - - /* TODO: Better error handling. */ - return false; -} diff --git a/core/true.c b/core/true.c deleted file mode 100644 index 4eb43cf..0000000 --- a/core/true.c +++ /dev/null @@ -1,31 +0,0 @@ -/* true - return true value - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -int main() { - return 0; -} diff --git a/core/uname.c b/core/uname.c deleted file mode 100644 index db91d50..0000000 --- a/core/uname.c +++ /dev/null @@ -1,90 +0,0 @@ -/* uname - return system name - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <sys/utsname.h> -#include <errno.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument; - struct utsname name; - char param[256]; - for (int i = 0; i < 256; i++) param[i] = 0; - - while ((argument = getopt(argc, argv, "amnrsv")) != -1) { - if (argument == '?') { - printUsage(); - return 1; - } - param[argument] = argument; - if (argument == 'a') { - param['s'] = 's'; - param['n'] = 'n'; - param['r'] = 'r'; - param['v'] = 'v'; - param['m'] = 'm'; - } - } - - uname(&name); - if (errno) return errno; - if (argc > 1) { - if (param['s']) printf("%s ", name.sysname); - if (param['n']) printf("%s ", name.nodename); - if (param['r']) printf("%s ", name.release); - if (param['v']) printf("%s ", name.version); - if (param['m']) printf("%s", name.machine); - printf("\n"); - } - else - printf("%s\n", name.sysname); - - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: uname [-amnrsv] \n\n" - "Return system name.\n\n" - "\t-a\tSame as -mnrsv\n" - "\t-m\tWrite the hardware's architecture\n" - "\t-n\tWrite the network node hostname\n" - "\t-r\tWrite the operating system release\n" - "\t-s\tWrite the name of the operating system implementation\n" - "\t-v\tWrite the current version of this release of the operating " - "system implementation\n", COMPILETIME); -} diff --git a/core/unlink.c b/core/unlink.c deleted file mode 100644 index 7e2ed6f..0000000 --- a/core/unlink.c +++ /dev/null @@ -1,57 +0,0 @@ -/* unlink - call the unlink() function - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <unistd.h> -#include <stdio.h> -#include <errno.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -int getopt(int argc, char *const argv[], const char *optstring); -void printUsage(); - -int main(int argc, char *const argv[]) { - int argument, i = 1; - - if (argc != 2) { - printUsage(); - return 1; - } - unlink(argv[1]); - if (errno) return errno; - return 0; -} - -void printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: unlink file\n\n" - "Call the unlink() function.\n\n", COMPILETIME); -} diff --git a/core/vi.c b/core/vi.c deleted file mode 100644 index 97fa02f..0000000 --- a/core/vi.c +++ /dev/null @@ -1,242 +0,0 @@ -/* vi/ex - visual/text editor - * Copyright (C) 2022 Ferass EL HAFIDI - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include <stdio.h> -#include <unistd.h> -#include <fcntl.h> -#include <errno.h> -#include <signal.h> -#include <string.h> -#include <time.h> -#include <termios.h> -#include <sys/ioctl.h> -#include <libgen.h> -#include "version.h" - -#ifndef COMPILETIME -#define COMPILETIME -#endif - -/*** ANSI VT100 escape codes ***/ -/* Colors */ -#define ANSI_WHITEBG "\33[38;5;0;48;5;255m" -#define ANSI_RESET "\33[m" -/* Other escape codes */ -#define ANSI_CLEARLINE "\33[2K\r" -#define ANSI_CLEARALL "\e[1;1H\e[2J" - -/********** Common - used by both vi and ex **********/ -int print_string(char *string); - -int print_string(char *string) { - return write(STDOUT_FILENO, string, strlen(string)); -} - -/********************** ex mode **********************/ -int _ex_main(int argc, char *argv[]); - -int _ex_main(int argc, char *argv[]) { - print_string("<Not implemented>\n"); - return 0; /* Not implemented yet. */ -} - -/********************** vi mode **********************/ -struct cursor_pos { - /* Neat way to keep track of the cursor's position. */ - unsigned int x; - unsigned int y; -}; - -int _vi_main(int argc, char *argv[]); -int _vi_set_cursor_pos(unsigned int x, unsigned int y); -int _vi_init(unsigned int lines, unsigned int columns, - char buffer[BUFSIZ], struct cursor_pos cposition, long int offset_position, - FILE *filstr); -void _vi_printUsage(); -int _vi_print_buffer_line(char s[BUFSIZ], unsigned int numlines); - -int _vi_main(int argc, char *argv[]) { - int argument, offset; - char cmd; - FILE *filstr; - char buffer[BUFSIZ]; - struct sigaction signal_action; - struct termios oldattr, newattr; - struct winsize w; - struct cursor_pos cposition, fposition; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - unsigned int lines = w.ws_row - 1; - unsigned int columns = w.ws_col - 1; - - while ((argument = getopt(argc, argv, "")) != -1) { - if (argument == '?') { - _vi_printUsage(); - return 1; - } - } - argc -= optind; - argv += optind; - - signal_action.sa_handler = SIG_IGN; - sigemptyset(&signal_action.sa_mask); - sigaction(SIGINT, &signal_action, NULL); - setvbuf(stdout, NULL, _IONBF, 0); - /* Initialise vi */ - if (argv[0]) { - filstr = fopen(argv[0], "r+"); - if (errno) { - if (errno != ENOENT) - return errno; - } - } _vi_init(lines, columns, buffer, cposition, offset, filstr); - for (;;) { - /* Get a character */ - tcgetattr(0, &oldattr); /* Get previous terminal parameters */ - newattr = oldattr; - newattr.c_lflag &= ~( ICANON | ECHO ); /* Disable ECHO and ICANON */ - tcsetattr(0, TCSANOW, &newattr); /* Set net parameters */ - cmd = getchar(); - switch (cmd) { - /*********************** Moving the cursor ***********************/ - case 'k': /* up */ - if (fposition.y > 0) fposition.y--; - else break; - if (cposition.y != 1) - _vi_set_cursor_pos(cposition.x, (cposition.y = cposition.y - 1)); - else if (fposition.y == 0) { - _vi_set_cursor_pos(1, 1); - print_string(ANSI_CLEARALL); /* Clear screen */ - fposition.y--; /* Say, 118 */ - for (unsigned int i = lines - 1 /* 79 */; i > 0; i--) { - _vi_print_buffer_line(buffer, fposition.y - i); /* 120 - 78 */ - } - _vi_set_cursor_pos(cposition.x, cposition.y); - } - break; - case 'j': /* down */ - if (cposition.y != (lines - 1) && fposition.y++) - _vi_set_cursor_pos(cposition.x, (cposition.y = cposition.y + 1)); - else if (cposition.y == lines - 1) { - _vi_set_cursor_pos(1, 1); - print_string(ANSI_CLEARALL); /* Clear screen */ - fposition.y++; /* Say, 120 */ - for (unsigned int i = lines - 1 /* 79 */; i > 0; i--) { - _vi_print_buffer_line(buffer, fposition.y - i); /* 120 - 78 */ - } - _vi_set_cursor_pos(cposition.x, cposition.y); - } - break; - case 'l': /* right */ - if (cposition.x != columns && fposition.x++) - _vi_set_cursor_pos((cposition.x = cposition.x + 1), cposition.y); - break; - case 'h': /* left */ - if (cposition.x != 1 && fposition.x--) - _vi_set_cursor_pos((cposition.x = cposition.x - 1), cposition.y); - break; - /************************* Command mode: *************************/ - case ':': - _vi_set_cursor_pos(1, lines); - print_string(ANSI_CLEARLINE ":"); - tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */ - cmd = getchar(); - switch (cmd) { - case '\33': - _vi_set_cursor_pos(1, lines); - print_string(ANSI_CLEARLINE); - break; - case 'q': - print_string(ANSI_CLEARALL); /* Clear screen */ - _vi_set_cursor_pos(1, 1); /* Reset the cursor position */ - if (filstr) fclose(filstr); - return 0; - case '\n': - break; - default: - _vi_set_cursor_pos(1, lines); - print_string("unknown command"); - break; - } - _vi_set_cursor_pos(cposition.x, cposition.y); - } - tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */ - } - return 0; -} - -int _vi_init(unsigned int lines, unsigned int columns, - char buffer[BUFSIZ], struct cursor_pos cposition, - long int offset_position, FILE *filstr) { - int status = 0; - print_string(ANSI_CLEARALL); /* Clear screen */ - _vi_set_cursor_pos(1, 1); - if (filstr) { - fread(buffer, BUFSIZ, 1, filstr); - //if (fgets(buffer, 4096, filstr) != NULL) { - // offset_position = ftell(filstr); - // fputs(buffer, stdout); - //} - for (unsigned int i = 0; i < lines - 1; i++) - _vi_print_buffer_line(buffer, i); - } else { - for (unsigned int i = 1; i < (lines ); i++) - fputs("~\n", stdout); - status = 2; /* No file */ - } _vi_set_cursor_pos((cposition.x = 1), (cposition.y = 1)); - return status; -} - -int _vi_set_cursor_pos(unsigned int x, unsigned int y) { - return printf("\033[%u;%uH", (y), (x)); -} - -void _vi_printUsage() { - printf("Ferass' Base System. (%s)\n\n" - "Usage: vi [filename]\n\n" - "Visual editor.\n\n", COMPILETIME); -} - -int _vi_print_buffer_line(char s[BUFSIZ], unsigned int numlines) { - unsigned int lines_found, chars; - /* Search for <numlines> - 1 lines and skip them entirely. */ - for (chars = 0, lines_found = 0; - s[chars] && lines_found < numlines; s[chars] == '\n' && lines_found++, chars++) - /* TODO: Use strchr(). */ ; - /* Print the last line found. */ - for (; s[chars] && s[chars] != '\n'; chars++) putchar((int)s[chars]) - /* TODO: Do not print character-by-character. */ ; - putchar((int)'\n'); - return 0; -} - -/************ main() ************/ -int main(int argc, char *argv[]) { - if (!strcmp(basename(argv[0]), "ex")) return _ex_main(argc, argv); - else /* Default to vi */ return _vi_main(argc, argv); - return 0; /* Never reached. */ -} |