Thread: Can I get more "homework"?

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

    Can I get more "homework"?

    About a week ago, I completed my task of creating a mini cat program. No, not a program involving cats either...

    Anyone want to give me some homework?

    I'll post the code to the previous project to give you guys a clue where I'm at in terms of "skill" (I will also admit that I had the OpenBSD cat.c source file to look at, but the code below is mine):

    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
    Banned
    Join Date
    Aug 2017
    Posts
    861
    reading other peoples code is recommended. It shows you methodologies of program design , programing styles and how to do something. Somethings cannot be copied per se' opening a file for instance, it's going to be the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Homework help: Sudoku and "explosive recursion"
    By green_ghoul in forum C Programming
    Replies: 13
    Last Post: 11-22-2011, 11:16 PM
  3. Homework - "custom" array
    By Wheaties in forum C Programming
    Replies: 19
    Last Post: 03-14-2011, 08:21 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread