0

I have a C program where I can offer as input different commands. The commands are processed by children and the answers are send it back to the parent. One of my command is quit and one command is login. If I have as a input commands in this order : login and quit, the quit must pe introduced twice to close the program. Also the parent takes the input in a while loop and quit is supposed to close the program. Also I'm getting the result from the first login as answer.

If you know what could be, please let me know. Thanks! Have a great day!

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#define verde printf("\033[0;32m")
#define alb printf("\033[0m")
#define rosu printf("\033[0;31m")
#define blue printf("\033[1;34m")

typedef int bool;
#define true 1
#define false 0

int main(int argc, char **argv)
{
    char *comanda = (char *)malloc(55);
    bool loggedIn = false;

    while (1)
    {
        blue;
        printf("Insert the command: ");
        alb;
        fgets(comanda, 55, stdin); 
        
        if(strstr(comanda, "quit") != NULL)
        {
            exit(0);
        }

        if (strstr(comanda, "login : ") != NULL )
        {   
            pid_t pid;
            int fd[2];

            if (pipe(fd) == -1)
            {
                rosu;
                perror("Am esuat in a crea un pipe...");
                exit(1);
            }

            if ((pid = fork()) == -1)
            {
                rosu;
                perror("Child failed...");
                exit(2);
            }
            else if (pid)
            { 

                FILE *ptr;

                close(fd[0]);
                if ((ptr = fopen("users.txt", "r")) == NULL)
                {
                    rosu;
                    perror("Failed to open users.txt ...");
                    exit(3);
                }

                char username[30];
                bool gasit = false;


                while (fgets(username, 30, ptr))
                {
                    if (strstr(comanda, username) != NULL)
                    {
                        gasit = true;
                        break;
                    }
                }
                if (write(fd[1], &gasit, sizeof(bool)) == -1)
                {
                    rosu;
                    perror("Failed to write in pipe...");
                    exit(4);
                }
                close(fd[1]);
            }


            wait(NULL);

            bool raspuns;

            close(fd[1]); 

            if (read(fd[0], &raspuns, sizeof(bool)) == -1)
            {
                rosu;
                perror("Can't write from pipe...");
                exit(5);
            }

            if (raspuns == true)
            {
                verde;
                printf("Succes!\n");
                alb;
            }
            else
            {

                rosu;
                printf("Failed to login!\n");
                alb;
            }

            close(fd[0]);
        }
        else if(strstr(comanda, "myfind : ") != NULL)
        {  
        }
        else if(strstr(comanda,"mystat : ")!=NULL)
        {  
        }
        else
        {   
            rosu;
            printf("Wrong command!\n");
            alb;
        }
        
    }

    return 0;
}
mybest
  • 19
  • 1
  • 5
  • Have you tried to use a debugger to step through the code to see what really happens? – Some programmer dude Jul 08 '20 at 17:14
  • 1
    Instead of the `typedef` and `#define`s, use `#include ` – pmg Jul 08 '20 at 17:14
  • 1
    I'd rather `#define verde "\033[0;32m"` and `printf(verde "Succes!\n" alb);` – pmg Jul 08 '20 at 17:16
  • 1
    When you `fork()` you have two processes. The `exit()` in question terminates one of those processes only. You need to maybe find a way to have the child tell the parent it's quitting so that the parent can aslo terminate... or re-think your approach – pmg Jul 08 '20 at 17:19
  • @pmg thanks for advice. I've tried to use _exit(0) but doesn't work. I'll try to find something else. – mybest Jul 08 '20 at 17:23
  • Please be aware that `fgets()` usually retains the newline, see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) Also expecting a user to enter `"myfind : "` with exactly that spacing is a bit of an ask. – Weather Vane Jul 08 '20 at 17:25
  • *I've tried to use `_exit(0)` but doesn't work.* What do you mean by "doesn't work"? What **does** `_exit(0)` actually do? – Andrew Henle Jul 08 '20 at 17:30
  • @WeatherVane I know that spacing is kinda stupid but that was the requirement, not my choice. Also I'll deal with that function. Thanks – mybest Jul 08 '20 at 17:30
  • @AndrewHenle First I thought that child doens't finish his job and tricks that wait(null), but later i figured out that child finishes his job, so yeah.. – mybest Jul 08 '20 at 17:33

0 Answers0