Import sendmail 8.13.7
[dragonfly.git] / contrib / sendmail-8.13.7 / libsmutil / snprintf.c
blobff3aa3b90c7c8ffc6dcbed7c4a2ef0be563ec3c1
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.
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.
14 #include <sendmail.h>
16 SM_RCSID("@(#)$Id: snprintf.c,v 8.44 2001/09/11 04:04:56 gshapiro Exp $")
19 ** SHORTENSTRING -- return short version of a string
21 ** If the string is already short, just return it. If it is too
22 ** long, return the head and tail of the string.
24 ** Parameters:
25 ** s -- the string to shorten.
26 ** m -- the max length of the string (strlen()).
28 ** Returns:
29 ** Either s or a short version of s.
32 char *
33 shortenstring(s, m)
34 register const char *s;
35 size_t m;
37 size_t l;
38 static char buf[MAXSHORTSTR + 1];
40 l = strlen(s);
41 if (l < m)
42 return (char *) s;
43 if (m > MAXSHORTSTR)
44 m = MAXSHORTSTR;
45 else if (m < 10)
47 if (m < 5)
49 (void) sm_strlcpy(buf, s, m + 1);
50 return buf;
52 (void) sm_strlcpy(buf, s, m - 2);
53 (void) sm_strlcat(buf, "...", sizeof buf);
54 return buf;
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;