lib/sg_lib.c
void
dWordHex(const unsigned short* words, int num, int no_ascii, int swapb)
{
    const unsigned short * p = words;
    unsigned short c;
    char buff[82];
    unsigned char upp, low;
    int a = 0;
    const int bpstart = 3;
    const int cpstart = 52;
    int cpos = cpstart;
    int bpos = bpstart;
    int i, k, blen;

    if (num <= 0)
        return;
    blen = (int)sizeof(buff);
    memset(buff, ' ', 80);
    buff[80] = '\0';
    if (no_ascii < 0) {
        for (k = 0; k < num; k++) {
            c = *p++;
            if (swapb)
                c = swapb_ushort(c);
            bpos += 5;
            my_snprintf(&buff[bpos], blen - bpos, "%.4x", (unsigned int)c);
            buff[bpos + 4] = ' ';
            if ((k > 0) && (0 == ((k + 1) % 8))) {
                if (-2 == no_ascii)
                    printf("%.39s\n", buff +8);
                else
                    printf("%.47s\n", buff);
                bpos = bpstart;
                memset(buff, ' ', 80);
            }
        }
        if (bpos > bpstart) {
            if (-2 == no_ascii)
                printf("%.39s\n", buff +8);
            else
                printf("%.47s\n", buff);
        }
        return;
    }
    /* no_ascii>=0, start each line with address (offset) */
    k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
    buff[k + 1] = ' ';

    for (i = 0; i < num; i++) {
        c = *p++;
        if (swapb)
            c = swapb_ushort(c);
        bpos += 5;
        my_snprintf(&buff[bpos], blen - bpos, "%.4x", (unsigned int)c);
        buff[bpos + 4] = ' ';
        if (no_ascii) {
            buff[cpos++] = ' ';
            buff[cpos++] = ' ';
            buff[cpos++] = ' ';
        } else {
            upp = (c >> 8) & 0xff;
            low = c & 0xff;
            if ((upp < 0x20) || (upp >= 0x7f))
                upp = '.';
            buff[cpos++] = upp;
            if ((low < 0x20) || (low >= 0x7f))
                low = '.';
            buff[cpos++] = low;
            buff[cpos++] = ' ';
        }
        if (cpos > (cpstart + 23)) {
            printf("%.76s\n", buff);
            bpos = bpstart;
            cpos = cpstart;
            a += 8;
            memset(buff, ' ', 80);
            k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
            buff[k + 1] = ' ';
        }
    }
    if (cpos > cpstart)
        printf("%.76s\n", buff);
}
/* Note the ASCII-hex output goes to stdout. [Most other output from functions
 * in this file go to sg_warnings_strm (default stderr).]
 * 'no_ascii' allows for 3 output types:
 *     > 0     each line has address then up to 8 ASCII-hex 16 bit words
 *     = 0     in addition, the ASCI bytes pairs are listed to the right
 *     = -1    only the ASCII-hex words are listed (i.e. without address)
 *     = -2    only the ASCII-hex words, formatted for "hdparm --Istdin"
 *     < -2    same as -1
 * If 'swapb' non-zero then bytes in each word swapped. Needs to be set
 * for ATA IDENTIFY DEVICE response on big-endian machines. */
