13de7135002f0465d2dde597681bae92c4ee475f
[util-vserver.git] / util-vserver / src / chroot-cat.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003,2004,2005 Enrico Scholz <>
4 //  
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; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 #define ENSC_WRAPPERS_UNISTD    1
32 #define ENSC_WRAPPERS_FCNTL     1
33 #define ENSC_WRAPPERS_IO        1
34 #include <wrappers.h>
35
36 int     wrapper_exit_code = 1;
37
38 static void
39 showHelp(char const UNUSED *cmd)
40 {
41   WRITE_MSG(1,
42             "Usage: chroot-cat [-i|-o|-a] [--] <file>\n"
43             "\n"
44             "Does something similarly to 'chroot . cat OP <file>' without\n"
45             "the need for a 'cat' in the chroot.\n"
46             "\n"
47             "Options:\n"
48             "    -a          ...  append to <file> (OP = '>>')\n"
49             "    -o          ...  truncate and append to <file> (OP = '>')\n"
50             "    -i          ...  use file as input (OP = '<') (default)\n"
51             "\n"
52             "Please report bugs to " PACKAGE_BUGREPORT "\n");
53   exit(0);
54 }
55
56 static void
57 showVersion()
58 {
59   WRITE_MSG(1,
60             "chroot-cat " VERSION " -- cat stdin into a chrooted file\n"
61             "This program is part of " PACKAGE_STRING "\n\n"
62             "Copyright (C) 2003,2004,2005 Enrico Scholz\n"
63             VERSION_COPYRIGHT_DISCLAIMER);
64   exit(0);
65 }
66
67
68 int main(int argc, char *argv[])
69 {
70   int                   fd;
71   int                   idx   = 1;
72   int                   fd_in;
73   int                   fd_out;
74   
75   int                   xflag = O_RDONLY|O_NOCTTY;
76   enum {dirIN, dirOUT}  dir   = dirIN;
77
78   if (argc>=2) {
79     if (strcmp(argv[idx], "--help")   ==0) showHelp(argv[0]);
80     if (strcmp(argv[idx], "--version")==0) showVersion();
81     if (strcmp(argv[idx], "-a")       ==0) { xflag = O_WRONLY|O_CREAT|O_APPEND; dir = dirOUT; ++idx; }
82     if (strcmp(argv[idx], "-o")       ==0) { xflag = O_WRONLY|O_CREAT|O_TRUNC;  dir = dirOUT; ++idx; }
83     if (strcmp(argv[idx], "-i")       ==0) ++idx;
84     if (strcmp(argv[idx], "--")       ==0) ++idx;
85   }
86
87   if (xflag==0) {
88     WRITE_MSG(2, "chroot-cat: Not mode specified; use '--help' for more information\n");
89     return wrapper_exit_code;
90   }
91   
92   if (argc<idx+1) {
93     WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
94     return wrapper_exit_code;
95   }
96
97   if (argc>idx+1) {
98     WRITE_MSG(2, "Too much parameters; use '--help' for more information\n");
99     return wrapper_exit_code;
100   }
101   
102   Echroot(".");
103   Echdir("/");
104
105   fd = EopenD(argv[idx], xflag, 0644);
106
107   switch (dir) {
108     case dirIN          :  fd_in = fd; fd_out =  1; break;
109     default             :  fd_in =  0; fd_out = fd; break;
110   }
111   
112   for (;;) {
113     char                buf[4096];
114     char const *        ptr=buf;
115     ssize_t             len;
116
117     len = Eread(fd_in, buf, sizeof(buf));
118     if (len<=0) break;
119
120     EwriteAll(fd_out, ptr, len);
121   }
122   Eclose(fd);
123
124   return EXIT_SUCCESS;
125 }