Qbasicnews.com

Full Version: c Shelling problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
}
Couple of things:
What does startargs() do?
What are you expecting fflush() to do?
How exactly does fflush() fail?

getchar() buffers input, so it will not return until you press return. The returned value will be the last character entered before pressing return, therefore getchar() won't return '\n'.

The first argument to strcat is the destination string, it should not be a literal. The statement:
Code:
printf("%s", strcat(strcat("/n", path), " "));
makes no sense, I think you want:
Code:
printf("%s\n", path);

Your other strcat:
Code:
strcat(tmp, &c);
Is also likely to be broken since c is a char, not a string. If whatever happens to be after c in memory is not null, then you may end up strcat'ing a whole bunch of crap into tmp. You probably want:
Code:
int len = strlen(tmp);

/* Assuming there is available space */
tmp[len] = c;
tmp[len + 1] = '\0';

Nitpicks:
You don't need to cast the return value of malloc() in C. Not doing the cast can actually help find errors in some cases.
You shouldn't use // comments in C.
You should place all variable declarations before any statements in C.
This:
Code:
args[0] = "clear";   //this will fail the internal test and be sent to UNIX as an external command
Might not fail always, if it doesn't it will just trash something and make it harder to debug stuff elsewhere. If you want to stop somewhere in your code, using a debugger and put a breakpoint there. If you really want to induce a segfault, do this:
Code:
char *p = NULL;
*p = 1;
Even faster than:
Code:
memset(tmp, 0, 256);  //Flash reset of tmp, faster than for loop
Would be:
Code:
tmp[0] = '\0';
;-).
This comment:
Code:
//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 ^^
Not sure exactly what you mean, but the general rules of thumb in C are:
1. If you didn't initalise it, it is probably not NULL
2. If you didn't malloc it, don't try and free it. Its not your responsibility.
3. If you did malloc it, make sure you free it, otherwise you have a memory leak.
Not sure exactly what you're trying to do with fflush() either, but you can't use it to "flush" input - it only works on output streams.

EDIT: also, ' Analyzer; ' is not a function call - you must use parentheses even if there are no arguments.