Get rid of all "#if 0" content.
[oweals/busybox.git] / miscutils / hdparm.c
index 3c0bcc9e90385509f505e65b3f11a934bc1d1d7f..925644e1fc8943ff1cf3ae45de60e584bbd714fa 100644 (file)
  *          - by Mark Lord (C) 1994-2002 -- freely distributable
  */
 
-#include <unistd.h>
+#include "busybox.h"
 #include <string.h>
-#include <stdlib.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <ctype.h>
-#include <endian.h>
 #include <sys/ioctl.h>
-#include <sys/shm.h>
 #include <sys/sysmacros.h>
-#include <sys/time.h>
 #include <sys/times.h>
-#include <sys/mount.h>
-#include <getopt.h>
-#include "busybox.h"
+#include <sys/mman.h>
 #include <linux/types.h>
 #include <linux/hdreg.h>
-#include <asm/byteorder.h>
-
 
 #if BB_BIG_ENDIAN && !defined(__USE_XOPEN)
 # define __USE_XOPEN
 #endif
+#include <unistd.h>
 
 /* device types */
 /* ------------ */
@@ -181,11 +174,11 @@ static const char * const pkt_str[] = {
        "",                                     /* word 0, bits 12-8 = 1c */
        "",                                     /* word 0, bits 12-8 = 1d */
        "",                                     /* word 0, bits 12-8 = 1e */
-       "Unknown",                              /* word 0, bits 12-8 = 1f */
+       "Unknown",                      /* word 0, bits 12-8 = 1f */
 };
 
 static const char * const ata1_cfg_str[] = {                   /* word 0 in ATA-1 mode */
-       "reserved",                             /* bit 0 */
+       "Reserved",                             /* bit 0 */
        "hard sectored",                        /* bit 1 */
        "soft sectored",                        /* bit 2 */
        "not MFM encoded ",                     /* bit 3 */
@@ -263,39 +256,39 @@ static const char * const ata1_cfg_str[] = {                      /* word 0 in ATA-1 mode */
 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
 static const char *minor_str[MINOR_MAX+2] = {                  /* word 81 value: */
        "Unspecified",                                  /* 0x0000       */
-       "ATA-1 X3T9.2 781D prior to revision 4",        /* 0x0001       */
+       "ATA-1 X3T9.2 781D prior to rev.4",     /* 0x0001       */
        "ATA-1 published, ANSI X3.221-1994",            /* 0x0002       */
-       "ATA-1 X3T9.2 781D revision 4",                 /* 0x0003       */
+       "ATA-1 X3T9.2 781D rev.4",                      /* 0x0003       */
        "ATA-2 published, ANSI X3.279-1996",            /* 0x0004       */
-       "ATA-2 X3T10 948D prior to revision 2k",        /* 0x0005       */
-       "ATA-3 X3T10 2008D revision 1",                 /* 0x0006       */
-       "ATA-2 X3T10 948D revision 2k",                 /* 0x0007       */
-       "ATA-3 X3T10 2008D revision 0",                 /* 0x0008       */
-       "ATA-2 X3T10 948D revision 3",                  /* 0x0009       */
+       "ATA-2 X3T10 948D prior to rev.2k",     /* 0x0005       */
+       "ATA-3 X3T10 2008D rev.1",                      /* 0x0006       */
+       "ATA-2 X3T10 948D rev.2k",                      /* 0x0007       */
+       "ATA-3 X3T10 2008D rev.0",                      /* 0x0008       */
+       "ATA-2 X3T10 948D rev.3",                       /* 0x0009       */
        "ATA-3 published, ANSI X3.298-199x",            /* 0x000a       */
-       "ATA-3 X3T10 2008D revision 6",                 /* 0x000b       */
-       "ATA-3 X3T13 2008D revision 7 and 7a",          /* 0x000c       */
-       "ATA/ATAPI-4 X3T13 1153D revision 6",           /* 0x000d       */
-       "ATA/ATAPI-4 T13 1153D revision 13",            /* 0x000e       */
-       "ATA/ATAPI-4 X3T13 1153D revision 7",           /* 0x000f       */
-       "ATA/ATAPI-4 T13 1153D revision 18",            /* 0x0010       */
-       "ATA/ATAPI-4 T13 1153D revision 15",            /* 0x0011       */
+       "ATA-3 X3T10 2008D rev.6",                      /* 0x000b       */
+       "ATA-3 X3T13 2008D rev.7 and 7a",               /* 0x000c       */
+       "ATA/ATAPI-4 X3T13 1153D rev.6",                /* 0x000d       */
+       "ATA/ATAPI-4 T13 1153D rev.13",         /* 0x000e       */
+       "ATA/ATAPI-4 X3T13 1153D rev.7",                /* 0x000f       */
+       "ATA/ATAPI-4 T13 1153D rev.18",         /* 0x0010       */
+       "ATA/ATAPI-4 T13 1153D rev.15",         /* 0x0011       */
        "ATA/ATAPI-4 published, ANSI INCITS 317-1998",  /* 0x0012       */
-       "ATA/ATAPI-5 T13 1321D revision 3",
-       "ATA/ATAPI-4 T13 1153D revision 14",            /* 0x0014       */
-       "ATA/ATAPI-5 T13 1321D revision 1",             /* 0x0015       */
+       "ATA/ATAPI-5 T13 1321D rev.3",
+       "ATA/ATAPI-4 T13 1153D rev.14",         /* 0x0014       */
+       "ATA/ATAPI-5 T13 1321D rev.1",          /* 0x0015       */
        "ATA/ATAPI-5 published, ANSI INCITS 340-2000",  /* 0x0016       */
-       "ATA/ATAPI-4 T13 1153D revision 17",            /* 0x0017       */
-       "ATA/ATAPI-6 T13 1410D revision 0",             /* 0x0018       */
-       "ATA/ATAPI-6 T13 1410D revision 3a",            /* 0x0019       */
-       "ATA/ATAPI-7 T13 1532D revision 1",             /* 0x001a       */
-       "ATA/ATAPI-6 T13 1410D revision 2",             /* 0x001b       */
-       "ATA/ATAPI-6 T13 1410D revision 1",             /* 0x001c       */
+       "ATA/ATAPI-4 T13 1153D rev.17",         /* 0x0017       */
+       "ATA/ATAPI-6 T13 1410D rev.0",          /* 0x0018       */
+       "ATA/ATAPI-6 T13 1410D rev.3a",         /* 0x0019       */
+       "ATA/ATAPI-7 T13 1532D rev.1",          /* 0x001a       */
+       "ATA/ATAPI-6 T13 1410D rev.2",          /* 0x001b       */
+       "ATA/ATAPI-6 T13 1410D rev.1",          /* 0x001c       */
        "ATA/ATAPI-7 published, ANSI INCITS 397-2005",  /* 0x001d       */
-       "ATA/ATAPI-7 T13 1532D revision 0",             /* 0x001e       */
+       "ATA/ATAPI-7 T13 1532D rev.0",          /* 0x001e       */
        "Reserved"                                      /* 0x001f       */
        "Reserved"                                      /* 0x0020       */
-       "ATA/ATAPI-7 T13 1532D revision 4a",            /* 0x0021       */
+       "ATA/ATAPI-7 T13 1532D rev.4a",         /* 0x0021       */
        "ATA/ATAPI-6 published, ANSI INCITS 361-2002",  /* 0x0022       */
        "Reserved"                                      /* 0x0023-0xfffe*/
 };
@@ -368,8 +361,8 @@ static const char * const cmd_feat_str[] = {
                                                /* --------------*/
        "",                                     /* word 83 bit 15: !valid bit */
        "",                                     /* word 83 bit 14:  valid bit */
-       "FLUSH CACHE EXT command",              /* word 83 bit 13 */
-       "Mandatory FLUSH CACHE command ",       /* word 83 bit 12 */
+       "FLUSH CACHE EXT cmd",          /* word 83 bit 13 */
+       "Mandatory FLUSH CACHE cmd ",   /* word 83 bit 12 */
        "Device Configuration Overlay feature set ",
        "48-bit Address feature set ",          /* word 83 bit 10 */
        "",
@@ -403,7 +396,8 @@ static const char * const cmd_feat_str[] = {
 
 static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN;
 static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
-
+#else
+void identify_from_stdin(void);
 #endif
 
 
@@ -454,8 +448,6 @@ static const char * const secu_str[] = {
 #define SIG                    0x00ff  /* signature location */
 #define SIG_VAL                        0x00A5  /* signature value */
 
-#define VERSION "v5.4"
-
 #define TIMING_MB              64
 #define TIMING_BUF_MB          1
 #define TIMING_BUF_BYTES       (TIMING_BUF_MB * 1024 * 1024)
@@ -465,10 +457,6 @@ static const char * const secu_str[] = {
 #undef DO_FLUSHCACHE           /* under construction: force cache flush on -W0 */
 
 /* Busybox messages and functions */
-
-static const char bb_msg_shared_mem[] = "could not %s sharedmem buf";
-static const char bb_msg_op_not_supp[] = " operation not supported on %s disks";
-
 static int bb_ioctl(int fd, int request, void *argp, const char *string)
 {
        int e = ioctl(fd, request, argp);
@@ -485,51 +473,31 @@ static int bb_ioctl_alt(int fd, int cmd, unsigned char *args, int alt, const cha
        return bb_ioctl(fd, cmd, args, string);
 }
 
-
-static void if_printf(unsigned long i, char *fmt, ... )
-{
-       va_list ap;
-       va_start(ap, fmt);
-       if (i)
-               vprintf(fmt, ap);
-       va_end(ap);
-}
-
 static void on_off(unsigned int value);
 
-static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
+static void print_flag_on_off(unsigned long get_arg, const char *s, unsigned long arg)
 {
+
        if (get_arg)
        {
-               printf(fmt, arg);
+               printf(" setting %s to %ld", s, arg);
                on_off(arg);
        }
 }
 
 static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
-                                                        const char * fmt)
+                                                        const char * str)
 {
        if (ioctl(fd, request, &argp) != 0)
                bb_perror_msg(" %s", string);
        else
        {
-               printf(fmt, (unsigned long) argp);
+               printf(" %s\t= %2ld", str, (unsigned long) argp);
                on_off((unsigned long) argp);
        }
 }
 
 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
-static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
-{
-       va_list ap;
-       va_start(ap, fmt2);
-       if (i)
-               vprintf(fmt1, ap);
-       else
-               vprintf(fmt2, ap);
-       va_end(ap);
-}
-
 static void print_ascii(uint16_t *p, uint8_t length);
 
 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
@@ -540,34 +508,7 @@ static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
                print_ascii(&val[i], n);
        }
 }
-
-static void if_strcat(unsigned long test, char *modes, char *string)
-{
-       if (test)
-               strcat(modes,string);
-}
 #endif
-
-static void sync_and_sleep(int i)
-{
-       sync();
-       sleep(i);
-}
-
-static uint16_t check_if_min_and_set_val(uint16_t a, uint16_t b)
-{
-       if ( a < b)
-               a = b;
-       return a;
-}
-
-static uint16_t check_if_maj_and_set_val(uint16_t a, uint16_t b)
-{
-       if ( a > b)
-               a = b;
-       return a;
-}
-
 /* end of  busybox specific stuff */
 
 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
@@ -605,7 +546,7 @@ static void print_ascii(uint16_t *p, uint8_t length) {
                        break;
                if ((cl = (char) 0x00ff&(*p)) != ' ')
                {
-                       if_printf((cl != '\0'),"%c",cl);
+                       if (cl != '\0') printf("%c",cl);
                        p++;
                        ii++;
                        break;
@@ -623,10 +564,8 @@ static void print_ascii(uint16_t *p, uint8_t length) {
        printf("\n");
 }
 
-/* identify() is the only extern function used across two source files.  The
-   others, though, were declared in hdparm.c with global scope; since other
-   functions in that file have static (file) scope, I assume the difference is
-   intentional. */
+// Parse 512 byte disk identification block and print much crap.
+
 static void identify(uint16_t *id_supplied)
 {
        uint16_t buf[256];
@@ -637,6 +576,9 @@ static void identify(uint16_t *id_supplied)
        uint8_t  chksum = 0;
        uint32_t ll, mm, nn, oo;
        uint64_t bbbig; /* (:) */
+       const char *strng;
+
+       // Adjust for endianness if necessary.
 
        if (BB_BIG_ENDIAN) {
                swab(id_supplied, buf, sizeof(buf));
@@ -669,9 +611,7 @@ static void identify(uint16_t *id_supplied)
                /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
                bb_error_msg_and_die("Unknown device type");
 
-       if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
-       printf("removable media\n");
-
+       printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
        /* Info from the specific configuration word says whether or not the
         * ID command completed correctly.  It is only defined, however in
         * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
@@ -683,10 +623,10 @@ static void identify(uint16_t *id_supplied)
           (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
        {
                like_std = 5;
-               if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
-                                       "powers-up in standby; SET FEATURES subcmd spins-up.\n");
-               if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
-                                       "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
+               if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
+                       printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
+               if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
+                       printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
        }
 
        /* output the model and serial numbers and the fw revision */
@@ -703,9 +643,9 @@ static void identify(uint16_t *id_supplied)
        {
                if (val[MINOR] && (val[MINOR] <= MINOR_MAX))
                {
-                       like_std=check_if_min_and_set_val(like_std, 3);
+                       if(like_std < 3) like_std = 3;
                        std = actual_ver[val[MINOR]];
-                       if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
+                       if(std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]);
 
                }
                /* looks like when they up-issue the std, they obsolete one;
@@ -726,17 +666,17 @@ static void identify(uint16_t *id_supplied)
                                                like_std = ii;
                                                kk = like_std >4 ? like_std-4: 0;
                                        }
-                                       min_std=check_if_maj_and_set_val(min_std, ii);
+                                       if(min_std > ii) min_std = ii;
                                }
                                jj <<= 1;
                        }
-                       like_std=check_if_min_and_set_val(like_std, 3);
+                       if(like_std < 3) like_std = 3;
                }
                /* Figure out what standard the device is using if it hasn't told
                 * us.  If we know the std, check if the device is using any of
                 * the words from the next level up.  It happens.
                 */
-               like_std=check_if_min_and_set_val(like_std, std);
+               if(like_std < std) like_std = std;
 
                if (((std == 5) || (!std && (like_std < 6))) &&
                        ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
@@ -798,11 +738,11 @@ static void identify(uint16_t *id_supplied)
                        jj = val[CDR_MAJOR] >> 1;
                        for (ii = 1; ii <15; ii++)
                        {
-                               if_printf((jj & 0x0001),"-%u ", ii);
+                               if(jj & 0x0001) printf("-%u ", ii);
                                jj >>= 1;
                        }
                }
-               if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
+               printf("%s\n", (!kk) ? "\n\tLikely used CD-ROM ATAPI-1" : "" );
                /* the cdrom stuff is more like ATA-2 than anything else, so: */
                like_std = 2;
        }
@@ -817,41 +757,29 @@ static void identify(uint16_t *id_supplied)
                jj = val[GEN_CONFIG] >> 1;
                for (ii = 1; ii < 15; ii++)
                {
-                       if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
+                       if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]);
                        jj >>=1;
                }
        }
        if (dev == ATAPI_DEV)
        {
-               printf("\tDRQ response: "); /* Data Request (DRQ) */
-               switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
-               {
-                       case DRQ_3MS_VAL :
-                               printf("3ms.\n");
-                               break;
-                       case DRQ_INTR_VAL :
-                               printf("<=10ms with INTRQ\n");
-                               break;
-                       case DRQ_50US_VAL :
-                               printf("50us.\n");
-                               break;
-                       default :
-                               printf("unknown.\n");
-                               break;
-               }
-               printf("\tPacket size: ");
-               switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
-               {
-                       case PKT_SIZE_12_VAL :
-                               printf("12 bytes\n");
-                               break;
-                       case PKT_SIZE_16_VAL :
-                               printf("16 bytes\n");
-                               break;
-                       default :
-                               printf("Unknown\n");
-                               break;
-               }
+               if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) ==  DRQ_3MS_VAL)
+                       strng = "3ms";
+               else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) ==  DRQ_INTR_VAL)
+                       strng = "<=10ms with INTRQ";
+               else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) ==  DRQ_50US_VAL)
+                       strng ="50us";
+               else 
+                       strng = "Unknown";
+               printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
+
+               if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
+                       strng = "12 bytes";
+               else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
+                       strng = "16 bytes";
+               else
+                       strng = "Unknown";
+               puts(strng);
        }
        else
        {
@@ -866,8 +794,8 @@ static void identify(uint16_t *id_supplied)
                        printf("\tLogical\t\tmax\tcurrent\n\tcylinders\t%u\t%u\n\theads\t\t%u\t%u\n\tsectors/track\t%u\t%u\n\t--\n",
                                        val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
 
-                       if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
-                                               "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
+                       if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
+                               printf("\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
 
                        if (jj)
                        {
@@ -892,17 +820,19 @@ static void identify(uint16_t *id_supplied)
                                (uint64_t)val[LBA_48_MSB]       << 32 |
                                (uint64_t)val[LBA_MID]  << 16 |
                                        val[LBA_LSB] ;
-                       printf("\tLBA48  user addressable sectors:%11llu\n",bbbig);
+                       printf("\tLBA48  user addressable sectors:%11"PRIu64"\n",bbbig);
                }
 
                if (!bbbig)
                        bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
-               printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
+               printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n",bbbig>>11);
                bbbig = (bbbig<<9)/1000000;
-               printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
-
-               if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
+               printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ",bbbig);
 
+               if (bbbig > 1000)
+                       printf("(%"PRIu64" GB)\n", bbbig/1000);
+               else 
+                       printf("\n");
        }
 
        /* hw support of commands (capabilities) */
@@ -910,48 +840,39 @@ static void identify(uint16_t *id_supplied)
 
        if (dev == ATAPI_DEV)
        {
-               if (eqpt != CDROM)
-                       if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
-
-               if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
+               if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
+               if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
        }
-       if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
+       if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
 
        if (like_std != 1)
        {
-               printf("IORDY");
-               if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
-               if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
-               printf(" be disabled)\n");
+               printf("IORDY%s(can%s be disabled)\n", 
+                               !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "", 
+                               (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
        }
        else
                printf("no IORDY\n");
 
        if ((like_std == 1) && val[BUF_TYPE])
        {
-               kk = val[BUF_TYPE];
-               printf("\tBuffer type: %04x: ",kk);
-               if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
-               if_printf((kk > 2)," with read caching ability");
-               printf("\n");
+               printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
+                               (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector", 
+                               (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
        }
-       jj = 0;
+
        if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
        {
-               printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
-               jj = 1;
+               printf("\tBuffer size: %.1fkB\n",(float)val[BUFFER__SIZE]/2);
        }
        if ((min_std < 4) && (val[RW_LONG]))
        {
-               printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
-               jj = 1;
+               printf("\tbytes avail on r/w long: %u\n",val[RW_LONG]);
        }
        if ((eqpt != CDROM) && (like_std > 3))
        {
-               printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
-               jj = 1;
+               printf("\tQueue depth: %u\n",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
        }
-       if_printf(jj,"\n");
 
        if (dev == ATA_DEV)
        {
@@ -959,8 +880,7 @@ static void identify(uint16_t *id_supplied)
                        printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
                else
                {
-                       printf("\tStandby timer values: spec'd by ");
-                       if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
+                       printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
                        if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
                                printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
                        else
@@ -972,8 +892,10 @@ static void identify(uint16_t *id_supplied)
                else
                {
                        printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
-                       if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
-                                                       "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
+                       if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
+                               printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
+                       else
+                               printf("?\n");
                }
                if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
                {
@@ -989,23 +911,22 @@ static void identify(uint16_t *id_supplied)
                        else
                                printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
                }
-               if (like_std > 5)
-               {
-                       if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
+               if (like_std > 5 && val[ACOUSTIC]) {
+                       printf("\tRecommended acoustic management value: %u, current value: %u\n",
                                                                        (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
                }
        }
        else
        {
                 /* ATAPI */
-               if (eqpt != CDROM)
-                       if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
+               if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
+                       printf("\tATA sw reset required\n");
 
                if (val[PKT_REL] || val[SVC_NBSY])
                {
                        printf("\tOverlap support:");
-                       if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
-                       if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
+                       if (val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]);
+                       if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
                        printf("\n");
                }
        }
@@ -1016,8 +937,8 @@ static void identify(uint16_t *id_supplied)
                printf("not supported\n");
        else
        {
-               if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
-                                       " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
+               if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
+                       printf(" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
                if (val[SINGLE_DMA])
                {
                        jj = val[SINGLE_DMA];
@@ -1036,18 +957,18 @@ static void identify(uint16_t *id_supplied)
                        kk = val[ULTRA_DMA] >> 8;
                        err_dma += mode_loop(jj,kk,'u',&have_mode);
                }
-               if_printf((err_dma || !have_mode),"(?)");
+               if (err_dma || !have_mode) printf("(?)");
                printf("\n");
 
-               if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
-                                       "\t     Interleaved DMA support\n");
+               if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
+                       printf("\t\tInterleaved DMA support\n");
 
                if ((val[WHATS_VALID] & OK_W64_70) &&
                   (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
                {
-                       printf("\t     Cycle time:");
-                       if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
-                       if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
+                       printf("\t\tCycle time:");
+                       if (val[DMA_TIME_MIN]) printf(" min=%uns",val[DMA_TIME_MIN]);
+                       if (val[DMA_TIME_NORM]) printf(" recommended=%uns",val[DMA_TIME_NORM]);
                        printf("\n");
                }
        }
@@ -1061,7 +982,7 @@ static void identify(uint16_t *id_supplied)
                jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
                for (ii = 0; ii <= PIO_MODE_MAX ; ii++)
                {
-                       if_printf((jj & 0x0001),"pio%d ",ii);
+                       if (jj & 0x0001) printf("pio%d ",ii);
                        jj >>=1;
                }
                printf("\n");
@@ -1079,9 +1000,9 @@ static void identify(uint16_t *id_supplied)
        {
                if (val[PIO_NO_FLOW] || val[PIO_FLOW])
                {
-                       printf("\t     Cycle time:");
-                       if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
-                       if_printf(val[PIO_FLOW],"  IORDY flow control=%uns", val[PIO_FLOW]);
+                       printf("\t\tCycle time:");
+                       if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
+                       if (val[PIO_FLOW]) printf("  IORDY flow control=%uns", val[PIO_FLOW]);
                        printf("\n");
                }
        }
@@ -1095,8 +1016,7 @@ static void identify(uint16_t *id_supplied)
                {
                        if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
                        {
-                               if_else_printf((kk & 0x8000),"\t   *","\t");
-                               printf("\t%s\n",cmd_feat_str[ii]);
+                               printf("\t%s\t%s\n", (kk & 0x8000) ? "   *" : "", cmd_feat_str[ii]);
                        }
                        jj <<=1; kk<<=1;
                        if (ii%16 == 15)
@@ -1111,29 +1031,29 @@ static void identify(uint16_t *id_supplied)
                        }
                }
        }
-       if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
-                               "\tRemovable Media Status Notification feature set supported\n");
+       /* Removable Media Status Notification feature set */
+       if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
+               printf("\t%s supported\n", cmd_feat_str[27]);
 
 
        /* security */
        if ((eqpt != CDROM) && (like_std > 3) &&
           (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
        {
-               printf("Security: \n");
-               if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
-                                       "\tMaster password revision code = %u\n",val[PSWD_CODE]);
+               printf("Security:\n");
+               if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
+                       printf("\tMaster password revision code = %u\n",val[PSWD_CODE]);
                jj = val[SECU_STATUS];
                if (jj)
                {
                        for (ii = 0; ii < NUM_SECU_STR; ii++)
                        {
-                               if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
+                               printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "",  secu_str[ii]);
                                jj >>=1;
                        }
                        if (val[SECU_STATUS] & SECU_ENABLED)
                        {
-                               printf("\tSecurity level ");
-                               if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
+                               printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
                        }
                }
                jj =  val[ERASE_TIME]     & ERASE_BITS;
@@ -1141,34 +1061,26 @@ static void identify(uint16_t *id_supplied)
                if (jj || kk)
                {
                        printf("\t");
-                       if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
-                       if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
+                       if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
+                       if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
                        printf("\n");
                }
        }
 
        /* reset result */
-       if ((val[HWRST_RSLT] & VALID) == VALID_VAL)
+       jj = val[HWRST_RSLT];
+       if ((jj & VALID) == VALID_VAL)
        {
-               printf("HW reset results:\n");
-               if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
-
-               if (val[HWRST_RSLT] & RST0)
-               {
-                       printf("\tDevice num = 0");
-                       jj = val[HWRST_RSLT];
-               }
-               else
-               {
-                       printf("\tDevice num = 1");
-                       jj = val[HWRST_RSLT] >> 8;
-               }
-
+               if (!(oo = (jj & RST0)))
+                       jj >>= 8;
                if ((jj & DEV_DET) == JUMPER_VAL)
-                       printf(" determined by the jumper");
+                       strng = " determined by the jumper";
                else if ((jj & DEV_DET) == CSEL_VAL)
-                       printf(" determined by CSEL");
-               printf("\n");
+                       strng = " determined by CSEL";
+               else 
+                       strng = "";
+               printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n", 
+                               (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
        }
 
        /* more stuff from std 5 */
@@ -1176,30 +1088,24 @@ static void identify(uint16_t *id_supplied)
        {
                if (val[CFA_PWR_MODE] & VALID_W160)
                {
-                       printf("CFA power mode 1:\n\t");
-                       if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
+                       printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
+                                                                               (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
 
-                       if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
-                       printf("\n");
-
-                       if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
+                       if (val[CFA_PWR_MODE] & MAX_AMPS) printf("\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
                }
                if ((val[INTEGRITY] & SIG) == SIG_VAL)
                {
-                       printf("Checksum: ");
-                       if_printf(chksum,"in");
-                       printf("correct\n");
+                       printf("Checksum: %scorrect\n", chksum ? "in" : "");
                }
        }
 
-       exit(0);
+       exit(EXIT_SUCCESS);
 }
 #endif
 
-static int verbose, get_identity, get_geom, noisy = 1, quiet;
-static int flagcount, do_flush;
+static int get_identity, get_geom;
+static int do_flush;
 static int do_ctimings, do_timings;
-
 static unsigned long set_readahead, get_readahead, Xreadahead;
 static unsigned long set_readonly, get_readonly, readonly;
 static unsigned long set_unmask, get_unmask, unmask;
@@ -1267,38 +1173,31 @@ static unsigned long    perform_tristate,       tristate;
 
 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
 static const char * const cfg_str[] =
-{      "",          " HardSect",   " SoftSect",  " NotMFM",
-       " HdSw>15uSec", " SpinMotCtl", " Fixed",     " Removeable",
-       " DTR<=5Mbs",   " DTR>5Mbs",   " DTR>10Mbs", " RotSpdTol>.5%",
-       " dStbOff",     " TrkOff",     " FmtGapReq", " nonMagnetic"
+{      "",          "HardSect",   "SoftSect",   "NotMFM",
+       "HdSw>15uSec", "SpinMotCtl", "Fixed",     "Removeable",
+       "DTR<=5Mbs",   "DTR>5Mbs",   "DTR>10Mbs", "RotSpdTol>.5%",
+       "dStbOff",     "TrkOff",     "FmtGapReq", "nonMagnetic"
 };
 
-static const char * const BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
+static const char * const BuffType[] = {"Unknown", "1Sect", "DualPort", "DualPortCache"};
 
 static void dump_identity(const struct hd_driveid *id)
 {
        int i;
-       char pmodes[64], dmodes[128], umodes[128];
        const unsigned short int *id_regs= (const void*) id;
-       unsigned long capacity;
-
-       pmodes[0] = dmodes[0] = umodes[0] = '\0';
 
        printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
                                id->model, id->fw_rev, id->serial_no);
-       for (i=0; i<=15; i++)
-               if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
-
-       printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
+       for (i=0; i<=15; i++) {
+               if (id->config & (1<<i))
+                       printf(" %s", cfg_str[i]);
+       }
+       printf( " }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
+                       " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
                                id->cyls, id->heads, id->sectors, id->track_bytes,
-                               id->sector_bytes, id->ecc_bytes);
-
-       if (id->buf_type > 3)
-               printf("%s%u", " BuffType=", id->buf_type);
-       else
-               printf("%s%s", " BuffType=", BuffType[id->buf_type]);
-
-       printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
+                               id->sector_bytes, id->ecc_bytes,
+                               id->buf_type, BuffType[(id->buf_type > 3) ? 0 :  id->buf_type],
+                               id->buf_size/2, id->max_multsect);
        if (id->max_multsect)
        {
                printf(", MultSect=");
@@ -1310,93 +1209,92 @@ static void dump_identity(const struct hd_driveid *id)
                        printf("off");
        }
        printf("\n");
-       if (id->tPIO <= 5)
-       {
-               strcat(pmodes, "pio0 ");
-               if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
-               if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
 
-       }
-       if_printf((!(id->field_valid&1))," (maybe):");
-       if (BB_BIG_ENDIAN) capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
-       else capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
+       if(!(id->field_valid&1))
+               printf(" (maybe):");
+
        printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
-                                                                                                       id->cur_sectors, capacity ,
-                                                                                       ((id->capability&2)==0)?"no":"yes");
+                                                                                                       id->cur_sectors, 
+                                                                                                       (BB_BIG_ENDIAN) ? 
+                                                                                                       (long unsigned int)(id->cur_capacity0 << 16) | id->cur_capacity1 :
+                                                                                                       (long unsigned int)(id->cur_capacity1 << 16) | id->cur_capacity0,
+                                                                                                       ((id->capability&2) == 0) ? "no" : "yes");
 
-       if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
+       if (id->capability&2)
+               printf(", LBAsects=%u", id->lba_capacity);
 
-       if (id->capability&1)
+       printf("\n IORDY=%s", (id->capability&8) ? (id->capability&4) ?  "on/off" : "yes" : "no");
+
+       if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&2)
+               printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
+       
+       if ((id->capability&1) && (id->field_valid&2))
+               printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
+
+       printf("\n PIO modes:  ");
+       if (id->tPIO <= 5)
        {
-               if (id->dma_1word | id->dma_mword)
-               {
-                       if_strcat((id->dma_1word & 0x100),      dmodes, "*");
-                       if_strcat((id->dma_1word & 1),          dmodes, "sdma0 ");
-                       if_strcat((id->dma_1word & 0x200),      dmodes, "*");
-                       if_strcat((id->dma_1word & 2),          dmodes, "sdma1 ");
-                       if_strcat((id->dma_1word & 0x400),      dmodes, "*");
-                       if_strcat((id->dma_1word & 4),          dmodes, "sdma2 ");
-                       if_strcat((id->dma_1word & 0xf800),     dmodes, "*");
-                       if_strcat((id->dma_1word & 0xf8),       dmodes, "sdma? ");
-                       if_strcat((id->dma_mword & 0x100),      dmodes, "*");
-                       if_strcat((id->dma_mword & 1),          dmodes, "mdma0 ");
-                       if_strcat((id->dma_mword & 0x200),      dmodes, "*");
-                       if_strcat((id->dma_mword & 2),          dmodes, "mdma1 ");
-                       if_strcat((id->dma_mword & 0x400),      dmodes, "*");
-                       if_strcat((id->dma_mword & 4),          dmodes, "mdma2 ");
-                       if_strcat((id->dma_mword & 0xf800),     dmodes, "*");
-                       if_strcat((id->dma_mword & 0xf8),       dmodes, "mdma? ");
-               }
+               printf("pio0 ");
+               if (id->tPIO >= 1) printf("pio1 ");
+               if (id->tPIO >= 2) printf("pio2 ");
        }
-       printf("\n IORDY=");
-       if (id->capability&8)
-               printf((id->capability&4) ? "on/off" : "yes");
-       else
-               printf("no");
-
-       if ((id->capability&8) || (id->field_valid&2))
+       if (id->field_valid&2)
        {
-               if (id->field_valid&2)
-               {
-                       printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
-                       if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
-                       if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
-                       if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
-               }
-               if (id->field_valid&4)
+               if (id->eide_pio_modes & 1) printf("pio3 ");
+               if (id->eide_pio_modes & 2) printf("pio4 ");
+               if (id->eide_pio_modes &~3) printf("pio? ");
+       }
+       if (id->capability&1)
+       {
+               if (id->dma_1word | id->dma_mword)
                {
-                       if_strcat((id->dma_ultra & 0x100),umodes,"*");
-                       if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
-                       if_strcat((id->dma_ultra & 0x200),umodes,"*");
-                       if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
-                       if_strcat((id->dma_ultra & 0x400),umodes,"*");
-                       if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
+                       printf("\n DMA modes:  ");
+                       if (id->dma_1word & 0x100) printf("*");
+                       if (id->dma_1word & 1) printf("sdma0 ");
+                       if (id->dma_1word & 0x200) printf("*");
+                       if (id->dma_1word & 2) printf("sdma1 ");
+                       if (id->dma_1word & 0x400) printf("*");
+                       if (id->dma_1word & 4) printf("sdma2 ");
+                       if (id->dma_1word & 0xf800) printf("*");
+                       if (id->dma_1word & 0xf8) printf("sdma? ");
+                       if (id->dma_mword & 0x100) printf("*");
+                       if (id->dma_mword & 1) printf("mdma0 ");
+                       if (id->dma_mword & 0x200) printf("*");
+                       if (id->dma_mword & 2) printf("mdma1 ");
+                       if (id->dma_mword & 0x400) printf("*");
+                       if (id->dma_mword & 4) printf("mdma2 ");
+                       if (id->dma_mword & 0xf800) printf("*");
+                       if (id->dma_mword & 0xf8) printf("mdma? ");
+               }
+       }
+       if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&4)
+       {       
+               printf("\n UDMA modes: ");
+               if (id->dma_ultra & 0x100) printf("*");
+               if (id->dma_ultra & 0x001) printf("udma0 ");
+               if (id->dma_ultra & 0x200) printf("*");
+               if (id->dma_ultra & 0x002) printf("udma1 ");
+               if (id->dma_ultra & 0x400) printf("*");
+               if (id->dma_ultra & 0x004) printf("udma2 ");
 #ifdef __NEW_HD_DRIVE_ID
-                       if (id->hw_config & 0x2000)
-                       {
+               if (id->hw_config & 0x2000)
+               {
 #else /* !__NEW_HD_DRIVE_ID */
-                       if (id->word93 & 0x2000)
-                       {
+               if (id->word93 & 0x2000)
+               {
 #endif /* __NEW_HD_DRIVE_ID */
-                               if_strcat((id->dma_ultra & 0x0800),umodes,"*");
-                               if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
-                               if_strcat((id->dma_ultra & 0x1000),umodes,"*");
-                               if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
-                               if_strcat((id->dma_ultra & 0x2000),umodes,"*");
-                               if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
-                               if_strcat((id->dma_ultra & 0x4000),umodes,"*");
-                               if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
-                               if_strcat((id->dma_ultra & 0x8000),umodes,"*");
-                               if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
-                       }
+                       if (id->dma_ultra & 0x0800) printf("*");
+                       if (id->dma_ultra & 0x0008) printf("udma3 ");
+                       if (id->dma_ultra & 0x1000) printf("*");
+                       if (id->dma_ultra & 0x0010) printf("udma4 ");
+                       if (id->dma_ultra & 0x2000) printf("*");
+                       if (id->dma_ultra & 0x0020) printf("udma5 ");
+                       if (id->dma_ultra & 0x4000) printf("*");
+                       if (id->dma_ultra & 0x0040) printf("udma6 ");
+                       if (id->dma_ultra & 0x8000) printf("*");
+                       if (id->dma_ultra & 0x0080) printf("udma7 ");
                }
        }
-       if_printf(((id->capability&1) && (id->field_valid&2)),
-                               ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
-       printf("\n PIO modes:  %s", pmodes);
-       if_printf((*dmodes),"\n DMA modes:  %s", dmodes);
-       if_printf((*umodes),"\n UDMA modes: %s", umodes);
-
        printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
        if (id_regs[83] & 8)
        {
@@ -1407,20 +1305,22 @@ static void dump_identity(const struct hd_driveid *id)
                else
                        printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
        }
-       if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
+       if (id_regs[82]&0x20)
+               printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
 #ifdef __NEW_HD_DRIVE_ID
        if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
        {
-               printf("\n Drive conforms to: ");
-               if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
+               printf("\n Drive conforms to: %s: ", (id->minor_rev_num <= 31) ? minor_str[id->minor_rev_num] : "Unknown");
                if (id->major_rev_num != 0x0000 &&  /* NOVAL_0 */
                    id->major_rev_num != 0xFFFF) {  /* NOVAL_1 */
-                       for (i=0; i <= 15; i++)
-                               if_printf((id->major_rev_num & (1<<i))," ATA/ATAPI-%u", i);
+                       for (i=0; i <= 15; i++) {
+                               if (id->major_rev_num & (1<<i))
+                                               printf(" ATA/ATAPI-%u", i);
+                       }
                }
        }
 #endif /* __NEW_HD_DRIVE_ID */
-       printf("\n\n * signifies the current active mode\n\n");
+       printf("\n\n * current active mode\n\n");
 }
 #endif
 
@@ -1444,32 +1344,41 @@ static int seek_to_zero(int fd)
 
 static int read_big_block(int fd, char *buf)
 {
-       const char *string;
-       int i, rc;
-       if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
-       {
-               switch(rc)
-               {
-                       case -1:
-                               string = "read()";
-                               break;
-                       case  0:
-                               string = "read() hit EOF - device too small";
-                               break;
-                       default:
-                               string = "read(%u) returned %u bytes";
-               }
-               bb_error_msg(string, TIMING_BUF_BYTES, rc);
+       int i;
+
+       if ((i = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
+               bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
                return 1;
        }
-
        /* access all sectors of buf to ensure the read fully completed */
        for (i = 0; i < TIMING_BUF_BYTES; i += 512)
                buf[i] &= 1;
        return 0;
 }
 
-static double correction;
+static void print_timing(int t, double e)
+{
+       if (t >= e)  /* more than 1MB/s */
+               printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e, 'M');
+       else
+               printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e * 1024, 'k');
+}
+
+static int do_blkgetsize (int fd, unsigned long long *blksize64)
+{
+       int             rc;
+       unsigned int    blksize32 = 0;
+
+       if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) {  // returns bytes
+               *blksize64 /= 512;
+               return 0;
+       }
+       rc = ioctl(fd, BLKGETSIZE, &blksize32); // returns sectors
+       if (rc)
+               bb_perror_msg("BLKGETSIZE");
+       *blksize64 = blksize32;
+       return rc;
+}
 
 static void do_time(int flag, int fd)
 /*
@@ -1477,91 +1386,59 @@ static void do_time(int flag, int fd)
        flag = 1 time_device
 */
 {
-       int i;
-       char *buf;
-       double elapsed;
        struct itimerval e1, e2;
-       int shmid;
-       int timing_MB = TIMING_MB;
+       double elapsed, elapsed2;
+       unsigned int max_iterations = 1024, total_MB, iterations;
+       unsigned long long blksize;
+       RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
 
-       if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
-       {
-               bb_error_msg(bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
-               return;
+       if (mlock(buf, TIMING_BUF_BYTES)) {
+               bb_perror_msg("mlock");
+               goto quit2;
        }
-       if (shmctl(shmid, SHM_LOCK, NULL) == -1)
-       {
-               bb_error_msg(bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
-               (void) shmctl(shmid, IPC_RMID, NULL);
-               return;
-       }
-       if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
-       {
-               bb_error_msg(bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
-               (void) shmctl(shmid, IPC_RMID, NULL);
-               return;
+
+       if (0 == do_blkgetsize(fd, &blksize)) {
+               max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
        }
-       if (shmctl(shmid, IPC_RMID, NULL) == -1)
-               bb_error_msg("shmctl(,IPC_RMID,)");
 
        /* Clear out the device request queues & give them time to complete */
-       sync_and_sleep(3);
+       sync();
+       sleep(3);
+
+       setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
 
        if (flag  == 0) /* Time cache */
        {
-               /* Calculate a correction factor for the basic
-               * overhead of doing a read() from the buffer cache.
-               * To do this, we read the data once to "cache it" and
-               * to force full preallocation of our timing buffer,
-               * and then we re-read it 10 times while timing it.
-               *
-               * getitimer() is used rather than gettimeofday() because
-               * it is much more consistent (on my machine, at least).
-               */
-               setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
-               if (seek_to_zero(fd))
-                       return;
-               if (read_big_block(fd, buf))
-                       return;
-               printf(" Timing buffer-cache reads:   ");
+               if (seek_to_zero (fd)) return;
+               if (read_big_block (fd, buf)) return;
+               printf(" Timing cached reads:   ");
                fflush(stdout);
-
-               /* Clear out the device request queues & give them time to complete */
-               sync_and_sleep(1);
-
-               /* Time re-reading from the buffer-cache */
+       
+               /* Now do the timing */
+               iterations = 0;
                getitimer(ITIMER_REAL, &e1);
-               for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
-               {
-                       if (seek_to_zero(fd))
-                               goto quit;
-                       if (read_big_block(fd, buf))
+               do {
+                       ++iterations;
+                       if (seek_to_zero (fd) || read_big_block (fd, buf))
                                goto quit;
-               }
-               getitimer(ITIMER_REAL, &e2);
-               correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
+                       getitimer(ITIMER_REAL, &e2);
+                       elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+                       + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
+               } while (elapsed < 2.0);
+               total_MB = iterations * TIMING_BUF_MB;
 
-               /* Now remove the lseek() from the correction factor */
+               /* Now remove the lseek() and getitimer() overheads from the elapsed time */
                getitimer(ITIMER_REAL, &e1);
-               for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
-               {
-                       if (seek_to_zero(fd))
+               do {
+                       if (seek_to_zero (fd))
                                goto quit;
-               }
-               getitimer(ITIMER_REAL, &e2);
-               correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
-               + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
-
-               if ((BUFCACHE_FACTOR * timing_MB) >= correction)  /* more than 1MB/s */
-                       printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
-                               (BUFCACHE_FACTOR * timing_MB),  correction,
-                               (BUFCACHE_FACTOR * timing_MB) / correction);
-               else
-                       printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
-                               (BUFCACHE_FACTOR * timing_MB),  correction,
-                               (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
-               correction /= BUFCACHE_FACTOR;
-
+                       getitimer(ITIMER_REAL, &e2);
+                       elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+                       + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
+               } while (--iterations);
+       
+               elapsed -= elapsed2;
+               print_timing(BUFCACHE_FACTOR * total_MB, elapsed);      
                flush_buffer_cache(fd);
                sleep(1);
        }
@@ -1569,49 +1446,29 @@ static void do_time(int flag, int fd)
        {
                printf(" Timing buffered disk reads:  ");
                fflush(stdout);
-
                /*
                * getitimer() is used rather than gettimeofday() because
                * it is much more consistent (on my machine, at least).
                */
-               setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
-
                /* Now do the timings for real */
+               iterations = 0;
                getitimer(ITIMER_REAL, &e1);
-               for (i = TIMING_BUF_COUNT; i > 0; --i)
-               {
-                       if (read_big_block(fd, buf))
+               do {
+                       ++iterations;
+                       if (read_big_block (fd, buf))
                                goto quit;
-               }
-               getitimer(ITIMER_REAL, &e2);
-
-               elapsed =       (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
-
-               if (timing_MB >= elapsed)  /* more than 1MB/s */
-                       printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
-               else
-                       printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
-
-               /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
-               if (elapsed <= (correction * 2))
-                                       bb_error_msg(bb_msg_memory_exhausted);
-
-#if 0  /* the "estimate" is just plain wrong for many systems.. */
-               else if (correction != 0.0) {
-                       printf(" Estimating raw driver speed: ");
-                       elapsed -= correction;
-                       if (timing_MB >= elapsed)  /* more than 1MB/s */
-                               printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
-                                       timing_MB, elapsed, timing_MB / elapsed);
-                       else
-                               printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
-                                       timing_MB, elapsed, timing_MB / elapsed * 1024);
-               }
-#endif
+                       getitimer(ITIMER_REAL, &e2);
+                       elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+                       + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
+               } while (elapsed < 3.0 && iterations < max_iterations);
+       
+               total_MB = iterations * TIMING_BUF_MB;
+               print_timing(total_MB, elapsed);
        }
 quit:
-       if (-1 == shmdt(buf))
-               bb_error_msg(bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
+       munlock(buf, TIMING_BUF_BYTES);
+quit2:
+       RELEASE_CONFIG_BUFFER(buf);
 }
 
 static void on_off (unsigned int value)
@@ -1622,70 +1479,42 @@ static void on_off (unsigned int value)
 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
 static void bus_state_value(unsigned int value)
 {
-       const char *string;
-
-       switch (value)
-       {
-               case BUSSTATE_ON:
-                       string = " (on)\n";
-                       break;
-               case BUSSTATE_OFF:
-                       string = " (off)\n";
-                       break;
-               case BUSSTATE_TRISTATE:
-                       string = " (tristate)\n";
-                       break;
-               default:
-                       string = " (unknown: %d)\n";
-                       break;
-       }
-       printf(string, value);
+       if (value == BUSSTATE_ON)
+               on_off(1);
+       else if (value == BUSSTATE_OFF)
+               on_off(0);
+       else if (value == BUSSTATE_TRISTATE)
+               printf(" (tristate)\n");
+       else
+               printf(" (unknown: %d)\n", value);
 }
 #endif
 
 #ifdef HDIO_DRIVE_CMD
 static void interpret_standby(unsigned int standby)
 {
-       printf(" (");
-       switch(standby)
-       {
-               case 0:
-                       printf("off");
-                       break;
-               case 252:
-                       printf("21 minutes");
-                       break;
-               case 253:
-                       printf("vendor-specific");
-                       break;
-               case 254:
-                       printf("?reserved");
-                       break;
-               case 255:
-                       printf("21 minutes + 15 seconds");
-                       break;
-               default:
-                       if (standby <= 240)
-                       {
-                               unsigned int secs = standby * 5;
-                               unsigned int mins = secs / 60;
-                               secs %= 60;
-                               if_printf(mins,"%u minutes", mins);
-                               if_printf((mins && secs)," + ");
-                               if_printf(secs,"%u seconds", secs);
-                       }
-                       else if (standby <= 251)
-                       {
-                               unsigned int mins = (standby - 240) * 30;
-                               unsigned int hrs  = mins / 60;
-                               mins %= 60;
-                               if_printf(hrs,"%u hours", hrs);
-                               if_printf((hrs && mins)," + ");
-                               if_printf(mins,"%u minutes", mins);
-                       }
-                       else
-                               printf("illegal value");
-                       break;
+       unsigned int t;
+
+
+       if (standby == 0)
+               printf("off");
+       else if (standby == 252)
+               printf("21 minutes");
+       else if (standby == 253)
+               printf("vendor-specific");
+       else if (standby == 254)
+               printf("Reserved");
+       else if (standby == 255)
+               printf("21 minutes + 15 seconds");
+       else {
+               if (standby <= 240) {
+                       t = standby * 5;
+                       printf("%u minutes + %u seconds", t / 60, t % 60);
+               } else if (standby <= 251) {
+                       t = (standby - 240) * 30;
+                       printf("%u hours + %u minutes", t / 60, t % 60);
+               } else
+                       printf("illegal value");
        }
        printf(")\n");
 }
@@ -1742,7 +1571,6 @@ static int translate_xfermode(char * name)
        {
                if (!strcmp(name, tmp->name))
                        return tmp->val;
-
        }
 
        val = strtol(name, &endptr, 10);
@@ -1755,61 +1583,30 @@ static int translate_xfermode(char * name)
 static void interpret_xfermode(unsigned int xfermode)
 {
        printf(" (");
-       switch(xfermode) {
-               case 0:
-                       printf("default PIO mode");
-                       break;
-               case 1:
-                       printf("default PIO mode, disable IORDY");
-                       break;
-               case 8:
-               case 9:
-               case 10:
-               case 11:
-               case 12:
-               case 13:
-               case 14:
-               case 15:
-                       printf("PIO flow control mode%u", xfermode-8);
-                       break;
-               case 16:
-               case 17:
-               case 18:
-               case 19:
-               case 20:
-               case 21:
-               case 22:
-               case 23:
-                       printf("singleword DMA mode%u", xfermode-16);
-                       break;
-               case 32:
-               case 33:
-               case 34:
-               case 35:
-               case 36:
-               case 37:
-               case 38:
-               case 39:
-                       printf("multiword DMA mode%u", xfermode-32);
-                       break;
-               case 64:
-               case 65:
-               case 66:
-               case 67:
-               case 68:
-               case 69:
-               case 70:
-               case 71:
-                       printf("UltraDMA mode%u", xfermode-64);
-                       break;
-               default:
-                       printf("unknown, probably not valid");
-                       break;
-       }
+       if (xfermode == 0)
+               printf("default PIO mode");
+       else if (xfermode == 1)
+               printf("default PIO mode, disable IORDY");
+       else if (xfermode >= 8 && xfermode <= 15)
+               printf("PIO flow control mode%u", xfermode-8);
+       else if (xfermode >= 16 && xfermode <= 23)
+               printf("singleword DMA mode%u", xfermode-16);
+       else if (xfermode >= 32 && xfermode <= 39)
+               printf("multiword DMA mode%u", xfermode-32);
+       else if (xfermode >= 64 && xfermode <= 71)
+               printf("UltraDMA mode%u", xfermode-64);
+       else
+               printf("Unknown");
        printf(")\n");
 }
 #endif /* HDIO_DRIVE_CMD */
 
+static void print_flag(unsigned long flag, char *s, unsigned long value)
+{
+       if(flag)
+               printf(" setting %s to %ld\n", s, value);
+}
+
 static void process_dev(char *devname)
 {
        int fd;
@@ -1820,13 +1617,14 @@ static void process_dev(char *devname)
        /* Please restore args[n] to these values after each ioctl
           except for args[2] */
        unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
+       const char *fmt = " %s\t= %2ld";
 
        fd = bb_xopen(devname, O_RDONLY|O_NONBLOCK);
-       if_printf(!quiet, "\n%s:\n", devname);
+       printf("\n%s:\n", devname);
 
        if (set_readahead)
        {
-               if_printf(get_readahead," setting fs readahead to %ld\n", Xreadahead);
+               print_flag(get_readahead,"fs readahead", Xreadahead);
                bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
        }
 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
@@ -1866,49 +1664,48 @@ static void process_dev(char *devname)
        }
        if (set_io32bit)
        {
-               if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
+               print_flag(get_io32bit,"32-bit IO_support flag", io32bit);
                bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
        }
        if (set_mult)
        {
-               if_printf(get_mult, " setting multcount to %ld\n", mult);
-               if (ioctl(fd, HDIO_SET_MULTCOUNT, mult))
-                       bb_perror_msg("HDIO_SET_MULTCOUNT");
-#ifndef HDIO_DRIVE_CMD
-               else
-                       force_operation = 1;
+               print_flag(get_mult, "multcount", mult);
+#ifdef HDIO_DRIVE_CMD
+               bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT");
+#else
+               force_operation |= (!bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT"));
 #endif
        }
        if (set_readonly)
        {
-               if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
+               print_flag_on_off(get_readonly,"readonly", readonly);
                bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
        }
        if (set_unmask)
        {
-               if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
+               print_flag_on_off(get_unmask,"unmaskirq", unmask);
                bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
        }
 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
        if (set_dma)
        {
-               if_printf_on_off(get_dma," setting using_dma to %ld", dma);
+               print_flag_on_off(get_dma, "using_dma", dma);
                bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
        }
 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
        if (set_dma_q)
        {
-               if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
+               print_flag_on_off(get_dma_q,"DMA queue_depth", dma_q);
                bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
        }
        if (set_nowerr)
        {
-               if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
+               print_flag_on_off(get_nowerr,"nowerr", nowerr);
                bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
        }
        if (set_keep)
        {
-               if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
+               print_flag_on_off(get_keep,"keep_settings", keep);
                bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
        }
 #ifdef HDIO_DRIVE_CMD
@@ -1916,28 +1713,28 @@ static void process_dev(char *devname)
        {
                args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
                args[2] = 0;
-               if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
+               print_flag_on_off(get_doorlock,"drive doorlock", doorlock);
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
                args[0] = WIN_SETFEATURES;
        }
        if (set_dkeep)
        {
                /* lock/unlock the drive's "feature" settings */
-               if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
+               print_flag_on_off(get_dkeep,"drive keep features", dkeep);
                args[2] = dkeep ? 0x66 : 0xcc;
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
        }
        if (set_defects)
        {
                args[2] = defects ? 0x04 : 0x84;
-               if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
+               print_flag(get_defects,"drive defect-mgmt", defects);
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
        }
        if (set_prefetch)
        {
                args[1] = prefetch;
                args[2] = 0xab;
-               if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
+               print_flag(get_prefetch,"drive prefetch", prefetch);
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
                args[1] = 0;
        }
@@ -1947,7 +1744,7 @@ static void process_dev(char *devname)
                args[2] = 3;
                if (get_xfermode)
                {
-                       printf(" setting xfermode to %d", xfermode_requested);
+                       print_flag(1,"xfermode", xfermode_requested);
                        interpret_xfermode(xfermode_requested);
                }
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
@@ -1956,27 +1753,15 @@ static void process_dev(char *devname)
        if (set_lookahead)
        {
                args[2] = lookahead ? 0xaa : 0x55;
-               if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
+               print_flag_on_off(get_lookahead,"drive read-lookahead", lookahead);
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
        }
        if (set_apmmode)
        {
-               apmmode=check_if_min_and_set_val(apmmode,1);
-               apmmode=check_if_maj_and_set_val(apmmode,255);
-               if_printf(get_apmmode," setting APM level to");
-               if (apmmode==255)
-               {
-                       /* disable Advanced Power Management */
-                       args[2] = 0x85; /* feature register */
-                       if_printf(get_apmmode," disabled\n");
-               }
-               else
-               {
-                       /* set Advanced Power Management mode */
-                       args[2] = 0x05; /* feature register */
-                       args[1] = apmmode; /* sector count register */
-                       if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
-               }
+               args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
+               args[1] = apmmode; /* sector count register 1-255 */
+               if (get_apmmode)
+                       printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
                args[1] = 0;
        }
@@ -1989,7 +1774,7 @@ static void process_dev(char *devname)
                static unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
 #endif /* DO_FLUSHCACHE */
                args[2] = wcache ? 0x02 : 0x82;
-               if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
+               print_flag_on_off(get_wcache,"drive write-caching", wcache);
 #ifdef DO_FLUSHCACHE
                if (!wcache)
                        bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
@@ -2013,7 +1798,7 @@ static void process_dev(char *devname)
 #ifndef WIN_STANDBYNOW2
 #define WIN_STANDBYNOW2 0x94
 #endif
-               if_printf(get_standbynow," issuing standby command\n");
+               if (get_standbynow) printf(" issuing standby command\n");
                args[0] = WIN_STANDBYNOW1;
                bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2, "HDIO_DRIVE_CMD(standby)");
        }
@@ -2025,14 +1810,14 @@ static void process_dev(char *devname)
 #ifndef WIN_SLEEPNOW2
 #define WIN_SLEEPNOW2 0x99
 #endif
-               if_printf(get_sleepnow," issuing sleep command\n");
+               if (get_sleepnow) printf(" issuing sleep command\n");
                args[0] = WIN_SLEEPNOW1;
                bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2, "HDIO_DRIVE_CMD(sleep)");
        }
        if (set_seagate)
        {
                args[0] = 0xfb;
-               if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
+               if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
        }
        if (set_standby)
@@ -2041,7 +1826,7 @@ static void process_dev(char *devname)
                args[1] = standby_requested;
                if (get_standby)
                {
-                       printf(" setting standby to %lu", standby_requested);
+                       print_flag(1,"standby", standby_requested);
                        interpret_standby(standby_requested);
                }
                bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
@@ -2053,66 +1838,55 @@ static void process_dev(char *devname)
                char buf[512];
                flush_buffer_cache(fd);
                if (-1 == read(fd, buf, sizeof(buf)))
-                       bb_error_msg("access failed");
+                       bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
        }
 #endif /* HDIO_DRIVE_CMD */
 
-       if (!flagcount)
-               verbose = 1;
-
-       if (verbose || get_mult || get_identity)
+       if (get_mult || get_identity)
        {
                multcount = -1;
                if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
                {
-                       if (verbose || get_mult)
+                       if (get_mult)
                                bb_perror_msg("HDIO_GET_MULTCOUNT");
                }
-               else if (verbose | get_mult)
+               else if (get_mult)
                {
-                       printf(" multcount    = %2ld", multcount);
+                       printf(fmt, "multcount", multcount);
                        on_off(multcount);
                }
        }
-       if (verbose || get_io32bit)
+       if (get_io32bit)
        {
                if (!bb_ioctl(fd, HDIO_GET_32BIT, &parm, "HDIO_GET_32BIT"))
                {
-                       printf(" IO_support   =%3ld (", parm);
-                       switch (parm)
-                       {
-                               case 0:
-                                       printf("default ");
-                               case 2:
-                                       printf("16-bit)\n");
-                                       break;
-                               case 1:
-                                       printf("32-bit)\n");
-                                       break;
-                               case 3:
-                                       printf("32-bit w/sync)\n");
-                                       break;
-                               case 8:
-                                       printf("Request-Queue-Bypass)\n");
-                                       break;
-                               default:
-                                       printf("\?\?\?)\n");
-                               /*esac*/
-                       }
+                       printf(" IO_support\t=%3ld (", parm);
+                       if (parm == 0)
+                               printf("default 16-bit)\n");
+                       else if (parm == 2)
+                               printf("16-bit)\n");
+                       else if (parm == 1)
+                               printf("32-bit)\n");
+                       else if (parm == 3)
+                               printf("32-bit w/sync)\n");
+                       else if (parm == 8)
+                               printf("Request-Queue-Bypass)\n");
+                       else
+                               printf("\?\?\?)\n");
                }
        }
-       if (verbose || get_unmask)
+       if (get_unmask)
        {
                bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
-                                               "HDIO_GET_UNMASKINTR"," unmaskirq    = %2ld");
+                                               "HDIO_GET_UNMASKINTR","unmaskirq");
        }
 
 
 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
-       if (verbose || get_dma) {
+       if (get_dma) {
                if (!bb_ioctl(fd, HDIO_GET_DMA, &parm, "HDIO_GET_DMA"))
                {
-                       printf(" using_dma    = %2ld", parm);
+                       printf(fmt, "using_dma", parm);
                        if (parm == 8)
                                printf(" (DMA-Assisted-PIO)\n");
                        else
@@ -2123,37 +1897,37 @@ static void process_dev(char *devname)
        if (get_dma_q)
        {
                bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
-                                                 "HDIO_GET_QDMA"," queue_depth  = %2ld");
+                                                 "HDIO_GET_QDMA","queue_depth");
        }
-       if (verbose || get_keep)
+       if (get_keep)
        {
                bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
-                                                       "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
+                                                       "HDIO_GET_KEEPSETTINGS","keepsettings");
        }
 
        if (get_nowerr)
        {
                bb_ioctl_on_off  (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
-                                                       " HDIO_GET_NOWERR"," nowerr       = %2ld");
+                                                       "HDIO_GET_NOWERR","nowerr");
        }
-       if (verbose || get_readonly)
+       if (get_readonly)
        {
                bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
-                                                 " BLKROGET"," readonly     = %2ld");
+                                                 "BLKROGET","readonly");
        }
-       if (verbose || get_readahead)
+       if (get_readahead)
        {
                bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
-                                                       " BLKRAGET"," readahead    = %2ld");
+                                                       "BLKRAGET","readahead");
        }
-       if (verbose || get_geom)
+       if (get_geom)
        {
                if (!bb_ioctl(fd, BLKGETSIZE, &parm, "BLKGETSIZE"))
                {
                        struct hd_geometry g;
 
                        if (!bb_ioctl(fd, HDIO_GETGEO, &g, "HDIO_GETGEO"))
-                               printf(" geometry     = %u/%u/%u, sectors = %ld, start = %ld\n",
+                               printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
                                                g.cylinders, g.heads, g.sectors, parm, g.start);
                }
        }
@@ -2172,7 +1946,7 @@ static void process_dev(char *devname)
                if (bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2, 0))
                {
                        if (errno != EIO || args[0] != 0 || args[1] != 0)
-                               state = "unknown";
+                               state = "Unknown";
                        else
                                state = "sleeping";
                }
@@ -2222,16 +1996,12 @@ static void process_dev(char *devname)
        if (get_IDentity)
        {
                unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
-               unsigned i;
 
                memset(args1, 0, sizeof(args1));
                args1[0] = WIN_IDENTIFY;
                args1[3] = 1;
-               if (!bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY, "HDIO_DRIVE_CMD(identify)")) {
-                       for (i=0; i<(sizeof args1)/2; i+=2)
-                               __le16_to_cpus((uint16_t *)(&args1[i]));
-                       identify((void *)&args1[4]);
-               }
+               if (!bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY, "HDIO_DRIVE_CMD(identify)"))
+                       identify((void *)(args1 + 4));
        }
 #endif
 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
