summaryrefslogtreecommitdiff
path: root/pith/charconv/filesys.c
blob: 3be45a5f77a863c86609b9268f4edadc2fe7d35a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: filesys.c 770 2007-10-24 00:23:09Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2007 University of Washington
 * Copyright 2013-2014 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

/* includable WITHOUT dependency on c-client */
#include "../../c-client/mail.h"
#include "../../c-client/utf8.h"

#ifdef _WINDOWS
/* wingdi.h uses ERROR (!) and we aren't using the c-client ERROR so... */
#undef ERROR
#endif

#include <system.h>
#include <general.h>

#include "../../c-client/fs.h"

/* includable WITHOUT dependency on pico */
#include "../../pico/keydefs.h"
#ifdef _WINDOWS
#include "../../pico/osdep/mswin.h"
#endif

#include "filesys.h"
#include "utf8.h"


#define bad_char ((UCS) '?')


/*
 * Make it easier to use the convert_to_locale function for filenames
 * and directory names. Note, only one at a time because there's only
 * one buffer.
 * This isn't being freed as it stands now.
 */
char *
fname_to_locale(char *fname)
{
    static char *fname_locale_buf = NULL;
    static size_t fname_locale_len = 0;
    char *converted_fname, *p;

    p = convert_to_locale(fname);
    if(p)
      converted_fname = p;
    else
      converted_fname = fname;

    if(converted_fname){
	if(strlen(converted_fname)+1 > fname_locale_len){
	    if(fname_locale_buf)
	      fs_give((void **) &fname_locale_buf);

	    fname_locale_len = strlen(converted_fname)+1;
	    fname_locale_buf = (char *) fs_get(fname_locale_len * sizeof(char));
	}

	strncpy(fname_locale_buf, converted_fname, fname_locale_len);
	fname_locale_buf[fname_locale_len-1] = '\0';
    }
    else{
	if(fname_locale_len == 0){
	    fname_locale_len = 1;
	    fname_locale_buf = (char *) fs_get(fname_locale_len * sizeof(char));
	}

	fname_locale_buf[0] = '\0';
    }

    if(p)
      fs_give((void **) &p);

    return(fname_locale_buf);
}


/*
 * Make it easier to use the convert_to_utf8 function for filenames
 * and directory names. Note, only one at a time because there's only
 * one buffer.
 * This isn't being freed as it stands now.
 */
char *
fname_to_utf8(char *fname)
{
    static char *fname_utf8_buf = NULL;
    static size_t fname_utf8_len = 0;
    char *converted_fname, *p;

    p = convert_to_utf8(fname, NULL, 0);
    if(p)
      converted_fname = p;
    else
      converted_fname = fname;

    if(converted_fname){
	if(strlen(converted_fname)+1 > fname_utf8_len){
	    if(fname_utf8_buf)
	      fs_give((void **) &fname_utf8_buf);

	    fname_utf8_len = strlen(converted_fname)+1;
	    fname_utf8_buf = (char *) fs_get(fname_utf8_len * sizeof(char));
	}

	strncpy(fname_utf8_buf, converted_fname, fname_utf8_len);
	fname_utf8_buf[fname_utf8_len-1] = '\0';
    }
    else{
	if(fname_utf8_len == 0){
	    fname_utf8_len = 1;
	    fname_utf8_buf = (char *) fs_get(fname_utf8_len * sizeof(char));
	}

	fname_utf8_buf[0] = '\0';
    }

    if(p)
      fs_give((void **) &p);

    return(fname_utf8_buf);
}


/*
 * The fp file pointer is open for read on a file which has contents
 * that are encoded in the user's locale charset. That multibyte stream
 * of characters is converted to wide characters and returned one at
 * a time.
 *
 * Not sure what to do if an uninterpretable character happens. Returning
 * the bad character now.
 */
