13a63c4d96083a95dc47b3e620915b66ee9047d6
[platform/upstream/coreutils.git] / tests / misc / stat-printf
1 #!/bin/sh
2
3 : ${PERL=perl}
4 : ${srcdir=.}
5
6 $PERL -e 1 > /dev/null 2>&1 || {
7   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
8     "so can't run this test"
9   exit 77
10 }
11
12 exec $PERL -w -I$srcdir/.. -MCoreutils -- - <<\EOF
13 require 5.003;
14 use strict;
15
16 (my $ME = $0) =~ s|.*/||;
17 my $prog = 'stat';
18
19 # Turn off localisation of executable's ouput.
20 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
21
22 my @Tests =
23     (
24      # test-name, [option, option, ...] {OUT=>"expected-output"}
25      #
26      ['nl', q!--printf='\n' .!,          {OUT=>"\n"}],
27      ['no-nl', "--printf=%n .",          {OUT=>"."}],
28      ['pct-and-esc', q!--printf='\0%n\0' .!,    {OUT=>"\0.\0"}],
29      ['backslash', q!--printf='\\\\' .!, {OUT=>"\\"}],
30      ['nul', q!--printf='\0' .!,         {OUT=>"\0"}],
31      # Don't bother testing \v, since Perl doesn't handle it.
32      ['bel-etc', q!--printf='\a\b\f\n\r\t' .!, {OUT=>"\a\b\f\n\r\t"}],
33      ['octal-1', q!--printf='\012\377' .!,     {OUT=>"\012\377"}],
34      ['octal-2', q!--printf='.\012a\377b' .!,  {OUT=>".\012a\377b"}],
35      ['hex-1',   q!--printf='\x34\xf' .!,      {OUT=>"\x34\xf"}],
36      ['hex-2',   q!--printf='.\x18p\xfq' .!,   {OUT=>".\x18p\x0fq"}],
37      ['hex-3',   q!--printf='\x' .!,           {OUT=>'x'},
38          {ERR=>"$prog: warning: unrecognized escape `\\x'\n"}],
39
40      # With --format, there *is* a trailing newline.
41      ['f-nl', "--format=%n .",          {OUT=>".\n"}],
42      ['f-nl2', "--format=%n . .",       {OUT=>".\n.\n"}],
43
44      ['end-pct', "--printf=% .",       {OUT=>"%"}],
45      ['pct-pct', "--printf=%% .",      {OUT=>"%"}],
46      ['end-bs',  "--printf='\\' .",    {OUT=>'\\'},
47          {ERR=>"$prog: warning: backslash at end of format\n"}],
48
49      ['err-1', "--printf=%9% .",       {EXIT => 1},
50          {ERR=>"$prog: %9%: invalid directive\n"}],
51      ['err-2', "--printf=%9 .",        {EXIT => 1},
52          {ERR=>"$prog: %9: invalid directive\n"}],
53     );
54
55 my $save_temps = $ENV{DEBUG};
56 my $verbose = $ENV{VERBOSE};
57
58 my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
59 exit $fail;
60 EOF