1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is ipc-pipe.
16
* The Initial Developer of the Original Code is Patrick Brunschwig.
17
* Portions created by Patrick Brunschwig <patrick@enigmail.net> are
18
* Copyright (C) 2010 Patrick Brunschwig. All Rights Reserved.
22
* Alternatively, the contents of this file may be used under the terms of
23
* either the GNU General Public License Version 2 or later (the "GPL"), or
24
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25
* in which case the provisions of the GPL or the LGPL are applicable instead
26
* of those above. If you wish to allow use of your version of this file only
27
* under the terms of either the GPL or the LGPL, and not to allow others to
28
* use your version of this file under the terms of the MPL, indicate your
29
* decision by deleting the provisions above and replace them with the notice
30
* and other provisions required by the GPL or the LGPL. If you do not delete
31
* the provisions above, a recipient may use your version of this file under
32
* the terms of any one of the MPL, the GPL or the LGPL.
33
* ***** END LICENSE BLOCK ***** */
37
* Helper tool to read or write data to/from stdin/stdout
40
* IpcCat {write|read|dump|getenv} arg
43
* write: read from stdin and write to file <arg>
44
* read: read from file <arg> and write to stdout
45
* dump: read from stdin; write to stdout
46
* getenv: print value of environment variable <arg>
57
int main(int argc, char* argv[])
64
const int maxLen = 255;
65
char buffer[maxLen + 1];
69
if (strcmp(argv[1], "dump") == 0) {
70
fputs("Starting dump\n", stderr);
71
while(fgets(buffer, maxLen, stdin)) {
72
fputs(buffer, stdout);
73
totalBytes += strlen(buffer);
77
sprintf(buffer, "Dumped %ld bytes\n", totalBytes);
78
fputs(buffer, stderr);
82
else if (strcmp(argv[1], "write") == 0) {
90
FILE* outfile = fopen(fileName, "w");
94
fputs("Starting write\n", stderr);
95
while(fgets(buffer, maxLen, stdin)) {
96
fputs(buffer, outfile);
97
totalBytes += strlen(buffer);
103
sprintf(buffer, "Wrote %ld bytes\n", totalBytes);
104
fputs(buffer, stderr);
108
else if (strcmp(argv[1], "read") == 0) {
114
FILE* infile = fopen(fileName, "r");
121
fputs("Starting read\n", stderr);
122
while (bytesRead == (size_t) maxLen) {
123
bytesRead = fread (buffer, 1, maxLen, infile);
124
totalBytes += bytesRead;
125
buffer[bytesRead] = 0;
126
fputs(buffer, stdout);
130
sprintf(buffer, "Read %ld bytes\n", totalBytes);
131
fputs(buffer, stderr);
135
else if (strcmp(argv[1], "getenv") == 0) {
139
sprintf(buffer, "Reading environment variable %s\n", argv[2]);
140
fputs(buffer, stderr);
141
char* envVar = getenv(argv[2]);
144
fputs("not defined\n", stdout);
146
fputs(envVar, stdout);