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