Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
c Shelling problem
#1
This doesn't work for some reason. It fails on fflush, and when I do get past that it doesn't register when I press enter.

Code:
int main(int argc, char *argv[]){
    startargs();
    char *tmp = (char *) malloc(sizeof(char) * 256);  // I don't think there will be any commands longer than 256 characters
            //*tmp has to be declared in here because initilizer element is not constant
    char *path = (char *) malloc(sizeof(char) * 256);
    char c; //used for input
    getpath(path);  //gets the env variable and stores it in path
    fflush(stdout); //flush buffer from old input
    if (argc > 1){  //there is always one argc (the command)
        //Analyze looks at the args[] array so I copy the commandline arguments to it
        //no need to free them, they are already set to null strings
            // ^^ Make sure C does this ^^
        for (i = 0; i < argc; i++){
            args[i] = argv[i];        //just copy the pointers (faster and no strcpy)
        }
        Analyzer;
    }
    else{
        destroyargs();
        printf("%s", strcat(strcat("/n", path), " "));
    }    
    while(strcmp(args[0], "quit")){    //main loop
        while (c != EOF){
            c = getchar(); //get input, I prefer this to cin
            switch(c){
                case '\n': //Pressed Enter or Return
                    if (tmp[0] == '\0'){ //blank line, no command
                        printf("%s", strcat(strcat("/n", path), " "));
                        break;
                    }
                    else {  //was a good command, we need to parse it and analyze it
                        parse(tmp);
                        if (strcmp(args[0], "cls") == 0)
                            args[0] = "clear";    //this will fail the internal test and be sent to UNIX as an external command
                        Analyzer();
                        memset(tmp, 0, 256);  //Flash reset of tmp, faster than for loop
                    }
                    printf("%s", path);
                    break;
                default:
                    strcat(tmp, &c);
                    //last option, no need for a break
            }
        }
    }
    free(path);  //have to get give RAM back to UNIX
    free(tmp);
    freeargs();
    return 0;    //exit code 0 = successful quit
}
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply


Messages In This Thread
c Shelling problem - by wallace - 03-02-2007, 05:41 AM
c Shelling problem - by LooseCaboose - 03-02-2007, 10:02 AM
c Shelling problem - by DrV - 03-02-2007, 08:08 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)