xref: /titanic_50/usr/src/cmd/sendmail/libsmutil/snprintf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include <sendmail.h>
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: snprintf.c,v 8.41 2001/08/28 23:07:01 gshapiro Exp $")
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate /*
21*7c478bd9Sstevel@tonic-gate **  SHORTENSTRING -- return short version of a string
22*7c478bd9Sstevel@tonic-gate **
23*7c478bd9Sstevel@tonic-gate **	If the string is already short, just return it.  If it is too
24*7c478bd9Sstevel@tonic-gate **	long, return the head and tail of the string.
25*7c478bd9Sstevel@tonic-gate **
26*7c478bd9Sstevel@tonic-gate **	Parameters:
27*7c478bd9Sstevel@tonic-gate **		s -- the string to shorten.
28*7c478bd9Sstevel@tonic-gate **		m -- the max length of the string (strlen()).
29*7c478bd9Sstevel@tonic-gate **
30*7c478bd9Sstevel@tonic-gate **	Returns:
31*7c478bd9Sstevel@tonic-gate **		Either s or a short version of s.
32*7c478bd9Sstevel@tonic-gate */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate char *
35*7c478bd9Sstevel@tonic-gate shortenstring(s, m)
36*7c478bd9Sstevel@tonic-gate 	register const char *s;
37*7c478bd9Sstevel@tonic-gate 	size_t m;
38*7c478bd9Sstevel@tonic-gate {
39*7c478bd9Sstevel@tonic-gate 	size_t l;
40*7c478bd9Sstevel@tonic-gate 	static char buf[MAXSHORTSTR + 1];
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 	l = strlen(s);
43*7c478bd9Sstevel@tonic-gate 	if (l < m)
44*7c478bd9Sstevel@tonic-gate 		return (char *) s;
45*7c478bd9Sstevel@tonic-gate 	if (m > MAXSHORTSTR)
46*7c478bd9Sstevel@tonic-gate 		m = MAXSHORTSTR;
47*7c478bd9Sstevel@tonic-gate 	else if (m < 10)
48*7c478bd9Sstevel@tonic-gate 	{
49*7c478bd9Sstevel@tonic-gate 		if (m < 5)
50*7c478bd9Sstevel@tonic-gate 		{
51*7c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(buf, s, m + 1);
52*7c478bd9Sstevel@tonic-gate 			return buf;
53*7c478bd9Sstevel@tonic-gate 		}
54*7c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(buf, s, m - 2);
55*7c478bd9Sstevel@tonic-gate 		(void) sm_strlcat(buf, "...", sizeof buf);
56*7c478bd9Sstevel@tonic-gate 		return buf;
57*7c478bd9Sstevel@tonic-gate 	}
58*7c478bd9Sstevel@tonic-gate 	m = (m - 3) / 2;
59*7c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(buf, s, m + 1);
60*7c478bd9Sstevel@tonic-gate 	(void) sm_strlcat2(buf, "...", s + l - m, sizeof buf);
61*7c478bd9Sstevel@tonic-gate 	return buf;
62*7c478bd9Sstevel@tonic-gate }
63