2
|
1 /*
|
|
2
|
|
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
|
|
4 the GPL version 2 or any later version at your choice available at
|
|
5 http://www.fsf.org/licenses/gpl.txt
|
|
6
|
|
7 Based on readpst.c by David Smith
|
|
8
|
|
9 */
|
|
10
|
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <time.h>
|
|
14 #include <string.h>
|
|
15 #include <ctype.h>
|
|
16 #include <limits.h>
|
|
17 #include <errno.h>
|
|
18
|
|
19 #ifndef _WIN32
|
|
20 # include <unistd.h>
|
|
21 # include <sys/stat.h> //mkdir
|
|
22
|
|
23 // for reading of directory and clearing in function mk_separate_dir
|
|
24 # include <sys/types.h>
|
|
25 # include <dirent.h>
|
|
26 #else
|
|
27 # include <direct.h>
|
|
28 # define chdir _chdir
|
|
29 # define int32_t __int32
|
|
30 #endif
|
|
31
|
|
32 #ifndef __GNUC__
|
|
33 # include "XGetopt.h"
|
|
34 #endif
|
|
35
|
|
36 #include "libstrfunc.h" // for base64_encoding
|
|
37
|
|
38 #include "define.h"
|
|
39 #include "libpst.h"
|
|
40 #include "common.h"
|
|
41 #include "timeconv.h"
|
|
42 #include "lzfu.h"
|
|
43
|
|
44 #define OUTPUT_TEMPLATE "%s"
|
|
45
|
7
|
46 #define VERSION "0.2"
|
2
|
47 // max size of the c_time char*. It will store the date of the email
|
|
48 #define C_TIME_SIZE 500
|
|
49 #define PERM_DIRS 0777
|
|
50
|
|
51 // macro used for creating directories
|
|
52 #ifndef WIN32
|
|
53 #define D_MKDIR(x) mkdir(x, PERM_DIRS)
|
|
54 #else
|
|
55 #define D_MKDIR(x) mkdir(x)
|
|
56 #endif
|
|
57
|
|
58 int32_t usage();
|
|
59 int32_t version();
|
|
60 char *my_stristr(char *haystack, char *needle);
|
|
61 char *check_filename(char *fname);
|
|
62 char *single(char *str);
|
7
|
63 char *folded(char *str);
|
|
64 void multi(char *fmt, char *str);
|
2
|
65 char *rfc2426_escape(char *str);
|
|
66 int32_t chr_count(char *str, char x);
|
|
67
|
|
68 char *prog_name;
|
|
69 pst_file pstfile;
|
|
70 char *ldap_base = NULL; // 'o=some.domain.tld, c=US'
|
|
71 char *ldap_class = NULL; // 'newPerson'
|
|
72 char *ldap_org = NULL; // 'o=some.domain.tld', computed from ldap_base
|
|
73
|
|
74
|
|
75 char *trim(char *name);
|
|
76 char *trim(char *name) {
|
|
77 char *p;
|
|
78 while (*name == ' ') name++;
|
|
79 p = name + strlen(name) - 1;
|
|
80 while ((p >= name) && (*p == ' ')) *p-- = '\0';
|
|
81 return name;
|
|
82 }
|
|
83
|
|
84
|
|
85 void process(pst_desc_ll *d_ptr);
|
|
86 void process(pst_desc_ll *d_ptr) {
|
|
87 pst_item *item = NULL;
|
|
88 while (d_ptr) {
|
|
89 if (d_ptr->desc) {
|
|
90 item = _pst_parse_item(&pstfile, d_ptr);
|
|
91 if (item) {
|
|
92 if (item->message_store) {
|
|
93 // there should only be one message_store, and we have already done it
|
|
94 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
|
|
95 }
|
|
96
|
7
|
97 if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) {
|
|
98 //if this is a non-empty folder other than deleted items, we want to recurse into it
|
|
99 fprintf(stderr, "entering folder %s\n", item->file_as);
|
2
|
100 process(d_ptr->child);
|
|
101 } else if (item->contact) {
|
|
102 // deal with a contact
|
|
103 if (item->type != PST_TYPE_CONTACT) {
|
|
104 DIE(("type should be contact\n"));
|
|
105 }
|
|
106 else if (item->contact == NULL) { // this is an incorrect situation. Inform user
|
|
107 DIE(("null item contact\n"));
|
|
108 } else {
|
|
109 char cn[1000];
|
|
110 snprintf(cn, sizeof(cn), "%s %s %s %s",
|
|
111 single(item->contact->display_name_prefix),
|
|
112 single(item->contact->first_name),
|
|
113 single(item->contact->surname),
|
|
114 single(item->contact->suffix));
|
|
115 if (strcmp(cn, " ")) {
|
7
|
116 // fprintf(stderr, "\n\n\n");
|
|
117 // fprintf(stderr, "access_method %s\n", item->contact->access_method);
|
|
118 // fprintf(stderr, "account_name %s\n", item->contact->account_name);
|
|
119 // fprintf(stderr, "address1 %s\n", item->contact->address1);
|
|
120 // fprintf(stderr, "address1_desc %s\n", item->contact->address1_desc);
|
|
121 // fprintf(stderr, "address1_transport %s\n", item->contact->address1_transport);
|
|
122 // fprintf(stderr, "address2 %s\n", item->contact->address2);
|
|
123 // fprintf(stderr, "address2_desc %s\n", item->contact->address2_desc);
|
|
124 // fprintf(stderr, "address2_transport %s\n", item->contact->address2_transport);
|
|
125 // fprintf(stderr, "address3 %s\n", item->contact->address3);
|
|
126 // fprintf(stderr, "address3_desc %s\n", item->contact->address3_desc);
|
|
127 // fprintf(stderr, "address3_transport %s\n", item->contact->address3_transport);
|
|
128 // fprintf(stderr, "assistant_name %s\n", item->contact->assistant_name);
|
|
129 // fprintf(stderr, "assistant_phone %s\n", item->contact->assistant_phone);
|
|
130 // fprintf(stderr, "billing_information %s\n", item->contact->billing_information);
|
|
131 // fprintf(stderr, "business_address %s\n", item->contact->business_address);
|
|
132 // fprintf(stderr, "business_city %s\n", item->contact->business_city);
|
|
133 // fprintf(stderr, "business_country %s\n", item->contact->business_country);
|
|
134 // fprintf(stderr, "business_fax %s\n", item->contact->business_fax);
|
|
135 // fprintf(stderr, "business_homepage %s\n", item->contact->business_homepage);
|
|
136 // fprintf(stderr, "business_phone %s\n", item->contact->business_phone);
|
|
137 // fprintf(stderr, "business_phone2 %s\n", item->contact->business_phone2);
|
|
138 // fprintf(stderr, "business_po_box %s\n", item->contact->business_po_box);
|
|
139 // fprintf(stderr, "business_postal_code %s\n", item->contact->business_postal_code);
|
|
140 // fprintf(stderr, "business_state %s\n", item->contact->business_state);
|
|
141 // fprintf(stderr, "business_street %s\n", item->contact->business_street);
|
|
142 // fprintf(stderr, "callback_phone %s\n", item->contact->callback_phone);
|
|
143 // fprintf(stderr, "car_phone %s\n", item->contact->car_phone);
|
|
144 // fprintf(stderr, "company_main_phone %s\n", item->contact->company_main_phone);
|
|
145 // fprintf(stderr, "company_name %s\n", item->contact->company_name);
|
|
146 // fprintf(stderr, "computer_name %s\n", item->contact->computer_name);
|
|
147 // fprintf(stderr, "customer_id %s\n", item->contact->customer_id);
|
|
148 // fprintf(stderr, "def_postal_address %s\n", item->contact->def_postal_address);
|
|
149 // fprintf(stderr, "department %s\n", item->contact->department);
|
|
150 // fprintf(stderr, "display_name_prefix %s\n", item->contact->display_name_prefix);
|
|
151 // fprintf(stderr, "first_name %s\n", item->contact->first_name);
|
|
152 // fprintf(stderr, "followup %s\n", item->contact->followup);
|
|
153 // fprintf(stderr, "free_busy_address %s\n", item->contact->free_busy_address);
|
|
154 // fprintf(stderr, "ftp_site %s\n", item->contact->ftp_site);
|
|
155 // fprintf(stderr, "fullname %s\n", item->contact->fullname);
|
|
156 // fprintf(stderr, "gov_id %s\n", item->contact->gov_id);
|
|
157 // fprintf(stderr, "hobbies %s\n", item->contact->hobbies);
|
|
158 // fprintf(stderr, "home_address %s\n", item->contact->home_address);
|
|
159 // fprintf(stderr, "home_city %s\n", item->contact->home_city);
|
|
160 // fprintf(stderr, "home_country %s\n", item->contact->home_country);
|
|
161 // fprintf(stderr, "home_fax %s\n", item->contact->home_fax);
|
|
162 // fprintf(stderr, "home_phone %s\n", item->contact->home_phone);
|
|
163 // fprintf(stderr, "home_phone2 %s\n", item->contact->home_phone2);
|
|
164 // fprintf(stderr, "home_po_box %s\n", item->contact->home_po_box);
|
|
165 // fprintf(stderr, "home_postal_code %s\n", item->contact->home_postal_code);
|
|
166 // fprintf(stderr, "home_state %s\n", item->contact->home_state);
|
|
167 // fprintf(stderr, "home_street %s\n", item->contact->home_street);
|
|
168 // fprintf(stderr, "initials %s\n", item->contact->initials);
|
|
169 // fprintf(stderr, "isdn_phone %s\n", item->contact->isdn_phone);
|
|
170 // fprintf(stderr, "job_title %s\n", item->contact->job_title);
|
|
171 // fprintf(stderr, "keyword %s\n", item->contact->keyword);
|
|
172 // fprintf(stderr, "language %s\n", item->contact->language);
|
|
173 // fprintf(stderr, "location %s\n", item->contact->location);
|
|
174 // fprintf(stderr, "manager_name %s\n", item->contact->manager_name);
|
|
175 // fprintf(stderr, "middle_name %s\n", item->contact->middle_name);
|
|
176 // fprintf(stderr, "mileage %s\n", item->contact->mileage);
|
|
177 // fprintf(stderr, "mobile_phone %s\n", item->contact->mobile_phone);
|
|
178 // fprintf(stderr, "nickname %s\n", item->contact->nickname);
|
|
179 // fprintf(stderr, "office_loc %s\n", item->contact->office_loc);
|
|
180 // fprintf(stderr, "org_id %s\n", item->contact->org_id);
|
|
181 // fprintf(stderr, "other_address %s\n", item->contact->other_address);
|
|
182 // fprintf(stderr, "other_city %s\n", item->contact->other_city);
|
|
183 // fprintf(stderr, "other_country %s\n", item->contact->other_country);
|
|
184 // fprintf(stderr, "other_phone %s\n", item->contact->other_phone);
|
|
185 // fprintf(stderr, "other_po_box %s\n", item->contact->other_po_box);
|
|
186 // fprintf(stderr, "other_postal_code %s\n", item->contact->other_postal_code);
|
|
187 // fprintf(stderr, "other_state %s\n", item->contact->other_state);
|
|
188 // fprintf(stderr, "other_street %s\n", item->contact->other_street);
|
|
189 // fprintf(stderr, "pager_phone %s\n", item->contact->pager_phone);
|
|
190 // fprintf(stderr, "personal_homepage %s\n", item->contact->personal_homepage);
|
|
191 // fprintf(stderr, "pref_name %s\n", item->contact->pref_name);
|
|
192 // fprintf(stderr, "primary_fax %s\n", item->contact->primary_fax);
|
|
193 // fprintf(stderr, "primary_phone %s\n", item->contact->primary_phone);
|
|
194 // fprintf(stderr, "profession %s\n", item->contact->profession);
|
|
195 // fprintf(stderr, "radio_phone %s\n", item->contact->radio_phone);
|
|
196 // fprintf(stderr, "spouse_name %s\n", item->contact->spouse_name);
|
|
197 // fprintf(stderr, "suffix %s\n", item->contact->suffix);
|
|
198 // fprintf(stderr, "surname %s\n", item->contact->surname);
|
|
199 // fprintf(stderr, "telex %s\n", item->contact->telex);
|
|
200 // fprintf(stderr, "transmittable_display_name %s\n", item->contact->transmittable_display_name);
|
|
201 // fprintf(stderr, "ttytdd_phone %s\n", item->contact->ttytdd_phone);
|
2
|
202 // have a valid cn
|
|
203 printf("dn: cn=%s, %s\n", trim(cn), ldap_base);
|
|
204 printf("cn: %s\n", trim(cn));
|
|
205 if (item->contact->first_name) {
|
|
206 snprintf(cn, sizeof(cn), "%s %s",
|
|
207 single(item->contact->display_name_prefix),
|
|
208 single(item->contact->first_name));
|
|
209 printf("givenName: %s\n", trim(cn));
|
|
210 }
|
|
211 if (item->contact->surname) {
|
|
212 snprintf(cn, sizeof(cn), "%s %s",
|
|
213 single(item->contact->surname),
|
|
214 single(item->contact->suffix));
|
|
215 printf("sn: %s\n", trim(cn));
|
|
216 }
|
|
217 if (item->contact->job_title)
|
|
218 printf("personalTitle: %s\n", single(item->contact->job_title));
|
|
219 if (item->contact->company_name)
|
|
220 printf("company: %s\n", single(item->contact->company_name));
|
|
221 if (item->contact->address1)
|
|
222 printf("mail: %s\n", single(item->contact->address1));
|
|
223 if (item->contact->address2)
|
|
224 printf("mail: %s\n", single(item->contact->address2));
|
|
225 if (item->contact->address3)
|
|
226 printf("mail: %s\n", single(item->contact->address3));
|
|
227 if (item->contact->business_address) {
|
|
228 if (item->contact->business_po_box)
|
|
229 printf("postalAddress: %s\n", single(item->contact->business_po_box));
|
|
230 if (item->contact->business_street)
|
|
231 multi("postalAddress: %s\n", item->contact->business_street);
|
|
232 if (item->contact->business_city)
|
|
233 printf("l: %s\n", single(item->contact->business_city));
|
|
234 if (item->contact->business_state)
|
|
235 printf("st: %s\n", single(item->contact->business_state));
|
|
236 if (item->contact->business_postal_code)
|
|
237 printf("postalCode: %s\n", single(item->contact->business_postal_code));
|
|
238 }
|
|
239 else if (item->contact->home_address) {
|
|
240 if (item->contact->home_po_box)
|
|
241 printf("postalAddress: %s\n", single(item->contact->home_po_box));
|
|
242 if (item->contact->home_street)
|
|
243 multi("postalAddress: %s\n", item->contact->home_street);
|
|
244 if (item->contact->home_city)
|
|
245 printf("l: %s\n", single(item->contact->home_city));
|
|
246 if (item->contact->home_state)
|
|
247 printf("st: %s\n", single(item->contact->home_state));
|
|
248 if (item->contact->home_postal_code)
|
|
249 printf("postalCode: %s\n", single(item->contact->home_postal_code));
|
|
250 }
|
|
251 else if (item->contact->other_address) {
|
|
252 if (item->contact->other_po_box)
|
|
253 printf("postalAddress: %s\n", single(item->contact->other_po_box));
|
|
254 if (item->contact->other_street)
|
|
255 multi("postalAddress: %s\n", item->contact->other_street);
|
|
256 if (item->contact->other_city)
|
|
257 printf("l: %s\n", single(item->contact->other_city));
|
|
258 if (item->contact->other_state)
|
|
259 printf("st: %s\n", single(item->contact->other_state));
|
|
260 if (item->contact->other_postal_code)
|
|
261 printf("postalCode: %s\n", single(item->contact->other_postal_code));
|
|
262 }
|
|
263 if (item->contact->business_fax)
|
|
264 printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax));
|
|
265 else if (item->contact->home_fax)
|
|
266 printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax));
|
|
267
|
|
268 if (item->contact->business_phone)
|
|
269 printf("telephoneNumber: %s\n", single(item->contact->business_phone));
|
|
270 if (item->contact->home_phone)
|
|
271 printf("homePhone: %s\n", single(item->contact->home_phone));
|
|
272
|
|
273 if (item->contact->car_phone)
|
|
274 printf("mobile: %s\n", single(item->contact->car_phone));
|
|
275 else if (item->contact->mobile_phone)
|
|
276 printf("mobile: %s\n", single(item->contact->mobile_phone));
|
|
277 else if (item->contact->other_phone)
|
|
278 printf("mobile: %s\n", single(item->contact->other_phone));
|
|
279
|
|
280
|
|
281 if (item->comment)
|
|
282 printf("description: %s\n", single(item->comment));
|
|
283
|
|
284 printf("objectClass: %s\n\n", ldap_class);
|
|
285 }
|
|
286 }
|
|
287 }
|
|
288 }
|
|
289 _pst_freeItem(item);
|
|
290 }
|
|
291 d_ptr = d_ptr->next;
|
|
292 }
|
|
293 }
|
|
294
|
|
295
|
|
296 int main(int argc, char** argv) {
|
|
297 pst_desc_ll *d_ptr;
|
|
298 char *fname = NULL;
|
|
299 char *temp = NULL; //temporary char pointer
|
|
300 char c;
|
|
301 prog_name = argv[0];
|
|
302 pst_item *item = NULL;
|
|
303
|
|
304 while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) {
|
|
305 switch (c) {
|
|
306 case 'b':
|
|
307 ldap_base = optarg;
|
|
308 temp = strchr(ldap_base, ',');
|
|
309 if (temp) {
|
|
310 *temp = '\0';
|
|
311 ldap_org = strdup(ldap_base);
|
|
312 *temp = ',';
|
|
313 }
|
|
314 break;
|
|
315 case 'c':
|
|
316 ldap_class = optarg;
|
|
317 break;
|
|
318 case 'h':
|
|
319 usage();
|
|
320 exit(0);
|
|
321 break;
|
|
322 case 'V':
|
|
323 version();
|
|
324 exit(0);
|
|
325 break;
|
|
326 default:
|
|
327 usage();
|
|
328 exit(1);
|
|
329 break;
|
|
330 }
|
|
331 }
|
|
332
|
|
333 if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) {
|
|
334 fname = argv[optind];
|
|
335 } else {
|
|
336 usage();
|
|
337 exit(2);
|
|
338 }
|
|
339
|
|
340 DEBUG_INIT("pst2ldif.log");
|
|
341 DEBUG_REGISTER_CLOSE();
|
|
342 DEBUG_ENT("main");
|
|
343 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
|
|
344 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
|
|
345
|
|
346 pst_load_extended_attributes(&pstfile);
|
|
347
|
|
348 d_ptr = pstfile.d_head; // first record is main record
|
|
349 item = _pst_parse_item(&pstfile, d_ptr);
|
|
350 if (!item || !item->message_store) {
|
|
351 DIE(("main: Could not get root record\n"));
|
|
352 }
|
|
353
|
|
354 d_ptr = pst_getTopOfFolders(&pstfile, item);
|
|
355 if (!d_ptr) {
|
|
356 DIE(("Top of folders record not found. Cannot continue\n"));
|
|
357 }
|
|
358
|
|
359 _pst_freeItem(item);
|
|
360
|
|
361 // write the ldap header
|
|
362 printf("dn: %s\n", ldap_base);
|
|
363 printf("o: %s\n", ldap_org);
|
|
364 printf("objectClass: organization\n\n");
|
|
365 printf("dn: cn=root, %s\n", ldap_base);
|
|
366 printf("cn: root\n");
|
|
367 printf("objectClass: %s\n\n", ldap_class);
|
|
368
|
|
369 process(d_ptr->child); // do the children of TOPF
|
|
370 pst_close(&pstfile);
|
|
371 return 0;
|
|
372 }
|
|
373
|
|
374
|
|
375 int usage() {
|
|
376 version();
|
|
377 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
|
|
378 printf("OPTIONS:\n");
|
|
379 printf("\t-h\t- Help. This screen\n");
|
|
380 printf("\t-V\t- Version. Display program version\n");
|
|
381 printf("\t-b ldapbase\t- set the ldap base value\n");
|
4
|
382 printf("\t-c class \t- set the class of the ldap objects\n");
|
2
|
383 return 0;
|
|
384 }
|
|
385
|
|
386
|
|
387 int version() {
|
|
388 printf("pst2ldif v%s using LibPST v%s\n", VERSION, PST_VERSION);
|
|
389 #if BYTE_ORDER == BIG_ENDIAN
|
|
390 printf("Big Endian implementation being used.\n");
|
|
391 #elif BYTE_ORDER == LITTLE_ENDIAN
|
|
392 printf("Little Endian implementation being used.\n");
|
|
393 #else
|
|
394 # error "Byte order not supported by this library"
|
|
395 #endif
|
|
396 #ifdef __GNUC__
|
|
397 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
|
|
398 #endif
|
|
399 return 0;
|
|
400 }
|
|
401
|
|
402
|
|
403 // my_stristr varies from strstr in that its searches are case-insensitive
|
|
404 char * my_stristr(char *haystack, char *needle) {
|
|
405 char *x=haystack, *y=needle, *z = NULL;
|
|
406 if (haystack == NULL || needle == NULL)
|
|
407 return NULL;
|
|
408 while (*y != '\0' && *x != '\0') {
|
|
409 if (tolower(*y) == tolower(*x)) {
|
|
410 // move y on one
|
|
411 y++;
|
|
412 if (z == NULL) {
|
|
413 z = x; // store first position in haystack where a match is made
|
|
414 }
|
|
415 } else {
|
|
416 y = needle; // reset y to the beginning of the needle
|
|
417 z = NULL; // reset the haystack storage point
|
|
418 }
|
|
419 x++; // advance the search in the haystack
|
|
420 }
|
|
421 return z;
|
|
422 }
|
|
423
|
|
424
|
|
425 char *check_filename(char *fname) {
|
|
426 char *t = fname;
|
|
427 if (t == NULL) {
|
|
428 return fname;
|
|
429 }
|
|
430 while ((t = strpbrk(t, "/\\:"))) {
|
|
431 // while there are characters in the second string that we don't want
|
|
432 *t = '_'; //replace them with an underscore
|
|
433 }
|
|
434 return fname;
|
|
435 }
|
|
436
|
|
437
|
|
438 char *single(char *str) {
|
|
439 if (!str) return "";
|
|
440 char *ret = rfc2426_escape(str);
|
|
441 char *n = strchr(ret, '\n');
|
|
442 if (n) *n = '\0';
|
|
443 return ret;
|
|
444 }
|
|
445
|
|
446
|
7
|
447 char *folded(char *str) {
|
2
|
448 if (!str) return "";
|
|
449 char *ret = rfc2426_escape(str);
|
|
450 char *n = ret;
|
7
|
451 while (n = strchr(n, '\n')) {
|
|
452 *n = ' ';
|
|
453 }
|
|
454 n = ret;
|
|
455 while (n = strchr(n, ',')) {
|
|
456 *n = ' ';
|
|
457 }
|
|
458 return ret;
|
|
459 }
|
|
460
|
|
461
|
|
462 void multi(char *fmt, char *str) {
|
|
463 if (!str) return;
|
|
464 char *ret = rfc2426_escape(str);
|
|
465 char *n = ret;
|
2
|
466 while (n = strchr(ret, '\n')) {
|
|
467 *n = '\0';
|
|
468 printf(fmt, ret);
|
|
469 ret = n+1;
|
|
470 }
|
|
471 if (*ret) printf(fmt, ret);
|
|
472 }
|
|
473
|
|
474
|
|
475 char *rfc2426_escape(char *str) {
|
|
476 static char* buf = NULL;
|
|
477 char *ret, *a, *b;
|
|
478 int x = 0, y, z;
|
|
479 if (str == NULL)
|
|
480 ret = str;
|
|
481 else {
|
|
482
|
|
483 // calculate space required to escape all the following characters
|
|
484 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2));
|
|
485 z = chr_count(str, '\r');
|
|
486 if (y == 0 && z == 0)
|
|
487 // there isn't any extra space required
|
|
488 ret = str;
|
|
489 else {
|
|
490 buf = (char*) realloc(buf, x+1);
|
|
491 a = str;
|
|
492 b = buf;
|
|
493 while (*a != '\0') {
|
|
494 switch(*a) {
|
|
495 // case ',' :
|
|
496 case '\\':
|
|
497 case ';' :
|
|
498 // case '\n':
|
|
499 *(b++)='\\';
|
|
500 *b=*a;
|
|
501 break;
|
|
502 case '\r':
|
|
503 break;
|
|
504 default:
|
|
505 *b=*a;
|
|
506 }
|
|
507 b++;
|
|
508 a++;
|
|
509 }
|
|
510 *b = '\0';
|
|
511 ret = buf;
|
|
512 }
|
|
513 }
|
|
514 return ret;
|
|
515 }
|
|
516
|
|
517
|
|
518 int chr_count(char *str, char x) {
|
|
519 int r = 0;
|
|
520 while (*str != '\0') {
|
|
521 if (*str == x)
|
|
522 r++;
|
|
523 str++;
|
|
524 }
|
|
525 return r;
|
|
526 }
|
|
527
|