Advertisement
dmilicev

safe_string_functions.c

Nov 27th, 2019
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.00 KB | None | 0 0
  1. /*
  2.  
  3.     safe_string_functions.c     by Dragan Milicev
  4.  
  5.  
  6. char *safe_strcpy( char **destination, const char *source )
  7.  
  8. char *safe_strcat( char **destination, const char *source )
  9.  
  10. char *safe_strncat( char **destination, const char *source, size_t n )
  11.  
  12.  
  13.     You can find all my C programs at Dragan Milicev's pastebin:
  14.  
  15.     https://pastebin.com/u/dmilicev
  16.  
  17.     https://www.facebook.com/dmilicev
  18.  
  19. */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>     // for malloc(), calloc(), realloc(), free()
  24. #include <string.h>     // for string functions
  25.  
  26.  
  27. // safe_strcpy() is a safe function because it takes care that
  28. // the destination string has enough memory space
  29. // to be copied to it the entire source string
  30. char *safe_strcpy( char **destination, const char *source )
  31. {
  32.     // Checking that destination has enough space for source + '\0'
  33.     if( strlen(*destination) < strlen(source) + 1 )
  34.         *destination = malloc( strlen(source) + 1 );
  35.  
  36.     // Checking whether memory reallocation succeeded
  37.     if( *destination == NULL )
  38.     {
  39.         fprintf(stderr, "\n\n realloc() for destination error! \n\n");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.  
  43.     strcpy( *destination, source );
  44.  
  45.     return *destination;
  46. } // safe_strcpy()
  47.  
  48.  
  49. // safe_strcat() is a safe function because it takes care that
  50. // the destination string has enough memory space
  51. // to be copied to it the entire source string and destination string
  52. char *safe_strcat( char **destination, const char *source )
  53. {
  54.     char *mem_destination = NULL;
  55.  
  56.     // reserve memory space for the mem_destination string
  57.     // to make room for source and destination string and '\0'
  58.     mem_destination = malloc( strlen(source) + strlen(*destination) + sizeof(char) );
  59.  
  60.     // Checking whether memory reallocation succeeded
  61.     if( mem_destination == NULL )
  62.     {
  63.         fprintf(stderr, "\n\n realloc() for mem_destination error! \n\n");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.  
  67.     strcpy( mem_destination, *destination );
  68.     strcat( mem_destination, source );
  69.  
  70.     *destination = mem_destination;
  71.  
  72.     return *destination;
  73. } // safe_strcat()
  74.  
  75.  
  76. // safe_strncat() is a safe function because it takes care that
  77. // the destination string has enough memory space
  78. // to be copied to it n characters of source string and '\0'
  79. char *safe_strncat( char **destination, const char *source, size_t n )
  80. {
  81.     char *mem_destination = NULL;
  82.  
  83.     // reserve memory space for the mem_destination string
  84.     // to make room for source and destination string and '\0'
  85.     mem_destination = malloc( strlen(*destination) + n * sizeof(char) + 1 );
  86.  
  87.     // Checking whether memory reallocation succeeded
  88.     if( mem_destination == NULL )
  89.     {
  90.         fprintf(stderr, "\n\n realloc() for mem_destination error! \n\n");
  91.         exit(EXIT_FAILURE);
  92.     }
  93.  
  94.     strcpy( mem_destination, *destination );
  95.     strncat( mem_destination, source, n );
  96.  
  97.     *destination = mem_destination;
  98.  
  99.     return *destination;
  100. } // safe_strncat()
  101.  
  102.  
  103. int main()
  104. {
  105.     char *source = NULL;
  106.     char *destination = NULL;
  107.     int source_size = 20;
  108.     int destination_size = 20;
  109.     int source_len;
  110.     int destination_len;
  111.  
  112.     printf("\n Safe C strings functions \n");
  113.  
  114.     printf("\n---------------------------------------------------------\n");
  115.  
  116.     printf("\n We reserve memory for source to %d \n", source_size );
  117.     source = (char *) malloc((size_t)source_size);  // Initial memory allocation
  118.     if( source == NULL )        // Checking whether memory allocation succeeded
  119.     {
  120.         fprintf(stderr, "\n\n malloc() for source error! \n\n");
  121.         exit(EXIT_FAILURE);
  122.     }
  123.  
  124.     printf("\n We reserve memory for destination to %d \n", destination_size );
  125.     destination = (char *) malloc((size_t)destination_size);// Initial memory allocation
  126.     if( destination == NULL )   // Checking whether memory allocation succeeded
  127.     {
  128.         fprintf(stderr, "\n\n malloc() for destination error! \n\n");
  129.         exit(EXIT_FAILURE);
  130.     }
  131.  
  132.     source_len = strlen(source);
  133.     destination_len = strlen(destination);
  134.     printf("\n main: source      (%2d): |%s| ", source_len, source );
  135.     printf("\n main: destination (%2d): |%s| \n", destination_len, destination );
  136.  
  137.  
  138.     printf("\n---------------------------------------------------------\n");
  139.     printf("\n safe_strcpy(source, \"1234567890\") \n");
  140.  
  141.     safe_strcpy( &source, "1234567890" );   // Fill source
  142.  
  143.     source_len = strlen(source);
  144.     destination_len = strlen(destination);
  145.     printf("\n main: source      (%2d): |%s| ", source_len, source );
  146.     printf("\n main: destination (%2d): |%s| \n", destination_len, destination );
  147.  
  148.     printf("\n safe_strcpy(destination, source) \n");
  149.     safe_strcpy( &destination, source );    // Fill destination
  150.  
  151.     source_len = strlen(source);
  152.     destination_len = strlen(destination);
  153.     printf("\n main: source      (%2d): |%s| ", source_len, source );
  154.     printf("\n main: destination (%2d): |%s| \n", destination_len, destination );
  155.  
  156.     printf("\n---------------------------------------------------------\n");
  157.     printf("\n safe_strcat() function \n");
  158.  
  159.     printf("\n safe_strcpy(source, \"ABCDEFG\") \n");
  160.     safe_strcpy( &source, "ABCDEFG" );  // Fill source
  161.  
  162.     printf("\n safe_strcat(destination, source) \n");
  163.     safe_strcat( &destination, source );    // Fill destination
  164.  
  165.     source_len = strlen(source);
  166.     destination_len = strlen(destination);
  167.     printf("\n main: source      (%2d): |%s| ", source_len, source );
  168.     printf("\n main: destination (%2d): |%s| \n", destination_len, destination );
  169.  
  170.     printf("\n---------------------------------------------------------\n");
  171.     printf("\n safe_strncat() function \n");
  172.  
  173.     safe_strncat( &destination, source, 4 );
  174.  
  175.     source_len = strlen(source);
  176.     destination_len = strlen(destination);
  177.     printf("\n main: source      (%2d): |%s| ", source_len, source );
  178.     printf("\n main: destination (%2d): |%s| \n", destination_len, destination );
  179.  
  180.     safe_strncat( &destination, "1234567890", 5 );
  181.  
  182.     source_len = strlen(source);
  183.     destination_len = strlen(destination);
  184.     printf("\n main: source      (%2d): |%s| ", source_len, source );
  185.     printf("\n main: destination (%2d): |%s| \n", destination_len, destination );
  186.  
  187.     printf("\n---------------------------------------------------------\n");
  188.  
  189.  
  190.     free(source);
  191.     free(destination);
  192.  
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement