summaryrefslogtreecommitdiffstats
path: root/src/util/line_number.c
blob: 557e0538971c788ba901f8630a9f4c4e3905c988 (plain)
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
/*++
/* NAME
/*	line_number 3
/* SUMMARY
/*	line number utilities
/* SYNOPSIS
/*	#include <line_number.h>
/*
/*	char	*format_line_number(result, first, last)
/*	VSTRING	*buffer;
/*	ssize_t	first;
/*	ssize_t	lastl
/* DESCRIPTION
/*	format_line_number() formats a line number or number range.
/*	The output is <first-number>-<last-number> when the numbers
/*	differ, <first-number> when the numbers are identical.
/* .IP result
/*	Result buffer, or null-pointer. In the latter case the
/*	result is stored in a static buffer that is overwritten
/*	with subsequent calls. The function result value is a
/*	pointer into the result buffer.
/* .IP first
/*	First line number.
/* .IP last
/*	Last line number.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

 /*
  * System library.
  */
#include <sys_defs.h>

 /*
  * Utility library.
  */
#include <vstring.h>
#include <line_number.h>

/* format_line_number - pretty-print line number or number range */

char   *format_line_number(VSTRING *result, ssize_t first, ssize_t last)
{
    static VSTRING *buf;

    /*
     * Your buffer or mine?
     */
    if (result == 0) {
	if (buf == 0)
	    buf = vstring_alloc(10);
	result = buf;
    }

    /*
     * Print a range only when the numbers differ.
     */
    vstring_sprintf(result, "%ld", (long) first);
    if (first != last)
	vstring_sprintf_append(result, "-%ld", (long) last);

    return (vstring_str(result));
}