1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   int STcopyMax (const char Si[], char So[], int Maxchar)
6 
7 Purpose:
8   Copy at most Maxchar characters to a string
9 
10 Description:
11   This routine copies characters from the input string to the output string.
12   Characters are copied until a null is seen in the input string or the number
13   of characters copied is Maxchar.  Then a trailing null character is appended
14   to the output string.  If the input string is longer than Maxchar (not
15   including the null character), a string truncated warning message is printed.
16 
17 Parameters:
18  <-   int STcopyMax
19       Number of characters in the output string
20    -> const char Si[]
21       Input character string
22   <-  char So[]
23       Output character string.  This string is always null terminated, with
24       at most Maxchar characters not including the terminating null character.
25       If the input string is longer than Maxchar, only the first Maxchar
26       characters are copied and a warning message is printed.
27    -> int Maxchar
28       Maximum number of characters (not including the trailing null character)
29       to be placed in So.
30 
31 Author / revision:
32   P. Kabal  Copyright (C) 2003
33   $Revision: 1.17 $  $Date: 2003/05/09 03:02:44 $
34 
35 -------------------------------------------------------------------------*/
36 
37 #include <libtsp.h>
38 #include <libtsp/STmsg.h>
39 
40 #define MINV(a, b)	(((a) < (b)) ? (a) : (b))
41 
42 
43 int
STcopyMax(const char Si[],char So[],int Maxchar)44 STcopyMax (const char Si[], char So[], int Maxchar)
45 
46 {
47   const char *si;
48   int n;
49 
50   /* Save the initial input pointer */
51   si = Si;
52 
53   /* Copy at most Maxchar characters */
54   for (n = 0; n < Maxchar && *si != '\0'; n++)
55     *So++ = *si++;
56 
57   /* Add a trailing null */
58   *So = '\0';
59 
60   /* Check for truncation */
61   if (*si != '\0')
62     UTwarn ("STcopyMax - %s: \"%.*s...\"", STM_StrTrunc, MINV (30, n), Si);
63 
64   /* Return the number of characters in So */
65   return n;
66 }
67