[hackers] [sbase] Fix remaining endian-issue in od(1) || FRIGN

From: <git_AT_suckless.org>
Date: Mon, 26 Oct 2015 12:55:51 +0100 (CET)

commit 914991f5f657ddceadf474c18b897e13ed74baaf
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Mon Oct 26 12:13:34 2015 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Mon Oct 26 11:55:41 2015 +0000

    Fix remaining endian-issue in od(1)
    
    After setting up qemu and testing od(1) in a Big Endian environment,
    I found out that the conditional in the printing function was not
    right.
    Instead, it's supposed to be way simpler. While at it, we don't need
    HOST_BIG_ENDIAN any more. Just set big_endian properly in main()
    and be done with it.

diff --git a/od.c b/od.c
index d04b3da..0bd8f6c 100644
--- a/od.c
+++ b/od.c
_AT_@ -7,8 +7,6 @@
 #include "queue.h"
 #include "util.h"
 
-#define HOST_BIG_ENDIAN (*(uint16_t *)"\0\xff" == 0xff)
-
 struct type {
         unsigned char format;
         unsigned int len;
_AT_@ -72,14 +70,14 @@ printchunk(unsigned char *s, unsigned char format, size_t len) {
                 }
                 break;
         default:
- if (big_endian == HOST_BIG_ENDIAN) {
- for (res = 0, basefac = 1, i = 0; i < len; i++) {
- res += s[i] * basefac;
+ if (big_endian) {
+ for (res = 0, basefac = 1, i = len; i; i--) {
+ res += s[i - 1] * basefac;
                                 basefac <<= 8;
                         }
                 } else {
- for (res = 0, basefac = 1, i = len; i; i--) {
- res += s[i - 1] * basefac;
+ for (res = 0, basefac = 1, i = 0; i < len; i++) {
+ res += s[i] * basefac;
                                 basefac <<= 8;
                         }
                 }
_AT_@ -180,7 +178,7 @@ main(int argc, char *argv[])
         int ret = 0;
         char *s;
 
- big_endian = HOST_BIG_ENDIAN;
+ big_endian = (*(uint16_t *)"\0\xff" == 0xff);
 
         ARGBEGIN {
         case 'A':
Received on Mon Oct 26 2015 - 12:55:51 CET

This archive was generated by hypermail 2.3.0 : Mon Oct 26 2015 - 13:00:27 CET