Back to index

enigmail  1.4.3
IpcCat.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public
00005  * License Version 1.1 (the "MPL"); you may not use this file
00006  * except in compliance with the MPL. You may obtain a copy of
00007  * the MPL at http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the MPL is distributed on an "AS
00010  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00011  * implied. See the MPL for the specific language governing
00012  * rights and limitations under the MPL.
00013  *
00014  * The Original Code is ipc-pipe.
00015  *
00016  * The Initial Developer of the Original Code is Patrick Brunschwig.
00017  * Portions created by Patrick Brunschwig <patrick@mozilla-enigmail.org> are
00018  * Copyright (C) 2010 Patrick Brunschwig. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *
00022  * Alternatively, the contents of this file may be used under the terms of
00023  * either the GNU General Public License Version 2 or later (the "GPL"), or
00024  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00025  * in which case the provisions of the GPL or the LGPL are applicable instead
00026  * of those above. If you wish to allow use of your version of this file only
00027  * under the terms of either the GPL or the LGPL, and not to allow others to
00028  * use your version of this file under the terms of the MPL, indicate your
00029  * decision by deleting the provisions above and replace them with the notice
00030  * and other provisions required by the GPL or the LGPL. If you do not delete
00031  * the provisions above, a recipient may use your version of this file under
00032  * the terms of any one of the MPL, the GPL or the LGPL.
00033  * ***** END LICENSE BLOCK ***** */
00034 
00035 
00036 /*
00037  * Helper tool to read or write data to/from stdin/stdout
00038  *
00039  * Usage:
00040  * IpcCat {write|read|dump|getenv} arg
00041  *
00042  * Parameters:
00043  *   write:  read from stdin and write to file <arg>
00044  *   read:   read from file <arg> and write to stdout
00045  *   dump:   read from stdin; write to stdout
00046  *   getenv: print value of environment variable <arg>
00047  *
00048  * Exit codes:
00049  *   0:    success
00050  *   > 0:  failure
00051  */
00052 
00053 #include <stdio.h>
00054 #include <string.h>
00055 #include <stdlib.h>
00056 
00057 int main(int argc, char* argv[])
00058 {
00059 
00060   if (argc < 2)
00061     return 1;
00062 
00063   const char* fileName;
00064   const int maxLen = 255;
00065   char buffer[maxLen + 1];
00066   long totalBytes = 0;
00067 
00068   // "dump" function
00069   if (strcmp(argv[1], "dump") == 0) {
00070     fputs("Starting dump\n", stderr);
00071     while(fgets(buffer, maxLen, stdin)) {
00072       fputs(buffer, stdout);
00073       totalBytes += strlen(buffer);
00074       if (! stdin)
00075         fputs("\n", stdout);
00076     }
00077     sprintf(buffer, "Dumped %ld bytes\n", totalBytes);
00078     fputs(buffer, stderr);
00079   }
00080 
00081   // "write" function
00082   else if (strcmp(argv[1], "write") == 0) {
00083     if (argc < 3)
00084       return 1;
00085 
00086     fileName = argv[2];
00087     if (!fileName)
00088       return 2;
00089 
00090     FILE* outfile = fopen(fileName, "w");
00091     if (!outfile)
00092       return 3;
00093 
00094     fputs("Starting write\n", stderr);
00095     while(fgets(buffer, maxLen, stdin)) {
00096       fputs(buffer, outfile);
00097       totalBytes += strlen(buffer);
00098       if (! stdin) {
00099         fputs("\n", outfile);
00100       }
00101     }
00102     fclose(outfile);
00103     sprintf(buffer, "Wrote %ld bytes\n", totalBytes);
00104     fputs(buffer, stderr);
00105   }
00106 
00107   // "read" function
00108   else if (strcmp(argv[1], "read") == 0) {
00109     if (argc < 3)
00110       return 1;
00111 
00112     fileName = argv[2];
00113 
00114     FILE* infile = fopen(fileName, "r");
00115     if (!infile)
00116       return 5;
00117 
00118     size_t bytesRead;
00119     bytesRead = maxLen;
00120 
00121     fputs("Starting read\n", stderr);
00122     while (bytesRead == (size_t) maxLen) {
00123       bytesRead = fread (buffer, 1, maxLen, infile);
00124       totalBytes += bytesRead;
00125       buffer[bytesRead] = 0;
00126       fputs(buffer, stdout);
00127     }
00128     fclose (infile);
00129 
00130     sprintf(buffer, "Read %ld bytes\n", totalBytes);
00131     fputs(buffer, stderr);
00132   }
00133 
00134   // "getenv" function
00135   else if (strcmp(argv[1], "getenv") == 0) {
00136     if (argc < 3)
00137       return 1;
00138 
00139     sprintf(buffer, "Reading environment variable %s\n", argv[2]);
00140     fputs(buffer, stderr);
00141     char* envVar = getenv(argv[2]);
00142 
00143     if (! envVar)
00144       fputs("not defined\n", stdout);
00145     else
00146       fputs(envVar, stdout);
00147   }
00148   else {
00149     return 4;
00150   }
00151 
00152   fflush(stderr);
00153   fflush(stdout);
00154 
00155   return 0;
00156 }