home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / strfcpy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  1.7 KB  |  70 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: strfcpy.c,v 5.1 1993/01/19 04:46:21 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1993 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: strfcpy.c,v $
  16.  * Revision 5.1  1993/01/19  04:46:21  syd
  17.  * Initial Checkin
  18.  *
  19.  *
  20.  ******************************************************************************/
  21.  
  22. /*
  23.  * This is just like strncpy() except:
  24.  *
  25.  * - The result is guaranteed to be '\0' terminated.
  26.  *
  27.  * - strncpy is supposed to copy _exactly_ "len" characters.  We copy
  28.  *   _at_most_ "len" characters.  (Actually "len-1" to save space for
  29.  *   the trailing '\0'.  That is, strncpy() fills in the end with '\0'
  30.  *   if strlen(src)<len.  We don't bother.
  31.  */
  32. char *strfcpy(dest, src, len)
  33. register char *dest, *src;
  34. register int len;
  35. {
  36.     char *dest0 = dest;
  37.     while (--len > 0 && *src != '\0')
  38.         *dest++ = *src++;
  39.     *dest = '\0';
  40.     return dest0;
  41. }
  42.  
  43. #ifdef _TEST
  44. #include <stdio.h>
  45. main()
  46. {
  47.     char src[1024], dest[1024];
  48.     int len;
  49.  
  50.     for (;;) {
  51.         printf("string > ");
  52.         fflush(stdout);
  53.         if (gets(src) == NULL)
  54.             break;
  55.         printf("maxlen > ");
  56.         fflush(stdout);
  57.         if (gets(dest) == NULL)
  58.             break;
  59.         len = atoi(dest);
  60.         (void) strfcpy(dest, src, len);
  61.         printf("dest=\"%s\" maxlen=%d len=%d\n",
  62.             dest, len, strlen(dest));
  63.         putchar('\n');
  64.     }
  65.     putchar('\n');
  66.     exit(0);
  67. }
  68. #endif
  69.  
  70.