ALcune correzioni sparse, risistemati i commenti interni delle varie
[gapil.git] / sources / myls.c
1 /* myls.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 myls.c: An example ls
22  *
23  * Author: S. Piccardi Jan. 2003
24  *
25  * $Id: myls.c,v 1.4 2003/05/02 09:55:14 piccardi Exp $
26  *
27  *****************************************************************************/
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <dirent.h>        /* directory */
31 #include <stdlib.h>        /* C standard library */
32 #include <unistd.h>
33
34 #include "Gapil.h"
35 /*
36  * Program myls
37  *
38  * List files and their size inside a given directory
39  */
40 /* Help printing routine */
41 void usage(void);
42 /* computation function for DirScan */
43 int do_ls(struct dirent * direntry);
44
45 int main(int argc, char *argv[]) 
46 {
47     int i;
48     /*
49      * Input section: decode command line parameters 
50      * Use getopt function
51      */
52     opterr = 0;  /* don't want writing to stderr */
53     while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
54         switch (i) {
55         /* 
56          * Handling options 
57          */ 
58         case 'h':                                            /* help option */
59             printf("Wrong -h option use\n");
60             usage();
61             return -1;
62             break;
63         case '?':                                    /* unrecognized options */
64             printf("Unrecognized options -%c\n",optopt);
65             usage();
66         default:                                       /* should not reached */
67             usage();
68         }
69     }
70     /* ***********************************************************
71      * 
72      *           Options processing completed
73      *
74      *                Main code beginning
75      * 
76      * ***********************************************************/
77     if ((argc - optind) != 1) {          /* There must be remaing parameters */
78         printf("Wrong number of arguments %d\n", argc - optind);
79         usage();
80     }
81     DirScan(argv[1], do_ls);
82     exit(0);
83 }
84 /*
85  * Routine to print file name and size inside DirScan
86  */
87 int do_ls(struct dirent * direntry) 
88 {
89     struct stat data;
90     stat(direntry->d_name, &data);                          /* get stat data */
91     printf("File: %s \t size: %d\n", direntry->d_name, data.st_size);
92     return 0;
93 }
94 /*
95  * routine to print usage info and exit
96  */
97 void usage(void) {
98     printf("Program myls: list file in a directory \n");
99     printf("Usage:\n");
100     printf("  myls [-h] dirname \n");
101     printf("  -h           print this help\n");
102     exit(1);
103 }