164562Sgshapiro/*
2261363Sgshapiro * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
364562Sgshapiro *	All rights reserved.
464562Sgshapiro * Copyright (c) 1997 Eric P. Allman.  All rights reserved.
564562Sgshapiro * Copyright (c) 1988, 1993
664562Sgshapiro *	The Regents of the University of California.  All rights reserved.
764562Sgshapiro *
864562Sgshapiro * By using this file, you agree to the terms and conditions set
964562Sgshapiro * forth in the LICENSE file which can be found at the top level of
1064562Sgshapiro * the sendmail distribution.
1164562Sgshapiro *
1264562Sgshapiro */
1364562Sgshapiro
1464562Sgshapiro#include <sendmail.h>
1564562Sgshapiro
16266692SgshapiroSM_RCSID("@(#)$Id: snprintf.c,v 8.45 2013-11-22 20:51:50 ca Exp $")
1764562Sgshapiro
1864562Sgshapiro/*
1964562Sgshapiro**  SHORTENSTRING -- return short version of a string
2064562Sgshapiro**
2164562Sgshapiro**	If the string is already short, just return it.  If it is too
2264562Sgshapiro**	long, return the head and tail of the string.
2364562Sgshapiro**
2464562Sgshapiro**	Parameters:
2564562Sgshapiro**		s -- the string to shorten.
2664562Sgshapiro**		m -- the max length of the string (strlen()).
2764562Sgshapiro**
2864562Sgshapiro**	Returns:
2964562Sgshapiro**		Either s or a short version of s.
3064562Sgshapiro*/
3164562Sgshapiro
3264562Sgshapirochar *
3364562Sgshapiroshortenstring(s, m)
3464562Sgshapiro	register const char *s;
3590792Sgshapiro	size_t m;
3664562Sgshapiro{
3790792Sgshapiro	size_t l;
3864562Sgshapiro	static char buf[MAXSHORTSTR + 1];
3964562Sgshapiro
4064562Sgshapiro	l = strlen(s);
4164562Sgshapiro	if (l < m)
4264562Sgshapiro		return (char *) s;
4364562Sgshapiro	if (m > MAXSHORTSTR)
4464562Sgshapiro		m = MAXSHORTSTR;
4564562Sgshapiro	else if (m < 10)
4664562Sgshapiro	{
4764562Sgshapiro		if (m < 5)
4864562Sgshapiro		{
4990792Sgshapiro			(void) sm_strlcpy(buf, s, m + 1);
5064562Sgshapiro			return buf;
5164562Sgshapiro		}
5290792Sgshapiro		(void) sm_strlcpy(buf, s, m - 2);
5390792Sgshapiro		(void) sm_strlcat(buf, "...", sizeof buf);
5464562Sgshapiro		return buf;
5564562Sgshapiro	}
5664562Sgshapiro	m = (m - 3) / 2;
5790792Sgshapiro	(void) sm_strlcpy(buf, s, m + 1);
5890792Sgshapiro	(void) sm_strlcat2(buf, "...", s + l - m, sizeof buf);
5964562Sgshapiro	return buf;
6064562Sgshapiro}
61