Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/sgimips/stand/sgivol use err



details:   https://anonhg.NetBSD.org/src/rev/21ff259cdb4c
branches:  trunk
changeset: 794900:21ff259cdb4c
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Mar 28 15:00:53 2014 +0000

description:
use err

diffstat:

 sys/arch/sgimips/stand/sgivol/sgivol.c |  259 ++++++++++++++------------------
 1 files changed, 111 insertions(+), 148 deletions(-)

diffs (truncated from 467 to 300 lines):

diff -r b3ffee3aebc7 -r 21ff259cdb4c sys/arch/sgimips/stand/sgivol/sgivol.c
--- a/sys/arch/sgimips/stand/sgivol/sgivol.c    Fri Mar 28 13:35:13 2014 +0000
+++ b/sys/arch/sgimips/stand/sgivol/sgivol.c    Fri Mar 28 15:00:53 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sgivol.c,v 1.20 2014/03/26 16:16:06 christos Exp $     */
+/*     $NetBSD: sgivol.c,v 1.21 2014/03/28 15:00:53 christos Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -53,6 +53,7 @@
 #include <string.h>
 #include <fcntl.h>
 #include <util.h>
+#include <err.h>
 #ifndef HAVE_NBTOOL_CONFIG_H
 #include <sys/endian.h>
 #endif
@@ -80,7 +81,7 @@
 struct disklabel lbl;
 #endif
 
-unsigned char buf[512];
+char buf[512];
 
 const char *sgi_types[] = {
        "Volume Header",
@@ -99,20 +100,18 @@
        "XVM"
 };
 
-int    main(int, char *[]);
-
 void   display_vol(void);
-void   init_volhdr(void);
+void   init_volhdr(const char *);
 void   read_file(void);
-void   write_file(void);
-void   delete_file(void);
-void   move_file(void);
-void   modify_partition(void);
-void   write_volhdr(void);
+void   write_file(const char *);
+void   delete_file(const char *);
+void   move_file(const char *);
+void   modify_partition(const char *);
+void   write_volhdr(const char *);
 int    allocate_space(int);
 void   checksum_vol(void);
 int    names_match(int, const char *);
-void   usage(void);
+void   usage(void) __dead;
 
 int
 main(int argc, char *argv[])
@@ -201,80 +200,63 @@
        
        fd = open(argv[0],
            (opt_i | opt_m | opt_w | opt_d | opt_p) ? O_RDWR : O_RDONLY);
-       if (fd < 0) {
-#if HAVE_NBTOOL_CONFIG_H
-               perror("File open");
-               exit(1);
-#else
+       if (fd == -1) {
+#ifndef HAVE_NBTOOL_CONFIG_H
                snprintf(buf, sizeof(buf), "/dev/r%s%c", argv[0],
                    'a' + getrawpartition());
-               fd = open((char *)buf, (opt_i | opt_w | opt_d | opt_p) 
-                               ? O_RDWR : O_RDONLY);
-               if (fd < 0) {
-                       printf("Error opening device %s: %s\n",
-                               argv[0], strerror(errno));
-                       exit(1);
-               }
+               fd = open(buf, (opt_i | opt_w | opt_d | opt_p) 
+                   ? O_RDWR : O_RDONLY);
+               if (fd == -1)
 #endif
-       }
-       if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
-               perror("read volhdr");
-               exit(1);
-       }
-#if HAVE_NBTOOL_CONFIG_H
-       if (fstat(fd, &st) < 0) {
-               perror("stat error");
-               exit(1);
+               err(EXIT_FAILURE, "Error opening device `%s'", argv[0]);
        }
-       if (!S_ISREG(st.st_mode)) {
-               printf("Must be regular file\n");
-               exit(1);
-       }
-       if (st.st_size % SGI_BOOT_BLOCK_BLOCKSIZE) {
-               printf("Size must be multiple of %d\n", 
+
+       if (read(fd, buf, sizeof(buf)) != sizeof(buf))
+               err(EXIT_FAILURE, "Can't read volhdr from `%s'", argv[0]);
+
+#if HAVE_NBTOOL_CONFIG_H
+       if (fstat(fd, &st) == -1)
+               err(EXIT_FAILURE, "Can't stat `%s'", argv[0]);
+       if (!S_ISREG(st.st_mode))
+               errx(EXIT_FAILURE, "Not a regular file `%s'", argv[0]);
+
+       if (st.st_size % SGI_BOOT_BLOCK_BLOCKSIZE)
+               errx(EXIT_FAILURE, "Size must be multiple of %d", 
                    SGI_BOOT_BLOCK_BLOCKSIZE);
-               exit(1);
-       }
-       if (st.st_size < (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS)) {
-               printf("Minimum size of %d required\n",
+       if (st.st_size < (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS))
+               errx(EXIT_FAILURE, "Minimum size of %d required",
                    SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS);
-               exit(1);
-       }
 #else
-       if (ioctl(fd, DIOCGDINFO, &lbl) < 0) {
-               perror("DIOCGDINFO");
-               exit(1);
-       }
+       if (ioctl(fd, DIOCGDINFO, &lbl) == -1)
+               err(EXIT_FAILURE, "ioctl DIOCGDINFO failed");
 #endif
        volhdr = (struct sgi_boot_block *) buf;
        if (opt_i) {
-               init_volhdr();
-               exit(0);
+               init_volhdr(argv[0]);
+               return 0;
        }
-       if (be32toh(volhdr->magic) != SGI_BOOT_BLOCK_MAGIC) {
-               printf("No Volume Header found, magic=%x.  Use -i first.\n", 
-                      be32toh(volhdr->magic));
-               exit(1);
-       }
+       if (be32toh(volhdr->magic) != SGI_BOOT_BLOCK_MAGIC)
+               errx(EXIT_FAILURE, "No Volume Header found, magic=%x. "
+                   "Use -i first.\n", be32toh(volhdr->magic));
        if (opt_r) {
                read_file();
-               exit(0);
+               return 0;
        }
        if (opt_w) {
-               write_file();
-               exit(0);
+               write_file(argv[0]);
+               return 0;
        }
        if (opt_d) {
-               delete_file();
-               exit(0);
+               delete_file(argv[0]);
+               return 0;
        }
        if (opt_m) {
-               move_file();
-               exit(0);
+               move_file(argv[0]);
+               return 0;
        }
        if (opt_p) {
-               modify_partition();
-               exit(0);
+               modify_partition(argv[0]);
+               return 0;
        }
 
        if (!opt_q)
@@ -295,15 +277,14 @@
 {
        int cmp;
 
-       if (slot < 0 || slot >= SGI_BOOT_BLOCK_MAXVOLDIRS) {
-               printf("Internal error: bad slot in %s()\n", __func__);
-               exit(1);
-       }
+       if (slot < 0 || slot >= SGI_BOOT_BLOCK_MAXVOLDIRS)
+               errx(EXIT_FAILURE, "Internal error: bad slot in %s()",
+                   __func__);
 
        cmp = strncmp(volhdr->voldir[slot].name, b,
            sizeof(volhdr->voldir[slot].name));
 
-       return (cmp == 0 && strlen(b) <= sizeof(volhdr->voldir[slot].name));
+       return cmp == 0 && strlen(b) <= sizeof(volhdr->voldir[slot].name);
 }
 
 void
@@ -350,7 +331,7 @@
 }
 
 void
-init_volhdr(void)
+init_volhdr(const char *fname)
 {
        memset(buf, 0, sizeof(buf));
        volhdr->magic = htobe32(SGI_BOOT_BLOCK_MAGIC);
@@ -402,7 +383,7 @@
 #endif
        volhdr->partitions[0].first = htobe32(volhdr_size);
        volhdr->partitions[0].type = htobe32(SGI_PTYPE_BSD);
-       write_volhdr();
+       write_volhdr(fname);
 }
 
 void
@@ -418,23 +399,17 @@
                            strlen(volhdr->voldir[i].name)) == 0)
                        break;
        }
-       if (i >= SGI_BOOT_BLOCK_MAXVOLDIRS) {
-               printf("File '%s' not found\n", vfilename);
-               exit(1);
-       }
+       if (i >= SGI_BOOT_BLOCK_MAXVOLDIRS)
+               errx(EXIT_FAILURE, "File `%s' not found", vfilename);
        /* XXX assumes volume header starts at 0? */
        lseek(fd, be32toh(volhdr->voldir[i].block) * 512, SEEK_SET);
        fp = fopen(ufilename, "w");
