WHAT'S GOING ON... NOT NEEDED THOUGH!
Well, let me first ask the general question/problem that's plaguing me in this "Coded Adventure"...
I hope I haven't already turned you off with this verbiage but, it is about to get a wee bit worse! (You can skip this if you want to just view my code and try to help me figure out just how to get a single word from a buffer)
Anyway, enough talking... here is my CODE:
int main(int argc, char *argv[])
{
char s[MAX_BUFF];
char stock[5];
char cust[4];
char *out;
int numBytes, fd, fd2;
unsigned output;
FILE *file;
FILE *ofile;
FILE *datafile;
mkfifo(strcat(argv[3], "_fifo"), S_IFIFO | 0666);
if(!fork())
{
fd = open(argv[3], O_WRONLY);
printf("Broker is waiting for request...\n");
gets(s);
if((numBytes = write(fd, s, strlen(s))) == -1)
perror("Write1");
else
{
printf("\nClient says, \"%s.\"\n", s);
ofile = fopen(argv[2], "a");
fputs(s, ofile);
fputc('\n', ofile);
fclose(ofile);
}
}
else
{
fd2 = open(argv[3], O_RDONLY);
signal(SIGALRM, handler);
do
{
if((numBytes = read(fd2, s, 300)) == -1)
perror("Read1");
else
{
s[numBytes] = '\0';
if(strstr(s, "quote") && strstr(s, "buy")) // Went the LOOONG way... had to use exact index.
{ // Could you tell me a better way of doing this? Thanks!
int i = 21;
int j = 29;
printf("Client wants to buy...\n");
while(i < 26)
{
stock[(i-21)] = s[i];
i++;
}
printf("The stock is: %s\n", stock);
while(j < 34)
{
cust[(j-29)] = s[j];
j++;
}
printf("The cust_id is: %s\n", cust);
printf("Broker is generating quote...\n");
mkfifo(strcat(argv[4], "_fifo"), S_IFIFO | 0666);
fd = open(argv[4], O_WRONLY | O_NDELAY);
out = "Buy at 50 per share of YADAD for 5555."; //This was just to test if it was writing to logfile..
write(fd, out, strlen(out));
// printf("\nBroker says, \"%s.\"\n", out);
ofile = fopen(argv[2], "a");
fputs(out, ofile);
fputc('\n', ofile);
fclose(ofile);
alarm(5);
}
else if (strstr(s, "quote") && strstr(s, "sell")) // Again, I'm going about this the WRONG way!
{
int i = 22;
printf("Client wants to sell...\n");
while(i < 27)
{
stock[(i-22)] = s[i];
i++;
}
printf("The stock is: %s\n", stock);
}
// printf("Broker read %d bytes.\n", numBytes);
}
}
while(numBytes > 0);
}
return 0;
}