UCS
read_a_wide_char(FILE *fp,
		 void *input_cs)	/* input_cs ignored in Windows */
{
#ifdef _WINDOWS
    _TINT val;

    val = _fgettc(fp);
    if(val == _TEOF)
      return(CCONV_EOF);

    return((UCS) val);
#else /* UNIX */
    unsigned long octets_so_far, remaining_octets;
    unsigned char *inputp;
    unsigned char inputbuf[20];
    int c;
    UCS ucs;

    c = fgetc(fp);
    if(c == EOF)
      return(CCONV_EOF);

    /*
     * Read enough bytes to make up a character and convert it to UCS-4.
     */
    memset(inputbuf, 0, sizeof(inputbuf));
    inputbuf[0] = (unsigned char) c;
    octets_so_far = 1;
    for(;;){
	remaining_octets = octets_so_far;
	inputp = inputbuf;
	ucs = mbtow(input_cs, &inputp, &remaining_octets);
	switch(ucs){
	  case CCONV_BADCHAR:
	    return(bad_char);
	  
	  case CCONV_NEEDMORE:
	    if(octets_so_far >= sizeof(inputbuf))
	      return(bad_char);

	    c = fgetc(fp);
	    if(c == EOF)
	      return(CCONV_EOF);

	    inputbuf[octets_so_far++] = (unsigned char) c;
	    break;

	  default:
	    /* got a good UCS-4 character */
	    return(ucs);
	}
    }

    return(bad_char);
#endif /* UNIX */
}


int
write_a_wide_char(UCS ucs, FILE *fp)
{
#ifdef _WINDOWS
    int   rv = 1;
    TCHAR w;

    w = (TCHAR) ucs;
    if(_fputtc(w, fp) == _TEOF)
      rv = EOF;

    return(rv);
#else /* UNIX */
    int rv = 1;
    int i, outchars;
    unsigned char obuf[MAX(MB_LEN_MAX,32)];

     if(ucs < 0x80){
	obuf[0] = (unsigned char) ucs;
	outchars = 1;
    }
    else{
	outchars = wtomb((char *) obuf, ucs);
	if(outchars < 0){
	    outchars = 1;
	    obuf[0] = bad_char;		/* ??? */
	}
    }

    for(i = 0; i < outchars; i++)
      if(fputc(obuf[i], fp) == EOF){
	  rv = EOF;
	  break;
      }
        
    return(rv);
#endif /* UNIX */
}


int
our_stat(char *filename, struct stat *sbuf)
{
#ifdef _WINDOWS
    LPTSTR f = NULL;
    int    ret = -1;
    struct _stat s;

    f = utf8_to_lptstr((LPSTR) filename);
    if(f){
	ret = _tstat(f, &s);

	sbuf->st_dev = s.st_dev;
	sbuf->st_ino = s.st_ino;
	sbuf->st_mode = s.st_mode;
	sbuf->st_nlink = s.st_nlink;
	sbuf->st_uid = s.st_uid;
	sbuf->st_gid = s.st_gid;
	sbuf->st_rdev = s.st_rdev;
	sbuf->st_size = s.st_size;
	sbuf->st_atime = (time_t) s.st_atime;
	sbuf->st_mtime = (time_t) s.st_mtime;
	sbuf->st_ctime = (time_t) s.st_ctime;

	fs_give((void **) &f);
    }

    return ret;
#else /* UNIX */
    return(stat(fname_to_locale(filename), sbuf));
#endif /* UNIX */
}


int
our_lstat(char *filename, struct stat *sbuf)
{
#ifdef _WINDOWS
    assert(0);		/* lstat not used in Windows */
    return(-1);
#else /* UNIX */
    return(lstat(fname_to_locale(filename), sbuf));
#endif /* UNIX */
}


FILE *
our_fopen(char *path, char *mode)
{
#ifdef _WINDOWS
    LPTSTR p = NULL, m = NULL;
    FILE  *ret = NULL;
    char  *mode_with_ccs = NULL;
    char   buf[500];
    size_t len;

    if(mode && (*mode == 'r' || *mode == 'a')){
	char  *force_bom_check = ", ccs=UNICODE";

	if(strchr(mode, 'b'))
	  mode_with_ccs = mode;
	else{
	    /*
	     * The docs seem to say that we don't need the ccs parameter and
	     * if the file has a BOM at the beginning it will notice that and
	     * use it. However, we're not seeing that. Instead, what we see is
	     * that giving a parameter of UNICODE causes the desired behavior.
	     * This causes it to check for a BOM and if it finds one it uses it.
	     * If it doesn't find one, it treats the file as ANSI, which is what
	     * we want.
	     */
	    if((len = strlen(mode) + strlen(force_bom_check)) < sizeof(buf)){
		len = sizeof(buf)-1;
		mode_with_ccs = buf;
	    }
	    else
	      mode_with_ccs = (char *) MemAlloc((len+1) * sizeof(char));

	    if(mode_with_ccs)
	      snprintf(mode_with_ccs, len+1, "%s%s", mode, force_bom_check);
	    else
	      mode_with_ccs = mode;	/* can't happen */
	}
    }
    else if(mode && (*mode == 'w')){
	char  *force_utf8 = ", ccs=UTF-8";

	if(strchr(mode, 'b'))
	  mode_with_ccs = mode;
	else{
	    if((len = strlen(mode) + strlen(force_utf8)) < sizeof(buf)){
		len = sizeof(buf)-1;
		mode_with_ccs = buf;
	    }
	    else
	      mode_with_ccs = (char *) MemAlloc((len+1) * sizeof(char));

	    if(mode_with_ccs)
	      snprintf(mode_with_ccs, len+1, "%s%s", mode, force_utf8);
	    else
	      mode_with_ccs = mode;	/* can't happen */
	}
    }

    p = utf8_to_lptstr((LPSTR) path);

    if(p){
	m = utf8_to_lptstr((LPSTR) mode_with_ccs);
	if(m){
	    ret = _tfopen(p, m);
	    MemFree((void *) m);
	}

	fs_give((void **) &p);
    }

    if(mode_with_ccs && mode_with_ccs != buf && mode_with_ccs != mode)
      MemFree((void *) mode_with_ccs);

    return ret;
#else /* UNIX */
    return(fopen(fname_to_locale(path), mode));
#endif /* UNIX */
}


