summaryrefslogtreecommitdiff
path: root/find/print.c
blob: 598f4a20af4ecaad140be4b48549a85c7e27d11f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
/* print.c -- print/printf-related code.
   Copyright (C) 1990-2021 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/* We always include config.h first. */
#include <config.h>

/* system headers go here. */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <math.h>
#include <pwd.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>

/* gnulib headers. */
#include "areadlink.h"
#include "dirname.h"
#include "error.h"
#include "filemode.h"
#include "human.h"
#include "printquoted.h"
#include "stat-size.h"
#include "stat-time.h"
#include "verify.h"
#include "xalloc.h"

/* find-specific headers. */
#include "system.h"
#include "defs.h"
#include "die.h"
#include "print.h"


#if defined STDC_HEADERS
# define ISDIGIT(c) isdigit ((unsigned char)c)
#else
# define ISDIGIT(c) (isascii ((unsigned char)c) && isdigit ((unsigned char)c))
#endif
#undef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))

static void checked_fprintf (struct format_val *dest, const char *fmt, ...)
  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD(2, 3);


/* Create a new fprintf segment in *SEGMENT, with type KIND,
   from the text in FORMAT, which has length LEN.
   Return the address of the `next' pointer of the new segment. */
struct segment **
make_segment (struct segment **segment,
              char *format,
              int len,
              int kind,
              char format_char,
              char aux_format_char,
              struct predicate *pred)
{
  enum EvaluationCost mycost = NeedsNothing;
  char *fmt;

  assert (format_char != '{');
  assert (format_char != '[');
  assert (format_char != '(');

  *segment = xmalloc (sizeof (struct segment));

  (*segment)->segkind = kind;
  (*segment)->format_char[0] = format_char;
  (*segment)->format_char[1] = aux_format_char;
  (*segment)->next = NULL;
  (*segment)->text_len = len;

  fmt = (*segment)->text = xmalloc (len + sizeof "d");
  strncpy (fmt, format, len);
  fmt += len;

  if (kind == KIND_PLAIN     /* Plain text string, no % conversion. */
      || kind == KIND_STOP)  /* Terminate argument, no newline. */
    {
      assert (0 == format_char);
      assert (0 == aux_format_char);
      *fmt = '\0';
      if (mycost > pred->p_cost)
        pred->p_cost = NeedsNothing;
      return &(*segment)->next;
    }

  assert (kind == KIND_FORMAT);
  switch (format_char)
    {
    case '%':                   /* literal % */
      *fmt++ = '%';
      break;

    case 'l':                   /* object of symlink */
      pred->need_stat = true;
      mycost = NeedsLinkName;
      *fmt++ = 's';
      break;

    case 'y':                   /* file type */
      pred->need_type = true;
      mycost = NeedsType;
      *fmt++ = 's';
      break;

    case 'i':                   /* inode number */
      pred->need_inum = true;
      mycost = NeedsInodeNumber;
      *fmt++ = 's';
      break;

    case 'a':                   /* atime in `ctime' format */
    case 'A':                   /* atime in user-specified strftime format */
    case 'B':                   /* birth time in user-specified strftime format */
    case 'c':                   /* ctime in `ctime' format */
    case 'C':                   /* ctime in user-specified strftime format */
    case 'F':                   /* file system type */
    case 'g':                   /* group name */
    case 'M':                   /* mode in `ls -l' format (eg., "drwxr-xr-x") */
    case 's':                   /* size in bytes */
    case 't':                   /* mtime in `ctime' format */
    case 'T':                   /* mtime in user-specified strftime format */
    case 'u':                   /* user name */
      pred->need_stat = true;
      mycost = NeedsStatInfo;
      *fmt++ = 's';
      break;

    case 'S':                   /* sparseness */
      pred->need_stat = true;
      mycost = NeedsStatInfo;
      *fmt++ = 'g';
      break;

    case 'Y':                   /* symlink pointed file type */
      pred->need_stat = true;
      mycost = NeedsType;       /* true for amortised effect */
      *fmt++ = 's';
      break;

    case 'f':                   /* basename of path */
    case 'h':                   /* leading directories part of path */
    case 'p':                   /* pathname */
    case 'P':                   /* pathname with ARGV element stripped */
      *fmt++ = 's';
      break;

    case 'Z':                   /* SELinux security context */
      mycost = NeedsAccessInfo;
      *fmt++ = 's';
      break;

    case 'H':                   /* ARGV element file was found under */
      *fmt++ = 's';
      break;

      /* Numeric items that one might expect to honour
       * #, 0, + flags but which do not.
       */
    case 'G':                   /* GID number */
    case 'U':                   /* UID number */
    case 'b':                   /* size in 512-byte blocks (NOT birthtime in ctime fmt)*/
    case 'D':                   /* Filesystem device on which the file exits */
    case 'k':                   /* size in 1K blocks */
    case 'n':                   /* number of links */
      pred->need_stat = true;
      mycost = NeedsStatInfo;
      *fmt++ = 's';
      break;

      /* Numeric items that DO honour #, 0, + flags.
       */
    case 'd':                   /* depth in search tree (0 = ARGV element) */
      *fmt++ = 'd';
      break;

    case 'm':                   /* mode as octal number (perms only) */
      *fmt++ = 'o';
      pred->need_stat = true;
      mycost = NeedsStatInfo;
      break;
    }
  *fmt = '\0';

