Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <malloc.h> // malloc_usable_size
- #include "wrappers.h"
- struct my_ptr* generate_str(){
- char format[] = "%qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_-0987654321^#";
- char* r;
- struct my_ptr* ptr = malloc(sizeof(struct my_ptr));
- size_t i;
- size_t max = rand() % 300;
- r = calloc(max+1,sizeof(*r));
- if(r==NULL)
- goto exit;
- for(i=0;i<max;i++)
- r[i] = format[rand() % sizeof(format)];
- r[max] = '\0';
- exit:
- ptr->ptr = r;
- ptr->len = max;
- ptr->writeable =1;
- return ptr;
- }
- struct my_ptr* asctime_wrapper()
- {
- struct my_ptr* ptr = malloc(sizeof(struct my_ptr));
- char* temp;
- time_t czas;
- time( & czas );
- temp = asctime( localtime( & czas ) ) ;
- ptr->ptr = calloc(strlen(temp)+1,1);
- memcpy(ptr->ptr,temp,strlen(temp));
- ptr->len = strlen(temp);
- ptr->writeable = 1;
- return ptr;
- }
- void memcpy_wrapper (struct my_ptr* ptr,int num)
- {
- if(ptr->ptr!=NULL && num>0 && ptr->len>num)
- {
- void* destination = malloc(ptr->len+1);
- memcpy(destination,ptr->ptr,num);
- free(destination);
- }
- }
- struct my_ptr * memmove_wrapper(struct my_ptr* ptr, int num)
- {
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- if(ptr && ptr->ptr!=NULL && num>0 && ptr->len>num)
- {
- void* destination = calloc(ptr->len+1,1);
- memcpy(destination,ptr->ptr,num);
- ret->ptr = destination;
- ret->len = num;
- ret->writeable = 1;
- memmove(ret->ptr,ptr->ptr,num);
- return ret;
- }
- return NULL;
- }
- void strcpy_wrapper (struct my_ptr* ptr )
- {
- if(ptr->ptr!=NULL && ptr->len > 0){
- char* destination = malloc(ptr->len+1);
- ((char*)ptr->ptr)[ptr->len] = '\0';
- strcpy(destination,ptr->ptr);
- free(destination);
- }
- }
- void strncpy_wrapper(const char * source, size_t num )
- {
- if(source!=NULL && num>0){
- char* destination = malloc(num);
- if(!destination)
- return;
- strncpy(destination,source,num);
- free(destination);
- }
- }
- struct my_ptr * strcat_wrapper ( struct my_ptr * dst, struct my_ptr * src )
- {
- if(dst->ptr != src->ptr && src->ptr!=NULL && dst->ptr !=NULL ){
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->ptr = malloc(dst->len+src->len+2);
- ret->len = dst->len+src->len;
- memcpy(ret->ptr,dst->ptr,dst->len);
- strcat(ret->ptr,src->ptr);
- return ret;
- }
- return NULL;
- }
- struct my_ptr *strncat_wrapper ( struct my_ptr * dst, struct my_ptr * src, size_t num )
- {
- //num is maximium, so is not a requirement
- if(dst->ptr != src->ptr && src->ptr!=NULL && dst->ptr !=NULL){
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->ptr = malloc(dst->len+num+2);
- ret->len = dst->len+num;
- memcpy(ret->ptr,dst->ptr,dst->len);
- strncat(ret->ptr,src->ptr,num);
- return ret;
- }
- return NULL;
- }
- int memcmp_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2, size_t num )
- {
- if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
- return memcmp(ptr1->ptr,ptr2->ptr,num);
- return 0;
- }
- int strcmp_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2 )
- {
- if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
- return strcmp(ptr1->ptr,ptr2->ptr);
- return 0;
- }
- int strcoll_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2 )
- {
- if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
- return strcoll(ptr1->ptr,ptr2->ptr);
- return 0;
- }
- int strncmp_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2, size_t num )
- {
- if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
- return strncmp(ptr1->ptr,ptr2->ptr,num);
- return 0;
- }
- void strxfrm_wrapper (struct my_ptr * src)
- {
- if(src->ptr !=NULL)
- {
- strxfrm(NULL,src->ptr,0);
- }
- }
- void memchr_wrapper ( struct my_ptr * src, int val, int num)
- {
- if(src->ptr != NULL)
- {
- int len = num;
- if(num>src->len)
- len = src->len;
- memchr(src->ptr,val,len);
- }
- }
- void strchr_wrapper ( struct my_ptr * src, int character)
- {
- if(src->ptr!=NULL)
- strchr(src->ptr,character);
- }
- void strcspn_wrapper ( struct my_ptr * str1, struct my_ptr * str2 )
- {
- if(str1->ptr!=NULL && str2->ptr!=NULL)
- strcspn(str1->ptr,str2->ptr);
- }
- void strpbrk_wrapper ( struct my_ptr * str1, struct my_ptr * str2)
- {
- if(str1->ptr!=NULL && str2->ptr!=NULL)
- strpbrk(str1->ptr,str2->ptr);
- }
- void strrchr_wrapper ( struct my_ptr * str, int character)
- {
- if(str->ptr!=NULL)
- strrchr(str->ptr,character);
- }
- void strspn_wrapper ( struct my_ptr * str1, struct my_ptr * str2 )
- {
- if(str1->ptr!=NULL && str2->ptr!=NULL)
- strspn(str1->ptr,str2->ptr);
- }
- void strstr_wrapper ( struct my_ptr * str1, struct my_ptr * str2)
- {
- if(str1->ptr!=NULL && str2->ptr!=NULL)
- strstr(str1->ptr,str2->ptr);
- }
- void strtok_wrapper ( struct my_ptr * str, struct my_ptr * delimiters )
- {
- if(str->ptr!=NULL && delimiters->ptr!=NULL)
- {
- char* new_str = malloc(str->len);
- memcpy(new_str,str->ptr,str->len);
- strtok(new_str,delimiters->ptr);
- free(new_str);
- }
- }
- void strlen_wrapper ( struct my_ptr * str )
- {
- if(str->ptr!=NULL)
- strlen(str);
- }
- struct my_ptr* memset_wrapper (int value, size_t num )
- {
- if(num >1){
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->ptr = malloc(num+1);
- memset(ret->ptr,value,num);
- ((char*)ret->ptr)[num] = '\0';
- ret->len = num;
- return ret;
- }
- return NULL;
- }
- struct my_ptr* strerror_wrapper(int errnum)
- {
- char* src = strerror(errnum);
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->len = strlen(src);
- ret->ptr = calloc(ret->len+1,1);
- memcpy(ret->ptr,src,ret->len);
- return ret;
- }
- struct my_ptr* ctime_wrapper(time_t * a)
- {
- if(a)
- {
- char* src = ctime(a);
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->len = strlen(src);
- ret->ptr = calloc(ret->len+1,1);
- memcpy(ret->ptr,src,ret->len);
- return ret;
- }
- return NULL;
- }
- struct my_ptr* ctime_r_wrapper(time_t * a)
- {
- if(a)
- {
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->len = 64;
- ret->ptr = calloc(64,1);
- ctime_r(a,ret->ptr);
- }
- return NULL;
- }
- struct tm *localtime_r_wrapper(const time_t * timep)
- {
- if(timep!=NULL )
- {
- struct tm* ret = malloc(sizeof(struct tm));
- localtime_r(timep,ret);
- }
- return NULL;
- }
- struct my_ptr* strptime_wrapper(const char * format,struct tm * tm)
- {
- if(format && tm)
- {
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->len = 64;
- ret->ptr = calloc(64,1);
- strptime(ret->ptr,format,tm);
- return ret;
- }
- return NULL;
- }
- time_t mktime_wrapper(struct tm * tm)
- {
- time_t ret_zero; // TODO: How to declare an empty time_t ?
- if(tm)
- return mktime(tm);
- else
- return ret_zero;
- }
- struct my_ptr* asctime_r_wrapper(const struct tm * tm)
- {
- if(tm)
- {
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->len = 64;
- ret->ptr = calloc(64,1);
- asctime_r(tm,ret->ptr);
- return ret;
- }
- return NULL;
- }
- struct tm *localtime_wrapper(const time_t * t)
- {
- if(t)
- return localtime(t);
- return NULL;
- }
- struct my_ptr* strftime_wrapper(struct my_ptr* format,struct tm* timeptr )
- {
- if(timeptr && format->ptr)
- {
- struct my_ptr* ret = malloc(sizeof(struct my_ptr));
- ret->len = 64;
- ret->ptr = calloc(64,1);
- strftime(ret->ptr,ret->len,format->ptr, timeptr);
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement