Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <malloc.h> // malloc_usable_size
  7. #include "wrappers.h"
  8.  
  9. struct my_ptr* generate_str(){
  10. char format[] = "%qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_-0987654321^#";
  11.  
  12. char* r;
  13. struct my_ptr* ptr = malloc(sizeof(struct my_ptr));
  14. size_t i;
  15. size_t max = rand() % 300;
  16. r = calloc(max+1,sizeof(*r));
  17. if(r==NULL)
  18. goto exit;
  19. for(i=0;i<max;i++)
  20. r[i] = format[rand() % sizeof(format)];
  21. r[max] = '\0';
  22.  
  23. exit:
  24. ptr->ptr = r;
  25. ptr->len = max;
  26. ptr->writeable =1;
  27. return ptr;
  28. }
  29. struct my_ptr* asctime_wrapper()
  30. {
  31. struct my_ptr* ptr = malloc(sizeof(struct my_ptr));
  32. char* temp;
  33. time_t czas;
  34. time( & czas );
  35. temp = asctime( localtime( & czas ) ) ;
  36. ptr->ptr = calloc(strlen(temp)+1,1);
  37. memcpy(ptr->ptr,temp,strlen(temp));
  38. ptr->len = strlen(temp);
  39. ptr->writeable = 1;
  40. return ptr;
  41. }
  42. void memcpy_wrapper (struct my_ptr* ptr,int num)
  43. {
  44.  
  45. if(ptr->ptr!=NULL && num>0 && ptr->len>num)
  46. {
  47. void* destination = malloc(ptr->len+1);
  48. memcpy(destination,ptr->ptr,num);
  49. free(destination);
  50. }
  51. }
  52. struct my_ptr * memmove_wrapper(struct my_ptr* ptr, int num)
  53. {
  54. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  55. if(ptr && ptr->ptr!=NULL && num>0 && ptr->len>num)
  56. {
  57. void* destination = calloc(ptr->len+1,1);
  58. memcpy(destination,ptr->ptr,num);
  59. ret->ptr = destination;
  60. ret->len = num;
  61. ret->writeable = 1;
  62. memmove(ret->ptr,ptr->ptr,num);
  63. return ret;
  64. }
  65. return NULL;
  66. }
  67. void strcpy_wrapper (struct my_ptr* ptr )
  68. {
  69. if(ptr->ptr!=NULL && ptr->len > 0){
  70. char* destination = malloc(ptr->len+1);
  71. ((char*)ptr->ptr)[ptr->len] = '\0';
  72. strcpy(destination,ptr->ptr);
  73. free(destination);
  74. }
  75. }
  76. void strncpy_wrapper(const char * source, size_t num )
  77. {
  78. if(source!=NULL && num>0){
  79. char* destination = malloc(num);
  80. if(!destination)
  81. return;
  82. strncpy(destination,source,num);
  83. free(destination);
  84. }
  85. }
  86.  
  87. struct my_ptr * strcat_wrapper ( struct my_ptr * dst, struct my_ptr * src )
  88. {
  89. if(dst->ptr != src->ptr && src->ptr!=NULL && dst->ptr !=NULL ){
  90. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  91. ret->ptr = malloc(dst->len+src->len+2);
  92. ret->len = dst->len+src->len;
  93. memcpy(ret->ptr,dst->ptr,dst->len);
  94. strcat(ret->ptr,src->ptr);
  95. return ret;
  96. }
  97. return NULL;
  98. }
  99. struct my_ptr *strncat_wrapper ( struct my_ptr * dst, struct my_ptr * src, size_t num )
  100. {
  101. //num is maximium, so is not a requirement
  102. if(dst->ptr != src->ptr && src->ptr!=NULL && dst->ptr !=NULL){
  103. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  104. ret->ptr = malloc(dst->len+num+2);
  105. ret->len = dst->len+num;
  106. memcpy(ret->ptr,dst->ptr,dst->len);
  107. strncat(ret->ptr,src->ptr,num);
  108. return ret;
  109. }
  110. return NULL;
  111. }
  112. int memcmp_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2, size_t num )
  113. {
  114. if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
  115. return memcmp(ptr1->ptr,ptr2->ptr,num);
  116. return 0;
  117. }
  118. int strcmp_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2 )
  119. {
  120. if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
  121. return strcmp(ptr1->ptr,ptr2->ptr);
  122. return 0;
  123. }
  124. int strcoll_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2 )
  125. {
  126. if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
  127. return strcoll(ptr1->ptr,ptr2->ptr);
  128. return 0;
  129. }
  130. int strncmp_wrapper ( struct my_ptr * ptr1, struct my_ptr * ptr2, size_t num )
  131. {
  132. if(ptr1->ptr!=NULL && ptr2->ptr!=NULL)
  133. return strncmp(ptr1->ptr,ptr2->ptr,num);
  134. return 0;
  135. }
  136. void strxfrm_wrapper (struct my_ptr * src)
  137. {
  138. if(src->ptr !=NULL)
  139. {
  140. strxfrm(NULL,src->ptr,0);
  141. }
  142. }
  143. void memchr_wrapper ( struct my_ptr * src, int val, int num)
  144. {
  145. if(src->ptr != NULL)
  146. {
  147. int len = num;
  148. if(num>src->len)
  149. len = src->len;
  150. memchr(src->ptr,val,len);
  151. }
  152. }
  153. void strchr_wrapper ( struct my_ptr * src, int character)
  154. {
  155. if(src->ptr!=NULL)
  156. strchr(src->ptr,character);
  157. }
  158. void strcspn_wrapper ( struct my_ptr * str1, struct my_ptr * str2 )
  159. {
  160. if(str1->ptr!=NULL && str2->ptr!=NULL)
  161. strcspn(str1->ptr,str2->ptr);
  162. }
  163. void strpbrk_wrapper ( struct my_ptr * str1, struct my_ptr * str2)
  164. {
  165. if(str1->ptr!=NULL && str2->ptr!=NULL)
  166. strpbrk(str1->ptr,str2->ptr);
  167. }
  168. void strrchr_wrapper ( struct my_ptr * str, int character)
  169. {
  170. if(str->ptr!=NULL)
  171. strrchr(str->ptr,character);
  172. }
  173. void strspn_wrapper ( struct my_ptr * str1, struct my_ptr * str2 )
  174. {
  175. if(str1->ptr!=NULL && str2->ptr!=NULL)
  176. strspn(str1->ptr,str2->ptr);
  177. }
  178. void strstr_wrapper ( struct my_ptr * str1, struct my_ptr * str2)
  179. {
  180. if(str1->ptr!=NULL && str2->ptr!=NULL)
  181. strstr(str1->ptr,str2->ptr);
  182. }
  183. void strtok_wrapper ( struct my_ptr * str, struct my_ptr * delimiters )
  184. {
  185. if(str->ptr!=NULL && delimiters->ptr!=NULL)
  186. {
  187. char* new_str = malloc(str->len);
  188. memcpy(new_str,str->ptr,str->len);
  189. strtok(new_str,delimiters->ptr);
  190. free(new_str);
  191. }
  192. }
  193. void strlen_wrapper ( struct my_ptr * str )
  194. {
  195. if(str->ptr!=NULL)
  196. strlen(str);
  197. }
  198. struct my_ptr* memset_wrapper (int value, size_t num )
  199. {
  200. if(num >1){
  201. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  202. ret->ptr = malloc(num+1);
  203. memset(ret->ptr,value,num);
  204. ((char*)ret->ptr)[num] = '\0';
  205. ret->len = num;
  206. return ret;
  207. }
  208. return NULL;
  209. }
  210. struct my_ptr* strerror_wrapper(int errnum)
  211. {
  212. char* src = strerror(errnum);
  213. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  214. ret->len = strlen(src);
  215. ret->ptr = calloc(ret->len+1,1);
  216. memcpy(ret->ptr,src,ret->len);
  217. return ret;
  218. }
  219.  
  220. struct my_ptr* ctime_wrapper(time_t * a)
  221. {
  222. if(a)
  223. {
  224. char* src = ctime(a);
  225. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  226. ret->len = strlen(src);
  227. ret->ptr = calloc(ret->len+1,1);
  228. memcpy(ret->ptr,src,ret->len);
  229. return ret;
  230. }
  231. return NULL;
  232. }
  233. struct my_ptr* ctime_r_wrapper(time_t * a)
  234. {
  235. if(a)
  236. {
  237. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  238. ret->len = 64;
  239. ret->ptr = calloc(64,1);
  240. ctime_r(a,ret->ptr);
  241. }
  242. return NULL;
  243. }
  244. struct tm *localtime_r_wrapper(const time_t * timep)
  245. {
  246. if(timep!=NULL )
  247. {
  248. struct tm* ret = malloc(sizeof(struct tm));
  249. localtime_r(timep,ret);
  250. }
  251. return NULL;
  252. }
  253. struct my_ptr* strptime_wrapper(const char * format,struct tm * tm)
  254. {
  255. if(format && tm)
  256. {
  257. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  258. ret->len = 64;
  259. ret->ptr = calloc(64,1);
  260. strptime(ret->ptr,format,tm);
  261. return ret;
  262. }
  263. return NULL;
  264. }
  265. time_t mktime_wrapper(struct tm * tm)
  266. {
  267. time_t ret_zero; // TODO: How to declare an empty time_t ?
  268. if(tm)
  269. return mktime(tm);
  270. else
  271. return ret_zero;
  272. }
  273. struct my_ptr* asctime_r_wrapper(const struct tm * tm)
  274. {
  275. if(tm)
  276. {
  277. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  278. ret->len = 64;
  279. ret->ptr = calloc(64,1);
  280. asctime_r(tm,ret->ptr);
  281. return ret;
  282. }
  283. return NULL;
  284. }
  285.  
  286. struct tm *localtime_wrapper(const time_t * t)
  287. {
  288. if(t)
  289. return localtime(t);
  290. return NULL;
  291. }
  292.  
  293. struct my_ptr* strftime_wrapper(struct my_ptr* format,struct tm* timeptr )
  294. {
  295. if(timeptr && format->ptr)
  296. {
  297. struct my_ptr* ret = malloc(sizeof(struct my_ptr));
  298. ret->len = 64;
  299. ret->ptr = calloc(64,1);
  300. strftime(ret->ptr,ret->len,format->ptr, timeptr);
  301. }
  302. return NULL;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement