1 /* $NetBSD: line_number.c,v 1.2 2017/02/14 01:16:49 christos Exp $ */
2
3 /*++
4 /* NAME
5 /* line_number 3
6 /* SUMMARY
7 /* line number utilities
8 /* SYNOPSIS
9 /* #include <line_number.h>
10 /*
11 /* char *format_line_number(result, first, last)
12 /* VSTRING *buffer;
13 /* ssize_t first;
14 /* ssize_t lastl
15 /* DESCRIPTION
16 /* format_line_number() formats a line number or number range.
17 /* The output is <first-number>-<last-number> when the numbers
18 /* differ, <first-number> when the numbers are identical.
19 /* .IP result
20 /* Result buffer, or null-pointer. In the latter case the
21 /* result is stored in a static buffer that is overwritten
22 /* with subsequent calls. The function result value is a
23 /* pointer into the result buffer.
24 /* .IP first
25 /* First line number.
26 /* .IP last
27 /* Last line number.
28 /* LICENSE
29 /* .ad
30 /* .fi
31 /* The Secure Mailer license must be distributed with this software.
32 /* AUTHOR(S)
33 /* Wietse Venema
34 /* IBM T.J. Watson Research
35 /* P.O. Box 704
36 /* Yorktown Heights, NY 10598, USA
37 /*--*/
38
39 /*
40 * System library.
41 */
42 #include <sys_defs.h>
43
44 /*
45 * Utility library.
46 */
47 #include <vstring.h>
48 #include <line_number.h>
49
50 /* format_line_number - pretty-print line number or number range */
51
format_line_number(VSTRING * result,ssize_t first,ssize_t last)52 char *format_line_number(VSTRING *result, ssize_t first, ssize_t last)
53 {
54 static VSTRING *buf;
55
56 /*
57 * Your buffer or mine?
58 */
59 if (result == 0) {
60 if (buf == 0)
61 buf = vstring_alloc(10);
62 result = buf;
63 }
64
65 /*
66 * Print a range only when the numbers differ.
67 */
68 vstring_sprintf(result, "%ld", (long) first);
69 if (first != last)
70 vstring_sprintf_append(result, "-%ld", (long) last);
71
72 return (vstring_str(result));
73 }
74