Justplayit94
Justplayit94

Reputation: 423

Linux pipe bad behavior

I have this linux program that uses a pipe to transmit data from the parent to child, and give an answer based on the returned value;

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>


int fd[2], nbytes;
pid_t childpid;
char readbuffer[80];
int log_variable;
char login[]="login";


void toLower(char str[]){//converts UPPERCASE to lowercase
    int i;
    for(i=0; i<strlen(str); i++)
        str[i]=tolower(str[i]);
}
//end of toLower

int compareStrings(char str1[], char str2[]){//compares 2 strings

    if(strlen(str1) == strlen(str2))
    {
        int i;
        for( i=0; i<strlen(str1); i++){
            if(str1[i] != str2[i])
                return 0;
            return 1;
        }
    }
    else return 0;
}


int loginCommand(char argument[]){//test function so far for login, myfind etc
    int fileDescr;

    pipe(fd);//defines the pipe

    switch(childpid=fork()){//switch statement to control parent and child

      case -1:
        perror("fork -1\n");
        exit(0);


      case 0://child
        close (fd[1]);
        char givenUsername[20];
        //open the config file and copy the username from it, assign to a variable, and then
        //compare it to readbuffer


        int nbytes = read(fd[0], readbuffer, sizeof(readbuffer));


        if(strcmp(readbuffer, login) == 0){
            printf("1: ");
            return 1;
        }

        else {
            printf("0: ");
            return 0;

        }
        exit(0);

      default:
        //parent
        close(fd[0]);
        write(fd[1], argument, sizeof(argument));
        wait(NULL)!=-1;
        exit(0);
    }
}

main(){
    char input[20];
    int logs;

    while(logs == 0){
        printf("Insert command: \n");
        scanf("%s", input);
        toLower(input);

        logs=(loginCommand(input));
        if(logs == 1) {printf("System accessed\n"); }
        if(logs == 0) {printf("This username doesnt exist\n"); }
    }


    return 0;
}

But my biggest question is that I input the value of "login", that is the same of the login variable above, the program responds correctly, but if I change that variable to of value of "loginloginlogin" let's say, and if I input from the console the same value, the program says that the value is incorrect. My assumption is that the program doesn't read the whole input from console, but I've changed the sizes of the strings, and still has the same behavior.

Can anyone know whats going on?

Upvotes: 0

Views: 114

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409442

The problem is here:

write(fd[1], argument, sizeof(argument));

When you pass an array to a function, it decays to a pointer to the first character. Doing sizeof on a pointer gives you the size of the pointer and not the size of what it point so.

To get a the length of a string you need to use strlen.

Oh, and don't forget to use strlen(argument) + 1 to also send the string terminator (alternatively terminate the string in the child process).

Upvotes: 3

Related Questions