fgets error message loop -Linux shell in C - c

Trying to build a linux shell everything works great except this one error message at the top!
i set LINE=81, and it prints out the error message total length/LINE times. im having trouble finding a way to fix that. I think it's because it keeps looping and since itll always have that condition itll print. and I dont want it to terminate, just refresh and keep inputting, any ideas? I am required ot provide an error message.
edit:when adding the '\n' it still functions the same.
char commands[LINE];
while (fgets(commands, LINE, stdin)!= NULL) {
if (strlen(commands) >= LINE - 1) {
printf("Too Many arguments please enter %d characters\n",LINE-2);
continue;
}
childPID = fork();
if (childPID < 0) {
perror("fork");
exit(-1);
}
if (childPID == 0) {
parse(commands);
exit(0);
} else {
if (waitpid(childPID, &status, 0) !=childPID) {
perror("waitpid");
} else
if (!WIFEXITED(status)) {
printf("Exited Abnormally\n");
}
putc('\n', stdout);
fputs(PROMPT,stdout);
}
commands[0] = '\0';
}
exit(0);
}

You should put \n at the end of the error message:
printf("Too Many arguments please enter %d characters\n",LINE-2);
stdout is normally line-buffered, so the buffer isn't flushed until you print a newline. When your program forks, the buffer is copied into the child. When the child calls exit(), it flushes its copy of the buffer. And when the parent calls putc('\n') it flushes its copy. As a result, the message is printed twice.

so turned out I needed to iterate through and then print and move my prompt accordingly
while (fgets(commands, LINE, stdin)!= NULL) {
if (strlen(commands) > LINE-2) {
fprintf(stderr,"Too many chars\n");
while (getchar() != '\n' && !feof(stdin));
}
else
{
childPID = fork();
if (childPID < 0) {
perror("fork");
exit(-1);
}
if (childPID == 0) {
parse(commands);
exit(0);
} else {
if (waitpid(childPID, &status, 0) !=childPID) {
perror("waitpid is not responding");
} else
if (!WIFEXITED(status)) {
fprintf(stderr,"Unusually exit\n");
}
//putc('\n', stdout);
//fputs(PROMPT,stdout);
}
}
putc('\n', stdout);
fputs(PROMPT,stdout);
}
exit(0);
}

Related

Using a fork process on C failed while loop

I'm trying to create a simple shell interface program that takes shell commands as user input from the parent process and send the simple shell command to the child process via a pip IPC which actually executes the shell command. The child while loop keeps repeating even after typing "quit"
int main() {
char userInput[BUFFER_SIZE];
int fpipe[2];
pid_t pid;
// INTRODUCTION TO PROGRAM
printf("------- WELCOME TO THE FORKED PARENT -------\n");
printf("------- YOU MAY QUIT AT ANY TIME BY TYPING 'quit' or 'q' -------\n");
if (pipe(fpipe)== -1){
printf("------- PIPE HAS FAILED -------\n");
return 1;
}
pid = fork();
if (pid < 0){
printf("------- FORK HAS FAILED -------\n");
return 1;
}
if (pid > 0){
close(fpipe[0]);
printf("\nosh> ");
scanf("%s", userInput);
while ((strcmp(userInput,"quit") != 0 && strcmp(userInput,"q") != 0)){
printf("\nosh> ");
write(fpipe[1], userInput, strlen(userInput)+1);
scanf("%s", userInput);
}
close(fpipe[1]);
}
else{
close(fpipe[1]);
while(1){
if (read(fpipe[0], userInput, BUFFER_SIZE) == -1){
return 1;
}
if ((strcmp(userInput,"quit") != 0 && strcmp(userInput,"q") != 0)){
system(userInput);
printf("osh> ");
}
else{
break;
}
}
close(fpipe[0]);
}
return 0;
}
The problem is that the loop in the parent stops when the user enters quit, it doesn't send it to the child. So the child's condition for stopping the loop is never matched. It keeps reading from the pipe, which returns nothing because it's at EOF, so it keeps executing the last command that was sent.
The simplest solution is for the child to break out of the loop when it gets EOF from the pipe, rather than looking for quit.
while(1){
int n;
n = read(fpipe[0], userInput, BUFFER_SIZE);
if (n == -1) {
return 1;
}
if (n == 0) {
break;
}
system(userInput);
}