  if (mycost > pred->p_cost)
    pred->p_cost = mycost;
  return &(*segment)->next;
}

static bool
is_octal_char (char ch)
{
  return ch >= '0' && ch <= '7';
}

static char
parse_octal_escape(const char *p, size_t *consumed)
{
  register int n, i;
  size_t pos = 0;

  for (i = n = 0; i < 3 && is_octal_char(p[pos]); i++, pos++)
    {
      n = 8 * n + p[pos] - '0';
    }
  --pos;
  *consumed = pos;
  return n;
}

static int
parse_escape_char(const char ch)
{
  char value = 0;
  switch (ch)
    {
    case 'a':
      value = '\a';
      break;
    case 'b':
      value = '\b';
      break;
    case 'f':
      value = '\f';
      break;
    case 'n':
      value = '\n';
      break;
    case 'r':
      value = '\r';
      break;
    case 't':
      value = '\t';
      break;
    case 'v':
      value = '\v';
      break;
    case '\\':
      value = '\\';
      break;
    }
  return value;
}


static size_t
get_format_flags_length(const char *p)
{
  size_t n = 0;
  /* Scan past flags, width and precision, to verify kind. */
  for (; p[++n] && strchr ("-+ #", p[n]);)
    {
      /* Do nothing. */
    }
  while (ISDIGIT (p[n]))
    n++;
  if (p[n] == '.')
    for (n++; ISDIGIT (p[n]); n++)
      /* Do nothing. */ ;
  return n;
}

static size_t
get_format_specifer_length(char ch)
{
  if (strchr ("abcdDfFgGhHiklmMnpPsStuUyYZ%", ch))
    {
      return 1;
    }
  else if (strchr ("ABCT", ch))
    {
      return 2;
    }
  else
    {
      return 0;
    }
}


bool
insert_fprintf (struct format_val *vec,
                const struct parser_table *entry,
                char *format)
{
  char *segstart = format;
  char *fmt_editpos;       /* Current address in scanning `format'. */
  struct segment **segmentp;      /* Address of current segment. */
  struct predicate *our_pred;

  our_pred = insert_primary_withpred (entry, pred_fprintf, format);
  our_pred->side_effects = our_pred->no_default_print = true;
  our_pred->args.printf_vec = *vec;
  our_pred->need_type = false;
  our_pred->need_stat = false;
  our_pred->p_cost    = NeedsNothing;

  segmentp = &our_pred->args.printf_vec.segment;
  *segmentp = NULL;

  for (fmt_editpos = segstart; *fmt_editpos; fmt_editpos++)
    {
      if (fmt_editpos[0] == '\\' && fmt_editpos[1] == 'c')
        {
          make_segment (segmentp, segstart, fmt_editpos - segstart,
                        KIND_STOP, 0, 0,
                        our_pred);
          if (our_pred->need_stat && (our_pred->p_cost < NeedsStatInfo))
            our_pred->p_cost = NeedsStatInfo;
          return true;
        }
      else if (*fmt_editpos == '\\')
        {
          size_t readpos = 1;
          if (!fmt_editpos[readpos])
            {
              error (0, 0, _("warning: escape `\\' followed by nothing at all"));
              --readpos;
              /* (*fmt_editpos) is already '\\' and that's a reasonable result. */
            }
          else if (is_octal_char(fmt_editpos[readpos]))
            {
              size_t consumed = 0;
              *fmt_editpos = parse_octal_escape(fmt_editpos + readpos, &consumed);
              readpos += consumed;
            }
          else
            {
              const char val = parse_escape_char(fmt_editpos[readpos]);
              if (val)
                {
                  fmt_editpos[0] = val;
                }
              else
                {
                  error (0, 0, _("warning: unrecognized escape `\\%c'"),
                         fmt_editpos[readpos]);
                  fmt_editpos += readpos;
                  continue;
                }
            }
          segmentp = make_segment (segmentp,
                                   segstart, fmt_editpos - segstart + 1,
                                   KIND_PLAIN, 0, 0,
                                   our_pred);
          segstart = fmt_editpos + readpos + 1; /* Move past the escape. */
          fmt_editpos += readpos;  /* Incremented immediately by `for'. */
        }
      else if (fmt_editpos[0] == '%')
        {
          size_t len;
          if (fmt_editpos[1] == 0)
            {
              /* Trailing %.  We don't like those. */
              die (EXIT_FAILURE, 0,
                   _("error: %s at end of format string"), fmt_editpos);
            }

          if (fmt_editpos[1] == '%') /* %% produces just %. */
            len = 1;
          else
            len = get_format_flags_length(fmt_editpos);
          fmt_editpos += len;

          len = get_format_specifer_length (fmt_editpos[0]);
          if (len && (fmt_editpos[len-1]))
            {
              const char fmt2 = (len == 2) ? fmt_editpos[1] : 0;
              segmentp = make_segment (segmentp, segstart,
                                       fmt_editpos - segstart,
                                       KIND_FORMAT, fmt_editpos[0], fmt2,
                                       our_pred);
              fmt_editpos += (len - 1);
            }
          else
            {
              if (strchr ("{[(", fmt_editpos[0]))
                {
                  die (EXIT_FAILURE, 0,
                       _("error: the format directive `%%%c' is reserved for future use"),
                       (int)fmt_editpos[0]);
                  /*NOTREACHED*/
                }

              if (len == 2 && !fmt_editpos[1])
                {
                  error (0, 0,
                         _("warning: format directive `%%%c' "
                           "should be followed by another character"),
                         fmt_editpos[0]);
                }
              else
                {
                  /* An unrecognized % escape.  Print the char after the %. */
                  error (0, 0,
                         _("warning: unrecognized format directive `%%%c'"),
                         fmt_editpos[0]);
                }
              segmentp = make_segment (segmentp,
                                       segstart, fmt_editpos + 1 - segstart,
                                       KIND_PLAIN, 0, 0,
                                       our_pred);
            }
          segstart = fmt_editpos + 1;
        }
    }

  if (fmt_editpos > segstart)
    make_segment (segmentp, segstart, fmt_editpos - segstart, KIND_PLAIN, 0, 0,
                  our_pred);
  return true;
}

