snprintf.c revision 261363
198944Sobrien/*
298944Sobrien * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
398944Sobrien *	All rights reserved.
498944Sobrien * Copyright (c) 1997 Eric P. Allman.  All rights reserved.
598944Sobrien * Copyright (c) 1988, 1993
698944Sobrien *	The Regents of the University of California.  All rights reserved.
798944Sobrien *
898944Sobrien * By using this file, you agree to the terms and conditions set
998944Sobrien * forth in the LICENSE file which can be found at the top level of
1098944Sobrien * the sendmail distribution.
1198944Sobrien *
1298944Sobrien */
1398944Sobrien
1498944Sobrien#include <sendmail.h>
1598944Sobrien
1698944SobrienSM_RCSID("@(#)$Id: snprintf.c,v 8.45 2013/11/22 20:51:50 ca Exp $")
1798944Sobrien
1898944Sobrien/*
1998944Sobrien**  SHORTENSTRING -- return short version of a string
2098944Sobrien**
2198944Sobrien**	If the string is already short, just return it.  If it is too
2298944Sobrien**	long, return the head and tail of the string.
2398944Sobrien**
2498944Sobrien**	Parameters:
2598944Sobrien**		s -- the string to shorten.
2698944Sobrien**		m -- the max length of the string (strlen()).
2798944Sobrien**
2898944Sobrien**	Returns:
2998944Sobrien**		Either s or a short version of s.
3098944Sobrien*/
3198944Sobrien
3298944Sobrienchar *
3398944Sobrienshortenstring(s, m)
3498944Sobrien	register const char *s;
3598944Sobrien	size_t m;
3698944Sobrien{
3798944Sobrien	size_t l;
3898944Sobrien	static char buf[MAXSHORTSTR + 1];
3998944Sobrien
4098944Sobrien	l = strlen(s);
4198944Sobrien	if (l < m)
4298944Sobrien		return (char *) s;
4398944Sobrien	if (m > MAXSHORTSTR)
4498944Sobrien		m = MAXSHORTSTR;
4598944Sobrien	else if (m < 10)
4698944Sobrien	{
4798944Sobrien		if (m < 5)
4898944Sobrien		{
4998944Sobrien			(void) sm_strlcpy(buf, s, m + 1);
5098944Sobrien			return buf;
5198944Sobrien		}
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