Back to index

enigmail  1.4.3
Functions
IpcCat.cpp File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 57 of file IpcCat.cpp.

{

  if (argc < 2)
    return 1;

  const char* fileName;
  const int maxLen = 255;
  char buffer[maxLen + 1];
  long totalBytes = 0;

  // "dump" function
  if (strcmp(argv[1], "dump") == 0) {
    fputs("Starting dump\n", stderr);
    while(fgets(buffer, maxLen, stdin)) {
      fputs(buffer, stdout);
      totalBytes += strlen(buffer);
      if (! stdin)
        fputs("\n", stdout);
    }
    sprintf(buffer, "Dumped %ld bytes\n", totalBytes);
    fputs(buffer, stderr);
  }

  // "write" function
  else if (strcmp(argv[1], "write") == 0) {
    if (argc < 3)
      return 1;

    fileName = argv[2];
    if (!fileName)
      return 2;

    FILE* outfile = fopen(fileName, "w");
    if (!outfile)
      return 3;

    fputs("Starting write\n", stderr);
    while(fgets(buffer, maxLen, stdin)) {
      fputs(buffer, outfile);
      totalBytes += strlen(buffer);
      if (! stdin) {
        fputs("\n", outfile);
      }
    }
    fclose(outfile);
    sprintf(buffer, "Wrote %ld bytes\n", totalBytes);
    fputs(buffer, stderr);
  }

  // "read" function
  else if (strcmp(argv[1], "read") == 0) {
    if (argc < 3)
      return 1;

    fileName = argv[2];

    FILE* infile = fopen(fileName, "r");
    if (!infile)
      return 5;

    size_t bytesRead;
    bytesRead = maxLen;

    fputs("Starting read\n", stderr);
    while (bytesRead == (size_t) maxLen) {
      bytesRead = fread (buffer, 1, maxLen, infile);
      totalBytes += bytesRead;
      buffer[bytesRead] = 0;
      fputs(buffer, stdout);
    }
    fclose (infile);

    sprintf(buffer, "Read %ld bytes\n", totalBytes);
    fputs(buffer, stderr);
  }

  // "getenv" function
  else if (strcmp(argv[1], "getenv") == 0) {
    if (argc < 3)
      return 1;

    sprintf(buffer, "Reading environment variable %s\n", argv[2]);
    fputs(buffer, stderr);
    char* envVar = getenv(argv[2]);

    if (! envVar)
      fputs("not defined\n", stdout);
    else
      fputs(envVar, stdout);
  }
  else {
    return 4;
  }

  fflush(stderr);
  fflush(stdout);

  return 0;
}