minor optimizations
[util-vserver.git] / util-vserver / src / chroot-cat.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2003 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 int main(int argc, char *argv[])
37 {
38   int           fd;
39
40   if (argc!=2) {
41     WRITE_MSG(2, "Usage: chroot-cat file\n");
42     return EXIT_FAILURE;
43   }
44
45   Echroot(".");
46   Echdir("/");
47
48   fd = Eopen(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
49   for (;;) {
50     char                buf[4096];
51     char const *        ptr=buf;
52     ssize_t             len;
53
54     len = Eread(0, buf, sizeof(buf));
55     if (len<=0) break;
56
57     while (len>0) {
58       size_t    l = Ewrite(fd, ptr, len);
59       ptr += l;
60       len -= l;
61     }
62   }
63   Eclose(fd);
64
65   return EXIT_SUCCESS;
66 }