1#!/bin/sh
2# verify that od --endian works properly
3
4# Copyright (C) 2014-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ od
21
22in='0123456789abcdef'
23
24NL='
25'
26
27# rev(1) is not generally available, so here's a simplistic
28# implementation sufficient for our purposes.
29rev() {
30  while read line; do
31    printf '%s' "$line" | sed "s/./&\\$NL/g" | tac | paste -s -d ''
32  done
33}
34
35in_swapped() { printf '%s' "$in" | sed "s/.\{$1\}/&\\$NL/g" | rev |tr -d '\n'; }
36
37for e in little big; do
38  test $e = little && eo=big || eo=little
39  for s in 1 2 4 8 16; do
40    for t in x f; do
41      od -t $t$s --endian=$e /dev/null > /dev/null 2>&1 || continue
42      printf '%s' "$in" | od -An -t $t$s --endian=$e  > out1
43      in_swapped  "$s"  | od -An -t $t$s --endian=$eo > out2
44      compare out1 out2 || fail=1
45    done
46  done
47done
48
49Exit $fail
50