I wrote 2 programs that normally 1.create the named pipe(server) and 2. pass the string from the shell to the server part.
I understand how to hand over a string from the terminal to the server part of the named pipe.But, i don't have any idea how to pass files as arguments into the 2 program so that the file's content should be read and therefore passed out to ( likely the server part of the named pipe) another file. The idea is to copy the first file's content to the second file. Unfortunately, i don't have any idea how to implement this.
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 512
int main (void)
{
FILE * fifo;
char * buf;
if (mkfifo ("myfifo", 0640) == -1) {
fprintf (stderr, "Can't create fifo\n");
return 1;
}
fifo = fopen (FIFO_NAME, "r");
if (fifo == NULL) {
fprintf (stderr, "Cannot open fifo\n");
return 1;
}
buf = (char *) malloc (BUF_SIZE);
if (buf == NULL) {
fprintf (stderr, "malloc () error\n");
return 1;
}
fscanf (fifo, "%s", buf);
printf ("%s\n", buf);
fclose (fifo);
free (buf);
unlink (FIFO_NAME);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define FIFO_NAME "myfifo"
int main (int argc, char ** argv)
{
int fifo;
if (argc < 2) {
fprintf (stderr, "Too few arguments\n");
return 1;
}
fifo = open (FIFO_NAME, O_WRONLY);
if (fifo == -1) {
fprintf (stderr, "Cannot open fifo\n");
return 1;
}
if (write (fifo, argv[1], strlen (argv[1])) == -1) {
fprintf (stderr, "write() error\n");
return 1;
}
close (fifo);
return 0;
}