@@ -2239,18 +2009,16 @@ static void process_dev(char *devname)
        {
                if (get_busstate)
                {
-                       printf(" setting bus state to %ld", busstate);
+                       print_flag(1, "bus state", busstate);
                        bus_state_value(busstate);
                }
                bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate, "HDIO_SET_BUSSTATE");
        }
-#endif
-#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
        if (get_busstate)
        {
                if (!bb_ioctl(fd, HDIO_GET_BUSSTATE, &parm, "HDIO_GET_BUSSTATE"))
                {
-                       printf(" busstate     = %2ld", parm);
+                       printf(fmt, "bus state", parm);
                        bus_state_value(parm);
                }
        }
@@ -2280,19 +2048,23 @@ static int fromhex(unsigned char c)
 
 static void identify_from_stdin(void)
 {
-       unsigned short sbuf[800];
-       unsigned char  buf[1600], *b = (unsigned char *)buf;
+       uint16_t sbuf[256];
+       unsigned char  buf[1280], *b = (unsigned char *)buf;
        int i, count = read(0, buf, 1280);
 
        if (count != 1280)
-               bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
-       for (i = 0; count >= 4; ++i)
+               bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
+
+       // Convert the newline-separated hex data into an identify block.
+
+       for (i = 0; i<256; i++)
        {
-               sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
-               __le16_to_cpus((uint16_t *)(&sbuf[i]));
-               b += 5;
-               count -= 5;
+               int j;
+               for(j=0;j<4;j++) sbuf[i] = (sbuf[i] <<4) + fromhex(*(b++));
        }
+
+       // Parse the data.
+
        identify(sbuf);
 }
 #endif
