• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4  */
5 
6 #include <errno.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #define TST_NO_DEFAULT_MAIN
11 #include "tst_test.h"
12 #include "old/old_device.h"
13 
14 extern struct tst_test *tst_test;
15 
16 static struct tst_test test = {
17 };
18 
print_help(void)19 static void print_help(void)
20 {
21 	fprintf(stderr, "\nUsage: tst_device acquire [size]\n");
22 	fprintf(stderr, "   or: tst_device release /path/to/device\n\n");
23 }
24 
acquire_device(int argc,char * argv[])25 static int acquire_device(int argc, char *argv[])
26 {
27 	unsigned int size = 0;
28 	const char *device;
29 
30 	if (argc > 3)
31 		return 1;
32 
33 	if (argc == 3) {
34 		size = atoi(argv[2]);
35 
36 		if (!size) {
37 			fprintf(stderr, "ERROR: Invalid device size '%s'",
38 				argv[2]);
39 			return 1;
40 		}
41 	}
42 
43 	device = tst_acquire_device__(size);
44 
45 	if (!device)
46 		return 1;
47 
48 	if (tst_clear_device(device)) {
49 		tst_release_device(device);
50 		return 1;
51 	}
52 
53 	printf("%s", device);
54 
55 	return 0;
56 }
57 
release_device(int argc,char * argv[])58 static int release_device(int argc, char *argv[])
59 {
60 	if (argc != 3)
61 		return 1;
62 
63 	return tst_release_device(argv[2]);
64 }
65 
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 	/*
69 	 * Force messages to be printed from the new library i.e. tst_test.c
70 	 *
71 	 * The new library prints messages into stderr while the old one prints
72 	 * them into stdout. When messages are printed into stderr we can
73 	 * safely do:
74 	 *
75 	 * DEV=$(tst_device acquire)
76 	 */
77 	tst_test = &test;
78 
79 	if (argc < 2)
80 		goto help;
81 
82 	if (!strcmp(argv[1], "acquire")) {
83 		if (acquire_device(argc, argv))
84 			goto help;
85 	} else if (!strcmp(argv[1], "release")) {
86 		if (release_device(argc, argv))
87 			goto help;
88 	} else {
89 		fprintf(stderr, "ERROR: Invalid COMMAND '%s'\n", argv[1]);
90 		goto help;
91 	}
92 
93 	return 0;
94 help:
95 	print_help();
96 	return 1;
97 }
98