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