Ignore:
Timestamp:
2012-05-24T14:24:15Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b4ca0a9c
Parents:
a58bc8b
Message:

Bdsh/cat can read from stdin as well

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/cat/cat.c

    ra58bc8b ra33706e  
    6262static sysarg_t console_rows = 0;
    6363static bool should_quit = false;
     64static bool dash_represents_stdin = false;
    6465
    6566static console_ctrl_t *console = NULL;
     
    7374        { "more", no_argument, 0, 'm' },
    7475        { "hex", no_argument, 0, 'x' },
     76        { "stdin", no_argument, 0, 's' },
    7577        { 0, 0, 0, 0 }
    7678};
     
    9395                "  -m, --more       Pause after each screen full\n"
    9496                "  -x, --hex        Print bytes as hex values\n"
     97                "  -s  --stdin      Treat `-' in file list as standard input\n"
    9598                "Currently, %s is under development, some options don't work.\n",
    9699                cmdname, cmdname);
     
    172175        off64_t file_size = 0, length = 0;
    173176
    174         fd = open(fname, O_RDONLY);
     177        bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0);
     178
     179        if (reading_stdin) {
     180                fd = fileno(stdin);
     181                /* Allow storing the whole UTF-8 character. */
     182                blen = STR_BOUNDS(1);
     183        } else {
     184                fd = open(fname, O_RDONLY);
     185        }
    175186        if (fd < 0) {
    176187                printf("Unable to open %s\n", fname);
     
    207218
    208219        do {
    209                 bytes = read(fd, buff + copied_bytes, (
    210                         (length != CAT_FULL_FILE && length - (off64_t)count <= (off64_t)(blen - copied_bytes)) ?
    211                         (size_t)(length - count) :
    212                         (blen - copied_bytes) ) );
     220                size_t bytes_to_read;
     221                if (reading_stdin) {
     222                        bytes_to_read = 1;
     223                } else {
     224                        if ((length != CAT_FULL_FILE)
     225                            && (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) {
     226                                bytes_to_read = (size_t) (length - count);
     227                        } else {
     228                                bytes_to_read = blen - copied_bytes;
     229                        }
     230                }
     231                bytes = read(fd, buff + copied_bytes, bytes_to_read);
    213232                bytes += copied_bytes;
    214233                copied_bytes = 0;
     
    242261                        reads++;
    243262                }
     263
     264                if (reading_stdin) {
     265                        fflush(stdout);
     266                }
    244267        } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));
    245268
     
    284307
    285308        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    286                 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind);
     309                c = getopt_long(argc, argv, "xhvmH:t:b:s", long_options, &opt_ind);
    287310                switch (c) {
    288311                case 'h':
     
    318341                        hex = true;
    319342                        break;
     343                case 's':
     344                        dash_represents_stdin = true;
     345                        break;
    320346                }
    321347        }
Note: See TracChangeset for help on using the changeset viewer.