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