static bool
scan_for_digit_differences (const char *p, const char *q,
                            size_t *first, size_t *n)
{
  bool seen = false;
  size_t i;

  for (i=0; p[i] && q[i]; i++)
    {
      if (p[i] != q[i])
        {
          if (!isdigit ((unsigned char)p[i]) || !isdigit ((unsigned char)q[i]))
            return false;

          if (!seen)
            {
              *first = i;
              *n = 1;
              seen = 1;
            }
          else
            {
              if (i-*first == *n)
                {
                  /* Still in the first sequence of differing digits. */
                  ++*n;
                }
              else
                {
                  /* More than one differing contiguous character sequence. */
                  return false;
                }
            }
        }
    }
  if (p[i] || q[i])
    {
      /* strings are different lengths. */
      return false;
    }
  return true;
}

static char*
do_time_format (const char *fmt, const struct tm *p, const char *ns, size_t ns_size)
{
  static char *buf = NULL;
  static size_t buf_size;
  char *timefmt = NULL;
  struct tm altered_time;


  /* If the format expands to nothing (%p in some locales, for
   * example), strftime can return 0.  We actually want to distinguish
   * the error case where the buffer is too short, so we just prepend
   * an otherwise uninteresting character to prevent the no-output
   * case.
   */
  timefmt = xmalloc (strlen (fmt) + 2u);
  timefmt[0] = '_';
  memcpy (timefmt + 1, fmt, strlen (fmt) + 1);

  /* altered_time is a similar time, but in which both
   * digits of the seconds field are different.
   */
  altered_time = *p;
  if (altered_time.tm_sec >= 11)
    altered_time.tm_sec -= 11;
  else
    altered_time.tm_sec += 11;

  /* If we call strftime() with buf_size=0, the program will coredump
   * on Solaris, since it unconditionally writes the terminating null
   * character.
   */
  if (buf == NULL)
    {
      buf_size = 1u;
      buf = xmalloc (buf_size);
    }
  while (true)
    {
      /* I'm not sure that Solaris will return 0 when the buffer is too small.
       * Therefore we do not check for (buf_used != 0) as the termination
       * condition.
       */
      size_t buf_used = strftime (buf, buf_size, timefmt, p);
      if (buf_used              /* Conforming POSIX system */
          && (buf_used < buf_size)) /* Solaris workaround */
        {
          char *altbuf;
          size_t i = 0, n = 0;
          size_t final_len = (buf_used
                              + 1u /* for \0 */
                              + ns_size);
          buf = xrealloc (buf, final_len);
          buf_size = final_len;
          altbuf = xmalloc (final_len);
          strftime (altbuf, buf_size, timefmt, &altered_time);

          /* Find the seconds digits; they should be the only changed part.
           * In theory the result of the two formatting operations could differ in
           * more than just one sequence of decimal digits (for example %X might
           * in theory return a spelled-out time like "thirty seconds past noon").
           * When that happens, we just avoid inserting the nanoseconds field.
           */
          if (scan_for_digit_differences (buf, altbuf, &i, &n)
              && (2==n) && !isdigit ((unsigned char)buf[i+n]))
            {
              const size_t end_of_seconds = i + n;
              const size_t suffix_len = buf_used-(end_of_seconds)+1;

              /* Move the tail (including the \0).  Note that this
               * is a move of an overlapping memory block, so we
               * must use memmove instead of memcpy.  Then insert
               * the nanoseconds (but not its trailing \0).
               */
              assert (end_of_seconds + ns_size + suffix_len == final_len);
              memmove (buf+end_of_seconds+ns_size,
                       buf+end_of_seconds,
                       suffix_len);
              memcpy (buf+i+n, ns, ns_size);
            }
          else
            {
              /* No seconds digits.  No need to insert anything. */
            }
          /* The first character of buf is the underscore, which we actually
           * don't want.
           */
          free (timefmt);
          free (altbuf);
          return buf+1;
        }
      else
        {
          buf = x2nrealloc (buf, &buf_size, sizeof *buf);
        }
    }
}

