Versione finale del client ECHO su TCP, con esempio di uso della funzione
[gapil.git] / sources / ReadMonitor.c
1 /* ReadMonitor.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  * File ReadMonitor: 
22  *
23  * An example for shared memory use: read data saved in a shared
24  * memory segment from the DirMonitor program
25  *
26  * Author: S. Piccardi Jan. 2003
27  *
28  * $Id: ReadMonitor.c,v 1.5 2003/05/02 09:55:14 piccardi Exp $
29  *
30  *****************************************************************************/
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <dirent.h>        /* directory */
34 #include <stdlib.h>        /* C standard library */
35 #include <unistd.h>
36
37 #include "Gapil.h"
38 #include "macros.h"
39
40 /* Help printing routine */
41 void usage(void);
42
43 /* variables for shared memory segment */
44 int shmid; 
45 struct DirProp {
46     int tot_size;    
47     int tot_files;   
48     int tot_regular; 
49     int tot_fifo;    
50     int tot_link;    
51     int tot_dir;     
52     int tot_block;   
53     int tot_char;    
54     int tot_sock;
55 };
56 struct DirProp *shmptr;
57 int mutex;
58
59 int main(int argc, char *argv[]) 
60 {
61     int i;
62     key_t key;
63     /*
64      * Input section: decode command line parameters 
65      * Use getopt function
66      */
67     opterr = 0;  /* don't want writing to stderr */
68     while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
69         switch (i) {
70         /* 
71          * Handling options 
72          */ 
73         case 'h':                                            /* help option */
74             printf("Wrong -h option use\n");
75             usage();
76             return -1;
77             break;
78         case '?':                                    /* unrecognized options */
79             printf("Unrecognized options -%c\n",optopt);
80             usage();
81         default:                                       /* should not reached */
82             usage();
83         }
84     }
85     /* ***********************************************************
86      * 
87      *           Options processing completed
88      *
89      *                Main code beginning
90      * 
91      * ***********************************************************/
92     /* create needed IPC objects */
93     key = ftok("~/gapil/sources/DirMonitor.c", 1);           /* define a key */
94     if (!(shmptr = ShmFind(key, 4096))) {     /* get a shared memory segment */
95         perror("Cannot find shared memory");
96         exit(1);
97     }
98     if ((mutex = MutexFind(key)) == -1) {                   /* get the Mutex */
99         perror("Cannot find mutex");
100     }
101     /* main loop */
102     MutexLock(mutex);                                  /* lock shared memory */
103     printf("Ci sono %d file dati\n", shmptr->tot_regular);
104     printf("Ci sono %d directory\n", shmptr->tot_dir);
105     printf("Ci sono %d link\n", shmptr->tot_link);
106     printf("Ci sono %d fifo\n", shmptr->tot_fifo);
107     printf("Ci sono %d socket\n", shmptr->tot_sock);
108     printf("Ci sono %d device a caratteri\n", shmptr->tot_char);
109     printf("Ci sono %d device a blocchi\n", shmptr->tot_block);
110     printf("Totale  %d file, per %d byte\n",
111            shmptr->tot_files, shmptr->tot_size);
112     MutexUnlock(mutex);                              /* unlock shared memory */
113 }
114 /*
115  * routine to print usage info and exit
116  */
117 void usage(void) {
118     printf("Program myls: list file in a directory \n");
119     printf("Usage:\n");
120     printf("  myls [-h] dirname \n");
121     printf("  -h           print this help\n");
122     exit(1);
123 }