-       if (fp == NULL) {
-               perror("open write");
-               exit(1);
-       }
+       if (fp == NULL)
+               err(EXIT_FAILURE, "Can't open `%s'", ufilename);
        i = be32toh(volhdr->voldir[i].bytes);
        while (i > 0) {
-               if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
-                       perror("read file");
-                       exit(1);
-               }
+               if (read(fd, buf, sizeof(buf)) != sizeof(buf))
+                       err(EXIT_FAILURE, "Error reading from `%s'", ufilename);
                fwrite(buf, 1, i > sizeof(buf) ? sizeof(buf) : i, fp);
                i -= i > sizeof(buf) ? sizeof(buf) : i;
        }
@@ -442,23 +417,23 @@
 }
 
 void
-write_file(void)
+write_file(const char *fname)
 {
        FILE *fp;
        int slot;
        size_t namelen;
        int block, i;
+       off_t off;
        struct stat st;
        char fbuf[512];
 
        if (!opt_q)
                printf("Writing file %s\n", ufilename);
-       if (stat(ufilename, &st) < 0) {
-               perror("stat");
-               exit(1);
-       }
+       if (stat(ufilename, &st) == -1)
+               err(EXIT_FAILURE, "Can't stat `%s'", ufilename);
        if (!opt_q)
-               printf("File %s has %lld bytes\n", ufilename, st.st_size);
+               printf("File %s has %ju bytes\n", ufilename,
+                   (uintmax_t)st.st_size);
        slot = -1;
        for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) {
                if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
@@ -468,10 +443,8 @@
                        break;
                }
        }
-       if (slot == -1) {
-               printf("No directory space for file %s\n", vfilename);
-               exit(1);
-       }
+       if (slot == -1)
+               errx(EXIT_FAILURE, "No directory space for file %s", vfilename);
        /* -w can overwrite, -a won't overwrite */
        if (be32toh(volhdr->voldir[slot].block) > 0) {
                if (!opt_q)
@@ -481,15 +454,13 @@
                volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
        }
        if (st.st_size == 0) {
-               printf("bad file size\n");
+               errx(EXIT_FAILURE, "Empty file `%s'", ufilename);
                exit(1);
        }
        /* XXX assumes volume header starts at 0? */
        block = allocate_space((int)st.st_size);
-       if (block < 0) {
-               printf("No space for file\n");
-               exit(1);
-       }
+       if (block < 0)
+               errx(EXIT_FAILURE, "No space for file `%s'", vfilename);
 
        /*



Home | Main Index | Thread Index | Old Index