Altre correzioni, con esempi sul server fortunes e relativa "demonizzazzione"
[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.2 2003/01/12 00:24:28 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     shmid = shmget(key, 4096, 0);                  /* get a shared memory ID */
95     if (shmid < 0) {
96         perror("Cannot find shared memory");
97         exit(1);
98     }
99     if ( (shmptr = shmat(shmid, NULL, 0)) == NULL ) {   /* attach to process */
100         perror("Cannot attach segment");
101         exit(1);
102     }
103     if ((mutex = MutexFind(key)) == -1) {                   /* get the Mutex */
104         perror("Cannot find mutex");
105         exit(1);
106     }
107     /* main loop */
108     MutexLock(mutex);                                  /* lock shared memory */
109     printf("Ci sono %d file dati\n", shmptr->tot_regular);
110     printf("Ci sono %d directory\n", shmptr->tot_dir);
111     printf("Ci sono %d link\n", shmptr->tot_link);
112     printf("Ci sono %d fifo\n", shmptr->tot_fifo);
113     printf("Ci sono %d socket\n", shmptr->tot_sock);
114     printf("Ci sono %d device a caratteri\n", shmptr->tot_char);
115     printf("Ci sono %d device a blocchi\n", shmptr->tot_block);
116     printf("Totale  %d file, per %d byte\n",
117            shmptr->tot_files, shmptr->tot_size);
118     MutexUnlock(mutex);                              /* unlock shared memory */
119 }
120 /*
121  * routine to print usage info and exit
122  */
123 void usage(void) {
124     printf("Program myls: list file in a directory \n");
125     printf("Usage:\n");
126     printf("  myls [-h] dirname \n");
127     printf("  -h           print this help\n");
128     exit(1);
129 }