/* Return a static string formatting the time WHEN according to the
 * strftime format character KIND.
 *
 * This function contains a number of assertions.  These look like
 * runtime checks of the results of computations, which would be a
 * problem since external events should not be tested for with
 * "assert" (instead you should use "if").  However, they are not
 * really runtime checks.  The assertions actually exist to verify
 * that the various buffers are correctly sized.
 */
static char *
format_date (struct timespec ts, int kind)
{
  /* In theory, we use an extra 10 characters for 9 digits of
   * nanoseconds and 1 for the decimal point.  However, the real
   * world is more complex than that.
   *
   * For example, some systems return junk in the tv_nsec part of
   * st_birthtime.  An example of this is the NetBSD-4.0-RELENG kernel
   * (at Sat Mar 24 18:46:46 2007) running a NetBSD-3.1-RELEASE
   * runtime and examining files on an msdos filesytem.  So for that
   * reason we set NS_BUF_LEN to 32, which is simply "long enough" as
   * opposed to "exactly the right size".  Note that the behaviour of
   * NetBSD appears to be a result of the use of uninitialized data,
   * as it's not 100% reproducible (more like 25%).
   */
  enum {
    NS_BUF_LEN = 32,
    DATE_LEN_PERCENT_APLUS=21   /* length of result of %A+ (it's longer than %c)*/
  };
  static char buf[128u+10u + MAX(DATE_LEN_PERCENT_APLUS,
                            MAX (LONGEST_HUMAN_READABLE + 2, NS_BUF_LEN+64+200))];
  char ns_buf[NS_BUF_LEN]; /* -.9999999990 (- sign can happen!)*/
  int  charsprinted, need_ns_suffix;
  struct tm *tm;
  char fmt[12];

  /* human_readable() assumes we pass a buffer which is at least as
   * long as LONGEST_HUMAN_READABLE.  We use an assertion here to
   * ensure that no nasty unsigned overflow happened in our calculation
   * of the size of buf.  Do the assertion here rather than in the
   * code for %@ so that we find the problem quickly if it exists.  If
   * you want to submit a patch to move this into the if statement, go
   * ahead, I'll apply it.  But include performance timings
   * demonstrating that the performance difference is actually
   * measurable.
   */
  verify (sizeof (buf) >= LONGEST_HUMAN_READABLE);

  charsprinted = 0;
  need_ns_suffix = 0;

  /* Format the main part of the time. */
  if (kind == '+')
    {
      /* Avoid %F, some Unix versions lack it.  For example:
         HP Tru64 UNIX V5.1B (Rev. 2650); Wed Feb 17 22:59:59 CST 2016
         Also, some older HP-UX versions expand %F as the full month (like %B).
         Reported by Steven M. Schweda <sms@antinode.info> */
      strcpy (fmt, "%Y-%m-%d+%T");
      need_ns_suffix = 1;
    }
  else
    {
      fmt[0] = '%';
      fmt[1] = kind;
      fmt[2] = '\0';

      /* %a, %c, and %t are handled in ctime_format() */
      switch (kind)
        {
        case 'S':
        case 'T':
        case 'X':
        case '@':
          need_ns_suffix = 1;
          break;
        default:
          need_ns_suffix = 0;
          break;
        }
    }

  if (need_ns_suffix)
    {
      /* Format the nanoseconds part.  Leave a trailing zero to
       * discourage people from writing scripts which extract the
       * fractional part of the timestamp by using column offsets.
       * The reason for discouraging this is that in the future, the
       * granularity may not be nanoseconds.
       */
      charsprinted = snprintf (ns_buf, NS_BUF_LEN, ".%09ld0", (long int)ts.tv_nsec);
      assert (charsprinted < NS_BUF_LEN);
    }
  else
    {
      charsprinted = 0;
      ns_buf[0] = 0;
    }

  if (kind != '@')
    {
      tm = localtime (&ts.tv_sec);
      if (tm)
        {
          char *s = do_time_format (fmt, tm, ns_buf, charsprinted);
          if (s)
            return s;
        }
    }

  /* If we get to here, either the format was %@, or we have fallen back to it
   * because strftime failed.
   */
  if (1)
    {
      uintmax_t w = ts.tv_sec;
      size_t used, len, remaining;

      /* XXX: note that we are negating an unsigned type which is the
       * widest possible unsigned type.
       */
      char *p = human_readable (ts.tv_sec < 0 ? -w : w, buf + 1,
                                human_ceiling, 1, 1);
      assert (p > buf);
      assert (p < (buf + (sizeof buf)));
      if (ts.tv_sec < 0)
        *--p = '-'; /* XXX: Ugh, relying on internal details of human_readable(). */

      /* Add the nanoseconds part.  Because we cannot enforce a
       * particlar implementation of human_readable, we cannot assume
       * any particular value for (p-buf).  So we need to be careful
       * that there is enough space remaining in the buffer.
       */
      if (need_ns_suffix)
        {
          len = strlen (p);
          used = (p-buf) + len; /* Offset into buf of current end */
          assert (sizeof buf > used); /* Ensure we can perform subtraction safely. */
          remaining = sizeof buf - used - 1u; /* allow space for NUL */

          if (strlen (ns_buf) >= remaining)
            {
              error (0, 0,
                     "charsprinted=%ld but remaining=%lu: ns_buf=%s",
                     (long)charsprinted, (unsigned long)remaining, ns_buf);
            }
          assert (strlen (ns_buf) < remaining);
          strcat (p, ns_buf);
        }
      return p;
    }
}

