Aggiornamento note copyright
[gapil.git] / sources / mylschroot.c
1 /* mylschroot.c
2  * 
3  * Copyright (C) 2005 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 mylschroot.c: An example ls done after a chroot
22  * List files and their size inside a given directory inside a chroot
23  *
24  * Author: S. Piccardi Mar. 2005
25  *
26  *****************************************************************************/
27 /* 
28  * Include needed headers
29  */
30 #include <sys/types.h>   /* primitive system data types */
31 #include <sys/stat.h>    /* file characteristics constants and functions */
32 #include <dirent.h>      /* directory operation constants and functions */
33 #include <stdlib.h>      /* C standard library */
34 #include <unistd.h>      /* unix standard library */
35
36 #include "Gapil.h"
37
38 /* 
39  * Function and globals definitions
40  */
41 void usage(void);                     /* Help printing routine */
42 int do_ls(struct dirent * direntry);  /* computation function for dir_scan */
43
44 /*
45  * Main program
46  */
47 int main(int argc, char *argv[]) 
48 {
49     int i;
50     char * arg[]={"ls","-l", NULL};
51     struct stat data;
52     /*
53      * Input section: decode command line parameters 
54      * Use getopt function
55      */
56     opterr = 0;  /* don't want writing to stderr */
57     while ( (i = getopt(argc, argv, "h")) != -1) {
58         switch (i) {
59         /* 
60          * Handling options 
61          */ 
62         case 'h':                                            /* help option */
63             printf("Wrong -h option use\n");
64             usage();
65             return -1;
66             break;
67         case '?':                                    /* unrecognized options */
68             printf("Unrecognized options -%c\n",optopt);
69             usage();
70         default:                                       /* should not reached */
71             usage();
72         }
73     }
74     /* ***********************************************************
75      * 
76      *           Options processing completed
77      *
78      *                Main code beginning
79      * 
80      * ***********************************************************/
81     if ((argc - optind) != 1) {          /* There must be remaing parameters */
82         printf("Wrong number of arguments %d\n", argc - optind);
83         usage();
84     }
85
86     printf("Chrooting to %s\n", argv[1]);
87     if (chroot(argv[1])) {
88         perror("Chroot fail");
89     }
90
91     if (execve("/ls", arg, NULL)) {
92         perror("errore in execve");
93     }
94     if (stat("/ls", &data)) {
95         perror("errore in stat");
96     } else {
97         printf("inode: %d\n", data.st_ino);
98     }
99     dir_scan("/", do_ls);
100
101     exit(0);
102 }
103 /*
104  * Routine to print file name and size inside dir_scan
105  */
106 int do_ls(struct dirent * direntry) 
107 {
108     struct stat data;
109     stat(direntry->d_name, &data);                          /* get stat data */
110     printf("File: %s \t inode: %d\n", direntry->d_name, data.st_ino);
111     return 0;
112 }
113 /*
114  * routine to print usage info and exit
115  */
116 void usage(void) {
117     printf("Program mylschroot: chroot in a directory and list files \n");
118     printf("Usage:\n");
119     printf("  myls [-h] dirname \n");
120     printf("  -h           print this help\n");
121     exit(1);
122 }