From 7f4d7b5f44a47d8698413cef99a0d71d02479494 Mon Sep 17 00:00:00 2001 From: Simone Piccardi Date: Tue, 25 May 2010 10:03:55 +0000 Subject: [PATCH] rimosse opzioni non usate e file rimasto indietro. --- sources/mylschroot.c | 2 +- sources/mylschrootout.c | 120 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 sources/mylschrootout.c diff --git a/sources/mylschroot.c b/sources/mylschroot.c index 56b3d63..13d93bb 100644 --- a/sources/mylschroot.c +++ b/sources/mylschroot.c @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) * Use getopt function */ opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) { + while ( (i = getopt(argc, argv, "h")) != -1) { switch (i) { /* * Handling options diff --git a/sources/mylschrootout.c b/sources/mylschrootout.c new file mode 100644 index 0000000..77ed860 --- /dev/null +++ b/sources/mylschrootout.c @@ -0,0 +1,120 @@ +/* mylschrootout.c + * + * Copyright (C) 2007 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/***************************************************************************** + * + * File myls.c: An example ls done after a chroot + * + * Author: S. Piccardi Apr. 2007 + * + *****************************************************************************/ +#include /* primitive system data types */ +#include /* file characteristics constants and functions */ +#include /* directory operation constants and functions */ +#include /* C standard library */ +#include /* unix standard library */ + +#include "Gapil.h" +/* + * Program mylschrootout + * + * List files and their size inside exiting from a chroot + */ +/* Help printing routine */ +void usage(void); +/* computation function for DirScan */ +int do_ls(struct dirent * direntry); + +int main(int argc, char *argv[]) +{ + int i; + char * arg[]={"ls","-l", NULL}; + struct stat data; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "h")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + if ((argc - optind) != 1) { /* There must be remaing parameters */ + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + + printf("Chrooting to %s\n", argv[1]); + if (chroot(argv[1])) { + perror("Chroot fail"); + } + + if (execve("/ls", arg, NULL)) { + perror("errore in execve"); + } + if (stat("/ls", &data)) { + perror("errore in stat"); + } else { + printf("inode: %d\n", data.st_ino); + } + mkdir("escape", 0777); + chroot("escape"); + DirScan("../", do_ls); + + exit(0); +} +/* + * Routine to print file name and size inside DirScan + */ +int do_ls(struct dirent * direntry) +{ + struct stat data; + stat(direntry->d_name, &data); /* get stat data */ + printf("File: %s \t inode: %d\n", direntry->d_name, data.st_ino); + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program mylschrootout: chroot in a directory and list files \n"); + printf("Usage:\n"); + printf(" myls [-h] dirname \n"); + printf(" -h print this help\n"); + exit(1); +} -- 2.30.2