Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / ep / ep_pprint.c @ master

History | View | Annotate | Download (2.56 KB)

1 a901db09 Eric Allman
/* vim: set ai sw=8 sts=8 ts=8 :*/
2 47c6ea64 Eric Allman
3
/***********************************************************************
4 055d3009 Eric Allman
**  ----- BEGIN LICENSE BLOCK -----
5
**        LIBEP: Enhanced Portability Library (Reduced Edition)
6
**
7 c87dd166 Eric Allman
**        Copyright (c) 2008-2019, Eric P. Allman.  All rights reserved.
8
**        Copyright (c) 2015-2019, Regents of the University of California.
9 6bd5476b Eric Allman
**        All rights reserved.
10 055d3009 Eric Allman
**
11 6bd5476b Eric Allman
**        Permission is hereby granted, without written agreement and without
12
**        license or royalty fees, to use, copy, modify, and distribute this
13
**        software and its documentation for any purpose, provided that the above
14
**        copyright notice and the following two paragraphs appear in all copies
15
**        of this software.
16 055d3009 Eric Allman
**
17 6bd5476b Eric Allman
**        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
18
**        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
19
**        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
20
**        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 055d3009 Eric Allman
**
22 6bd5476b Eric Allman
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23 055d3009 Eric Allman
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 6bd5476b Eric Allman
**        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
25
**        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
26
**        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
27
**        OR MODIFICATIONS.
28 055d3009 Eric Allman
**  ----- END LICENSE BLOCK -----
29 47c6ea64 Eric Allman
***********************************************************************/
30
31
#include <ep.h>
32
#include <ep_stat.h>
33
#include <ctype.h>
34
35
36
/***********************************************************************
37
**
38
**  EP_ST_PPRINT -- positional print
39
**
40
**        Interpret %N in fmt (where N is an number) as interpolating
41
**        the Nth argument.
42
**
43
**        Parameters:
44 9e55d093 Eric Allman
**                fp -- the stream to print to
45 47c6ea64 Eric Allman
**                fmt -- the format string
46
**                argc -- the number of arguments
47
**                argv -- the actual arguments
48
**
49
**        Returns:
50
**                none
51
*/
52
53
void
54 6c7e8d27 Eric Allman
ep_pprint(FILE *fp,
55 47c6ea64 Eric Allman
        const char *fmt,
56
        int argc,
57
        const char *const *argv)
58
{
59
        const char *ap;
60
        char c;
61
        int i;
62
63
        while ((c = *fmt++) != '\0')
64
        {
65
                // if it's not a percent, just copy through
66
                if (c != '%')
67
                {
68 9e55d093 Eric Allman
                        putc(c, fp);
69 47c6ea64 Eric Allman
                        continue;
70
                }
71
72
                // if it is, look at the next byte
73
                if ((c = *fmt++) == '\0')
74
                {
75
                        // hack for percent at end of string
76 9e55d093 Eric Allman
                        putc('%', fp);
77 47c6ea64 Eric Allman
                        break;
78
                }
79
80
                if (!isdigit(c))
81
                {
82
                        // bogus character, unless %%
83 9e55d093 Eric Allman
                        putc('%', fp);
84 47c6ea64 Eric Allman
                        if (c != '%')
85 9e55d093 Eric Allman
                                putc(c, fp);
86 47c6ea64 Eric Allman
                        continue;
87
                }
88
89
                // interpolate Nth argument
90
                i = c - '0';
91
                if (i <= 0 || i > argc)
92
                        ap = "(unknown)";
93 9e55d093 Eric Allman
                else if (argv[i - 1] == NULL)
94 47c6ea64 Eric Allman
                        ap = "(null)";
95
                else
96
                        ap = argv[i - 1];
97
98
                while ((c = *ap++) != '\0')
99 9e55d093 Eric Allman
                        putc(c, fp);
100 47c6ea64 Eric Allman
        }
101
}