int
our_open(char *path, int flags, mode_t mode)
{
#ifdef _WINDOWS
    LPTSTR p = NULL;
    int ret = -1;

    /*
     * Setting the _O_WTEXT flag when opening a file for reading
     * will cause us to read the first few bytes to check for
     * a BOM and to translate from that encoding if we find it.
     * This only works with stream I/O, not low-level read/write.
     *
     * When opening for writing the flag _O_U8TEXT will cause
     * us to put a UTF-8 BOM at the start of the file.
     *
     * O_TEXT will cause LF -> CRLF on output, opposite on input
     * O_BINARY suppresses that.
     * _O_U8TEXT implies O_TEXT.
     */

    p = utf8_to_lptstr((LPSTR) path);

    if(p){
	ret = _topen(p, flags, mode);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(open(fname_to_locale(path), flags, mode));
#endif /* UNIX */
}


int
our_creat(char *path, mode_t mode)
{
#ifdef _WINDOWS
    LPTSTR p = NULL;
    int ret = -1;

    p = utf8_to_lptstr((LPSTR) path);

    if(p){
	ret = _tcreat(p, mode);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(creat(fname_to_locale(path), mode));
#endif /* UNIX */
}


int
our_mkdir(char *path, mode_t mode)
{
#ifdef _WINDOWS
    /* mode is a noop for _WINDOWS */
    LPTSTR p = NULL;
    int ret = -1;

    p = utf8_to_lptstr((LPSTR) path);

    if(p){
	ret = _tmkdir(p);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(mkdir(fname_to_locale(path), mode));
#endif /* UNIX */
}


int
our_rename(char *oldpath, char *newpath)
{
#ifdef _WINDOWS
    LPTSTR pold = NULL, pnew = NULL;
    int ret = -1;

    pold = utf8_to_lptstr((LPSTR) oldpath);
    pnew = utf8_to_lptstr((LPSTR) newpath);

    if(pold && pnew)
      ret = _trename(pold, pnew);

    if(pold)
      fs_give((void **) &pold);
    if(pnew)
      fs_give((void **) &pnew);

    return ret;
#else /* UNIX */
    char *p, *pold;
    size_t len;
    int ret = -1;

    p = fname_to_locale(oldpath);
    if(p){
	len = strlen(p);
	pold = (char *) fs_get((len+1) * sizeof(char));
	strncpy(pold, p, len+1);
	pold[len] = '\0';

	ret = rename(pold, fname_to_locale(newpath));
	fs_give((void **) &pold);
    }

    return ret;
#endif /* UNIX */
}


int
our_unlink(char *path)
{
#ifdef _WINDOWS
    LPTSTR p = NULL;
    int ret = -1;

    p = utf8_to_lptstr((LPSTR) path);

    if(p){
	ret = _tunlink(p);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(unlink(fname_to_locale(path)));
#endif /* UNIX */
}


int
our_link(char *oldpath, char *newpath)
{
#ifdef _WINDOWS
    assert(0);		/* link not used in Windows */
    return(-1);
#else /* UNIX */
    char *p, *pold;
    size_t len;
    int ret = -1;

    p = fname_to_locale(oldpath);
    if(p){
	len = strlen(p);
	pold = (char *) fs_get((len+1) * sizeof(char));
	strncpy(pold, p, len+1);
	pold[len] = '\0';

	ret = link(pold, fname_to_locale(newpath));
	fs_give((void **) &pold);
    }

    return ret;
#endif /* UNIX */
}


int
our_truncate(char *path, off_t size)
{
    int ret = -1;
#if defined(_WINDOWS) || !defined(HAVE_TRUNCATE)
    int fdes;
#endif

#ifdef _WINDOWS
    if((fdes = our_open(path, O_RDWR | O_CREAT | S_IREAD | S_IWRITE | _O_U8TEXT, 0600)) != -1){
	if(chsize(fdes, size) == 0)
	  ret = 0;

	close(fdes);
    }

#else /* UNIX */

#ifdef	HAVE_TRUNCATE
    ret = truncate(fname_to_locale(path), size);
#else	/* !HAVE_TRUNCATE */

    if((fdes = our_open(path, O_RDWR, 0600)) != -1){
	ret = chsize(fdes, size) ;

	if(close(fdes))
	    ret = -1;
    }
#endif /* !HAVE_TRUNCATE */
#endif /* UNIX */

    return ret;
}


int
our_chmod(char *path, mode_t mode)
{
#ifdef _WINDOWS
    LPTSTR p = NULL;
    int    ret = -1;

    p = utf8_to_lptstr((LPSTR) path);
    if(p){
	ret = _tchmod(p, mode);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(chmod(fname_to_locale(path), mode));
#endif /* UNIX */
}


int
our_chown(char *path, uid_t owner, gid_t group)
{
#ifdef _WINDOWS
    return 0;
#else /* UNIX */
    return(chown(fname_to_locale(path), owner, group));
#endif /* UNIX */
}


int
our_utime(char *path, struct utimbuf *buf)
{
#ifdef _WINDOWS
    LPTSTR p = NULL;
    int ret = -1;

    p = utf8_to_lptstr((LPSTR) path);

    if(p){
	ret = _tutime(p, buf);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(utime(fname_to_locale(path), buf));
#endif /* UNIX */
}

/* 
 * Return a malloc'd utf8-encoded char * of the provided environment 
 * variable. The env_variable argument is assumed not to be UTF-8.  Returns
 * NULL if no such environment variable.
 *
 * We'll pretty much swap out getenv's where convenient.  Windows pretty
 * much doesn't want to do getenv once we do unicode
 */
char *
our_getenv(char *env_variable)
{
#ifdef _WINDOWS
    TCHAR lptstr_env_variable[MAXPATH+1], *p;
    int i;

    for(i = 0; env_variable[i] && i < MAXPATH; i++)
      lptstr_env_variable[i] = env_variable[i];
    lptstr_env_variable[i] = '\0';
    if(p = _tgetenv(lptstr_env_variable))
      return(lptstr_to_utf8(p));
    else
      return(NULL);
#else /* !_WINDOWS */
    char *p, *utf8_p, *env_cpy;
    size_t len;
    if((p = getenv(env_variable)) != NULL){
	/* all this when what we want is a cpystr */
	utf8_p = fname_to_utf8(p);
	len = strlen(utf8_p);
	env_cpy = (char *)fs_get((len+1)*sizeof(char));
	strncpy(env_cpy, utf8_p, len+1);
	env_cpy[len] = '\0';

	return(env_cpy);
    }
    else
      return(NULL);
#endif /* !_WINDOWS */
}


int
our_access(char *path, int mode)
{
#ifdef _WINDOWS
    LPTSTR p = NULL;
    int    ret = -1;

    p = utf8_to_lptstr((LPSTR) path);
    if(p){
	ret = _taccess(p, mode);
	fs_give((void **) &p);
    }

    return ret;
#else /* UNIX */
    return(access(fname_to_locale(path), mode));
#endif /* UNIX */
}


/*
 * Fgets that doesn't do any character encoding translation or any
 * of that Windows stuff.
 */
char *
fgets_binary(char *s, int size, FILE *fp)
{
#ifdef _WINDOWS
    char *p;
    char c;
    int r;

    /*
     * Use fread low-level input instead of fgets.
     * Maybe if we understood better we wouldn't need this.
     */
    if(!s)
      return s;

    p = s;
    while(p-s < size-1 && (r=fread(&c, sizeof(c), (size_t) 1, fp)) == 1 && c != '\n')
      *p++ = c;

    if(p-s < size-1 && r == 1){
	/* must have gotten to end of line */
	*p++ = '\n';
    }

    *p = '\0';
    return(s);

#else /* UNIX */
    return(fgets(s, size, fp));
#endif /* UNIX */
}