3 // Copyright (C) 2003,2004,2005 Enrico Scholz <>
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.
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.
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.
31 #define ENSC_WRAPPERS_UNISTD 1
32 #define ENSC_WRAPPERS_FCNTL 1
33 #define ENSC_WRAPPERS_IO 1
36 int wrapper_exit_code = 1;
39 showHelp(char const UNUSED *cmd)
42 "Usage: chroot-cat [-i|-o|-a] [--] <file>\n"
44 "Does something similarly to 'chroot . cat OP <file>' without\n"
45 "the need for a 'cat' in the chroot.\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"
52 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
68 int main(int argc, char *argv[])
75 int xflag = O_RDONLY|O_NOCTTY;
76 enum {dirIN, dirOUT} dir = dirIN;
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;
88 WRITE_MSG(2, "chroot-cat: Not mode specified; use '--help' for more information\n");
89 return wrapper_exit_code;
93 WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
94 return wrapper_exit_code;
98 WRITE_MSG(2, "Too much parameters; use '--help' for more information\n");
99 return wrapper_exit_code;
105 fd = EopenD(argv[idx], xflag, 0644);
108 case dirIN : fd_in = fd; fd_out = 1; break;
109 default : fd_in = 0; fd_out = fd; break;
114 char const * ptr=buf;
117 len = Eread(fd_in, buf, sizeof(buf));
120 EwriteAll(fd_out, ptr, len);