c8fad95d3f079c29aee3aa52f9b57b3e9cb770d1
[gapil.git] / sources / DirMonitor.c
1 /* DirMonitor.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 DirMonitor: 
22  *
23  * An example for shared memory use: monitor a directory status,
24  * saving data in a shared memory segment
25  *
26  * Author: S. Piccardi Jan. 2003
27  *
28  * $Id: DirMonitor.c,v 1.1 2003/01/03 23:16:05 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
39 /* Help printing routine */
40 void usage(void);
41 /* computation function for DirScan */
42 int ComputeValues(struct dirent * direntry);
43
44 /* global variable for shared memory segment */
45 int shmid;                                       /* Shared memory identifier */
46
47 int main(int argc, char *argv[]) 
48 {
49     int i;
50     int mutex;
51     /*
52      * Input section: decode command line parameters 
53      * Use getopt function
54      */
55     opterr = 0;  /* don't want writing to stderr */
56     while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
57         switch (i) {
58         /* 
59          * Handling options 
60          */ 
61         case 'h':                                            /* help option */
62             printf("Wrong -h option use\n");
63             usage();
64             return -1;
65             break;
66         case '?':                                    /* unrecognized options */
67             printf("Unrecognized options -%c\n",optopt);
68             usage();
69         default:                                       /* should not reached */
70             usage();
71         }
72     }
73     /* ***********************************************************
74      * 
75      *           Options processing completed
76      *
77      *                Main code beginning
78      * 
79      * ***********************************************************/
80     if ((argc - optind) != 1) {          /* There must be remaing parameters */
81         printf("Wrong number of arguments %d\n", argc - optind);
82         usage();
83     }
84     
85     DirScan(argv[1], ComputeValues);
86     exit(0);
87 }
88 /*
89  * Routine to print file name and size inside DirScan
90  */
91 int do_ls(struct dirent * direntry) 
92 {
93     struct stat data;
94     stat(direntry->d_name, &data);                          /* get stat data */
95     printf("File: %s \t size: %d\n", direntry->d_name, data.st_size);
96     return 0;
97 }
98 /*
99  * routine to print usage info and exit
100  */
101 void usage(void) {
102     printf("Program myls: list file in a directory \n");
103     printf("Usage:\n");
104     printf("  myls [-h] dirname \n");
105     printf("  -h           print this help\n");
106     exit(1);
107 }