@@ -2300,10 +2072,8 @@ static void identify_from_stdin(void)
 /* busybox specific stuff */
 static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *value, int min, int max)
 {
-               /* noisy is a global var */
-               if (get) { /* *get is initialized to 0 */
-                       *get = noisy;
-                       noisy = 1;
+               if (get) {
+                       *get = 1;
                }
                if (optarg) {
                        *set = 1;
@@ -2311,45 +2081,19 @@ static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *va
                }
 }
 
-#ifdef HDIO_DRIVE_CMD
-static void parse_opts_v2(int flag, unsigned long *get, unsigned long *set)
-{
-       if (flag) {
-               /* noisy is a global var */
-               *get = noisy;
-               noisy = 1;
-               *set = 1;
-       }
-}
-#endif
-
-static void parse_opts_v3(int flag, unsigned long *get, unsigned long *set, int *value)
+static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int *value)
 {
        if (flag) {
-               /* noisy is a global var */
-               *get = noisy;
-               noisy = 1;
+               *get = 1;
                if (optarg) {
                        *set = ((*value = translate_xfermode(optarg)) > -1);
                }
        }
 }
 
-/*-------------------------------------*/
-/* getopt long options */
-#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
-static const struct option HDPARM_LONG_OPT[] =
-{
-       {"Istdin", 0, NULL, '\256'},
-       {NULL, 0, NULL, 0}
-};
-#else
-#define HDPARM_LONG_OPT                        NULL
-#endif
-/*-------------------------------------*/
-/* getopt short options */
-static const char hdparm_options[]=    "Vvgfqu::n::p:r::m::c::k::a::B:tTh"\
-       USE_FEATURE_HDPARM_GET_IDENTITY("\256iI")
+/*------- getopt short options --------*/
+static const char hdparm_options[]=    "gfu::n::p:r::m::c::k::a::B:tTh"\
+       USE_FEATURE_HDPARM_GET_IDENTITY("iI")
        USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
 #ifdef HDIO_DRIVE_CMD
                                                                        "S::D::P::X::K::A::L::W::CyYzZ"
