1 /*
2  * Copyright © 2012 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <limits.h>
30 
31 #include "test-runner.h"
32 
33 static int
parse_count(const char * str,int * value)34 parse_count(const char *str, int *value)
35 {
36 	char *end;
37 	long v;
38 
39 	errno = 0;
40 	v = strtol(str, &end, 10);
41 	if ((errno == ERANGE && (v == LONG_MAX || v == LONG_MIN)) ||
42 	    (errno != 0 && v == 0) ||
43 	    (end == str) ||
44 	    (*end != '\0')) {
45 		return -1;
46 	}
47 
48 	if (v < 0 || v > INT_MAX) {
49 		return -1;
50 	}
51 
52 	*value = v;
53 	return 0;
54 }
55 
main(int argc,char * argv[])56 int main(int argc, char *argv[])
57 {
58 	int expected;
59 
60 	if (argc != 2)
61 		goto help_out;
62 
63 	if (parse_count(argv[1], &expected) < 0)
64 		goto help_out;
65 
66 	if (count_open_fds() == expected)
67 		return EXIT_SUCCESS;
68 	else
69 		return EXIT_FAILURE;
70 
71 help_out:
72 	fprintf(stderr, "Usage: %s N\n"
73 		"where N is the expected number of open file descriptors.\n"
74 		"This program exits with a failure if the number "
75 		"does not match exactly.\n", argv[0]);
76 
77 	return EXIT_FAILURE;
78 }
79