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