- added support for '--help' and '--version'
[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
30 #define ENSC_WRAPPERS_UNISTD 1
31 #define ENSC_WRAPPERS_FCNTL  1
32 #include <wrappers.h>
33
34 int     wrapper_exit_code = 1;
35
36 static void
37 showHelp(char const UNUSED *cmd)
38 {
39   WRITE_MSG(1,
40             "Usage: chroot-cat [-a] [--] <file>\n"
41             "\n"
42             "Does something similarly to 'chroot . cat > <file>' without\n"
43             "the need for a 'cat' in the chroot.\n"
44             "\n"
45             "Options:\n"
46             "    -a          ...  append instead of truncate (do '>> <file>'\n"
47             "                     instead of '> <file>)\n\n"
48             "Please report bugs to " PACKAGE_BUGREPORT "\n");
49   exit(0);
50 }
51
52 static void
53 showVersion()
54 {
55   WRITE_MSG(1,
56             "chroot-cat " VERSION " -- cat stdin into a chrooted file\n"
57             "This program is part of " PACKAGE_STRING "\n\n"
58             "Copyright (C) 2003,2004,2005 Enrico Scholz\n"
59             VERSION_COPYRIGHT_DISCLAIMER);
60   exit(0);
61 }
62
63
64 int main(int argc, char *argv[])
65 {
66   int           fd;
67   int           idx = 1;
68   bool          do_append = false;
69
70   if (argc>=2) {
71     if (strcmp(argv[idx], "--help")   ==0) showHelp(argv[0]);
72     if (strcmp(argv[idx], "--version")==0) showVersion();
73     if (strcmp(argv[idx], "-a")       ==0) { do_append = true; ++idx; }
74     if (strcmp(argv[idx], "--")       ==0) ++idx;
75   }
76   
77   if (argc!=idx+1) {
78     WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
79     return wrapper_exit_code;
80   }
81
82   Echroot(".");
83   Echdir("/");
84
85   fd = EopenD(argv[idx], O_WRONLY|O_CREAT| (do_append ? O_APPEND : O_TRUNC), 0644);
86   for (;;) {
87     char                buf[4096];
88     char const *        ptr=buf;
89     ssize_t             len;
90
91     len = Eread(0, buf, sizeof(buf));
92     if (len<=0) break;
93
94     while (len>0) {
95       size_t    l = Ewrite(fd, ptr, len);
96       ptr += l;
97       len -= l;
98     }
99   }
100   Eclose(fd);
101
102   return EXIT_SUCCESS;
103 }