Sat, 22 Apr 2000 17:34:20 +0200
trunk: changeset 11
Portability fixes
0 | 1 | /* |
2 | * libtu/misc.c | |
3 | * | |
4 | * Copyright (c) Tuomo Valkonen 1999-2000. | |
5 | * | |
6 | * This file is distributed under the terms of the "Artistic License". | |
7 | * See the included file LICENSE for details. | |
8 | */ | |
9 | ||
10 | #include <stdlib.h> | |
11 | #include <stdio.h> | |
12 | #include <string.h> | |
13 | ||
5 | 14 | #include <libtu/misc.h> |
0 | 15 | |
16 | ||
1 | 17 | void *malloczero(size_t size) |
0 | 18 | { |
19 | void *p=malloc(size); | |
20 | ||
21 | if(p!=NULL) | |
22 | memset(p, 0, size); | |
23 | ||
24 | return p; | |
25 | } | |
26 | ||
27 | ||
1 | 28 | void *remalloczero(void *ptr, size_t oldsize, size_t newsize) |
0 | 29 | { |
30 | void *p=NULL; | |
31 | ||
32 | if(newsize!=0){ | |
33 | p=malloc(newsize); | |
34 | ||
35 | if(p==NULL) | |
36 | return NULL; | |
37 | ||
38 | memset(p, 0, newsize); | |
39 | ||
1 | 40 | if(newsize<oldsize) |
41 | oldsize=newsize; | |
42 | ||
43 | if(ptr!=NULL) | |
0 | 44 | memcpy(p, ptr, oldsize); |
45 | } | |
46 | ||
47 | if(ptr!=NULL) | |
48 | free(ptr); | |
49 | ||
50 | return p; | |
51 | } | |
52 | ||
53 | ||
54 | char *scopy(const char *p) | |
55 | { | |
56 | char*pn; | |
57 | size_t l=strlen(p); | |
58 | ||
59 | pn=(char*)malloc(l+1); | |
60 | ||
61 | if(pn==NULL) | |
62 | return NULL; | |
63 | ||
64 | memcpy(pn, p, l+1); | |
65 | ||
66 | return pn; | |
67 | } | |
68 | ||
69 | ||
70 | char *scat(const char *p1, const char *p2) | |
71 | { | |
72 | size_t l1, l2; | |
73 | char*pn; | |
74 | ||
75 | l1=strlen(p1); | |
76 | l2=strlen(p2); | |
77 | ||
78 | pn=(char*)malloc(l1+l2+1); | |
79 | ||
80 | if(pn==NULL) | |
81 | return NULL; | |
82 | ||
83 | memcpy(pn, p1, l1); | |
84 | memcpy(pn+l1, p2, l2+1); | |
85 | ||
86 | return pn; | |
87 | } | |
88 | ||
89 | ||
90 | char *scat3(const char *p1, const char *p2, const char *p3) | |
91 | { | |
92 | size_t l1, l2, l3; | |
93 | char *pn; | |
94 | ||
95 | l1=strlen(p1); | |
96 | l2=strlen(p2); | |
97 | l3=strlen(p3); | |
98 | ||
99 | pn=(char*)malloc(l1+l2+l3+1); | |
100 | ||
101 | if(pn==NULL) | |
102 | return NULL; | |
103 | ||
104 | memcpy(pn, p1, l1); | |
105 | memcpy(pn+l1, p2, l2); | |
106 | memcpy(pn+l1+l2, p3, l3+1); | |
107 | ||
108 | return pn; | |
109 | } | |
110 | ||
111 | ||
1 | 112 | char *scatn(const char *s1, ssize_t l1, const char *s2, ssize_t l2) |
113 | { | |
114 | size_t tlen=1; | |
115 | char *s; | |
116 | ||
117 | if(l1<0) | |
118 | l1=strlen(s1); | |
119 | ||
120 | if(l2<0) | |
121 | l2=strlen(s2); | |
122 | ||
123 | tlen+=l1+l2; | |
124 | ||
125 | s=(char*)malloc(tlen); | |
126 | ||
127 | if(s==NULL) | |
128 | return NULL; | |
129 | ||
130 | memcpy(s, s1, l1); | |
131 | memcpy(s+l1, s2, l2); | |
132 | s[l1+l2]='\0'; | |
133 | ||
134 | return s; | |
135 | } | |
136 | ||
137 | ||
0 | 138 | /* */ |
139 | ||
140 | ||
141 | const char *simple_basename(const char *name) | |
142 | { | |
143 | const char *p; | |
144 | ||
145 | p=name+strlen(name)-1; | |
146 | ||
147 | /* Skip any trailing slashes */ | |
148 | while(*p=='/'){ | |
149 | /* root? */ | |
150 | if(p==name) | |
151 | return name; | |
152 | p--; | |
153 | } | |
154 | ||
155 | while(p!=name){ | |
156 | if(*p=='/') | |
157 | return p+1; | |
158 | p--; | |
159 | } | |
160 | ||
161 | return name; | |
162 | } | |
163 | ||
164 | ||
165 | /* */ | |
166 | ||
167 | ||
168 | bool readf(FILE *f, void *buf, size_t n) | |
169 | { | |
170 | return fread(buf, 1, n, f)!=1; | |
171 | } | |
172 | ||
173 | ||
174 | bool writef(FILE *f, const void *buf, size_t n) | |
175 | { | |
176 | return fwrite(buf, 1, n, f)!=1; | |
177 | } |