Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <error.h>
- #include <errno.h>
- #include "utils.h"
- /**
- * Get memory space and check, wheter I have it
- */
- void *
- xmalloc(size_t size){
- void * pointer;
- pointer = malloc(size);
- errno = 0;
- if(pointer == NULL){
- error(0, 0, "Could not allocate enough space!");
- }
- if(errno){
- error(0, errno, strerror(errno));
- }
- return pointer;
- }
- /**
- * reallocate space and check it
- */
- void *
- xrealloc(void * pointer, size_t size){
- pointer = realloc(pointer, size);
- errno = 0;
- if(pointer == NULL){
- error(0, 0, "Could not reallocate space!");
- }
- if(errno){
- error(0, errno, strerror(errno));
- }
- return pointer;
- }
- /**
- * safe version of strcat
- */
- void
- cat(char **to, char * from){
- char * buffer;
- buffer = (char *) xmalloc (sizeof(char) * (strlen(*to) + 1));
- cpy(&buffer, *to);
- printf("buffer: %s\n", buffer); //DEBUG
- int length = strlen(*to) + strlen(from) + 1;
- *to = (char *) xrealloc(*to, length * sizeof(char));
- cpy(to, buffer);
- printf("to: %s\n", *to); //DEBUG
- printf("from: %s\n", from); //DEBUG
- errno = 0;
- if(strcat(*to, from) != *to){
- error(0, 0, "Could not concate the string %s\n", from);
- }
- if(errno){
- error(0, errno, strerror(errno));
- }
- printf("to: %s\n", *to); //DEBUG
- }
- /**
- * safe version strcpy
- */
- void
- cpy(char **to, char * from){
- int length = strlen(from) + 1;
- *to = (char *) xrealloc(*to, length * sizeof(char));
- errno = 0;
- if(strcpy(*to, from) != *to){
- error(0, errno, "Could not cpy the string %s\n", from);
- }
- if(errno){
- error(0, errno, strerror(errno));
- }
- }
- /**
- * Add "/" to pathname
- */
- void
- addslash(char * path){
- //if(strcat(path, "/") != path){
- // fprintf(stderr, "ERROR: Could not add \"/\" to path %s!\n", path);
- // exit(1);
- //}
- cat(&path, "/");
- }
Advertisement
Add Comment
Please, Sign In to add comment