Thread: Finished my cat app...now with flags!

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    93

    Finished my cat app...now with flags!

    I've got it to accept flags and no flags.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    #include <unistd.h>
    
    
    void CookedCatArgs(char **argv);
    void CookedBuffer(FILE *fp);
    void GoingRaw(char **argv);
    void RawCat(FILE *fp);
    
    
    char *FileName;
    uint8_t eFlag, sFlag, tFlag, seFlag;
    
    
    int main(int argc, char **argv) {
        int8_t GetOptions;
        
        eFlag = sFlag = tFlag = seFlag = 0;
        
        while((GetOptions = getopt(argc, argv, "est")) != -1)
            switch(GetOptions) {
                case 'e':
                    eFlag = 1;
                    break;
                    
                case 's':
                    sFlag = 1;
                    break;
                    
                case 't':
                    tFlag = 1;
                    break;
                    
                default:
                    fprintf(stderr, "Usage: cat [-est] [file ...]\n");
                    return 1;
            }
        
        argv += optind;
        
        if(eFlag || sFlag || tFlag || seFlag) {
            CookedCatArgs(argv);
        }
        
        else
            GoingRaw(argv);
        
        return 0;
    }
    
    
    void CookedCatArgs(char **argv) {
        FILE *cooked_fp;
        
        cooked_fp = stdin;
        FileName = "stdin";
        
        do {
            if(*argv) {
                if(!strcmp(*argv, "-"))
                    cooked_fp = stdin;
                
                else if((cooked_fp = fopen(*argv, "r")) == NULL) {
                    fprintf(stderr, "%s not found.\n", *argv);
                    ++argv;
                    continue;
                }
                FileName = *argv++;
            }
            CookedBuffer(cooked_fp);
            
            if(cooked_fp == stdin)
                clearerr(cooked_fp);
            else
                fclose(cooked_fp);
        }
        while(*argv);
    }
    
    
    void CookedBuffer(FILE *fp) {
        int8_t CharacterChecker, NewLineInit, NewLineDetector;
        
        NewLineDetector = 0;
        
        for(NewLineInit = '\n'; (CharacterChecker = getc(fp)) != EOF; NewLineInit = CharacterChecker) {
            if(NewLineInit == '\n') {
                if(sFlag) {
                    if(CharacterChecker == '\n') {
                        if(NewLineDetector)
                            continue;
                        
                        NewLineDetector = 1;
                    }
                    else
                        NewLineDetector = 0;
                }
            }
            if(CharacterChecker == '\n') {
                if(eFlag && putchar('$') == EOF)
                    break;
            }
            else if(CharacterChecker == '\t') {
                if(tFlag) {
                    if(putchar('^') == EOF || putchar('I') == EOF)
                        break;
                    continue;
                }
            }
            if(putchar(CharacterChecker) == EOF)
                break;
        }
    }
    
    
    void GoingRaw(char **argv) {
        int8_t ForInit;
        char Character;
        FILE *fp;
        
        fp = stdin;
        FileName = "stdin";
        
        do {
            if(*argv) {
                if((fp = fopen(*argv, "r")) == NULL) {
                    fprintf(stderr, "%s not found.\n", *argv);
                    ++argv;
                    continue;
                }
                FileName = *argv++;
            }
            for(ForInit = 0; (Character = getc(fp)) != EOF; ForInit = Character)
                printf("%c", Character);
        }
        while(*argv);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You're using getopt(), nice. It's a bit difficult to set up but quite powerful.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    haha you figured out gtopts sweet! You know they got a subopts too that comes in handy for bigger projects.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Doesn't seem to handle caret notation properly:

    Code:
    c@mars:~$ echo 08 0a | xxd -p -r | cat -t
    ^H
    c@mars:~$ echo 08 0a | xxd -p -r | ./175464 -t
    c@mars:~$ cat --version
    cat (GNU coreutils) 8.26
    Copyright (C) 2016 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Torbjorn Granlund and Richard M. Stallman.
    The -e and -t options enable the -v flag (at least in GNU cat), which shows nonprinting characters with ^ and/or M-. To show $ at the end of each line or to show tabs as ^I, the options are -E and -T, rather than -e and -t.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    Yeah, I didn't get too involved with all the options available in GNU cat.

    I just have -set options. I may add the -n flag sometime though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flags on gcc
    By alex83 in forum C Programming
    Replies: 2
    Last Post: 12-04-2010, 08:58 AM
  2. Finished C++ 1
    By sonict in forum C++ Programming
    Replies: 15
    Last Post: 01-26-2003, 04:47 AM
  3. Finished!
    By Kyoto Oshiro in forum C++ Programming
    Replies: 22
    Last Post: 07-21-2002, 01:40 PM
  4. Finished
    By Kavity in forum Game Programming
    Replies: 4
    Last Post: 08-26-2001, 08:17 PM
  5. Finished
    By Kavity in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 11:59 AM

Tags for this Thread