tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

test-process.c (1900B)


      1 /* Copyright (c) 2011-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 
      4 #include "orconfig.h"
      5 #include <stdio.h>
      6 #ifdef _WIN32
      7 #define WINDOWS_LEAN_AND_MEAN
      8 #include <windows.h>
      9 #else
     10 #include <unistd.h>
     11 #endif /* defined(_WIN32) */
     12 #include <string.h>
     13 #include <stdlib.h>
     14 
     15 #ifdef _WIN32
     16 #define SLEEP(sec) Sleep((sec)*1000)
     17 #else
     18 #define SLEEP(sec) sleep(sec)
     19 #endif
     20 
     21 /* Trivial test program to test process_t. */
     22 int
     23 main(int argc, char **argv)
     24 {
     25  /* Does our process get the right arguments? */
     26  for (int i = 0; i < argc; ++i) {
     27    fprintf(stdout, "argv[%d] = '%s'\n", i, argv[i]);
     28    fflush(stdout);
     29  }
     30 
     31  /* Make sure our process got our environment variable. */
     32  fprintf(stdout, "Environment variable TOR_TEST_ENV = '%s'\n",
     33          getenv("TOR_TEST_ENV"));
     34  fflush(stdout);
     35 
     36  /* Test line handling on stdout and stderr. */
     37  fprintf(stdout, "Output on stdout\nThis is a new line\n");
     38  fflush(stdout);
     39 
     40  fprintf(stderr, "Output on stderr\nThis is a new line\n");
     41  fflush(stderr);
     42 
     43  fprintf(stdout, "Partial line on stdout ...");
     44  fflush(stdout);
     45 
     46  fprintf(stderr, "Partial line on stderr ...");
     47  fflush(stderr);
     48 
     49  SLEEP(2);
     50 
     51  fprintf(stdout, "end of partial line on stdout\n");
     52  fflush(stdout);
     53  fprintf(stderr, "end of partial line on stderr\n");
     54  fflush(stderr);
     55 
     56  /* Echo input from stdin. */
     57  char buffer[1024];
     58 
     59  int count = 0;
     60 
     61  while (fgets(buffer, sizeof(buffer), stdin)) {
     62    /* Strip the newline. */
     63    size_t size = strlen(buffer);
     64 
     65    if (size >= 1 && buffer[size - 1] == '\n') {
     66      buffer[size - 1] = '\0';
     67      --size;
     68    }
     69 
     70    if (size >= 1 && buffer[size - 1] == '\r') {
     71      buffer[size - 1] = '\0';
     72      --size;
     73    }
     74 
     75    fprintf(stdout, "Read line from stdin: '%s'\n", buffer);
     76    fflush(stdout);
     77 
     78    if (++count == 3)
     79      break;
     80  }
     81 
     82  fprintf(stdout, "We are done for here, thank you!\n");
     83 
     84  return 0;
     85 }