Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (c) 2017 Cyril Hrubis <chrubis (at) suse.cz>
      3  *
      4  * This program is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU General Public License as
      6  * published by the Free Software Foundation; either version 2 of
      7  * the License, or (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it would 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, write the Free Software Foundation,
     16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     17  */
     18 
     19 #include <errno.h>
     20 #include <limits.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #define TST_NO_DEFAULT_MAIN
     24 #include "tst_test.h"
     25 #include "old/old_device.h"
     26 
     27 extern struct tst_test *tst_test;
     28 
     29 static struct tst_test test = {
     30 };
     31 
     32 static void print_help(void)
     33 {
     34 	printf("\nUsage: tst_device acquire [size]\n");
     35 	printf("   or: tst_device release /path/to/device\n\n");
     36 }
     37 
     38 static int acquire_device(int argc, char *argv[])
     39 {
     40 	unsigned int size = 0;
     41 	const char *device;
     42 
     43 	if (argc > 3)
     44 		return 1;
     45 
     46 	if (argc == 3) {
     47 		size = atoi(argv[2]);
     48 
     49 		if (!size) {
     50 			fprintf(stderr, "ERROR: Invalid device size '%s'",
     51 				argv[2]);
     52 			return 1;
     53 		}
     54 	}
     55 
     56 	device = tst_acquire_device__(size);
     57 
     58 	if (!device)
     59 		return 1;
     60 
     61 	if (tst_clear_device(device)) {
     62 		tst_release_device(device);
     63 		return 1;
     64 	}
     65 
     66 	printf("%s", device);
     67 
     68 	return 0;
     69 }
     70 
     71 static int release_device(int argc, char *argv[])
     72 {
     73 	if (argc != 3)
     74 		return 1;
     75 
     76 	return tst_release_device(argv[2]);
     77 }
     78 
     79 int main(int argc, char *argv[])
     80 {
     81 	/*
     82 	 * Force messages to be printed from the new library i.e. tst_test.c
     83 	 *
     84 	 * The new library prints messages into stderr while the old one prints
     85 	 * them into stdout. When messages are printed into stderr we can
     86 	 * safely do:
     87 	 *
     88 	 * DEV=$(tst_device acquire)
     89 	 */
     90 	tst_test = &test;
     91 
     92 	if (argc < 2)
     93 		goto help;
     94 
     95 	if (!strcmp(argv[1], "acquire")) {
     96 		if (acquire_device(argc, argv))
     97 			goto help;
     98 	} else if (!strcmp(argv[1], "release")) {
     99 		if (release_device(argc, argv))
    100 			goto help;
    101 	} else {
    102 		fprintf(stderr, "ERROR: Invalid COMMAND '%s'\n", argv[1]);
    103 		goto help;
    104 	}
    105 
    106 	return 0;
    107 help:
    108 	print_help();
    109 	return 1;
    110 }
    111