snprintf.c revision 90792
1146515Sru/*
2146515Sru * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
356160Sru *	All rights reserved.
456160Sru * Copyright (c) 1997 Eric P. Allman.  All rights reserved.
556160Sru * Copyright (c) 1988, 1993
656160Sru *	The Regents of the University of California.  All rights reserved.
793139Sru *
856160Sru * By using this file, you agree to the terms and conditions set
993139Sru * forth in the LICENSE file which can be found at the top level of
1093139Sru * the sendmail distribution.
1193139Sru *
1256160Sru */
1393139Sru
1493139Sru#include <sendmail.h>
1556160Sru
1693139SruSM_RCSID("@(#)$Id: snprintf.c,v 8.44 2001/09/11 04:04:56 gshapiro Exp $")
1793139Sru
1856160Sru/*
1993139Sru**  SHORTENSTRING -- return short version of a string
2093139Sru**
2193139Sru**	If the string is already short, just return it.  If it is too
2293139Sru**	long, return the head and tail of the string.
23146515Sru**
2493139Sru**	Parameters:
2593139Sru**		s -- the string to shorten.
2693139Sru**		m -- the max length of the string (strlen()).
2793139Sru**
2893139Sru**	Returns:
2993139Sru**		Either s or a short version of s.
3093139Sru*/
3193139Sru
3293139Sruchar *
3393139Srushortenstring(s, m)
3493139Sru	register const char *s;
3593139Sru	size_t m;
3693139Sru{
3793139Sru	size_t l;
3893139Sru	static char buf[MAXSHORTSTR + 1];
39146515Sru
4093139Sru	l = strlen(s);
4193139Sru	if (l < m)
4293139Sru		return (char *) s;
4393139Sru	if (m > MAXSHORTSTR)
4493139Sru		m = MAXSHORTSTR;
45114472Sru	else if (m < 10)
46146515Sru	{
47146515Sru		if (m < 5)
48146515Sru		{
4993139Sru			(void) sm_strlcpy(buf, s, m + 1);
5093139Sru			return buf;
5156160Sru		}
52146515Sru		(void) sm_strlcpy(buf, s, m - 2);
5393139Sru		(void) sm_strlcat(buf, "...", sizeof buf);
5456160Sru		return buf;
55100513Sru	}
56100513Sru	m = (m - 3) / 2;
57100513Sru	(void) sm_strlcpy(buf, s, m + 1);
58100513Sru	(void) sm_strlcat2(buf, "...", s + l - m, sizeof buf);
59100513Sru	return buf;
60100513Sru}
6193139Sru