@@ -2372,55 +2116,41 @@ int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN;
 int hdparm_main(int argc, char **argv)
 {
        int c;
+       int flagcount = 0;
 
-       while ((c = getopt_long (argc, argv, hdparm_options, HDPARM_LONG_OPT , NULL)) >= 0) {
-               /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
+       while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
                flagcount++;
-#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
-               if (c == '\256') {
-                       identify_from_stdin(); /* EXIT */
-               }
-#endif
-               if (c == 'V') {
-                       printf("%s %s\n",bb_applet_name, VERSION);
-                       exit(EXIT_SUCCESS);
-               }       
-
-               verbose = (c == 'v');
-               USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity = (c == 'I'));
-               USE_FEATURE_HDPARM_GET_IDENTITY(get_identity = (c == 'i'));
-               get_geom = (c == 'g');
-               do_flush = (c == 'f');
-               if (c == 'q') {
-                       quiet = 1;
-                       noisy = 0;
-               }
+               if (c == 'h') bb_show_usage(); /* EXIT */
+               USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
+               USE_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
+               get_geom |= (c == 'g');
+               do_flush |= (c == 'f');
                if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
                USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
                if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
-               parse_opts_v3((c == 'p'),&noisy_piomode, &set_piomode, &piomode);
+               parse_xfermode((c == 'p'),&noisy_piomode, &set_piomode, &piomode);
                if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
                if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
                if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
                if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
                if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
                if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
-               do_flush |= do_timings = (c == 't');
-               do_flush |= do_ctimings = (c == 'T');
+               do_flush |= do_timings |= (c == 't');
+               do_flush |= do_ctimings |= (c == 'T');
 #ifdef HDIO_DRIVE_CMD
                if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);   
                if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
                if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
