xref: /freebsd/contrib/sendmail/libsmutil/snprintf.c (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
106f25ae9SGregory Neil Shapiro /*
25dd76dd0SGregory Neil Shapiro  * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro  *	All rights reserved.
406f25ae9SGregory Neil Shapiro  * Copyright (c) 1997 Eric P. Allman.  All rights reserved.
506f25ae9SGregory Neil Shapiro  * Copyright (c) 1988, 1993
606f25ae9SGregory Neil Shapiro  *	The Regents of the University of California.  All rights reserved.
706f25ae9SGregory Neil Shapiro  *
806f25ae9SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
906f25ae9SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
1006f25ae9SGregory Neil Shapiro  * the sendmail distribution.
1106f25ae9SGregory Neil Shapiro  *
1206f25ae9SGregory Neil Shapiro  */
1306f25ae9SGregory Neil Shapiro 
1406f25ae9SGregory Neil Shapiro #include <sendmail.h>
1506f25ae9SGregory Neil Shapiro 
16*4313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: snprintf.c,v 8.45 2013-11-22 20:51:50 ca Exp $")
1706f25ae9SGregory Neil Shapiro 
1806f25ae9SGregory Neil Shapiro /*
1906f25ae9SGregory Neil Shapiro **  SHORTENSTRING -- return short version of a string
2006f25ae9SGregory Neil Shapiro **
2106f25ae9SGregory Neil Shapiro **	If the string is already short, just return it.  If it is too
2206f25ae9SGregory Neil Shapiro **	long, return the head and tail of the string.
2306f25ae9SGregory Neil Shapiro **
2406f25ae9SGregory Neil Shapiro **	Parameters:
2506f25ae9SGregory Neil Shapiro **		s -- the string to shorten.
2606f25ae9SGregory Neil Shapiro **		m -- the max length of the string (strlen()).
2706f25ae9SGregory Neil Shapiro **
2806f25ae9SGregory Neil Shapiro **	Returns:
2906f25ae9SGregory Neil Shapiro **		Either s or a short version of s.
3006f25ae9SGregory Neil Shapiro */
3106f25ae9SGregory Neil Shapiro 
3206f25ae9SGregory Neil Shapiro char *
3306f25ae9SGregory Neil Shapiro shortenstring(s, m)
3406f25ae9SGregory Neil Shapiro 	register const char *s;
3540266059SGregory Neil Shapiro 	size_t m;
3606f25ae9SGregory Neil Shapiro {
3740266059SGregory Neil Shapiro 	size_t l;
3806f25ae9SGregory Neil Shapiro 	static char buf[MAXSHORTSTR + 1];
3906f25ae9SGregory Neil Shapiro 
4006f25ae9SGregory Neil Shapiro 	l = strlen(s);
4106f25ae9SGregory Neil Shapiro 	if (l < m)
4206f25ae9SGregory Neil Shapiro 		return (char *) s;
4306f25ae9SGregory Neil Shapiro 	if (m > MAXSHORTSTR)
4406f25ae9SGregory Neil Shapiro 		m = MAXSHORTSTR;
4506f25ae9SGregory Neil Shapiro 	else if (m < 10)
4606f25ae9SGregory Neil Shapiro 	{
4706f25ae9SGregory Neil Shapiro 		if (m < 5)
4806f25ae9SGregory Neil Shapiro 		{
4940266059SGregory Neil Shapiro 			(void) sm_strlcpy(buf, s, m + 1);
5006f25ae9SGregory Neil Shapiro 			return buf;
5106f25ae9SGregory Neil Shapiro 		}
5240266059SGregory Neil Shapiro 		(void) sm_strlcpy(buf, s, m - 2);
5340266059SGregory Neil Shapiro 		(void) sm_strlcat(buf, "...", sizeof buf);
5406f25ae9SGregory Neil Shapiro 		return buf;
5506f25ae9SGregory Neil Shapiro 	}
5606f25ae9SGregory Neil Shapiro 	m = (m - 3) / 2;
5740266059SGregory Neil Shapiro 	(void) sm_strlcpy(buf, s, m + 1);
5840266059SGregory Neil Shapiro 	(void) sm_strlcat2(buf, "...", s + l - m, sizeof buf);
5906f25ae9SGregory Neil Shapiro 	return buf;
6006f25ae9SGregory Neil Shapiro }
61