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