merge with 0.30.213
[util-vserver.git] / src / chain-echo.c
1 // $Id: chain-echo.c 1469 2004-04-20 17:54:40Z ensc $    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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 #include "util.h"
23
24 #include <errno.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28
29 #define ENSC_WRAPPERS_PREFIX    "chain-echo: "
30 #define ENSC_WRAPPERS_FCNTL     1
31 #define ENSC_WRAPPERS_UNISTD    1
32 #define ENSC_WRAPPERS_IO        1
33 #include <wrappers.h>
34
35 int     wrapper_exit_code = 255;
36
37 static void
38 showHelp(char const *cmd)
39 {
40   WRITE_MSG(1, "Usage:  ");
41   WRITE_STR(1, cmd);
42   WRITE_MSG(1,
43             " [--] <file> <data> <command> <args>*\n\n"
44             "Please report bugs to " PACKAGE_BUGREPORT "\n");
45   exit(0);
46 }
47
48 static void
49 showVersion()
50 {
51   WRITE_MSG(1,
52             "chain-echo " VERSION " -- puts data into a file within a command-chain\n"
53             "This program is part of " PACKAGE_STRING "\n\n"
54             "Copyright (C) 2004 Enrico Scholz\n"
55             VERSION_COPYRIGHT_DISCLAIMER);
56   exit(0);
57 }
58
59 int main(int argc, char *argv[])
60 {
61   int           idx = 1;
62   int           fd;
63
64   if (argc>=2) {
65     if (strcmp(argv[1], "--help")   ==0) showHelp(argv[0]);
66     if (strcmp(argv[1], "--version")==0) showVersion();
67     if (strcmp(argv[1], "--")       ==0) ++idx;
68   }
69   
70   if (argc<idx+3) {
71     WRITE_MSG(2, "Not enough parameters; use '--help' for more information\n");
72    return wrapper_exit_code;
73   }
74
75   if (argv[idx][0]=='\0')
76     fd = 1;
77   else {
78     fd = Eopen(argv[idx], O_WRONLY|O_NOFOLLOW, 0600);
79     Efcntl(fd, F_SETFD, FD_CLOEXEC);
80   }
81
82   if (argv[idx+1][0]!='\0')
83     EwriteAll(fd, argv[idx+1], strlen(argv[idx+1]));
84
85   Eexecv(argv[idx+2], argv+idx+2);
86 }