Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / FortuneClient.c
1 /* FortuneClient.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program fortune 
22  * Fortune client
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: fortune -h give all info
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
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 */
41 #include <sys/stat.h>
42
43 #include "macros.h"
44
45 /* Subroutines declaration */
46 void usage(void);
47
48 int main(int argc, char *argv[])
49 {
50 /* Variables definition */
51     int n = 0;
52     char *fortunefilename = "/tmp/fortune.fifo";
53     char line[80];
54     int fifo_server, fifo_client;
55     char fifoname[80];
56     int nread;
57     char buffer[PIPE_BUF];
58     /*
59      * Input section: decode parameters passed in the calling 
60      * Use getopt function
61      */
62     int i;
63     opterr = 0;  /* don't want writing to stderr */
64     while ( (i = getopt(argc, argv, "hf:")) != -1) {
65         switch (i) {
66         /* 
67          * Handling options 
68          */ 
69         case 'h':  
70             printf("Wrong -h option use\n");
71             usage();
72             return(0);
73             break;
74         case 'f':
75             fortunefilename = optarg;
76             break;
77         case '?':   /* unrecognized options */
78             printf("Unrecognized options -%c\n",optopt);
79             usage();
80         default:    /* should not reached */
81             usage();
82         }
83     }
84     /* ***********************************************************
85      * 
86      *           Options processing completed
87      *
88      *                Main code beginning
89      * 
90      * ***********************************************************/
91     snprintf(fifoname, 80, "/tmp/fortune.%d", getpid());  /* compose name */
92     if (mkfifo(fifoname, 0622)) {  /* open client fifo */
93         if (errno!=EEXIST) {
94             perror("Cannot create well known fifo");
95             exit(-1);
96         }
97     }
98     fifo_server = open(fortunefilename, O_WRONLY); /* open server fifo */
99     if (fifo_server < 0) {
100         perror("Cannot open well known fifo");
101         exit(-1);
102     }
103     debug("%s\n", fifoname);
104     nread = write(fifo_server, fifoname, strlen(fifoname)+1);  /* write name */
105     close(fifo_server);                        /* close server fifo */
106     fifo_client = open(fifoname, O_RDONLY);    /* open client fifo */
107     if (fifo_client < 0) {
108         perror("Cannot open well known fifo");
109         exit(-1);
110     }
111     nread = read(fifo_client, buffer, sizeof(buffer)); /* read answer */
112     printf("%s", buffer);                              /* print fortune */
113     close(fifo_client);                                /* close client */
114     unlink(fifoname);                                  /* remove client fifo */
115 }
116 /*
117  * routine to print usage info and exit
118  */
119 void usage(void) {
120     printf("Elementary fortune client\n");
121     printf("Usage:\n");
122     printf("  fortune [-h] \n");
123     printf("  -h   print this help\n");
124     exit(1);
125 }