OSDN Git Service

addid library source 20140221
[zither/ds-zither.git] / sources / lib / libms / safe_memcpy.c
1 // safe_memcpy.c
2 // $Id: safe_memcpy.c,v 1.2 2007/10/17 10:57:56 sendan Exp $
3 // masashi shimakura
4
5 #include<stdio.h>
6 #include<string.h>
7 #include<stdlib.h>
8 #include <errno.h>
9
10 #include "almemsys.h"
11
12
13
14 char * safe_memcpy(char * buf, char * newdata, int max_len)
15 {
16 int data_count;
17
18 if(newdata == NULL){
19    fprintf(stderr,"safe_memcpy(0): insert data is NULL.\n");
20    buf = null_free(buf);
21    return buf;
22    }
23 else if(newdata[0] == (char)0x00){
24    buf = null_free(buf);
25    return buf;
26    }
27
28 data_count = (int)strlen(newdata);
29
30 if(buf == NULL){
31    buf = (char *)calloc(BUF_LEN, sizeof(char));
32    }
33 else{
34    buf = null_free(buf);
35    }
36
37 if(max_len < data_count){
38    if((buf = (char *)realloc(buf, (max_len + 1)))==NULL){
39       fprintf(stderr,"The memory cannot be newly secured. \n");
40       mlexit();
41       }
42    memset(buf, (char)0x00, max_len);
43    count_memcpy(buf, newdata, max_len);
44    fprintf(stderr,"data size err %s %d %d\n", buf, data_count, max_len);
45    return buf;
46    }
47 else {
48    if((buf = (char *)realloc(buf, (data_count + 2)))==NULL){
49       fprintf(stderr,"The memory cannot be newly secured. \n");
50       mlexit();
51       }
52    memset(buf, (char)0x00, data_count + 1);
53    count_memcpy(buf, newdata, data_count);
54    #ifdef AL_DEBUG 
55    printf("COUNT = <%d> GET DATA = <%s>\n", data_count, buf);
56    #endif
57    return buf;
58    }
59
60 return NULL;
61 }
62
63
64
65