1// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#if defined(__has_feature)
10#if __has_feature(undefined_behavior_sanitizer)
11__attribute__((no_sanitize("undefined")))
12#endif
13#endif
14void Crash() {
15 int* segfault = NULL;
16 *segfault = 1;
17}
18
19/*
20 * Run ./process_test <outstream> <echocount> <exitcode> <crash>
21 * <outstream>: 0 = stdout, 1 = stderr, 2 = stdout and stderr
22 * <echocount>: program terminates after <echocount> replies
23 * <exitcode>: program terminates with exit code <exitcode>
24 * <crash>: 0 = program terminates regularly, 1 = program segfaults
25 */
26int main(int argc, char* argv[]) {
27 if (argc != 5) {
28 fprintf(stderr,
29 "./process_test <outstream> <echocount> <exitcode> <crash>\n");
30 exit(1);
31 }
32
33 int outstream = atoi(argv[1]);
34 if (outstream < 0 || outstream > 2) {
35 fprintf(stderr, "unknown outstream");
36 exit(1);
37 }
38
39 int echo_counter = 0;
40 int echo_count = atoi(argv[2]);
41 int exit_code = atoi(argv[3]);
42 int crash = atoi(argv[4]);
43
44 if (crash == 1) {
45 Crash();
46 }
47
48 const int kLineSize = 128;
49 char line[kLineSize];
50
51 while ((echo_count != echo_counter) &&
52 (fgets(line, kLineSize, stdin) != NULL)) {
53 if (outstream == 0) {
54 fprintf(stdout, "%s", line);
55 fflush(stdout);
56 } else if (outstream == 1) {
57 fprintf(stderr, "%s", line);
58 fflush(stderr);
59 } else if (outstream == 2) {
60 fprintf(stdout, "%s", line);
61 fprintf(stderr, "%s", line);
62 fflush(stdout);
63 fflush(stderr);
64 }
65 echo_counter++;
66 }
67
68 return exit_code;
69}
70