X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fsrc%2Fchroot-cat.c;h=13de7135002f0465d2dde597681bae92c4ee475f;hb=27fa2c9e2097e2d359994f613382c6838d580984;hp=b888243365167aaa8dcfbaccc3eef564fadfdf02;hpb=34c53fec6caf8868d54e3149ec562a2cd3af1e1d;p=util-vserver.git diff --git a/util-vserver/src/chroot-cat.c b/util-vserver/src/chroot-cat.c index b888243..13de713 100644 --- a/util-vserver/src/chroot-cat.c +++ b/util-vserver/src/chroot-cat.c @@ -1,6 +1,6 @@ -// $Id$ --*- c++ -*-- +// $Id$ --*- c -*-- -// Copyright (C) 2003 Enrico Scholz <> +// Copyright (C) 2003,2004,2005 Enrico Scholz <> // // 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 @@ -21,40 +21,103 @@ #endif #include "util.h" -#include "wrappers.h" #include #include #include +#include +#include + +#define ENSC_WRAPPERS_UNISTD 1 +#define ENSC_WRAPPERS_FCNTL 1 +#define ENSC_WRAPPERS_IO 1 +#include int wrapper_exit_code = 1; +static void +showHelp(char const UNUSED *cmd) +{ + WRITE_MSG(1, + "Usage: chroot-cat [-i|-o|-a] [--] \n" + "\n" + "Does something similarly to 'chroot . cat OP ' without\n" + "the need for a 'cat' in the chroot.\n" + "\n" + "Options:\n" + " -a ... append to (OP = '>>')\n" + " -o ... truncate and append to (OP = '>')\n" + " -i ... use file as input (OP = '<') (default)\n" + "\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n"); + exit(0); +} + +static void +showVersion() +{ + WRITE_MSG(1, + "chroot-cat " VERSION " -- cat stdin into a chrooted file\n" + "This program is part of " PACKAGE_STRING "\n\n" + "Copyright (C) 2003,2004,2005 Enrico Scholz\n" + VERSION_COPYRIGHT_DISCLAIMER); + exit(0); +} + + int main(int argc, char *argv[]) { - int fd; + int fd; + int idx = 1; + int fd_in; + int fd_out; + + int xflag = O_RDONLY|O_NOCTTY; + enum {dirIN, dirOUT} dir = dirIN; + + if (argc>=2) { + if (strcmp(argv[idx], "--help") ==0) showHelp(argv[0]); + if (strcmp(argv[idx], "--version")==0) showVersion(); + if (strcmp(argv[idx], "-a") ==0) { xflag = O_WRONLY|O_CREAT|O_APPEND; dir = dirOUT; ++idx; } + if (strcmp(argv[idx], "-o") ==0) { xflag = O_WRONLY|O_CREAT|O_TRUNC; dir = dirOUT; ++idx; } + if (strcmp(argv[idx], "-i") ==0) ++idx; + if (strcmp(argv[idx], "--") ==0) ++idx; + } - if (argc!=2) { - WRITE_MSG(2, "Usage: chroot-cat file\n"); - return EXIT_FAILURE; + if (xflag==0) { + WRITE_MSG(2, "chroot-cat: Not mode specified; use '--help' for more information\n"); + return wrapper_exit_code; + } + + if (argcidx+1) { + WRITE_MSG(2, "Too much parameters; use '--help' for more information\n"); + return wrapper_exit_code; + } + Echroot("."); Echdir("/"); - fd = Eopen(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644); + fd = EopenD(argv[idx], xflag, 0644); + + switch (dir) { + case dirIN : fd_in = fd; fd_out = 1; break; + default : fd_in = 0; fd_out = fd; break; + } + for (;;) { char buf[4096]; char const * ptr=buf; ssize_t len; - len = Eread(0, buf, sizeof(buf)); + len = Eread(fd_in, buf, sizeof(buf)); if (len<=0) break; - while (len>0) { - size_t l = Ewrite(fd, ptr, len); - ptr += l; - len -= l; - } + EwriteAll(fd_out, ptr, len); } Eclose(fd);