static const char *weekdays[] =
  {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
static const char * months[] =
  {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };


static char *
ctime_format (struct timespec ts)
{
  const struct tm * ptm;
#define TIME_BUF_LEN 1024
  static char resultbuf[TIME_BUF_LEN];
  int nout;

  ptm = localtime (&ts.tv_sec);
  if (ptm)
    {
      assert (ptm->tm_wday >=  0);
      assert (ptm->tm_wday <   7);
      assert (ptm->tm_mon  >=  0);
      assert (ptm->tm_mon  <  12);
      assert (ptm->tm_hour >=  0);
      assert (ptm->tm_hour <  24);
      assert (ptm->tm_min  <  60);
      assert (ptm->tm_sec  <= 61); /* allows 2 leap seconds. */

      /* wkday mon mday hh:mm:ss.nnnnnnnnn yyyy */
      nout = snprintf (resultbuf, TIME_BUF_LEN,
                       "%3s %3s %2d %02d:%02d:%02d.%09ld0 %04d",
                       weekdays[ptm->tm_wday],
                       months[ptm->tm_mon],
                       ptm->tm_mday,
                       ptm->tm_hour,
                       ptm->tm_min,
                       ptm->tm_sec,
                       (long int)ts.tv_nsec,
                       1900 + ptm->tm_year);

      assert (nout < TIME_BUF_LEN);
      return resultbuf;
    }
  else
    {
      /* The time cannot be represented as a struct tm.
         Output it as an integer.  */
      return format_date (ts, '@');
    }
}

static double
file_sparseness (const struct stat *p)
{
  if (0 == p->st_size)
    {
      if (0 == ST_NBLOCKS(*p))
        return 1.0;
      else
        return ST_NBLOCKS(*p) < 0 ? -HUGE_VAL : HUGE_VAL;
    }
  else
    {
      double blklen = ST_NBLOCKSIZE * (double)ST_NBLOCKS(*p);
      return blklen / p->st_size;
    }
}


static void
checked_fprintf (struct format_val *dest, const char *fmt, ...)
{
  int rv;
  va_list ap;

  va_start (ap, fmt);
  rv = vfprintf (dest->stream, fmt, ap);
  va_end (ap);
  if (rv < 0)
    nonfatal_nontarget_file_error (errno, dest->filename);
}

static void
checked_print_quoted (struct format_val *dest,
                      const char *format, const char *s)
{
  int rv = print_quoted (dest->stream, dest->quote_opts, dest->dest_is_tty,
                         format, s);
  if (rv < 0)
    nonfatal_nontarget_file_error (errno, dest->filename);
}


static void
checked_fwrite (void *p, size_t siz, size_t nmemb, struct format_val *dest)
{
  const size_t items_written = fwrite (p, siz, nmemb, dest->stream);
  if (items_written < nmemb)
    nonfatal_nontarget_file_error (errno, dest->filename);
}

static void
checked_fflush (struct format_val *dest)
{
  if (0 != fflush (dest->stream))
    {
      nonfatal_nontarget_file_error (errno, dest->filename);
    }
}

static const char*
mode_to_filetype (mode_t m)
{
#define HANDLE_TYPE(t,letter) if (m==t) { return letter; }
#ifdef S_IFREG
  HANDLE_TYPE(S_IFREG,  "f");   /* regular file */
#endif
#ifdef S_IFDIR
  HANDLE_TYPE(S_IFDIR,  "d");   /* directory */
#endif
#ifdef S_IFLNK
  HANDLE_TYPE(S_IFLNK,  "l");   /* symbolic link */
#endif
#ifdef S_IFSOCK
  HANDLE_TYPE(S_IFSOCK, "s");   /* Unix domain socket */
#endif
#ifdef S_IFBLK
  HANDLE_TYPE(S_IFBLK,  "b");   /* block device */
#endif
#ifdef S_IFCHR
  HANDLE_TYPE(S_IFCHR,  "c");   /* character device */
#endif
#ifdef S_IFIFO
  HANDLE_TYPE(S_IFIFO,  "p");   /* FIFO */
#endif
#ifdef S_IFDOOR
  HANDLE_TYPE(S_IFDOOR, "D");   /* Door (e.g. on Solaris) */
#endif
  return "U";                   /* Unknown */
}



static void
do_fprintf (struct format_val *dest,
            struct segment *segment,
            const char *pathname,
            const struct stat *stat_buf)
{
  char hbuf[LONGEST_HUMAN_READABLE + 1];
  const char *cp;

  switch (segment->segkind)
    {
    case KIND_PLAIN:    /* Plain text string (no % conversion). */
      /* trusted */
      checked_fwrite(segment->text, 1, segment->text_len, dest);
      break;

    case KIND_STOP:             /* Terminate argument and flush output. */
      /* trusted */
      checked_fwrite (segment->text, 1, segment->text_len, dest);
      checked_fflush (dest);
      break;

    case KIND_FORMAT:
      switch (segment->format_char[0])
        {
        case 'a':               /* atime in `ctime' format. */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text, ctime_format (get_stat_atime (stat_buf)));
          break;
        case 'b':               /* size in 512-byte blocks */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text,
                           human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf),
                                           hbuf, human_ceiling,
                                           ST_NBLOCKSIZE, 512));
          break;
        case 'c':               /* ctime in `ctime' format */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text, ctime_format (get_stat_ctime (stat_buf)));
          break;
        case 'd':               /* depth in search tree */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text, state.curdepth);
          break;
        case 'D':               /* Device on which file exists (stat.st_dev) */
          /* trusted */
          checked_fprintf (dest, segment->text,
                           human_readable ((uintmax_t) stat_buf->st_dev, hbuf,
                                           human_ceiling, 1, 1));
          break;
        case 'f':               /* base name of path */
          /* sanitised */
          {
            char *base = base_name (pathname);
            checked_print_quoted (dest, segment->text, base);
            free (base);
          }
          break;
        case 'F':               /* file system type */
          /* trusted */
          checked_print_quoted (dest, segment->text, filesystem_type (stat_buf, pathname));
          break;
        case 'g':               /* group name */
          /* trusted */
          /* (well, the actual group is selected by the user but
           * its name was selected by the system administrator)
           */
          {
            struct group *g;

            g = getgrgid (stat_buf->st_gid);
            if (g)
              {
                segment->text[segment->text_len] = 's';
                checked_fprintf (dest, segment->text, g->gr_name);
                break;
              }
          }
          FALLTHROUGH; /*...sometimes, so 'G' case.*/

        case 'G':               /* GID number */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text,
                           human_readable ((uintmax_t) stat_buf->st_gid, hbuf,
                                           human_ceiling, 1, 1));
          break;
        case 'h':               /* leading directories part of path */
          /* sanitised */
          {
            char *pname = xstrdup (pathname);

            /* Remove trailing slashes - unless it's the root '/' directory.  */
            char *s = pname + strlen (pname) -1;
            for ( ; pname <= s; s--)
              if (*s != '/')
                break;
            if (pname < s && *(s+1) == '/')
              *(s+1) = '\0';

            s = strrchr (pname, '/');
            if (s == NULL)     /* No leading directories. */
              {
                /* If there is no slash in the pathname, we still
                 * print the string because it contains characters
                 * other than just '%s'.  The %h expands to ".".
                 */
                checked_print_quoted (dest, segment->text, ".");
              }
            else
              {
                *s = '\0';
                checked_print_quoted (dest, segment->text, pname);
              }
            free (pname);
          }
          break;

        case 'H':               /* ARGV element file was found under */
          /* trusted */
          {
            char *s = xmalloc (state.starting_path_length+1);
            memcpy (s, pathname, state.starting_path_length);
            s[state.starting_path_length] = 0;
            checked_fprintf (dest, segment->text, s);
            free (s);
          }
          break;

        case 'i':               /* inode number */
          /* UNTRUSTED, but not exploitable I think */
          /* POSIX does not guarantee that ino_t is unsigned or even
           * integral (except as an XSI extension), but we'll work on
           * fixing that if we ever get a report of a system where
           * ino_t is indeed a signed integral type or a non-integral
           * arithmetic type. */
          checked_fprintf (dest, segment->text,
                           human_readable ((uintmax_t) stat_buf->st_ino, hbuf,
                                           human_ceiling,
                                           1, 1));
          break;
        case 'k':               /* size in 1K blocks */
          /* UNTRUSTED, but not exploitable I think */
          checked_fprintf (dest, segment->text,
                           human_readable ((uintmax_t) ST_NBLOCKS (*stat_buf),
                                           hbuf, human_ceiling,
                                           ST_NBLOCKSIZE, 1024));
          break;
        case 'l':               /* object of symlink */
          /* sanitised */
