raw: clean up coding style

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2012-03-17 20:46:12 +01:00 committed by Karel Zak
parent d5fc1b1581
commit cd12136371
1 changed files with 30 additions and 45 deletions

View File

@ -5,23 +5,23 @@
* *
* This file may be redistributed under the terms of the GNU General * This file may be redistributed under the terms of the GNU General
* Public License, version 2. * Public License, version 2.
* *
* Copyright Red Hat Software, 1999, 2000 * Copyright Red Hat Software, 1999, 2000
* *
*/ */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/sysmacros.h>
#include <linux/raw.h>
#include <linux/major.h>
#include <getopt.h> #include <getopt.h>
#include <linux/major.h>
#include <linux/raw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include "c.h" #include "c.h"
#include "nls.h" #include "nls.h"
@ -30,20 +30,18 @@
#define EXIT_RAW_ACCESS 3 #define EXIT_RAW_ACCESS 3
#define EXIT_RAW_IOCTL 4 #define EXIT_RAW_IOCTL 4
#define RAW_NR_MINORS 8192 #define RAW_NR_MINORS 8192
int do_query = 0; int do_query;
int do_query_all = 0; int do_query_all;
int master_fd; int master_fd;
int raw_minor; int raw_minor;
void open_raw_ctl(void); void open_raw_ctl(void);
static int query(int minor_raw, const char *raw_name, int quiet); static int query(int minor_raw, const char *raw_name, int quiet);
static int bind(int minor_raw, int block_major, int block_minor); static int bind(int minor_raw, int block_major, int block_minor);
static void __attribute__ ((__noreturn__)) usage(int err) static void __attribute__ ((__noreturn__)) usage(int err)
{ {
FILE *out = err == EXIT_SUCCESS ? stdout : stderr; FILE *out = err == EXIT_SUCCESS ? stdout : stderr;
@ -64,7 +62,6 @@ static void __attribute__ ((__noreturn__)) usage(int err)
exit(err); exit(err);
} }
long strtol_octal_or_err(const char *str, const char *errmesg) long strtol_octal_or_err(const char *str, const char *errmesg)
{ {
long num; long num;
@ -87,15 +84,14 @@ long strtol_octal_or_err(const char *str, const char *errmesg)
return 0; return 0;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int c; int c;
char * raw_name; char *raw_name;
char * block_name; char *block_name;
int retval; int retval;
int block_major, block_minor; int block_major, block_minor;
int i, rc; int i, rc;
struct stat statbuf; struct stat statbuf;
@ -111,7 +107,7 @@ int main(int argc, char *argv[])
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE); textdomain(PACKAGE);
while ((c = getopt_long(argc, argv, "qaVh", longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, "qaVh", longopts, NULL)) != -1)
switch (c) { switch (c) {
case 'q': case 'q':
do_query = 1; do_query = 1;
@ -127,12 +123,10 @@ int main(int argc, char *argv[])
default: default:
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
} }
}
/* /*
* Check for, and open, the master raw device, /dev/raw * Check for, and open, the master raw device, /dev/raw
*/ */
open_raw_ctl(); open_raw_ctl();
if (do_query_all) { if (do_query_all) {
@ -170,18 +164,16 @@ int main(int argc, char *argv[])
return query(raw_minor, raw_name, 0); return query(raw_minor, raw_name, 0);
/* /*
* It's not a query, so we still have some parsing to do. Have * It's not a query, so we still have some parsing to do. Have we been
* we been given a block device filename or a major/minor pair? * given a block device filename or a major/minor pair?
*/ */
switch (argc - optind) { switch (argc - optind) {
case 1: case 1:
block_name = argv[optind]; block_name = argv[optind];
retval = stat(block_name, &statbuf); retval = stat(block_name, &statbuf);
if (retval) if (retval)
err(EXIT_RAW_ACCESS, err(EXIT_RAW_ACCESS,
_("Cannot locate block device '%s'"), _("Cannot locate block device '%s'"), block_name);
block_name);
if (!S_ISBLK(statbuf.st_mode)) if (!S_ISBLK(statbuf.st_mode))
errx(EXIT_RAW_ACCESS, errx(EXIT_RAW_ACCESS,
_("Device '%s' is not a block device"), _("Device '%s' is not a block device"),
@ -200,14 +192,12 @@ int main(int argc, char *argv[])
break; break;
default: default:
block_major = block_minor = 0; /* just to keep gcc happy */
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
} }
return bind(raw_minor, block_major, block_minor); return bind(raw_minor, block_major, block_minor);
} }
void open_raw_ctl(void) void open_raw_ctl(void)
{ {
master_fd = open(_PATH_RAWDEVCTL, O_RDWR, 0); master_fd = open(_PATH_RAWDEVCTL, O_RDWR, 0);
@ -220,18 +210,15 @@ void open_raw_ctl(void)
} }
} }
static int query(int minor_raw, const char *raw_name, int quiet) static int query(int minor_raw, const char *raw_name, int quiet)
{ {
struct raw_config_request rq; struct raw_config_request rq;
static int has_worked = 0; static int has_worked = 0;
int retval;
if (raw_name) { if (raw_name) {
struct stat statbuf; struct stat statbuf;
retval = stat(raw_name, &statbuf); if (!stat(raw_name, &statbuf))
if (retval)
err(EXIT_RAW_ACCESS, err(EXIT_RAW_ACCESS,
_("Cannot locate raw device '%s'"), raw_name); _("Cannot locate raw device '%s'"), raw_name);
if (!S_ISCHR(statbuf.st_mode)) if (!S_ISCHR(statbuf.st_mode))
@ -245,8 +232,7 @@ static int query(int minor_raw, const char *raw_name, int quiet)
} }
rq.raw_minor = minor_raw; rq.raw_minor = minor_raw;
retval = ioctl(master_fd, RAW_GETBIND, &rq); if (ioctl(master_fd, RAW_GETBIND, &rq) < 0) {
if (retval < 0) {
if (quiet && errno == ENODEV) if (quiet && errno == ENODEV)
return 3; return 3;
if (has_worked && errno == EINVAL) if (has_worked && errno == EINVAL)
@ -254,9 +240,9 @@ static int query(int minor_raw, const char *raw_name, int quiet)
err(EXIT_RAW_IOCTL, _("Error querying raw device")); err(EXIT_RAW_IOCTL, _("Error querying raw device"));
} }
/* If one query has worked, mark that fact so that we don't /* If one query has worked, mark that fact so that we don't report
* report spurious fatal errors if raw(8) has been built to * spurious fatal errors if raw(8) has been built to support more raw
* support more raw minor numbers than the kernel has. */ * minor numbers than the kernel has. */
has_worked = 1; has_worked = 1;
if (quiet && !rq.block_major && !rq.block_minor) if (quiet && !rq.block_major && !rq.block_minor)
return 0; return 0;
@ -270,7 +256,7 @@ static int bind(int minor_raw, int block_major, int block_minor)
{ {
struct raw_config_request rq; struct raw_config_request rq;
rq.raw_minor = minor_raw; rq.raw_minor = minor_raw;
rq.block_major = block_major; rq.block_major = block_major;
rq.block_minor = block_minor; rq.block_minor = block_minor;
if (!ioctl(master_fd, RAW_SETBIND, &rq)) if (!ioctl(master_fd, RAW_SETBIND, &rq))
@ -280,4 +266,3 @@ static int bind(int minor_raw, int block_major, int block_minor)
(int)rq.block_minor); (int)rq.block_minor);
return 0; return 0;
} }