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