added '-i', '-o' and '-a' options to cat or override a file, or to
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 19 May 2005 18:01:06 +0000 (18:01 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Thu, 19 May 2005 18:01:06 +0000 (18:01 +0000)
append something

git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2093 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/src/chroot-cat.c

index aca42b9..13de713 100644 (file)
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <errno.h>
 
-#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 <wrappers.h>
 
 int    wrapper_exit_code = 1;
@@ -37,14 +39,16 @@ static void
 showHelp(char const UNUSED *cmd)
 {
   WRITE_MSG(1,
-           "Usage: chroot-cat [-a] [--] <file>\n"
+           "Usage: chroot-cat [-i|-o|-a] [--] <file>\n"
            "\n"
-           "Does something similarly to 'chroot . cat > <file>' without\n"
+           "Does something similarly to 'chroot . cat OP <file>' without\n"
            "the need for a 'cat' in the chroot.\n"
            "\n"
            "Options:\n"
-           "    -a          ...  append instead of truncate (do '>> <file>'\n"
-           "                     instead of '> <file>)\n\n"
+           "    -a          ...  append to <file> (OP = '>>')\n"
+           "    -o          ...  truncate and append to <file> (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 (argc<idx+1) {
     WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
     return wrapper_exit_code;
   }
 
+  if (argc>idx+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);