3 * Copyright (C) 2005 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File mylschroot.c: An example ls done after a chroot
22 * List files and their size inside a given directory inside a chroot
24 * Author: S. Piccardi Mar. 2005
26 *****************************************************************************/
28 * Include needed headers
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 */
39 * Function and globals definitions
41 void usage(void); /* Help printing routine */
42 int do_ls(struct dirent * direntry); /* computation function for DirScan */
47 int main(int argc, char *argv[])
50 char * arg[]={"ls","-l", NULL};
53 * Input section: decode command line parameters
56 opterr = 0; /* don't want writing to stderr */
57 while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
62 case 'h': /* help option */
63 printf("Wrong -h option use\n");
67 case '?': /* unrecognized options */
68 printf("Unrecognized options -%c\n",optopt);
70 default: /* should not reached */
74 /* ***********************************************************
76 * Options processing completed
80 * ***********************************************************/
81 if ((argc - optind) != 1) { /* There must be remaing parameters */
82 printf("Wrong number of arguments %d\n", argc - optind);
86 printf("Chrooting to %s\n", argv[1]);
87 if (chroot(argv[1])) {
88 perror("Chroot fail");
91 if (execve("/ls", arg, NULL)) {
92 perror("errore in execve");
94 if (stat("/ls", &data)) {
95 perror("errore in stat");
97 printf("inode: %d\n", data.st_ino);
104 * Routine to print file name and size inside DirScan
106 int do_ls(struct dirent * direntry)
109 stat(direntry->d_name, &data); /* get stat data */
110 printf("File: %s \t inode: %d\n", direntry->d_name, data.st_ino);
114 * routine to print usage info and exit
117 printf("Program mylschroot: chroot in a directory and list files \n");
119 printf(" myls [-h] dirname \n");
120 printf(" -h print this help\n");