xref: /onnv-gate/usr/src/cmd/sendmail/libsmutil/snprintf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3*0Sstevel@tonic-gate  *	All rights reserved.
4*0Sstevel@tonic-gate  * Copyright (c) 1997 Eric P. Allman.  All rights reserved.
5*0Sstevel@tonic-gate  * Copyright (c) 1988, 1993
6*0Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
9*0Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
10*0Sstevel@tonic-gate  * the sendmail distribution.
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #include <sendmail.h>
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate SM_RCSID("@(#)$Id: snprintf.c,v 8.41 2001/08/28 23:07:01 gshapiro Exp $")
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate /*
21*0Sstevel@tonic-gate **  SHORTENSTRING -- return short version of a string
22*0Sstevel@tonic-gate **
23*0Sstevel@tonic-gate **	If the string is already short, just return it.  If it is too
24*0Sstevel@tonic-gate **	long, return the head and tail of the string.
25*0Sstevel@tonic-gate **
26*0Sstevel@tonic-gate **	Parameters:
27*0Sstevel@tonic-gate **		s -- the string to shorten.
28*0Sstevel@tonic-gate **		m -- the max length of the string (strlen()).
29*0Sstevel@tonic-gate **
30*0Sstevel@tonic-gate **	Returns:
31*0Sstevel@tonic-gate **		Either s or a short version of s.
32*0Sstevel@tonic-gate */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate char *
35*0Sstevel@tonic-gate shortenstring(s, m)
36*0Sstevel@tonic-gate 	register const char *s;
37*0Sstevel@tonic-gate 	size_t m;
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate 	size_t l;
40*0Sstevel@tonic-gate 	static char buf[MAXSHORTSTR + 1];
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate 	l = strlen(s);
43*0Sstevel@tonic-gate 	if (l < m)
44*0Sstevel@tonic-gate 		return (char *) s;
45*0Sstevel@tonic-gate 	if (m > MAXSHORTSTR)
46*0Sstevel@tonic-gate 		m = MAXSHORTSTR;
47*0Sstevel@tonic-gate 	else if (m < 10)
48*0Sstevel@tonic-gate 	{
49*0Sstevel@tonic-gate 		if (m < 5)
50*0Sstevel@tonic-gate 		{
51*0Sstevel@tonic-gate 			(void) sm_strlcpy(buf, s, m + 1);
52*0Sstevel@tonic-gate 			return buf;
53*0Sstevel@tonic-gate 		}
54*0Sstevel@tonic-gate 		(void) sm_strlcpy(buf, s, m - 2);
55*0Sstevel@tonic-gate 		(void) sm_strlcat(buf, "...", sizeof buf);
56*0Sstevel@tonic-gate 		return buf;
57*0Sstevel@tonic-gate 	}
58*0Sstevel@tonic-gate 	m = (m - 3) / 2;
59*0Sstevel@tonic-gate 	(void) sm_strlcpy(buf, s, m + 1);
60*0Sstevel@tonic-gate 	(void) sm_strlcat2(buf, "...", s + l - m, sizeof buf);
61*0Sstevel@tonic-gate 	return buf;
62*0Sstevel@tonic-gate }
63