diff --git a/bin/cat/Main.cpp b/bin/cat/Main.cpp index b7ae411e3..59ed07339 100644 --- a/bin/cat/Main.cpp +++ b/bin/cat/Main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Niek Linnenbank + * Copyright (C) 2009 Niek Linnenbank, 2015 Dan Rulos, 2016 Alvaro Stagg [alvarostagg@openmailbox.org] * * 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 @@ -16,81 +16,204 @@ */ #include -#include #include -#include -#include #include -#include +#include +#include -int cat(char *prog, char *file) -{ - char buf[1025]; - int fd, e; +#define VERSION "v0.5.8" - /* Clear buffer. */ - memset(buf, 0, sizeof(buf)); +static int hflag, vflag, ret = 0; +static int flags[8]; - /* Attempt to open the file first. */ - if ((fd = open(file, O_RDONLY)) < 0) - { - printf("%s: failed to open '%s': %s\r\n", - prog, file, strerror(errno)); - return EXIT_FAILURE; - } - /* Read contents. */ - while (1) - { - e = read(fd, buf, sizeof(buf) - 1); - switch (e) - { - /* Error occurred. */ - case -1: - printf("%s: failed to read '%s': %s\r\n", - prog, file, strerror(errno)); - close(fd); - return EXIT_FAILURE; - - /* End of file. */ - case 0: - close(fd); - return EXIT_SUCCESS; - - /* Print out results. */ - default: - buf[e] = 0; - printf("%s", buf); - break; - } - } - return EXIT_FAILURE; -} +static void usage(void); +static void version(void); +static void cat(char *argv[], int argc, int flags[8]); +static void read(const char *fileName, int flags[8]); +static const char *fileName; + +int get_file_size(const char *fileName); int main(int argc, char **argv) { - int ret = EXIT_SUCCESS, result; - - /* Verify command-line arguments. */ if (argc < 2) { - printf("usage: %s FILE1 FILE2 ...\r\n", - argv[0]); - return EXIT_FAILURE; + printf("Uso: %s [OPCIÓN]... [FICHERO]...\n", argv[0]); + return 1; } - refreshMounts(0); - /* Cat all given files. */ - for (int i = 0; i < argc - 1; i++) + for (int i = 0; i <= 8; i++) + flags[i] = 0; + + for (int i = 1; i < argc; i++) { - /* Perform cat. */ - result = cat(argv[0], argv[i + 1]); - - /* Update exit code if needed. */ - if (result > ret) + if (strcmp(argv[i], "--help") == 0) + { + hflag = 1; + usage(); + break; + } + else if ((strcmp(argv[i], "--version") == 0)) + { + vflag = 1; + version(); + break; + } + else if ((strcmp(argv[i], "-n") == 0) || (strcmp(argv[i], "--number") == 0)) + flags[0] = 1; + else if ((strcmp(argv[i], "-E") == 0) || (strcmp(argv[i], "--show-ends") == 0) || (strcmp(argv[i], "-e") == 0)) + flags[1] = 1; + else if ((strcmp(argv[i], "-t") == 0) || (strcmp(argv[i], "-T") == 0) || (strcmp(argv[i], "--show-tabs") == 0)) + flags[2] = 1; + else if ((strcmp(argv[i], "-b") == 0) || (strcmp(argv[i], "--number-nonblank") == 0)) { - ret = result; + flags[0] = 0; + flags[3] = 1; } } - /* Done. */ + + if (hflag || vflag) + return ret; + else + cat(argv, argc, flags); + return ret; } + +static void usage(void) +{ + printf("Modo de empleo: cat [OPCIÓN]... [FICHERO]...\n\n"); + + printf(" -b, --number-nonblank No enumera las lineas en blanco, remplaza -n.\n"); + printf(" -e Lo mismo que -E.\n"); + printf(" -E, --show-ends Muestra un $ al final de cada linea.\n"); + printf(" -n, --number Enumera cada linea.\n"); + printf(" -t Lo mismo que -T.\n"); + printf(" -T, --show-tabs Muestra los caracteres de tabulación como ^I\n"); + printf(" --help Muestra esta ayuda y finaliza.\n"); + printf(" --version Informa de la versión y finaliza.\n\n"); + + printf("AmayaOS Coreutils %s (C) 2016 AmayaOS Team & Others\n", VERSION); + printf("Licencia GNU GPL v3 .\n"); + printf("Reportar errores a traves de http://bugs.amayaos.com o amaya@amayaos.com\n"); +} + +static void version(void) +{ + printf("cat (AmayaOS CoreUtils) %s\n", VERSION); + printf("Copyright © 2016 AmayaOS, Inc.\n"); + printf("Licencia GPLv3+: GPL de GNU versión 3 o posterior\n"); + printf(".\n"); + printf("Esto es software libre: usted es libre de cambiarlo y redistribuirlo.\n"); + printf("No hay NINGUNA GARANTÍA, hasta donde permite la ley.\n\n"); + + printf("Escrito por Dan Rulos y Alvaro Stagg.\n"); +} + +static void cat(char *argv[], int argc, int flags[8]) +{ + int fd; + + for (int i = 1; i < argc; i++) + { + if ((fd = open(argv[i], O_RDONLY)) < 0) + continue; + else + { + fileName = argv[i]; + break; + } + } + + if (fd < 0) + { + printf("%s: No existe el fichero o el directorio.\n", argv[0]); + ret = 1; + } + else + { + read(fileName, flags); + close(fd); + } +} + +static void read(const char *fileName, int flags[8]) +{ + FILE *fp = fopen(fileName, "r"); + int lines = 0; + + if (fp == NULL) + { + printf("cat: %s: No existe el fichero o el directorio.\n", fileName); + ret = 1; + } + else + { + char cnt[get_file_size(fileName)]; + int n_bytes = fread(cnt, 1, sizeof(cnt), fp); + + if (flags[0] == 1 || (flags[3] == 1 && cnt[0] != '\n')) + { + lines++; + printf(" %d ", lines); + } + + unsigned int i = 0; + + for (i = 0; i < n_bytes - 1; i++) + { + printf("%c", cnt[i]); + + if (flags[0] == 1) + { + if (cnt[i] == '\n') + { + lines++; + printf(" %d ", lines); + } + } + + if (flags[1] == 1) + { + if (cnt[i + 1] == '\n') + printf("$"); + } + + if (flags[2] == 1) + { + if (cnt[i] == '\t') + { + printf("^I"); + continue; + } + } + + if (flags[3] == 1) + { + if (cnt[i] == '\n') + { + if (cnt[i + 1] == '\n') + continue; + else + { + lines++; + printf(" %d ", lines); + } + } + } + } + + /* Imprime el último caracter perdido */ + printf("%c\n", cnt[i]); + } +} + +int get_file_size(const char *fileName) +{ + struct stat st; + + if (stat(fileName, &st) < 0) + return -1; + else + return st.st_size; +} diff --git a/bin/clear/Main.cpp b/bin/clear/Main.cpp new file mode 100644 index 000000000..b764641d4 --- /dev/null +++ b/bin/clear/Main.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Alvaro Stagg [alvarostagg@openmailbox.org] + * + * 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 . + */ + +#include + +int main(int argc, char* argv[]) +{ + char str[] = {0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a, '\0'}; + printf("%s", str); + return 0; +} diff --git a/bin/clear/SConscript b/bin/clear/SConscript new file mode 100644 index 000000000..3b1f45687 --- /dev/null +++ b/bin/clear/SConscript @@ -0,0 +1,22 @@ +# +# Copyright (C) 2010 Niek Linnenbank, 2016 Alvaro Stagg [alvarostagg@openmailbox.org] +# +# 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 . +# + +Import('build_env') + +env = build_env.Clone() +env.UseLibraries([ 'libposix', 'liballoc', 'libstd', 'libexec', 'libarch', 'libipc' ]) +env.TargetProgram('clear', 'Main.cpp', env['bin']) diff --git a/lib/libposix/string.h b/lib/libposix/string.h index 6bc0ebaa1..8419c41c4 100644 --- a/lib/libposix/string.h +++ b/lib/libposix/string.h @@ -1,6 +1,6 @@ /* - * Copyright (C) 2009 Niek Linnenbank - * + * Copyright (C) 2009 Niek Linnenbank, 2016 Alvaro Stagg [alvarostagg@openmailbox.org] + * * 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 @@ -131,23 +131,23 @@ extern C int strcpy(char *dest, const char *src); extern C int strncpy(char *dest, const char *src, size_t sz); /** - * Copy src to string dst of size siz. At most siz-1 characters + * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). * @note This function is copied from OpenBSD-4.3 * @param dst Destination string * @param src Source string * @param siz size_t of dst buffer - * @return strlen(src); if retval >= siz, truncation occurred. + * @return strlen(src); if retval >= siz, truncation occurred. */ extern C size_t strlcpy(char *dst, const char *src, size_t siz); /** * @brief Concatenate two strings. - * + * * The strcat() function shall append a copy of the string pointed to * by s2 (including the terminating NUL character) to the end of the * string pointed to by s1. - * + * * @param dest Destination string. * @param src Source string. * @return The strcat() function shall return s1; no return value is @@ -164,7 +164,7 @@ extern C char * strcat(char *dest, const char *src); * byte of s2 overwrites the NUL character at the end of s1. A terminating * NUL character is always appended to the result. If copying takes place * between objects that overlap, the behavior is undefined. - * + * * @param dest Destination string. * @param src Source string. * @param n Number of character to concatenate. @@ -211,6 +211,16 @@ extern C char * strchr(const char *s, int c); */ extern C char * strrchr(const char *s, int c); +/** + * @param ptr Pointer to the block of memory where the search is performed. + * @param value Value to be located. The value is passed as an int, but the + * function performs a byte per byte search using the unsigned char + * conversion of this value. + * @param num Number of bytes to be analyzed. size_t is an unsigned integral type. + * @return A pointer to the first occurrence of value in the block of memory pointed + * by ptr. If the value is not found, the function returns a null pointer. + */ +extern C void * memchr(const void *ptr, int value, size_t num); /** * @} */ diff --git a/lib/libposix/string/memchr.cpp b/lib/libposix/string/memchr.cpp new file mode 100644 index 000000000..5882e97c5 --- /dev/null +++ b/lib/libposix/string/memchr.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2016 Alvaro Stagg [alvarostagg@openmailbox.org] + * + * 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 . + */ + +#include "string.h" + +void *memchr(const void *ptr, int value, size_t num) +{ + const unsigned char *p_s = ptr; + + while (num-- != 0) + if ((unsigned char)value == *p_s++) + return (void*)(p_s - 1); + + return NULL; +}