From: Enrico Scholz Date: Thu, 19 May 2005 18:01:06 +0000 (+0000) Subject: added '-i', '-o' and '-a' options to cat or override a file, or to X-Git-Tag: version_0_30_210~188 X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf73866587a7e994cd1e2a30577c69b6339c7404;p=util-vserver.git added '-i', '-o' and '-a' options to cat or override a file, or to append something git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2093 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- diff --git a/util-vserver/src/chroot-cat.c b/util-vserver/src/chroot-cat.c index aca42b9..13de713 100644 --- a/util-vserver/src/chroot-cat.c +++ b/util-vserver/src/chroot-cat.c @@ -26,9 +26,11 @@ #include #include #include +#include -#define ENSC_WRAPPERS_UNISTD 1 -#define ENSC_WRAPPERS_FCNTL 1 +#define ENSC_WRAPPERS_UNISTD 1 +#define ENSC_WRAPPERS_FCNTL 1 +#define ENSC_WRAPPERS_IO 1 #include int wrapper_exit_code = 1; @@ -37,14 +39,16 @@ static void showHelp(char const UNUSED *cmd) { WRITE_MSG(1, - "Usage: chroot-cat [-a] [--] \n" + "Usage: chroot-cat [-i|-o|-a] [--] \n" "\n" - "Does something similarly to 'chroot . cat > ' without\n" + "Does something similarly to 'chroot . cat OP ' without\n" "the need for a 'cat' in the chroot.\n" "\n" "Options:\n" - " -a ... append instead of truncate (do '>> '\n" - " instead of '> )\n\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); } @@ -63,39 +67,57 @@ showVersion() int main(int argc, char *argv[]) { - int fd; - int idx = 1; - bool do_append = false; + 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) { do_append = true; ++idx; } + 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 (xflag==0) { + WRITE_MSG(2, "chroot-cat: Not mode specified; use '--help' for more information\n"); + return wrapper_exit_code; + } - if (argc!=idx+1) { + if (argcidx+1) { + WRITE_MSG(2, "Too much parameters; use '--help' for more information\n"); + return wrapper_exit_code; + } + Echroot("."); Echdir("/"); - fd = EopenD(argv[idx], O_WRONLY|O_CREAT| (do_append ? O_APPEND : 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);