/** * Brandon Rodriguez * CS 3240 * 09-19-17 */ /** * Helper functions to have consistent value copying and error handling. * * All functions use memcpy to copy data, and error.c's err_dump on failure. * * All functions require a "source" pointer of equivalent typing, and returns * a pointer to copied item's location. */ #include <stdlib.h> #include <string.h> #include "apue.h" /** * Copies string from destination to source. */ char* copy_string(char *source_ptr) { char *copy_ptr; copy_ptr = calloc((strlen(source_ptr) + 1), sizeof(char)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, ((strlen(source_ptr) + 1) * sizeof(char))); } else { err_dump("Could not allocate memory for string calloc."); } return copy_ptr; } /** * Copies float from destination to source. */ int* copy_int(int *source_ptr) { int *copy_ptr; copy_ptr = calloc(1, (sizeof(int) + 1)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, sizeof(int)); } else { err_dump("Could not allocate memory for int calloc."); } return copy_ptr; } /** * Copies float from destination to source. */ float* copy_float(float *source_ptr) { float *copy_ptr; copy_ptr = calloc(1, (sizeof(float) + 1)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, sizeof(float)); } else { err_dump("Could not allocate memory for float calloc."); } return copy_ptr; } /** * Copies double from destination to source. */ double* copy_double(double *source_ptr) { double *copy_ptr; copy_ptr = calloc(1, (sizeof(double) + 1)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, sizeof(double)); } else { err_dump("Could not allocate memory for double calloc."); } return copy_ptr; }