#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

typedef struct msgbuf {
	long mtype;
	FILE *mtext;
} message_buf;

main(){
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;
size_t buf_length;
key = 1234;
if ((msqid = msgget(key, msgflg )) < 0){
	perror("msgget");
	exit(1);
}

sbuf.mtype = 1;
sbuf.mtext = popen("df", "r");
buf_length = strlen(sbuf.mtext) + 1;

if(msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
	printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
	perror("msgsnd");
	exit(1);
}
else
	printf(sbuf.mtext);
	exit(0);
	}

Hi there, this is my code, i have a problem like that in the topic. I dunno where is the problem, anyone see anything ?

Recommended Answers

All 4 Replies

line 27: The argument to strlen() is a char* or const char*, not FILE*. FILE is not a character array.

so what do i have to change in my code to make it compilable, to let it use popen structure ?

you don't. Use fgets() to read the data from the port until there is no more data to read -- fgets() returns NULL. Its very much like reading data from a file.

I've seen another detail in line 34. Missing '{' after else.

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.