1 /* $NetBSD: line_number.c,v 1.1.1.1 2013/09/25 19:06:37 tron 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 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, first == last ? "%ld" : "%ld-%ld", 69 (long) first, (long) last); 70 71 return (vstring_str(result)); 72 } 73