-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdyn_test.c
1384 lines (1235 loc) · 35.2 KB
/
dyn_test.c
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
/*
* dyn_array_test - test the dynamic array facility
*
* Copyright (c) 2014,2015,2022-2024 by Landon Curt Noll. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
* EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* Share and enjoy! :-)
*/
/* special comments for the seqcexit tool */
/* exit code out of numerical order - ignore in sequencing - ooo */
/* exit code change of order - use new value in sequencing - coo */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* dyn_array_test - test the dynamic array facility
*/
#include "dyn_test.h"
/*
* definitions
*/
#define REQUIRED_ARGS (0) /* number of required arguments on the command line */
#define CHUNK (1024) /* allocate CHUNK elements at a time */
/*
* usage message
*
* Use the usage() function to print the usage_msg([0-9]?)+ strings.
*/
static const char * const usage_msg =
"usage: %s [-h] [-v level] [-V]\n"
"\n"
"\t-h\t\tprint help message and exit\n"
"\t-v level\tset verbosity level (def level: %d)\n"
"\t-V\t\tprint version string and exit\n"
"\n"
"\n"
"Exit codes:\n"
" 0\tall is OK\n"
" 1\ttest suite failed\n"
" 2\t-h and help string printed or -V and version string printed\n"
" 3\tcommand line error\n"
" >=10\tinternal error\n"
"\n"
"%s version: %s\n"
"dyn_array library version: %s\n";
/*
* forward declarations
*/
static void usage(int exitcode, char const *str, char const *prog) __attribute__((noreturn));
/*
* dbg forward declarations
*
* NOTICE: What follows are declarations lifted from dbg.c in the dbg repo:
*
* https://github.com/lcn2/dbg
*
* We reproduce it here so that one may run this test code here without having to have
* access to the dbg repo source code.
*/
#if !defined(DBG_USE)
# if !defined(DBG_NONE)
# define DBG_NONE (0) /* no debugging */
# endif
# if !defined(DBG_DEFAULT)
# define DBG_DEFAULT (DBG_NONE) /* default debugging level */
# endif
int verbosity_level = DBG_DEFAULT; /* maximum debug level for debug messages */
bool dbg_output_allowed = true; /* false ==> disable debug messages */
bool warn_output_allowed = true; /* false ==> disable warning messages */
bool err_output_allowed = true; /* false ==> disable error messages */
bool usage_output_allowed = true; /* false ==> disable usage messages */
bool msg_warn_silent = false; /* true ==> silence info & warnings if verbosity_level <= 0 */
static void fdbg_write(FILE *stream, char const *caller, int level, char const *fmt, va_list ap);
static void fwarn_write(FILE *stream, char const *caller, char const *name, char const *fmt, va_list ap);
static void ferrp_write(FILE *stream, int error_code, char const *caller,
char const *name, char const *fmt, va_list ap);
static void fwarnp_write(FILE *stream, char const *caller, char const *name, char const *fmt, va_list ap);
static void fusage_write(FILE *stream, int error_code, char const *caller, char const *fmt, va_list ap);
bool dbg_allowed(int level);
bool warn_allowed(void);
bool err_allowed(void);
bool usage_allowed(void);
void dbg(int level, char const *fmt, ...);
void fwarn(FILE *stream, char const *name, char const *fmt, ...);
void warnp(char const *name, char const *fmt, ...);
void errp(int exitcode, char const *name, char const *fmt, ...);
void fprintf_usage(int exitcode, FILE *stream, char const *fmt, ...);
int parse_verbosity(char const *optarg);
void check_invalid_option(char const *prog, int ch, int opt);
#endif
int
main(int argc, char *argv[])
{
char const *program = NULL; /* our name */
struct dyn_array *array; /* dynamic array to test */
double d; /* test double */
bool error = false; /* true ==> test error found */
intmax_t len = 0; /* length of the dynamic array */
int i;
/*
* parse args
*/
program = argv[0];
while ((i = getopt(argc, argv, ":hv:V")) != -1) {
switch (i) {
case 'h': /* -h - print help to stderr and exit 0 */
usage(2, program, ""); /*ooo*/
not_reached();
break;
case 'v': /* -v verbosity */
/*
* parse verbosity
*/
verbosity_level = parse_verbosity(optarg);
if (verbosity_level < 0) {
usage(3, program, "invalid -v verbosity"); /*ooo*/
not_reached();
}
break;
case 'V': /* -V - print version and exit */
(void) printf("%s version: %s\n", DYN_TEST_BASENAME, DYN_TEST_VERSION);
exit(2); /*ooo*/
not_reached();
break;
case ':': /* option requires an argument */
case '?': /* illegal option */
default: /* anything else but should not actually happen */
check_invalid_option(program, i, optopt);
usage(3, program, ""); /*ooo*/
not_reached();
break;
}
}
if (argc - optind != REQUIRED_ARGS) {
usage(3, program, "wrong number of arguments"); /*ooo*/
not_reached();
}
/*
* create dynamic array
*/
array = dyn_array_create(sizeof(double), CHUNK, CHUNK, true);
dbg(DBG_MED, "created array of size %ju", (uintmax_t)CHUNK);
/*
* load a million doubles
*/
for (d = 0.0; d < 1000000.0; d += 1.0) {
if (dyn_array_append_value(array, &d)) {
dbg(DBG_LOW, "moved data after appending d: %f", d);
}
}
/*
* verify values
*/
for (i = 0; i < 1000000; ++i) {
if ((intmax_t)i != (intmax_t)dyn_array_value(array, double, i)) {
warn(__func__, "value mismatch %d != %f", i, dyn_array_value(array, double, i));
error = true;
}
}
/*
* verify size
*/
dbg(DBG_HIGH, "determining size of array");
len = dyn_array_tell(array);
if (len != 1000000) {
warn(__func__, "dyn_array_tell(array): %jd != %jd", len, (intmax_t)1000000);
error = true;
}
dbg(DBG_VHIGH, "array size is %ju", (uintmax_t)len);
/*
* concatenate the array onto itself
*/
dbg(DBG_MED, "concatenating array on to itself");
if (dyn_array_concat_array(array, array)) {
dbg(DBG_LOW, "moved data after concatenation");
}
/*
* verify new size
*/
dbg(DBG_HIGH, "determining new size of array");
len = dyn_array_tell(array);
if (len != 2000000) {
warn(__func__, "dyn_array_tell(array): %jd != %jd", len, (intmax_t)2000000);
error = true;
}
dbg(DBG_VHIGH, "new size of array is %ju", (uintmax_t)len);
/*
* verify values again
*/
dbg(DBG_HIGH, "verifying values again");
for (i = 0; i < 1000000; ++i) {
if ((intmax_t)i != (intmax_t)dyn_array_value(array, double, i)) {
warn(__func__, "value mismatch %d != %f", i, dyn_array_value(array, double, i));
error = true;
}
if ((intmax_t)i != (intmax_t)dyn_array_value(array, double, i+1000000)) {
warn(__func__, "value mismatch %d != %f", i, dyn_array_value(array, double, i+1000000));
error = true;
}
}
/*
* free dynamic array
*/
if (array != NULL) {
dyn_array_free(array);
array = NULL;
}
/*
* exit based on the test result
*/
if (error == true) {
exit(1); /*ooo*/
}
exit(0); /*ooo*/
}
/*
* usage - print usage to stderr
*
* Example:
* usage(3, program, "missing required argument(s), program: %s");
*
* given:
* exitcode value to exit with
* str top level usage message
* program our program name
*
* NOTE: We warn with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*
* This function does not return.
*/
static void
usage(int exitcode, char const *prog, char const *str)
{
/*
* firewall
*/
if (str == NULL) {
str = "((NULL str))";
warn(__func__, "\nin usage(): str was NULL, forcing it to be: %s\n", str);
}
if (prog == NULL) {
prog = "((NULL prog))";
warn(__func__, "\nin usage(): prog was NULL, forcing it to be: %s\n", prog);
}
/*
* print the formatted usage stream
*/
if (*str != '\0') {
fprintf_usage(DO_NOT_EXIT, stderr, "%s\n", str);
}
fprintf_usage(exitcode, stderr, usage_msg, prog, DBG_DEFAULT, DYN_TEST_BASENAME, DYN_TEST_VERSION, dyn_array_version);
exit(exitcode); /*ooo*/
not_reached();
}
/*
* NOTICE: What follows is code lifted from dbg.c in the dbg repo:
*
* https://github.com/lcn2/dbg
*
* We reproduce it here so that one may run this test code here without having to have
* access to the dbg repo source code.
*/
#if !defined(DBG_USE)
/*
* fdbg_write - write a diagnostic message to a stream
*
* Write a formatted debug diagnostic message to a stream. The diagnostic is followed by
* a newline and then the stream is flushed.
*
* given:
* stream open stream on which to write
* caller name of the calling function
* level debug level
* fmt format of the warning
* ap variable argument list
*
* NOTE: If stream is NULL, stderr will be used. If stderr is also NULL,
* this function does nothing (just returns).
*
* NOTE: This function does nothing (just returns) if passed a NULL pointer.
*
* NOTE: We call warnp() with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*/
static void
fdbg_write(FILE *stream, char const *caller, int level, char const *fmt, va_list ap)
{
int ret; /* libc function return code */
int saved_errno; /* errno at function start */
/*
* firewall - if stream is NULL, try stderr
*/
if (stream == NULL) {
if (stderr != NULL) {
fwarn(stderr, __func__, "called with NULL stream, will use stderr");
}
stream = stderr;
}
/*
* firewall - just return if given a NULL ptr
*/
if (stream == NULL || caller == NULL || fmt == NULL) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* write debug header
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "debug[%d]: ", level);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): fprintf error\n",
__func__, caller, level, fmt);
}
/*
* write diagnostic to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = vfprintf(stream, fmt, ap);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): vfprintf error\n",
__func__, caller, level, fmt);
}
/*
* write final newline to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fputc('\n', stream);
if (ret != '\n') {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): fputc error\n",
__func__, caller, level, fmt);
}
/*
* flush the stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fflush(stream);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): fflush error\n",
__func__, caller, level, fmt);
}
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* fwarn_write - write a warning to a stream
*
* Write a warming message to a stream. The diagnostic is followed by
* a newline and then the stream is flushed.
*
* given:
* stream open stream on which to write
* caller name of the calling function
* name name of function issuing the warning
* fmt format of the warning
* ap variable argument list
*
* NOTE: If stream is NULL, stderr will be used. If stderr is also NULL,
* this function does nothing (just returns).
*
* NOTE: This function does nothing (just returns) if passed a NULL pointer.
*/
static void
fwarn_write(FILE *stream, char const *caller, char const *name, char const *fmt, va_list ap)
{
int ret; /* libc function return code */
int saved_errno; /* errno at function start */
/*
* firewall - if stream is NULL, try stderr
*/
if (stream == NULL) {
if (stderr != NULL) {
fwarn(stderr, __func__, "called with NULL stream, will use stderr");
}
stream = stderr;
}
/*
* firewall - just return if given a NULL ptr
*/
if (stream == NULL || caller == NULL || name == NULL || fmt == NULL) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* write warning header to stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = fprintf(stream, "Warning: %s: ", name);
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): fprintf returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* write warning to stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = vfprintf(stream, fmt, ap);
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): vfprintf returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* write final newline to stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = fputc('\n', stream);
if (ret != '\n') {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): fputc returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* flush the stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = fflush(stream);
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): fflush returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* ferrp_write - write an error diagnostic with errno details, to a stream
*
* Write a formatted an error diagnostic with errno details to a stream.
*
* given:
* stream open stream on which to write
* error_code error code
* caller name of the calling function
* name name of function issuing the error diagnostic
* fmt format of the warning
* ap variable argument list
*
* NOTE: If stream is NULL, stderr will be used. If stderr is also NULL,
* this function does nothing (just returns).
*
* NOTE: This function does nothing (just returns) if passed a other NULL pointers.
*
* NOTE: We call warnp() with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*/
static void
ferrp_write(FILE *stream, int error_code, char const *caller,
char const *name, char const *fmt, va_list ap)
{
int ret; /* libc function return code */
int saved_errno; /* errno at function start */
/*
* firewall - if stream is NULL, try stderr
*/
if (stream == NULL) {
if (stderr != NULL) {
fwarn(stderr, __func__, "called with NULL stream, will use stderr");
}
stream = stderr;
}
/*
* firewall - just return if given a NULL ptr
*/
if (stream == NULL || caller == NULL || name == NULL || fmt == NULL) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* write error diagnostic warning header to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "ERROR[%d]: %s: ", error_code, name);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, %s, ap): fprintf #0 error\n",
__func__, caller, error_code, name, fmt);
}
/*
* write error diagnostic warning to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = vfprintf(stream, fmt, ap);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, %s, ap): vfprintf error\n",
__func__, caller, error_code, name, fmt);
}
/*
* write errno details plus newline to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, ": errno[%d]: %s\n", saved_errno, strerror(saved_errno));
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, %s, ap): fprintf #1 error\n",
__func__, caller, error_code, name, fmt);
}
/*
* flush the stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fflush(stream);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, %s, ap): fflush error\n",
__func__, caller, error_code, name, fmt);
}
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* fwarnp_write - write a warning message with errno details, to a stream
*
* Write a warming message with errno info to a stream. The diagnostic is followed by
* a newline and then the stream is flushed.
*
* given:
* stream open stream on which to write
* caller name of the calling function
* name name of function issuing the warning
* fmt format of the warning
* ap variable argument list
*
* NOTE: If stream is NULL, stderr will be used. If stderr is also NULL,
* this function does nothing (just returns).
*
* NOTE: This function does nothing (just returns) if passed other NULL pointers.
*/
static void
fwarnp_write(FILE *stream, char const *caller, char const *name, char const *fmt, va_list ap)
{
int ret; /* libc function return code */
int saved_errno; /* errno at function start */
/*
* firewall - if stream is NULL, try stderr
*/
if (stream == NULL) {
if (stderr != NULL) {
fwarn(stderr, __func__, "called with NULL stream, will use stderr");
}
stream = stderr;
}
/*
* firewall - just return if given a NULL ptr
*/
if (stream == NULL || caller == NULL || name == NULL || fmt == NULL) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* write warning header to stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = fprintf(stream, "Warning: %s: ", name);
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): fprintf returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* write warning to stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = vfprintf(stream, fmt, ap);
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): vfprintf returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* write errno details plus newline to stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = fprintf(stream, ": errno[%d]: %s\n", saved_errno, strerror(saved_errno));
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in vwarnp(%s, %s, ap): fprintf with errno returned error: %s\n",
caller, name, fmt, strerror(errno));
}
/*
* flush the stream
*/
errno = 0; /* pre-clear errno for strerror() */
ret = fflush(stream);
if (ret < 0) {
/* we cannot call warn() because that would produce an infinite loop! */
(void) fprintf(stream, "\nWarning: %s: in %s(stream, %s, %s, %s, ap): fflush returned error: %s\n",
caller, __func__, caller, name, fmt, strerror(errno));
}
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* fusage_write - write the usage message, to a stream
*
* Write a formatted the usage message to a stream. Checks for write
* errors and call warnp() with a write error diagnostic.
*
* given:
* stream open stream on which to write
* error_code error code
* caller name of the calling function
* name name of function issuing the usage message
* fmt format of the warning
* ap variable argument list
*
* NOTE: If stream is NULL, stderr will be used. If stderr is also NULL,
* this function does nothing (just exits).
*
* NOTE: This function does nothing (just returns) if passed a NULL pointer.
*
* NOTE: We call warnp() with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*/
static void
fusage_write(FILE *stream, int error_code, char const *caller, char const *fmt, va_list ap)
{
int ret; /* libc function return code */
int saved_errno; /* errno at function start */
/*
* firewall - if stream is NULL, try stderr
*/
if (stream == NULL) {
if (stderr != NULL) {
fwarn(stderr, __func__, "called with NULL stream, will use stderr");
}
stream = stderr;
}
/*
* firewall - just return if given a NULL ptr
*/
if (stream == NULL || caller == NULL || fmt == NULL) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* write the usage message to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = vfprintf(stream, fmt, ap);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): vfprintf error\n",
__func__, caller, error_code, fmt);
}
/*
* write final newline to stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fputc('\n', stream);
if (ret != '\n') {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): fputc error\n",
__func__, caller, error_code, fmt);
}
/*
* flush the stream
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fflush(stream);
if (ret < 0) {
warnp(caller, "\nin %s(stream, %s, %d, %s, ap): fflush error\n",
__func__, caller, error_code, fmt);
}
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* dbg_allowed - determine if verbosity level allows for debug messages are allowed
*
* given:
* level write message if >= verbosity level
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
dbg_allowed(int level)
{
/*
* determine if verbosity level allows for debug messages
*/
if (dbg_output_allowed == false || level > verbosity_level) {
return false;
}
return true;
}
/*
* warn_allowed - determine if warning messages are allowed
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
warn_allowed(void)
{
/*
* determine if warning messages are allowed
*/
if (warn_output_allowed == false || (msg_warn_silent == true && verbosity_level <= 0)) {
return false;
}
return true;
}
/*
* err_allowed - determine if fatal error messages are allowed
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
err_allowed(void)
{
/*
* determine if fatal error messages are allowed
*/
if (err_output_allowed == false) {
return false;
}
return true;
}
/*
* usage_allowed - determine if command line usage messages are allowed
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
usage_allowed(void)
{
/*
* determine if conditions allow command line usage messages
*/
if (usage_output_allowed == false) {
return false;
}
return true;
}
/*
* dbg - write a verbosity level allowed debug message, to stderr
*
* given:
* level write message if >= verbosity level
* fmt printf format
* ...
*
* Example:
*
* dbg(1, "foobar information: %d", value);
*
* NOTE: If stderr is NULL, this function does nothing (just returns).
*/
void
dbg(int level, char const *fmt, ...)
{
va_list ap; /* variable argument list */
int saved_errno; /* errno at function start */
bool allowed = false; /* true ==> output is allowed */
/*
* firewall - do nothing if stderr is NULL
*/
if (stderr == NULL) {
return;
}
/*
* stage 0: determine if conditions allow function to write, return if not
*/
allowed = dbg_allowed(level);
if (allowed == false) {
return;
}
/*
* stage 1: save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* stage 2: stdarg variable argument list setup
*/
va_start(ap, fmt);
/*
* stage 3: firewall checks
*/
if (fmt == NULL) {
fmt = "((NULL fmt))";
fwarn(stderr, __func__, "\nfmt is NULL, forcing fmt to be: %s", fmt);
}
/*
* stage 4: write the diagnostic
*/
fdbg_write(stderr, __func__, level, fmt, ap);
/*
* stage 5: stdarg variable argument list cleanup
*/
va_end(ap);
/*
* stage 6: restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* fwarn - write a warning message, to a stream
*
* given:
* stream open stream to use
* name name of function issuing the warning
* fmt format of the warning
* ... optional format args
*
* Example:
*
* fwarn(stderr, __func__, "unexpected foobar: %d", value);
*
* NOTE: If stream is NULL, stderr will be used. If stderr is also NULL,
* this function does nothing (just returns).
*/
void
fwarn(FILE *stream, char const *name, char const *fmt, ...)
{
va_list ap; /* variable argument list */
int saved_errno; /* errno at function start */
bool allowed = false; /* true ==> output is allowed */
/*
* firewall - if stream is NULL, try stderr, unless that is also NULL
*/
if (stream == NULL) {
if (stderr != NULL) {
fwarn(stderr, __func__, "called with NULL stream, will use stderr");
stream = stderr;
} else {
return;
}
}
/*
* stage 0: determine if conditions allow function to write, return if not
*/
allowed = warn_allowed();
if (allowed == false) {
return;
}
/*
* stage 1: save errno so we can restore it before returning
*/
saved_errno = errno;