I am writing my own simple shell and currently I'm thinking of getting input ( command ) from user.
I wrote a following prototype:
while(1) {
printf("gsh> ");
fflush(stdout);
total_len = 0;
do {
len = read(0, buffer, MAX_LENGTH_OF_COMMAND-total_len -1);
total_len+= len;
} while( buffer[total_len-1] != '\n');
buffer[total_len]='\0';
parse(buffer);
}
And this soultion seems me to be best, but I am not sure. So, I am asking for correct and recommend/advice me something.
Thanks in advance.
readandfreadare for reading fixed-size data. This is usually binary data which is organised in fixed-size blocks. You want to read lines of variable length.fgetsis the function that does this for you. Or, if you want to write an interactive shell, you might consider unsinggetline, which can be coupled with acommand history and which allows advanced line editing.readline( cnswww.cns.cwru.edu/php/chet/readline/rltop.html ).readreturns 0 or a negative value?