home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRCHCAT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  78 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  STRCHCAT.C - Append a character to a string.
  5. **
  6. **  Arguments: 1 - Pointer to string to append to
  7. **             2 - Character to append
  8. **             3 - Maximum size of string buffer
  9. **
  10. **  Returns: Pointer to modified string, or NULL if insufficient space
  11. **
  12. **  Original Copyright 1990-95 by Robert B. Stout as part of
  13. **  the MicroFirm Function Library (MFL)
  14. **
  15. **  The user is granted a free limited license to use this source file
  16. **  to create royalty-free programs, subject to the terms of the
  17. **  license restrictions specified in the LICENSE.MFL file.
  18. **
  19. **  NOTE: The name of this funtion violates ANSI/ISO 9899:1990 sec. 7.1.3,
  20. **        but this violation seems preferable to either violating sec. 7.13.8
  21. **        or coming up with some hideous mixed-case or underscore infested
  22. **        naming. Also, many SNIPPETS str---() functions duplicate existing
  23. **        functions which are supported by various vendors, so the naming
  24. **        violation may be required for portability.
  25. */
  26.  
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "snip_str.h"
  30.  
  31. #if defined(__cplusplus) && __cplusplus
  32.  extern "C" {
  33. #endif
  34.  
  35. char *strchcat(char *string, int ch, size_t buflen)
  36. {
  37.       size_t len;
  38.  
  39.       if (NULL == string || ((len = strlen(string)) + 1) >= buflen)
  40.             return NULL;
  41.  
  42.       string[len++] = ch;
  43.       string[len]   = '\0';
  44.       return string;
  45. }
  46.  
  47. #if defined(__cplusplus) && __cplusplus
  48.  }
  49. #endif
  50.  
  51. #ifdef TEST
  52.  
  53. #include <stdio.h>
  54.  
  55. main()
  56. {
  57.       char buf1[80] = "This buffer's big enough",
  58.            buf2[] = "This one's not";
  59.       char *ptr;
  60.  
  61.       printf("strchcat(\"%s\", '!') ", buf1);
  62.       ptr = strchcat(buf1, '!', sizeof(buf1));
  63.       printf("returned %p...", ptr);
  64.       if (NULL != ptr)
  65.             printf("\n...which is \"%s\"", buf1);
  66.       puts("\n");
  67.  
  68.       printf("strchcat(\"%s\", '!') ", buf2);
  69.       ptr = strchcat(buf2, '!', sizeof(buf2));
  70.       printf("returned %p...", ptr);
  71.       if (NULL != ptr)
  72.             printf("\n...which is \"%s\"", buf2);
  73.       puts("\n");
  74.       return EXIT_SUCCESS;
  75. }
  76.  
  77. #endif /* TEST */
  78.