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