OK, so here's the deal.
I'm trying to create 2 programs, a server and a client, which communicates between them. I'm working on Ubuntu, throughout FIFO.
So, 1st of all, I create 2 FIFO's:
$ mkfifo pipe
$ mkfifo pipo

Ok, and after that I create the client and the server programs.
My objective is that, when running both programs (in two separate terminals) when I enter an input from the client's terminal, the server would take that input and use it to get some informations. Now my client sends a directory name, and the server will perform a ls operation and than returns to the client a list with the result of the ls command, or an error message otherwise.

But my problem arise when, having the information, as a line, to store it into a list. Basically, I need to store a line which I get out of the fgets() funtion to an array of strings/chars, but due to the fact that I mostly code in C++, getting back in C is really hard for me (and no, I can't do it in C++).

Ok, enough with the chichat, here's what I got till now.
The Client.c program:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define _GNU_SOURCE

int main(){
    FILE* fd;
    char* line=NULL;
    size_t len=0;
    ssize_t read1;
    int pipe, pipo;
    char s[256]="";
    char d[10240]="";
    printf("Folder name: ");
    scanf("%255s", s);

    pipe=open("pipe", O_WRONLY);
    pipo=open("pipo", O_RDONLY);

    write(pipe, &s, sizeof(s));
    printf("Client write done.\n");
    read(pipo, &d, sizeof(d));
    printf("Client read done.\n");

    close(pipe);
    close(pipo);
//  fd =fopen("/home/b.txt", "r");
//  while((read1 = getline(&line, &len, fd)) != -1){
//      printf("%s", line);}
//  free(line);
    return (0);
}

And the Server.c program:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int pipe, pipo;
    int k;
    char *p;
    pipe=open("pipe", O_RDONLY);
    pipo=open("pipo", O_WRONLY);
    char *lss[128];
    for(k=0;k<128;k++){
        lss[k]=malloc(sizeof(char)*20);
    }
    p=&lss[0];
    char s[256];
    char out[10240];
    char buf[256];
    char* ls="ls /";
    int i=0;    
    read(pipe, &s, sizeof(s));
    snprintf(buf, sizeof (buf), "%s%s", ls, s);
    FILE* f;
    f=popen(buf, "r");
    if (f!=NULL){
        while(fgets(out, sizeof(out), f) != NULL){
//          fputs(out, lss[i]);
//          i++;
        }
    }
    else{
        printf("F is NULL.\n");}
    pclose(f);
    write(pipo, &out, sizeof(out));
    for (;i==0;i--){
        printf("%s\n", lss[i]); 
}
    close(pipe);
    close(pipo);
    for (k=0;k<128;k++){
        free(lss[k]);
    }
    return (0);
}

Ok, but my problem is here

 if (f!=NULL){
        while(fgets(out, sizeof(out), f) != NULL){
//          fputs(out, lss[i]);
//          i++;
        }

I can't figure it out how to get the string from the out variable into the lss array, because out holds a string, or sorta.
Can any1 help me?

fputs() is the opposite of fgets(), which is not what you want. You want to call strcpy() or strcpy_s() to duplicate a string.

lines 18 and 24 of server.c are wrong too. Remove the & symbol because its already a pointer.

ok, I made some modifications, and I came up with this, but there is a problem: how can I write an array of chars throughout a pipe to another array? (and if you see some warnings that some variables aren't used, just initialised, it's because I'm working fast, and I don't have time to delete or comment these things, but I will eventually clean the project when it's finished).
server.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int pipe, pipo;
    int k;
    int m, n;
    char arra[128][128];
    char line[128];

    for(m=0; m<128; m++)
        for(n=0; n<128; n++)
            arra[m][n] = '\0';

    for(n=0; n<128; n++)
        line[n] = '\0';


    char *p;
    pipe=open("pipe", O_RDONLY);
    pipo=open("pipo", O_WRONLY);
    char s[256];
    char out[10240];
    char buf[256];
    char* ls="ls /";
    int i=0;    
    read(pipe, &s, sizeof(s));
    snprintf(buf, sizeof (buf), "%s%s", ls, s);
    FILE* f;
    m=0;
    f=popen(buf, "r");
    if (f!=NULL){
        while(fgets(line, sizeof(line), f) != NULL){
        strcpy(arra[i], line);   
        printf("%s", arra[i]);
     }
    }
    else{
        printf("F is NULL.\n");}
    pclose(f);
//  write(pipo, &arra, sizeof(out));
    for (k=0;k<128;k++){
        write(pipo, &arra[k], sizeof(arra));
        printf("I just wrote %s.\n", &arra[k]);
}
    close(pipe);
    close(pipo);
//  free(arra);
    for (k=0;k<128;k++){
//      for(n=0;n<128;n++){
            free(arra[k]);
    }
    return (0);
}

client.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int pipe, pipo;
    int k;
    int m, n;
    char arra[128][128];
    char line[128];

    for(m=0; m<128; m++)
        for(n=0; n<128; n++)
            arra[m][n] = '\0';

    for(n=0; n<128; n++)
        line[n] = '\0';


    char *p;
    pipe=open("pipe", O_RDONLY);
    pipo=open("pipo", O_WRONLY);
    char s[256];
    char out[10240];
    char buf[256];
    char* ls="ls /";
    int i=0;    
    read(pipe, &s, sizeof(s));
    snprintf(buf, sizeof (buf), "%s%s", ls, s);
    FILE* f;
    m=0;
    f=popen(buf, "r");
    if (f!=NULL){
        while(fgets(line, sizeof(line), f) != NULL){
        strcpy(arra[i], line);   
        printf("%s", arra[i]);
     }
    }
    else{
        printf("F is NULL.\n");}
    pclose(f);
//  write(pipo, &arra, sizeof(out));
    for (k=0;k<128;k++){
        write(pipo, &arra[k], sizeof(arra));
        printf("I just wrote %s.\n", &arra[k]);
}
    close(pipe);
    close(pipo);
//  free(arra);
    for (k=0;k<128;k++){
//      for(n=0;n<128;n++){
            free(arra[k]);
    }
    return (0);
}

The problem that arise now is that it sends back only limited information, and the program doesn't crush, but it ain't going further either.

Can you please check if I'm doing the writing correctly? Because, I have the array of strings now.
10x.

line 47: write(pipo, &arra[k], sizeof(arra));

Remove the & symvol -- arra[k] is already a pointer

line 55: free(arra[k]);

delete that line, arra can not be deleted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.