Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / ep / ep_pprint.c @ master

History | View | Annotate | Download (2.56 KB)

1
/* vim: set ai sw=8 sts=8 ts=8 :*/
2

    
3
/***********************************************************************
4
**  ----- BEGIN LICENSE BLOCK -----
5
**        LIBEP: Enhanced Portability Library (Reduced Edition)
6
**
7
**        Copyright (c) 2008-2019, Eric P. Allman.  All rights reserved.
8
**        Copyright (c) 2015-2019, Regents of the University of California.
9
**        All rights reserved.
10
**
11
**        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
**
17
**        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
**
22
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
**        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
**  ----- END LICENSE BLOCK -----
29
***********************************************************************/
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
**                fp -- the stream to print to
45
**                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
ep_pprint(FILE *fp,
55
        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
                        putc(c, fp);
69
                        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
                        putc('%', fp);
77
                        break;
78
                }
79

    
80
                if (!isdigit(c))
81
                {
82
                        // bogus character, unless %%
83
                        putc('%', fp);
84
                        if (c != '%')
85
                                putc(c, fp);
86
                        continue;
87
                }
88

    
89
                // interpolate Nth argument
90
                i = c - '0';
91
                if (i <= 0 || i > argc)
92
                        ap = "(unknown)";
93
                else if (argv[i - 1] == NULL)
94
                        ap = "(null)";
95
                else
96
                        ap = argv[i - 1];
97

    
98
                while ((c = *ap++) != '\0')
99
                        putc(c, fp);
100
        }
101
}