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 ****************************************************************/
31 * Include needed headers
33 #include <sys/types.h> /* primitive system data types */
34 #include <unistd.h> /* unix standard library */
35 #include <arpa/inet.h> /* IP addresses conversion utiliites */
36 #include <stdio.h> /* standard I/O library */
37 #include <errno.h>> /* error definitions and routines */
38 #include <fcntl.h> /* file control functions */
42 /* Subroutines declaration */
45 int main(int argc, char *argv[])
47 /* Variables definition */
49 char *fortunefilename = "/tmp/fortune.fifo";
51 int fifo_server, fifo_client;
54 char buffer[PIPE_BUF];
56 * Input section: decode parameters passed in the calling
60 opterr = 0; /* don't want writing to stderr */
61 while ( (i = getopt(argc, argv, "hf:")) != -1) {
67 printf("Wrong -h option use\n");
72 fortunefilename = optarg;
74 case '?': /* unrecognized options */
75 printf("Unrecognized options -%c\n",optopt);
77 default: /* should not reached */
81 /* ***********************************************************
83 * Options processing completed
87 * ***********************************************************/
88 snprintf(fifoname, 80, "/tmp/fortune.%d", getpid()); /* compose name */
89 if (mkfifo(fifoname, 0622)) { /* open client fifo */
91 perror("Cannot create well known fifo");
95 fifo_server = open(fortunefilename, O_WRONLY); /* open server fifo */
96 if (fifo_server < 0) {
97 perror("Cannot open well known fifo");
100 debug("%s\n", fifoname);
101 nread = write(fifo_server, fifoname, strlen(fifoname)+1); /* write name */
102 close(fifo_server); /* close server fifo */
103 fifo_client = open(fifoname, O_RDONLY); /* open client fifo */
104 if (fifo_client < 0) {
105 perror("Cannot open well known fifo");
108 nread = read(fifo_client, buffer, sizeof(buffer)); /* read answer */
109 printf("%s", buffer); /* print fortune */
110 close(fifo_client); /* close client */
111 unlink(fifoname); /* remove client fifo */
114 * routine to print usage info and exit
117 printf("Elementary fortune server\n");
119 printf(" fortune [-h] [-f] \n");
120 printf(" -h print this help\n");
121 printf(" -f filename set file for fortunes\n");