Inserito un inizio di esempio dell'uso delle funzioni delle capabilities.
[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 myls.c: An example ls done after a chroot
22  *
23  * Author: S. Piccardi Mar. 2005
24  *
25  * $Id$
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     char * arg[]={"ls","-l", NULL};
49     struct stat data;
50     /*
51      * Input section: decode command line parameters 
52      * Use getopt function
53      */
54     opterr = 0;  /* don't want writing to stderr */
55     while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
56         switch (i) {
57         /* 
58          * Handling options 
59          */ 
60         case 'h':                                            /* help option */
61             printf("Wrong -h option use\n");
62             usage();
63             return -1;
64             break;
65         case '?':                                    /* unrecognized options */
66             printf("Unrecognized options -%c\n",optopt);
67             usage();
68         default:                                       /* should not reached */
69             usage();
70         }
71     }
72     /* ***********************************************************
73      * 
74      *           Options processing completed
75      *
76      *                Main code beginning
77      * 
78      * ***********************************************************/
79     if ((argc - optind) != 1) {          /* There must be remaing parameters */
80         printf("Wrong number of arguments %d\n", argc - optind);
81         usage();
82     }
83
84     printf("Chrooting to %s\n", argv[1]);
85     if (chroot(argv[1])) {
86         perror("Chroot fail");
87     }
88
89     if (execve("/ls", arg, NULL)) {
90         perror("errore in execve");
91     }
92     if (stat("/ls", &data)) {
93         perror("errore in stat");
94     } else {
95         printf("inode: %d\n", data.st_ino);
96     }
97     DirScan("/", do_ls);
98
99     exit(0);
100 }
101 /*
102  * Routine to print file name and size inside DirScan
103  */
104 int do_ls(struct dirent * direntry) 
105 {
106     struct stat data;
107     stat(direntry->d_name, &data);                          /* get stat data */
108     printf("File: %s \t inode: %d\n", direntry->d_name, data.st_ino);
109     return 0;
110 }
111 /*
112  * routine to print usage info and exit
113  */
114 void usage(void) {
115     printf("Program myls: list file in a directory \n");
116     printf("Usage:\n");
117     printf("  myls [-h] dirname \n");
118     printf("  -h           print this help\n");
119     exit(1);
120 }