I'm trying to create a pipe in NetWare...
Articles and Tips: qna
01 Jan 2002
Q.
I'm trying to create a pipe in NetWare (building an NLM). I use fdopen to create my file handles and do some simple read and write operations. The code below does not work, as fread() simply returns 0 bytes read. I've also tried other standard C operations on the FILE pointers, but none seem to work. Then, I used read() and write() directly on the piped descriptors themselves and it did work, but this is not what I need.
Does anyone know how to get the following to work (opening FILE pointers to piped descriptors, then calling standard C functions such as fwrite/fread or fputs/fgets)? My sample code is below:
int main() { FILE *in, *out; int p[2]; char buff[32]; char c; char* data = "Hello, World!";
pipe(p); out = fdopen(p[1], "w"); fwrite(data, strlen(data), 1, out);
/* the following actually works! */
//write(p[1], "j", 1); fclose(out);
/* read and write work, but fread() and fwrite() do not */
//read(p[0], buff, 6); in = fdopen(p[0], "r"); c = fread(buff, strlen(data), 1, in); printf("bytes read: %d\n", c); printf(buff); fclose(in); }
A.
There are several things you need to do to make this work. First, set the flags for the pipe to O_NONBLOCK using fcntl(). You only need to do this for one side of the pipe because both ends map to the same FIFO. Second, open your streams with "wb" and "rb" instead of "w" and "r." Third, after writing data to the pipe, call fflush() to write the data through to the pipe. Finally, don't close the write stream with fclose() before reading from the pipe.
* Originally published in Novell AppNotes
Disclaimer
The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.