n-pipeline producing EOF on end

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
void tokenizer(char* input, char** output) { //My tokenizer
char* input_dup = strdup(input);
output[0] = strtok(input_dup, " ");
int i = 1;
while ((output[i] = strtok(NULL, " ")) != NULL) {
i++;
}
}
void run_command(char** args, int* fd) { //no pipe
pid_t pid = fork();
if (pid < 0) {
printf("Forking failed...\n");
}
else if (pid == 0) {
close(fd[0]);
if (fd[1] != 1)
dup2(fd[1], 1);
execvp(args[0], args);
printf("Command failed...\n");
exit(1);
}
else {
close(fd[1]);
wait(pid);
char buff[1];
while (read(fd[0], buff, 1) > 0) {
if (buff[0] == EOF || buff[0] == '\0') {
printf("Caught something, returning out...");
return;
}
else {
printf("%c", buff[0]);
}
}
}
}
//pipeline function
void run_pipe(char** args, int* fd) {
pid_t pid = fork();
if (pid < 0) {
printf("Forking failed...\n");
}
else if (pid == 0) {
if (fd[1] != 1) {
dup2(fd[1], 1);
}
execvp(args[0], args);
printf("Command failed...\n");
exit(1);
}
else {
close(fd[1]);
if (fd[0] != 0) {
dup2(fd[0], 0);
}
wait(pid);
}
}
int main(int argc, char** argv) {
printf ("Starting myshell (mysh) \n..\n..\n");
while (1) {
char cwd[1024];
printf ("mysh :: %s -> ", getcwd(cwd, sizeof(cwd)));
char ch[1024];
memset(ch, 0, 1023); //for cleanup
char c = 0;
int i = 0;
while (c != '\n') {
c = getchar();
if (c == EOF) {
printf ("EOF Received, exiting...\n");
return 0;
}
if (c != '\n')
ch[i] = c;
i++;
}
if (ch[0] != '\0') {
char* tokens[128];
tokenizer(ch, tokens);
//first check for keywords
if (strcmp(tokens[0], "cd") == 0) {
if (chdir(tokens[1]) < 0) {
printf("ERROR: Directory %s does not exist\n", tokens[1]);
}
}
else if (strcmp(tokens[0], "exit") == 0) {
printf("Leaving shell...\n");
return 0;
}
else {
char* commands[50];
memset(commands, 0, sizeof(commands));
int j = 0;
int k = 0;
int fd[2];
//try something different...
while (tokens[j] != NULL) {
if (strcmp(tokens[j], "|") == 0) {
commands[k] = NULL;
pipe(fd);
run_pipe(commands, fd);
j++;
k = 0;
}
//more cases here
else { //nothing special
commands[k] = tokens[j];
j++;
k++;
}
}
commands[k] = NULL;
pipe(fd);
run_command(commands, fd);
}
}
}
}
The above code is meant to simulate a shell. It handles single commands and it handles the pipelining properly (i.e. ps | sort | wc is returning the correct output) however when the pipelining is done it returns an EOF which is caught by the condition in the loop with getchar(). If I try to ignore this EOF it segfaults. Am I leaving a pipe open somewhere and stdin is getting flooded? Any help is appreciated.
Compilation fixes
You need to add #include <sys/wait.h> and then fix the calls to wait(). I used (twice):
int status;
int corpse = wait(&status);
printf("PID %d status 0x%.4X\n", corpse, status);
Arguably, that should be a loop looking for a specific PID, or you should use waitpid() instead. While debugging a shell, you want to know about every PID that exits and its status.
I ran `ps | wc' and got:
Starting myshell (mysh)
..
..
mysh :: /usr/local/google/home/jleffler/soq -> ps | wc
PID 25960 status 0x0000
PID 25961 status 0x0000
4 16 117
mysh :: /usr/local/google/home/jleffler/soq -> EOF Received, exiting...
If you mean "the code should have continued instead of getting EOF", then there's some more work to do.
Tangential issues
I note the line:
if (buff[0] == EOF || buff[0] == '\0')
The character in buff[0] is from a read() call. It will never be EOF meaningfully; EOF is distinct from every character (hence getchar() returns an int). This becomes significant later:
char c = 0;
while (c != '\n')
{
c = getchar();
if (c == EOF)
Since c is a char, you cannot reliably compare it with EOF. You must store the result of getchar() in an int.
I'm not yet convinced these are the cause of the trouble, but you must be careful.
Probable cause
I think the trouble is in run_pipe() in the parent code (as amended):
else
{
close(fd[1]);
if (fd[0] != 0)
{
dup2(fd[0], 0);
}
int status;
int corpse = wait(&status);
printf("PID %d status 0x%.4X\n", corpse, status);
}
The fd[0] != 0 condition will always be true (very improbable that it will be false), so you then change your shell's input to read from fd[0]. You should review that; it means that you're reading standard input from the read end of the pipe to the child. That's bad; you've lost your original input!
Your code also seems to have the parent waiting for the child to die, and then reads the pipe and echoes to standard output. This is not a good idea; it is better to make the child (last child in the pipeline) write to the standard output directly. There are two reasons for this:
The child might write more data than fits in a pipe, so it will block waiting for something to read its output, but the reader will be blocked waiting for the child to die, so you'll have a deadlock.
It slows things up, and output from the child may well be buffered instead of appearing timely on the terminal.
I'm a little sceptical about how a three-part pipeline would be handled. You need two pipes created before you run the middle process of the three; I don't see that in your code.
I was able to fix this issue. It's probably not the right way to do it, but I saved a copy of stdin and used dup2 to reset it when the pipelining was finished.
int in_bak = dup(0);
//stuff
dup2(in_bak, 0);
close(in_bak);

Issue with pipes and fork, it only goes to the child process once instead of everytime I want

So the code is this one:
int main ()
{
int pid, fd[2], i, j;
char comanda[1000], comm[100][100], *var, *var2, vect[100][100], text[1000], check[10000];
if(pipe(fd)<0)
{
perror("pipe error");
exit(1);
}
if((pid = fork()) < 0 )
{
perror("fork error");
exit(1);
}
j = 0;
if(pid){
do {
if( j > 0) fgets (check , 1000 , stdin); //just in case there's still something in the buffer
printf("enter command: \n");
scanf("%[^\t\n]", comanda);
if(var = strtok(comanda, " "))
{
i=0;
while(var != NULL)
{
strcpy(vect[i], var);
var = strtok(NULL, " ");
i++;
}
}
else
strcpy(vect[0], comanda);
if(strcmp(vect[0], "login") == 0)
{
write(fd[1], "login ", 6);
write(fd[1], vect[1], strlen(vect[1]));
printf("I got login");
}
else if(strcmp(vect[0], "quit") == 0)
{
exit(1);
}
else
printf("I got the command %s \n", vect[0]);
j++;
} while(1);
close(fd[0]);
close(fd[1]);
wait(NULL);
}
else
{
close(fd[1]);
printf("copil? \n");
int i=0;
read(fd[0], text, sizeof(text));
var2 = strtok(text, " ");
j=0;
while(var2 != NULL)
{
strcpy(comm[j], var2);
var2 = strtok(NULL, " ");
j++;
}
if( strcmp(comm[0], "login") == 0)
{
//comanda e login, deci verificam username-ul.
if(login(comm[1]))
{
printf("OK, Logged IN! \n");
}
else
{
printf("Username not in /etc/passwd \n");
}
}
close(fd[0]);
exit(0);
}
return 0;
}
What I want is to read commands from the console line and everytime I get a known command, to go to the child and execute it. Right now it reads the commands fine, the login works fine BUT only ONE time. After that, it still gets the login, it prints "I got login", but it doesn't go to the child and check it if it's ok or not.
You have three major problems that I can see:
The first is that you don't terminate the string you read from the pipe in the child;
The other, and the source of your problem, is that the child does not loop, but performs once and then exits;
The third would be if you change the child to loop, and you exit the parent process then the child process would be abandoned, and just keep on waiting for input that never comes.
This is because your child process just exits. It seems that first "read(fd[0], text, sizeof(text));" blocks until it will receive some data from parent process. Then it executes data and exits.
BTW it is good idea to call waitpid function to avoid zombie process which could be some problem with your application. What is more you should "close(fd[0]);" at the beginning of parent process not at the and with "close(fd[1]);"
I had the same problem. My idea was to control another process through a pipe like it would get input from the keyboard/stdin. It was the classical need of inter process communication with application that are not designed for it.
The problem was, that all the example codes just worked once at the beginning to send something to the child process stdin. It appeared to me that the pipe has to be closed on parents side so that the child side receive the data. Once closed I couldn't "reopen" it to send another command.
What saved my day was this:
http://www.rkoucha.fr/tech_corner/pty_pdip.html
http://en.wikipedia.org/wiki/Pseudo_terminal
I do not know why it took me almost two days to figure it out, because it sound really obvious thing to to after all. I'm posting this here, because I landed on this side and the answers wasn't really solving my problem.
Sincerely,
Robert

Confused about implementing multipiping in a shell

So I have been trying to implement piping in my own shell program so that I can actually understand what UNIX is doing. I'm very very close at the moment, but for some reason my program is going into an infinite loop when I pipe. I'm pretty sure my problem is stemming from my waitpid arguments not ever letting the last pipe close, because if I make the final loop in the code below be i is less than count-1 instead of i is less than count it will run the command on the left of the pipe. But once I put it back to i is less than count it just loops forever.
if(pipes)
{
for (i=0;i<count-1;i++)
{
if( pipe(fd) ==-1)
{
perror("Pipe failure");
return;
}
read[i+1] = fd[0];
write[i] = fd[1];
}
}
for(i=0;i<count;i++)
{
pid = fork();
if(pid < 0)
{printf("fork failed");}
else if(pid>0)
{pids[i] = pid;}
else
{
if(write[i] != -1)
{
if(dup2(write[i],STDOUT_FILENO) ==-1)
{
perror("dup2 failure");
exit(1);
}
}
if(read[i] !=-1)
{
if (dup2(read[i], STDIN_FILENO)==-1)
{
perror("dup2 failure");
exit(1);
}
}
for (j=0; j<count; j++)
{
close(write[j]);
close(read[j]);
}
execvp(input[i], input);
exit(1);
}//end else
}//end for
for(i=0; i < count; i++){
if(write[i] != -1)
close(write[i]);
if( read[i] != -1)
close (read [i]);
waitpid(pids[i], &status,0);
}
}
return (status);}
I think I'm really really close to the solution but for the time being I'm stuck. I've researched piping a ton, but I guess I'm just not quite getting it. Thanks for any help.
Please format your code. It is unreadable right now.
That being said, a few errors:
You are not setting write[0]. You are setting write[count-1] and read[count].
You are waiting for pids[i] while the parent still has read[i] open. If input[i] wants will not exit until it gets EOF on stdin, it will never happen.

problem with fork()

I'm writing a shell which forks, with the parent reading the input and the child process parsing and executing it with execvp.
pseudocode of main method:
do{
pid = fork();
print pid;
if (p<0) { error; exit; }
if (p>0) { wait for child to finish; read input; }
else { call function to parse input; exit; }
}while condition
return;
what happens is that i never seem to enter the child process (pid printed is always positive, i never enter the else). however, if i don't call the parse function and just have else exit, i do correctly enter parent and child alternatingly.
full code:
int main(int argc, char *argv[]){
char input[500];
pid_t p;
int firstrun = 1;
do{
p = fork();
printf("PID: %d", p);
if (p < 0) {printf("Error forking"); exit(-1);}
if (p > 0){
wait(NULL);
firstrun = 0;
printf("\n> ");
bzero(input, 500);
fflush(stdout);
read(0, input, 499);
input[strlen(input)-1] = '\0';
}
else exit(0);
else { if (parse(input) != 0 && firstrun != 1) { printf("Error parsing"); exit(-1); } exit(0); }
}while(strcmp(input, "exit") != 0);
return 0;
}
EDIT:
-that else exit(0) just something i forgot there from playing around
-adding a newlines to the prints shows that it does in fact correctly fork and enter the child process; thank you, the problem seems to be in the parse
One culprit is else exit(0);
That would execute in the child shell, which means it never gets to the parsing stage. The code is also syntactically invalid because after that you have another else.
`if (p >= 0) {
if (p == 0) {/* chile process */}
else if (p > 0) {/* parent process */}
} else {
/* handle the error returned by fork() */
}`
I'd do it like the above pseudo code.
else exit(0); is what the child process is doing in your code.
Your core is a tad messy with all the nested if's and else's. There are some dangling else statements as well (else exit(0);). I'd start by cleaning those up. I can't see any other logical problems with your code. It's simple enough.
Swap the lines
else exit(0);
and
else { if (parse(input) != 0 && firstrun != 1) { printf("Error parsing"); exit(-1); } exit(0); }
Apart from everything everybody else has said about the fact that the else's are a complete mess, there are some other issues you will hit when you have fixed them.
In the child, the input array will be garbage on the first run because you don't put anything in it before forking.
It seems completely pointless to fork at all since you are not exec'ing anything in the child but you are waiting for the child to finish in the parent. Why not just call parse from the parent?

Resources