#ifdef S_ISLNK
          {
            char *linkname = 0;

            if (S_ISLNK (stat_buf->st_mode))
              {
                linkname = areadlinkat (state.cwd_dir_fd, state.rel_pathname);
                if (linkname == NULL)
                  {
                    nonfatal_target_file_error (errno, pathname);
                    state.exit_status = EXIT_FAILURE;
                  }
              }
            if (linkname)
              {
                checked_print_quoted (dest, segment->text, linkname);
              }
            else
              {
                /* We still need to honour the field width etc., so this is
                 * not a no-op.
                 */
                checked_print_quoted (dest, segment->text, "");
              }
            free (linkname);
          }
#endif                          /* S_ISLNK */
          break;

        case 'M':               /* mode as 10 chars (eg., "-rwxr-x--x" */
          /* UNTRUSTED, probably unexploitable */
          {
            char modestring[16] ;
            filemodestring (stat_buf, modestring);
            modestring[10] = '\0';
            checked_fprintf (dest, segment->text, modestring);
          }
          break;

        case 'm':               /* mode as octal number (perms only) */
          /* UNTRUSTED, probably unexploitable */
          {
            /* Output the mode portably using the traditional numbers,
               even if the host unwisely uses some other numbering
               scheme.  But help the compiler in the common case where
               the host uses the traditional numbering scheme.  */
            mode_t m = stat_buf->st_mode;
            bool traditional_numbering_scheme =
              (S_ISUID == 04000 && S_ISGID == 02000 && S_ISVTX == 01000
               && S_IRUSR == 00400 && S_IWUSR == 00200 && S_IXUSR == 00100
               && S_IRGRP == 00040 && S_IWGRP == 00020 && S_IXGRP == 00010
               && S_IROTH == 00004 && S_IWOTH == 00002 && S_IXOTH == 00001);
            checked_fprintf (dest, segment->text,
                     (traditional_numbering_scheme
                      ? m & MODE_ALL
                      : ((m & S_ISUID ? 04000 : 0)
                         | (m & S_ISGID ? 02000 : 0)
                         | (m & S_ISVTX ? 01000 : 0)
                         | (m & S_IRUSR ? 00400 : 0)
                         | (m & S_IWUSR ? 00200 : 0)
                         | (m & S_IXUSR ? 00100 : 0)
                         | (m & S_IRGRP ? 00040 : 0)
                         | (m & S_IWGRP ? 00020 : 0)
                         | (m & S_IXGRP ? 00010 : 0)
                         | (m & S_IROTH ? 00004 : 0)
                         | (m & S_IWOTH ? 00002 : 0)
                         | (m & S_IXOTH ? 00001 : 0))));
          }
          break;

        case 'n':               /* number of links */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text,
                   human_readable ((uintmax_t) stat_buf->st_nlink,
                                   hbuf,
                                   human_ceiling,
                                   1, 1));
          break;

        case 'p':               /* pathname */
          /* sanitised */
          checked_print_quoted (dest, segment->text, pathname);
          break;

        case 'P':               /* pathname with ARGV element stripped */
          /* sanitised */
          if (state.curdepth > 0)
            {
              cp = pathname + state.starting_path_length;
              if (*cp == '/')
                /* Move past the slash between the ARGV element
                   and the rest of the pathname.  But if the ARGV element
                   ends in a slash, we didn't add another, so we've
                   already skipped past it.  */
                cp++;
            }
          else
            {
              cp = "";
            }
          checked_print_quoted (dest, segment->text, cp);
          break;

        case 's':               /* size in bytes */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text,
                   human_readable ((uintmax_t) stat_buf->st_size,
                                   hbuf, human_ceiling, 1, 1));
          break;

        case 'S':               /* sparseness */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text, file_sparseness (stat_buf));
          break;

        case 't':               /* mtime in `ctime' format */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text,
                           ctime_format (get_stat_mtime (stat_buf)));
          break;

        case 'u':               /* user name */
          /* trusted */
          /* (well, the actual user is selected by the user on systems
           * where chown is not restricted, but the user name was
           * selected by the system administrator)
           */
          {
            struct passwd *p;

            p = getpwuid (stat_buf->st_uid);
            if (p)
              {
                segment->text[segment->text_len] = 's';
                checked_fprintf (dest, segment->text, p->pw_name);
                break;
              }
          }
          FALLTHROUGH; /* .. to case U */

        case 'U':               /* UID number */
          /* UNTRUSTED, probably unexploitable */
          checked_fprintf (dest, segment->text,
                           human_readable ((uintmax_t) stat_buf->st_uid, hbuf,
                                           human_ceiling, 1, 1));
          break;

          /* %Y: type of file system entry like `ls -l`:
           *     (d,-,l,s,p,b,c,n) n=nonexistent (symlink)
           */
        case 'Y':               /* in case of symlink */
          /* trusted */
          {
#ifdef S_ISLNK
            if (S_ISLNK (stat_buf->st_mode))
              {
                struct stat sbuf;
                /* %Y needs to stat the symlink target regardless of
                 * whether we would normally follow symbolic links or not.
                 * (Actually we do not even come here when following_links()
                 *  other than the ENOENT case.)
                 */
                if (fstatat (state.cwd_dir_fd, state.rel_pathname, &sbuf, 0) != 0)
                  {
                    if ( (errno == ENOENT) || (errno == ENOTDIR) )
                      {
                        checked_fprintf (dest, segment->text, "N");
                        break;
                      }
                    else if ( errno == ELOOP )
                      {
                        checked_fprintf (dest, segment->text, "L");
                        break;
                      }
                    else
                      {
                        checked_fprintf (dest, segment->text, "?");
                        error (0, errno, "%s",
                               safely_quote_err_filename (0, pathname));
                        /* exit_status = EXIT_FAILURE;
                           return ; */
                        break;
                      }
                  }
                checked_fprintf (dest, segment->text,
                                 mode_to_filetype (sbuf.st_mode & S_IFMT));
              }
            else
#endif /* S_ISLNK */
              {
                checked_fprintf (dest, segment->text,
                                 mode_to_filetype (stat_buf->st_mode & S_IFMT));
              }
          }
          break;

        case 'y':
          /* trusted */
          {
            checked_fprintf (dest, segment->text,
                             mode_to_filetype (stat_buf->st_mode & S_IFMT));
          }
          break;

        case 'Z':               /* SELinux security context */
          {
            char *scontext;
            int rv = (*options.x_getfilecon) (state.cwd_dir_fd, state.rel_pathname,
                                              &scontext);
            if (rv < 0)
              {
                /* If getfilecon fails, there will in the general case
                   still be some text to print.   We just make %Z expand
                   to an empty string. */
                checked_fprintf (dest, segment->text, "");

                error (0, errno, _("getfilecon failed: %s"),
                    safely_quote_err_filename (0, pathname));
                state.exit_status = EXIT_FAILURE;
              }
            else
              {
                checked_fprintf (dest, segment->text, scontext);
                freecon (scontext);
              }
          }
          break;

        case '%':
          checked_fwrite (segment->text, 1, segment->text_len, dest);
          break;

        case 0:
	  /* Trailing single %.  This should have been rejected by
	     insert_fprintf.  We use %s here in the error message
	     simply to ensure that the error message matches the one
	     in insert_fprintf, easing the translation burden.
	   */
	  die (EXIT_FAILURE, 0, _("error: %s at end of format string"), "%");
	  /*NOTREACHED*/
	  break;
        }
      /* end of KIND_FORMAT case */
      break;
    }
}