-               parse_opts_v3((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
+               parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
                if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
                if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
                if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
                if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
-               parse_opts_v3((c == 'C'), &get_powermode, NULL, NULL);
-               parse_opts_v2((c == 'y'), &get_standbynow, &set_standbynow);
-               parse_opts_v2((c == 'Y'), &get_sleepnow, &set_sleepnow);
-               reread_partn = (c == 'z');
-               parse_opts_v2((c == 'Z'), &get_seagate, &set_seagate);
+               get_powermode |= (c == 'C');
+               get_standbynow = set_standbynow |= (c == 'y');
+               get_sleepnow = set_sleepnow |= (c == 'Y');
+               reread_partn |= (c == 'z');
+               get_seagate = set_seagate |= (c == 'Z');
 #endif
                USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX));   
 #ifdef HDIO_GET_QDMA
@@ -2432,7 +2162,7 @@ int hdparm_main(int argc, char **argv)
 #endif
                }
 #endif         
-               USE_FEATURE_HDPARM_HDIO_DRIVE_RESET( perform_reset = (c == 'r'));
+               USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
                USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));      
                USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2)); 
 #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
@@ -2446,12 +2176,18 @@ int hdparm_main(int argc, char **argv)
                }
 #endif
        }
-
+       /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
+       if (!flagcount){
+               get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
+               USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
+       }
        argc -= optind;
        argv += optind;
 
        if (argc < 1) {
-               bb_show_usage();
+               if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
+                       identify_from_stdin(); /* EXIT */
+               else bb_show_usage();
        }
 
        while (argc--) {