3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
24 * Author: Simone Piccardi
27 * Usage: fortune -h give all info
29 * $Id: FortuneClient.c,v 1.1 2002/08/18 10:34:09 piccardi Exp $
31 ****************************************************************/
33 * Include needed headers
35 #include <sys/types.h> /* predefined types */
36 #include <unistd.h> /* include unix standard library */
37 #include <arpa/inet.h> /* IP addresses conversion utiliites */
38 #include <stdio.h> /* include standard I/O library */
44 /* Subroutines declaration */
47 int main(int argc, char *argv[])
49 /* Variables definition */
51 char *fortunefilename = "/tmp/fortune.fifo";
53 int fifo_server, fifo_client;
56 char buffer[PIPE_BUF];
58 * Input section: decode parameters passed in the calling
62 opterr = 0; /* don't want writing to stderr */
63 while ( (i = getopt(argc, argv, "hf:")) != -1) {
69 printf("Wrong -h option use\n");
74 fortunefilename = optarg;
76 case '?': /* unrecognized options */
77 printf("Unrecognized options -%c\n",optopt);
79 default: /* should not reached */
83 /* ***********************************************************
85 * Options processing completed
89 * ***********************************************************/
90 snprintf(fifoname, 80, "/tmp/fortune.%d", getpid());
91 if (mkfifo(fifoname, 0622)) {
93 perror("Cannot create well known fifo");
97 fifo_server = open(fortunefilename, O_WRONLY);
98 if (fifo_server < 0) {
99 perror("Cannot open well known fifo");
102 debug("%s\n", fifoname);
103 nread = write(fifo_server, fifoname, strlen(fifoname)+1);
105 fifo_client = open(fifoname, O_RDONLY);
106 if (fifo_client < 0) {
107 perror("Cannot open well known fifo");
110 nread = read(fifo_client, buffer, sizeof(buffer));
111 printf("%s", buffer);
117 * routine to print usage info and exit
120 printf("Elementary fortune server\n");
122 printf(" fortune [-h] [-f] \n");
123 printf(" -h print this help\n");
124 printf(" -f filename set file for fortunes\n");