bool
pred_fprintf (const char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
{
  struct format_val *dest = &pred_ptr->args.printf_vec;
  struct segment *segment;

  for (segment = dest->segment; segment; segment = segment->next)
    {
      if ( (KIND_FORMAT == segment->segkind) && segment->format_char[1]) /* Component of date. */
        {
          struct timespec ts;
          int valid = 0;

          switch (segment->format_char[0])
            {
            case 'A':
              ts = get_stat_atime (stat_buf);
              valid = 1;
              break;
            case 'B':
              ts = get_stat_birthtime (stat_buf);
              if ('@' == segment->format_char[1])
                valid = 1;
              else
                valid = (ts.tv_nsec >= 0);
              break;
            case 'C':
              ts = get_stat_ctime (stat_buf);
              valid = 1;
              break;
            case 'T':
              ts = get_stat_mtime (stat_buf);
              valid = 1;
              break;
            default:
              assert (0);
              abort ();
            }
          /* We trust the output of format_date not to contain
           * nasty characters, though the value of the date
           * is itself untrusted data.
           */
          if (valid)
            {
              /* trusted */
              checked_fprintf (dest, segment->text,
                               format_date (ts, segment->format_char[1]));
            }
          else
            {
              /* The specified timestamp is not available, output
               * nothing for the timestamp, but use the rest (so that
               * for example find foo -printf '[%Bs] %p\n' can print
               * "[] foo").
               */
              /* trusted */
              checked_fprintf (dest, segment->text, "");
            }
        }
      else
        {
          /* Print a segment which is not a date. */
          do_fprintf (dest, segment, pathname, stat_buf);
        }
    }
  return true;
}