Changeset c9f5e24f in mainline


Ignore:
Timestamp:
2008-08-24T06:49:51Z (16 years ago)
Author:
Tim Post <echo@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ab5a45e0
Parents:
65267f6
Message:

Add a basic cat, cat —help shows future options

Location:
uspace/app/bdsh/cmds/modules/cat
Files:
2 edited

Legend:

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

    r65267f6 rc9f5e24f  
    1 /* Automatically generated by mknewcmd on Wed Aug 13 15:41:09 PHT 2008
    2  * This is machine generated output. The author of mknewcmd claims no
    3  * copyright over the contents of this file. Where legally permitted, the
    4  * contents herein are donated to the public domain.
     1/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
     2 * All rights reserved.
    53 *
    6  * You should apply any license and copyright that you wish to this file,
    7  * replacing this header in its entirety. */
     4 * Redistribution and use in source and binary forms, with or without
     5 * modification, are permitted provided that the following conditions are met:
     6 *
     7 * Redistributions of source code must retain the above copyright notice, this
     8 * list of conditions and the following disclaimer.
     9 *
     10 * Redistributions in binary form must reproduce the above copyright notice,
     11 * this list of conditions and the following disclaimer in the documentation
     12 * and/or other materials provided with the distribution.
     13 *
     14 * Neither the name of the original program's authors nor the names of its
     15 * contributors may be used to endorse or promote products derived from this
     16 * software without specific prior written permission.
     17 *
     18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28 * POSSIBILITY OF SUCH DAMAGE.
     29 */
    830
    931#include <stdio.h>
    1032#include <stdlib.h>
     33#include <unistd.h>
    1134#include <getopt.h>
     35#include <string.h>
     36#include <fcntl.h>
    1237
    1338#include "config.h"
     
    1944
    2045static char *cmdname = "cat";
     46#define CAT_VERSION "0.0.1"
     47#define CAT_DEFAULT_BUFLEN 1024
     48
     49static char *cat_oops = "That option is not yet supported\n";
    2150
    2251static struct option long_options[] = {
    23         { "help",               no_argument,            0,      'h' },
    24         { "version",    no_argument,            0,      'v' },
    25         { "file",               required_argument,      0,      'f' },
     52        { "help", no_argument, 0, 'h' },
     53        { "version", no_argument, 0, 'v' },
     54        { "head", required_argument, 0, 'H' },
     55        { "tail", required_argument, 0, 't' },
     56        { "buffer", required_argument, 0, 'b' },
     57        { "more", no_argument, 0, 'm' },
    2658        { 0, 0, 0, 0 }
    2759};
     
    3062void * help_cmd_cat(unsigned int level)
    3163{
    32         printf("This is the %s help for '%s'.\n",
    33                 level ? EXT_HELP : SHORT_HELP, cmdname);
     64        if (level == HELP_SHORT) {
     65                printf("`%s' shows the contents of files\n", cmdname);
     66        } else {
     67                help_cmd_cat(HELP_SHORT);
     68                printf(
     69                "Usage:  %s [options] <file1> [file2] [...]\n"
     70                "Options:\n"
     71                "  -h, --help       A short option summary\n"
     72                "  -v, --version    Print version information and exit\n"
     73                "  -H, --head ##    Print only the first ## bytes\n"
     74                "  -t, --tail ##    Print only the last ## bytes\n"
     75                "  -b, --buffer ##  Set the read buffer size to ##\n"
     76                "  -m, --more       Pause after each screen full\n"
     77                "Currently, %s is under development, some options don't work.\n",
     78                cmdname, cmdname);
     79        }
     80
    3481        return CMD_VOID;
     82}
     83
     84unsigned int cat_file(const char *fname, size_t blen)
     85{
     86        int fd, bytes = 0, count = 0, reads = 0;
     87        off_t total = 0;
     88        char *buff = NULL;
     89
     90        if (-1 == (fd = open(fname, O_RDONLY))) {
     91                printf("Unable to open %s\n", fname);
     92                return 1;
     93        }
     94
     95        total = lseek(fd, 0, SEEK_END);
     96        lseek(fd, 0, SEEK_SET);
     97        if (NULL == (buff = (char *) malloc(blen + 1))) {
     98                close(fd);
     99                printf("Unable to allocate enough memory to read %s\n",
     100                        fname);
     101                return 1;
     102        }
     103
     104        do {
     105                memset(buff, 0, sizeof(buff));
     106                bytes = read(fd, buff, blen);
     107                if (bytes > 0) {
     108                        count += bytes;
     109                        if (bytes < blen)
     110                                buff[bytes] = '\0';
     111                        printf(buff);
     112                        reads++;
     113                }
     114        } while (bytes > 0);
     115
     116        close(fd);
     117        if (bytes == -1) {
     118                printf("Error reading %s\n", fname);
     119                free(buff);
     120                return 1;
     121        }
     122
     123        /* Debug stuff, newline not added purposefully */
     124        printf("** %s is a file with the size of %ld bytes\n",
     125                fname, total);
     126        printf( "** %d bytes were read in a buffer of %d bytes in %d reads\n",
     127                count, blen, reads);
     128        printf("** Read %s\n", count == total ? "Succeeded" : "Failed");
     129        free(buff);
     130
     131        return 0;
    35132}
    36133
     
    38135int * cmd_cat(char **argv)
    39136{
    40         unsigned int argc;
    41         unsigned int i;
     137        unsigned int argc, i, ret = 0, buffer = 0;
    42138        int c, opt_ind;
    43139
    44         /* Count the arguments */
    45140        for (argc = 0; argv[argc] != NULL; argc ++);
    46141
    47         printf("%s %s\n", TEST_ANNOUNCE, cmdname);
    48         printf("%d arguments passed to %s\n", argc - 1, cmdname);
    49 
    50         for (i = 1; i < argc; i++)
    51                 printf("[%d] -> %s\n", i, argv[i]);
    52 
    53142        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    54                 c = getopt_long(argc, argv, "f:hv", long_options, &opt_ind);
     143                c = getopt_long(argc, argv, "hvmH:t:b:", long_options, &opt_ind);
    55144                switch (c) {
    56145                case 'h':
    57                         help_cmd_cat(HELP_SHORT);
     146                        help_cmd_cat(HELP_LONG);
     147                        return CMD_SUCCESS;
     148                case 'v':
     149                        printf("%s\n", CAT_VERSION);
     150                        return CMD_SUCCESS;
     151                case 'H':
     152                        printf(cat_oops);
     153                        return CMD_FAILURE;
     154                case 't':
     155                        printf(cat_oops);
     156                        return CMD_FAILURE;
     157                case 'b':
     158                        printf(cat_oops);
    58159                        break;
    59                 case 'v':
    60                         printf("I have no version, sorry.\n");
    61                         break;
    62                 case 'f':
    63                         printf("File : %s\n", optarg);
    64                         break;
     160                case 'm':
     161                        printf(cat_oops);
     162                        return CMD_FAILURE;
    65163                }
    66164        }
    67165
    68         printf("argc = %d, optind = %d\n", argc, optind);
     166        argc -= optind;
    69167
    70         return CMD_SUCCESS;
     168        if (argc < 1) {
     169                printf("%s - incorrect number of arguments. Try `%s --help'\n",
     170                        cmdname, cmdname);
     171                return CMD_FAILURE;
     172        }
     173
     174        i = optind;
     175
     176        if (buffer <= 0)
     177                buffer = CAT_DEFAULT_BUFLEN;
     178
     179        for (i = optind; argv[i] != NULL; i++)
     180                ret += cat_file(argv[i], buffer);
     181
     182        if (ret)
     183                return CMD_FAILURE;
     184        else
     185                return CMD_SUCCESS;
    71186}
    72187
  • uspace/app/bdsh/cmds/modules/cat/cat.h

    r65267f6 rc9f5e24f  
    44/* Prototypes for the cat command, excluding entry points */
    55
     6extern unsigned int cat_file(const char *, size_t);
    67
    78#endif /* CAT_H */
Note: See TracChangeset for help on using the changeset viewer.