File: util.c

package info (click to toggle)
blktool 4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 408 kB
  • ctags: 184
  • sloc: ansic: 1,369; sh: 791; makefile: 9
file content (142 lines) | stat: -rw-r--r-- 2,523 bytes parent folder | download | duplicates (8)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

/*
 * Copyright 2004 Jeff Garzik
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 */

#include "blktool-config.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/hdreg.h>
#include <scsi/sg.h>

#include <glib.h>
#include "blktool.h"


void pdie(const char *msg, int perr)
{
	if (perr)
		perror(msg);
	else
		fprintf(stderr, msg);
	if (blkdev >= 0)
		close(blkdev);
	exit(1);
}

void flag_push(const char *flag_str)
{
	g_ptr_array_add(flag_arr, (gpointer) flag_str);
}

void list_dump(const char *list_name, GPtrArray *arr)
{
	size_t name_start = strlen(list_name);
	int col = name_start;
	size_t len;
	int i;
	const char *s;

	if (arr->len > 0)
		printf("%s:", list_name);

	for (i = 0; i < arr->len; i++) {
		s = g_ptr_array_index(arr, i);
		len = strlen(s) + 1;

		if ((col + len) > 76) {
			printf("\n%s:", list_name);
			col = name_start;
		}

		printf(" %s", s);
		col += len;
	}

	if (arr->len > 0)
		printf("\n");
}

void ioctl_call(int cmd, const char *cmd_name, void *arg)
{
	if (ioctl(blkdev, cmd, arg))
		pdie(cmd_name, 1);
}

int parse_bool(int argc, char **argv, struct bool_command *bcm)
{
	const char *arg = argv[optind];

	if (!strcmp(arg, "1"))
		return 1;
	if (!strcmp(arg, "on"))
		return 1;
	if (!strcmp(arg, bcm->str_true))
		return 1;

	if (!strcmp(arg, "0"))
		return 0;
	if (!strcmp(arg, "off"))
		return 0;
	if (!strcmp(arg, bcm->str_false))
		return 0;

	fprintf(stderr, MSG_INVALID_ARG, arg, bcm->cmd.cmd);
	exit(1);
	return -1;
}

int parse_int(int argc, char **argv, struct command *cmd)
{
	const char *arg = argv[optind];
	char *endp = NULL;
	long tmp;

	errno = 0;

	tmp = strtol(arg, &endp, 10);
	if ((arg == endp) || (errno != 0))
		goto err_out;

	return tmp;

err_out:
	fprintf(stderr, MSG_INVALID_ARG, arg, cmd->cmd);
	exit(1);
	return -1;
}

void noread_arg_check(int argc, char **argv)
{
	if (optind == argc) {
		fprintf(stderr, MSG_INVALID_CMD_NOREAD, argv[optind - 1]);
		exit(1);
	}
	else if ((optind + 1) != argc) {
		fprintf(stderr, MSG_INVALID_CMD_ARGS, argv[optind - 1]);
		exit(1);
	}
}

int get_bool(int argc, char **argv, struct command *cmd)
{
	noread_arg_check(argc, argv);
	return parse_bool(argc, argv, (struct bool_command *) cmd);
}

int get_int(int argc, char **argv, struct command *cmd)
{
	noread_arg_check(argc, argv);
	return parse_int(argc, argv, cmd);
}