Tests for module 'system-quote'.
[gnulib.git] / tests / test-system-quote-child.c
1 /* Child program invoked by test-system-quote-main.
2    Copyright (C) 2012 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #define EXPECTED_DATA_FILE "t-sq-data.tmp"
23
24 int
25 main (int argc, char *argv[])
26 {
27   const char *arg;
28   char expected_data[1000];
29   size_t expected_data_len;
30
31   if (argc < 2)
32     /* Expected one data argument, received none.  */
33     return 2;
34   if (argc > 2)
35     /* Expected one data argument, received more than one.  */
36     return 3;
37   arg = argv[1];
38
39   /* Read the contents of EXPECTED_DATA_FILE.  */
40   {
41     FILE *fp = fopen (EXPECTED_DATA_FILE, "rb");
42     if (fp == NULL)
43       return 4;
44     expected_data_len = fread (expected_data, 1, sizeof (expected_data), fp);
45     if (fclose (fp))
46       return 5;
47   }
48
49   if (!(strlen (arg) == expected_data_len
50         && memcmp (arg, expected_data, expected_data_len) == 0))
51     {
52       /* arg is not as expected.  */
53       fprintf (stderr, "expected: %.*s\nreceived: %s\n",
54                (int)expected_data_len, expected_data, arg);
55       return 1;
56     }
57   else
58     return 0;
59 }