aboutsummaryrefslogtreecommitdiff
path: root/mariadb-connector-c-v_2.3.7/examples/test_output.c
blob: 6f38e3f52650c240da189120cbf6efede0e07a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
int main(int argc, char *argv[])
{
  char cmd_output[1024];
  char cmd_exp[1024];
  FILE *fp_exec, *fp_out, *fp_exp;

  if (argc < 3)
  {
    printf("Syntax: test_output test output expected");
    exit(-1);
  }
  if (!(fp_exec= popen(argv[1], "r")))
  {
    printf("Failed to run %s\n", argv[1]);
    exit(-1);
  }

  if (!(fp_out= fopen(argv[2], "w")))
  {
    printf("Failed to open %s for write\n", argv[2]);
    exit(-1);
  }

  while (NULL != fgets(cmd_output, sizeof(cmd_output-1), fp_exec))
  {
    fputs(cmd_output, fp_out);
  }
  pclose(fp_exec);
  fflush(fp_out);
  fclose(fp_out);

  if (argc == 3)
    return 0;

  if (!(fp_exp= fopen(argv[3], "r")))
  {
    /* if no exp file exists, we just return
       without an error = skip check */
    return(0);
  }
  if (!(fp_out= fopen(argv[2], "r")))
  {
    printf("Failed to open %s for read\n", argv[2]);
    exit(-1);
  }

  while (fgets(cmd_exp, sizeof(cmd_exp)-1, fp_exp))
  {
    if (!fgets(cmd_output, sizeof(cmd_output)-1, fp_out))
    {
      printf("Can't read from output file\n");
      goto error;
    }
    if (strcmp(cmd_output, cmd_exp))
    {
      printf("output and expected output are different\n");
      goto error;
    }
  }
  return 0;
error:
  fclose(fp_exp);
  fclose(fp_out);
  return 1;
}