Note the ASCII-hex output goes to stdout.
Note the ASCII - hex output goes to stdout .
void
dWordHex(const unsigned short* words, int num, int no_ascii, int swapb)
{
    const unsigned short * p = words;
    unsigned short c;
    char buff[82];
    unsigned char upp, low;
    int a = 0;
    const int bpstart = 3;
    const int cpstart = 52;
    int cpos = cpstart;
    int bpos = bpstart;
    int i, k, blen;
    if (num <= 0)
        return;
    blen = (int)sizeof(buff);
    memset(buff, ' ', 80);
    buff[80] = '\0';
    if (no_ascii < 0) {
        for (k = 0; k < num; k++) {
            c = *p++;
            if (swapb)
                c = swapb_ushort(c);
            bpos += 5;
            my_snprintf(&buff[bpos], blen - bpos, "%.4x", (unsigned int)c);
            buff[bpos + 4] = ' ';
            if ((k > 0) && (0 == ((k + 1) % 8))) {
                if (-2 == no_ascii)
                    printf("%.39s\n", buff +8);
                else
                    printf("%.47s\n", buff);
                bpos = bpstart;
                memset(buff, ' ', 80);
            }
        }
        if (bpos > bpstart) {
            if (-2 == no_ascii)
                printf("%.39s\n", buff +8);
            else
                printf("%.47s\n", buff);
        }
        return;
    }
    k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
    buff[k + 1] = ' ';
    for (i = 0; i < num; i++) {
        c = *p++;
        if (swapb)
            c = swapb_ushort(c);
        bpos += 5;
        my_snprintf(&buff[bpos], blen - bpos, "%.4x", (unsigned int)c);
        buff[bpos + 4] = ' ';
        if (no_ascii) {
            buff[cpos++] = ' ';
            buff[cpos++] = ' ';
            buff[cpos++] = ' ';
        } else {
            upp = (c >> 8) & 0xff;
            low = c & 0xff;
            if ((upp < 0x20) || (upp >= 0x7f))
                upp = '.';
            buff[cpos++] = upp;
            if ((low < 0x20) || (low >= 0x7f))
                low = '.';
            buff[cpos++] = low;
            buff[cpos++] = ' ';
        }
        if (cpos > (cpstart + 23)) {
            printf("%.76s\n", buff);
            bpos = bpstart;
            cpos = cpstart;
            a += 8;
            memset(buff, ' ', 80);
            k = my_snprintf(buff + 1, blen - 1, "%.2x", a);
            buff[k + 1] = ' ';
        }
    }
    if (cpos > cpstart)
        printf("%.76s\n", buff);
}
void dWordHex ( const unsigned short * words , int num , int no_ascii , int swapb ) { const unsigned short * p = words ; unsigned short c ; char buff [ 82 ] ; unsigned char upp , low ; int a = 0 ; const int bpstart = 3 ; const int cpstart = 52 ; int cpos = cpstart ; int bpos = bpstart ; int i , k , blen ; if ( num <= 0 ) return ; blen = ( int ) sizeof ( buff ) ; memset ( buff , ' ' , 80 ) ; buff [ 80 ] = ' \0 ' ; if ( no_ascii < 0 ) { for ( k = 0 ; k < num ; k ++ ) { c = * p ++ ; if ( swapb ) c = swapb_ushort ( c ) ; bpos += 5 ; my_snprintf ( & buff [ bpos ] , blen - bpos , " " , ( unsigned int ) c ) ; buff [ bpos + 4 ] = ' ' ; if ( ( k > 0 ) && ( 0 == ( ( k + 1 ) % 8 ) ) ) { if ( -2 == no_ascii ) printf ( " \n " , buff + 8 ) ; else printf ( " \n " , buff ) ; bpos = bpstart ; memset ( buff , ' ' , 80 ) ; } } if ( bpos > bpstart ) { if ( -2 == no_ascii ) printf ( " \n " , buff + 8 ) ; else printf ( " \n " , buff ) ; } return ; } k = my_snprintf ( buff + 1 , blen - 1 , " " , a ) ; buff [ k + 1 ] = ' ' ; for ( i = 0 ; i < num ; i ++ ) { c = * p ++ ; if ( swapb ) c = swapb_ushort ( c ) ; bpos += 5 ; my_snprintf ( & buff [ bpos ] , blen - bpos , " " , ( unsigned int ) c ) ; buff [ bpos + 4 ] = ' ' ; if ( no_ascii ) { buff [ cpos ++ ] = ' ' ; buff [ cpos ++ ] = ' ' ; buff [ cpos ++ ] = ' ' ; } else { upp = ( c >> 8 ) & 0xff ; low = c & 0xff ; if ( ( upp < 0x20 ) || ( upp >= 0x7f ) ) upp = ' ' ; buff [ cpos ++ ] = upp ; if ( ( low < 0x20 ) || ( low >= 0x7f ) ) low = ' ' ; buff [ cpos ++ ] = low ; buff [ cpos ++ ] = ' ' ; } if ( cpos > ( cpstart + 23 ) ) { printf ( " \n " , buff ) ; bpos = bpstart ; cpos = cpstart ; a += 8 ; memset ( buff , ' ' , 80 ) ; k = my_snprintf ( buff + 1 , blen - 1 , " " , a ) ; buff [ k + 1 ] = ' ' ; } } if ( cpos > cpstart ) printf ( " \n " , buff ) ; }
Note the ASCII-hex output goes to stdout.
Note the ASCII - hex output goes to stdout .
/* no_ascii>=0, start each line with address (offset) */