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 <stdio.h> /* standard I/O library */
36 #include <stdlib.h> /* C standard library */
37 #include <errno.h> /* error definitions and routines */
38 #include <fcntl.h> /* file control functions */
39 #include <string.h> /* C strings library */
40 #include <limits.h> /* system limits constants, types and functions */
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()); /* compose name */
91 if (mkfifo(fifoname, 0622)) { /* open client fifo */
93 perror("Cannot create well known fifo");
97 fifo_server = open(fortunefilename, O_WRONLY); /* open server fifo */
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); /* write name */
104 close(fifo_server); /* close server fifo */
105 fifo_client = open(fifoname, O_RDONLY); /* open client fifo */
106 if (fifo_client < 0) {
107 perror("Cannot open well known fifo");
110 nread = read(fifo_client, buffer, sizeof(buffer)); /* read answer */
111 printf("%s", buffer); /* print fortune */
112 close(fifo_client); /* close client */
113 unlink(fifoname); /* remove client fifo */
116 * routine to print usage info and exit
119 printf("Elementary fortune client\n");
121 printf(" fortune [-h] \n");
122 printf(" -h print this help\n");