Annotation of solidite/utils/cat.c, revision 1.1

1.1     ! user        1: /* Copyright (c) 2026 The Solidite Developers
        !             2:  *
        !             3:  * This program is free software: you can redistribute it and/or modify it under
        !             4:  * the terms of the GNU Affero General Public License as published by the Free
        !             5:  * Software Foundation, either version 3 of the License, or (at your option)
        !             6:  * any later version.
        !             7:  *
        !             8:  * This program is distributed in the hope that it will be useful, but WITHOUT
        !             9:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
        !            10:  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
        !            11:  * details.
        !            12:  *
        !            13:  * You should have received a copy of the GNU Affero General Public License
        !            14:  * along with this program. If not, see <https://www.gnu.org/licenses/>. */
        !            15: 
        !            16: #include <fcntl.h>
        !            17: #include <locale.h>
        !            18: #include <stdio.h>
        !            19: #include <string.h>
        !            20: #include <unistd.h>
        !            21: 
        !            22: #include "include/flags.h"
        !            23: 
        !            24: int main(int argc, char* argv[]) {
        !            25:   setlocale(LC_ALL, "");
        !            26: 
        !            27:   unsigned long options = 1;
        !            28:   /* options[0]=buffered (-u not encountered)
        !            29:    * options[1]=don't process options (-- encountered)
        !            30:    * options[2]=regular stream found
        !            31:    * options[3]=error occurred (will be return code)
        !            32:    * decimal 1 same as 0b0001, ISO C99 does not accept 0bXXX*/
        !            33:   char buf[8192];
        !            34:   ssize_t n;
        !            35: 
        !            36:   for (int i = 1; i < argc; ++i) {
        !            37:     if (strcmp(argv[i], "--") == 0)
        !            38:     set_flag(&options, 1);
        !            39:     else if (strcmp(argv[i], "-u") == 0 && read_flag(&options, 1) == 0) {
        !            40:       // implementation is already unbuffered so don't do anything
        !            41:       set_flag(&options, 0);
        !            42:       continue;
        !            43:     } else if (strcmp(argv[i], "-") == 0) {
        !            44:       set_flag(&options, 2);
        !            45:       while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 0)
        !            46:         (void)write(STDOUT_FILENO, buf, n);
        !            47:     } else {
        !            48:       set_flag(&options, 2);
        !            49:       int fd = open(argv[i], O_RDONLY);
        !            50:       if (fd == -1) {
        !            51:         const char ERROR_MESSAGE[] = "failed to open input\n";
        !            52:         (void)write(STDERR_FILENO, ERROR_MESSAGE, sizeof(ERROR_MESSAGE));
        !            53:              set_flag(&options, 3);
        !            54:              continue;
        !            55:       }
        !            56:       while ((n = read(fd, buf, sizeof(buf))) > 0)
        !            57:         (void)write(STDOUT_FILENO, buf, n);
        !            58:       close(fd);
        !            59:     }
        !            60:   }
        !            61: 
        !            62:   if (read_flag(&options, 2) == 0) {
        !            63:     while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 0)
        !            64:       (void)write(STDOUT_FILENO, buf, n);
        !            65:   } // in the case if a stream wasn't provided such as
        !            66:     // cat being called with "cat -u" or "cat" or "cat --" or "cat -- -u", etc.
        !            67:   return read_flag(&options, 3);
        !            68: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>