mpdm_regex() always return an array if matching an array of regexes.
[mpdm.git] / mpdm_f.c
blobbebe3e644f46ede8ee3389acb93cef1a2973c90e
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpdm_f.c - File management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #ifdef CONFOPT_CANONICALIZE_FILE_NAME
29 #define _GNU_SOURCE
30 #endif
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <wchar.h>
37 #ifdef CONFOPT_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #ifdef CONFOPT_GLOB_H
42 #include <glob.h>
43 #endif
45 #ifdef CONFOPT_WIN32
47 #include <windows.h>
48 #include <commctrl.h>
49 #include <shlobj.h>
51 #endif
53 #ifdef CONFOPT_SYS_TYPES_H
54 #include <sys/types.h>
55 #endif
57 #ifdef CONFOPT_SYS_WAIT_H
58 #include <sys/wait.h>
59 #endif
61 #ifdef CONFOPT_SYS_STAT_H
62 #include <sys/stat.h>
63 #endif
65 #ifdef CONFOPT_PWD_H
66 #include <pwd.h>
67 #endif
69 #include "mpdm.h"
71 #ifdef CONFOPT_ICONV
72 #include <iconv.h>
73 #endif
75 /* file structure */
76 struct mpdm_file {
77 FILE *in;
78 FILE *out;
80 wchar_t * (* f_read) (struct mpdm_file *, int *);
81 int (* f_write)(struct mpdm_file *, const wchar_t *);
83 #ifdef CONFOPT_ICONV
84 iconv_t ic_enc;
85 iconv_t ic_dec;
86 #endif /* CONFOPT_ICONV */
88 #ifdef CONFOPT_WIN32
89 HANDLE hin;
90 HANDLE hout;
91 #endif /* CONFOPT_WIN32 */
94 #include <errno.h>
95 extern int errno;
98 /** code **/
101 static void store_syserr(void)
102 /* stores the system error inside the global ERRNO */
104 mpdm_hset_s(mpdm_root(), L"ERRNO", MPDM_MBS(strerror(errno)));
108 static int get_char(struct mpdm_file *f)
109 /* reads a character from a file structure */
111 int c = EOF;
113 #ifdef CONFOPT_WIN32
115 if (f->hin != NULL) {
116 char tmp;
117 DWORD n;
119 if (ReadFile(f->hin, &tmp, 1, &n, NULL) && n > 0)
120 c = (int) tmp;
123 #endif /* CONFOPT_WIN32 */
125 if (f->in != NULL) {
126 /* read (converting to positive if needed) */
127 if ((c = fgetc(f->in)) < 0 && !feof(f->in))
128 c += 256;
131 return c;
135 static int put_buf(const char *ptr, int s, struct mpdm_file *f)
136 /* writes s bytes in the buffer in ptr to f */
138 #ifdef CONFOPT_WIN32
140 if (f->hout != NULL) {
141 DWORD n;
143 if (WriteFile(f->hout, ptr, s, &n, NULL) && n > 0)
144 s = n;
146 else
147 #endif /* CONFOPT_WIN32 */
149 if (f->out != NULL)
150 s = fwrite(ptr, s, 1, f->out);
152 return s;
156 static int put_char(int c, struct mpdm_file *f)
157 /* writes a character in a file structure */
159 char tmp = c;
161 if (put_buf(&tmp, 1, f) != 1)
162 c = EOF;
164 return c;
168 static wchar_t *read_mbs(struct mpdm_file *f, int *s)
169 /* reads a multibyte string from a mpdm_file into a dynamic string */
171 wchar_t *ptr = NULL;
172 char *auxptr = NULL;
173 char tmp[100];
174 int c, i = 0, n = 0;
176 while ((c = get_char(f)) != EOF) {
177 tmp[i++] = c;
179 if (c == '\n')
180 break;
182 if (i == sizeof(tmp) - 1) {
183 /* out of space; start allocating */
184 if ((auxptr = mpdm_poke(auxptr, &n, tmp, i, sizeof(char))) == NULL)
185 return NULL;
187 i = 0;
191 /* is there something to return? */
192 if (i || n) {
193 /* NULL-terminate */
194 tmp[i++] = '\0';
196 if (n) {
197 /* auxiliary space used; concat all */
198 if ((auxptr = mpdm_poke(auxptr, &n, tmp, i, sizeof(char))) == NULL)
199 return NULL;
201 /* do the conversion */
202 ptr = mpdm_mbstowcs(auxptr, s, -1);
204 free(auxptr);
206 else
207 ptr = mpdm_mbstowcs(tmp, s, -1);
210 return ptr;
214 static int write_wcs(struct mpdm_file *f, const wchar_t * str)
215 /* writes a wide string to an struct mpdm_file */
217 int s;
218 char *ptr;
220 ptr = mpdm_wcstombs(str, &s);
221 s = put_buf(ptr, s, f);
222 free(ptr);
224 return s;
228 #ifdef CONFOPT_ICONV
230 static wchar_t *read_iconv(struct mpdm_file *f, int *s)
231 /* reads a multibyte string transforming with iconv */
233 char tmp[128];
234 wchar_t *ptr = NULL;
235 int c, i;
236 wchar_t wc;
238 *s = i = 0;
240 /* resets the decoder */
241 iconv(f->ic_dec, NULL, NULL, NULL, NULL);
243 while ((c = get_char(f)) != EOF) {
244 size_t il, ol;
245 char *iptr, *optr;
247 tmp[i++] = c;
249 /* too big? shouldn't happen */
250 if (i == sizeof(tmp))
251 break;
253 il = i;
254 iptr = tmp;
255 ol = sizeof(wchar_t);
256 optr = (char *) &wc;
258 /* write to file */
259 if (iconv(f->ic_dec, &iptr, &il, &optr, &ol) == (size_t) -1) {
260 /* found incomplete multibyte character */
261 if (errno == EINVAL)
262 continue;
264 /* otherwise, return '?' */
265 wc = L'?';
268 i = 0;
270 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
271 break;
273 /* if it's an end of line, finish */
274 if (wc == L'\n')
275 break;
278 if (ptr != NULL) {
279 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
280 (*s)--;
283 return ptr;
287 static int write_iconv(struct mpdm_file *f, const wchar_t * str)
288 /* writes a wide string to a stream using iconv */
290 char tmp[128];
291 int cnt = 0;
293 /* resets the encoder */
294 iconv(f->ic_enc, NULL, NULL, NULL, NULL);
296 /* convert char by char */
297 for (; *str != L'\0'; str++) {
298 size_t il, ol;
299 char *iptr, *optr;
300 int n;
302 il = sizeof(wchar_t);
303 iptr = (char *) str;
304 ol = sizeof(tmp);
305 optr = tmp;
307 /* write to file */
308 if (iconv(f->ic_enc, &iptr, &il, &optr, &ol) == (size_t) -1) {
309 /* error converting; convert a '?' instead */
310 wchar_t q = L'?';
312 il = sizeof(wchar_t);
313 iptr = (char *) &q;
314 ol = sizeof(tmp);
315 optr = tmp;
317 iconv(f->ic_enc, &iptr, &il, &optr, &ol);
320 for (n = 0; n < (int)(sizeof(tmp) - ol); n++, cnt++) {
321 if (put_char(tmp[n], f) == EOF)
322 return -1;
326 return cnt;
330 #endif /* CONFOPT_ICONV */
332 #define UTF8_BYTE() if((c = get_char(f)) == EOF) break
334 static wchar_t *read_utf8(struct mpdm_file *f, int *s)
335 /* utf8 reader */
337 wchar_t *ptr = NULL;
338 wchar_t wc;
339 int c;
341 *s = 0;
343 for (;;) {
344 wc = L'\0';
346 UTF8_BYTE();
348 if ((c & 0x80) == 0)
349 wc = c;
350 else
351 if ((c & 0xe0) == 0xe0) {
352 wc = (c & 0x1f) << 12;
353 UTF8_BYTE();
354 wc |= (c & 0x3f) << 6;
355 UTF8_BYTE();
356 wc |= (c & 0x3f);
358 else {
359 wc = (c & 0x3f) << 6;
360 UTF8_BYTE();
361 wc |= (c & 0x3f);
364 /* store */
365 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
366 break;
368 /* if it's an end of line, finish */
369 if (wc == L'\n')
370 break;
373 if (ptr != NULL) {
374 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
375 (*s)--;
378 return ptr;
382 static int write_utf8(struct mpdm_file *f, const wchar_t * str)
383 /* utf8 writer */
385 int cnt = 0;
386 wchar_t wc;
388 /* convert char by char */
389 for (; (wc = *str) != L'\0'; str++) {
390 if (wc < 0x80)
391 put_char((int) wc, f);
392 else
393 if (wc < 0x800) {
394 put_char((int) (0xc0 | (wc >> 6)), f);
395 put_char((int) (0x80 | (wc & 0x3f)), f);
396 cnt++;
398 else {
399 put_char((int) (0xe0 | (wc >> 12)), f);
400 put_char((int) (0x80 | ((wc >> 6) & 0x3f)), f);
401 put_char((int) (0x80 | (wc & 0x3f)), f);
402 cnt += 2;
405 cnt++;
408 return cnt;
412 static wchar_t *read_utf8_bom(struct mpdm_file *f, int *s)
413 /* utf-8 reader with BOM detection */
415 wchar_t *enc = L"";
417 f->f_read = NULL;
419 /* autodetection */
420 if (get_char(f) == 0xef && get_char(f) == 0xbb && get_char(f) == 0xbf)
421 enc = L"utf-8bom";
422 else {
423 enc = L"utf-8";
424 fseek(f->in, 0, SEEK_SET);
427 mpdm_hset_s(mpdm_root(), L"DETECTED_ENCODING", MPDM_LS(enc));
429 /* we're utf-8 from now on */
430 f->f_read = read_utf8;
432 return f->f_read(f, s);
436 static int write_utf8_bom(struct mpdm_file *f, const wchar_t * str)
437 /* utf-8 writer with BOM */
439 /* store the BOM */
440 put_char(0xef, f);
441 put_char(0xbb, f);
442 put_char(0xbf, f);
444 /* we're utf-8 from now on */
445 f->f_write = write_utf8;
447 return f->f_write(f, str);
451 static wchar_t *read_iso8859_1(struct mpdm_file *f, int *s)
452 /* iso8859-1 reader */
454 wchar_t *ptr = NULL;
455 wchar_t wc;
456 int c;
458 *s = 0;
460 while ((c = get_char(f)) != EOF) {
461 wc = c;
463 /* store */
464 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
465 break;
467 /* if it's an end of line, finish */
468 if (wc == L'\n')
469 break;
472 if (ptr != NULL) {
473 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
474 (*s)--;
477 return ptr;
481 static int write_iso8859_1(struct mpdm_file *f, const wchar_t * str)
482 /* iso8859-1 writer */
484 int cnt = 0;
485 wchar_t wc;
487 /* convert char by char */
488 for (; (wc = *str) != L'\0'; str++)
489 put_char(wc <= 0xff ? (int) wc : '?', f);
491 return cnt;
495 static wchar_t *read_utf16ae(struct mpdm_file *f, int *s, int le)
496 /* utf16 reader, ANY ending */
498 wchar_t *ptr = NULL;
499 wchar_t wc;
500 int c1, c2;
502 *s = 0;
504 for (;;) {
505 wc = L'\0';
507 if ((c1 = get_char(f)) == EOF)
508 break;
510 if ((c2 = get_char(f)) == EOF)
511 break;
513 if (le)
514 wc = c1 | (c2 << 8);
515 else
516 wc = c2 | (c1 << 8);
518 /* store */
519 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
520 break;
522 /* if it's an end of line, finish */
523 if (wc == L'\n')
524 break;
527 if (ptr != NULL) {
528 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
529 (*s)--;
532 return ptr;
536 static int write_utf16ae(struct mpdm_file *f, const wchar_t * str, int le)
537 /* utf16 writer, ANY ending */
539 int cnt = 0;
540 wchar_t wc;
542 /* convert char by char */
543 for (; (wc = *str) != L'\0'; str++) {
545 if (le) {
546 put_char(wc & 0xff, f);
547 put_char((wc & 0xff00) >> 8, f);
549 else {
550 put_char((wc & 0xff00) >> 8, f);
551 put_char(wc & 0xff, f);
555 return cnt;
559 static wchar_t *read_utf16le(struct mpdm_file *f, int *s)
561 return read_utf16ae(f, s, 1);
565 static int write_utf16le(struct mpdm_file *f, const wchar_t * str)
567 return write_utf16ae(f, str, 1);
571 static wchar_t *read_utf16be(struct mpdm_file *f, int *s)
573 return read_utf16ae(f, s, 0);
577 static int write_utf16be(struct mpdm_file *f, const wchar_t * str)
579 return write_utf16ae(f, str, 0);
583 static wchar_t *read_utf16(struct mpdm_file *f, int *s)
585 int c1, c2;
586 wchar_t *enc = L"utf-16le";
588 /* assume little-endian */
589 f->f_read = read_utf16le;
591 /* autodetection */
592 c1 = get_char(f);
593 c2 = get_char(f);
595 if (c1 == 0xfe && c2 == 0xff) {
596 enc = L"utf-16be";
597 f->f_read = read_utf16be;
599 else
600 if (c1 != 0xff || c2 != 0xfe) {
601 /* no BOM; rewind and hope */
602 fseek(f->in, 0, SEEK_SET);
605 mpdm_hset_s(mpdm_root(), L"DETECTED_ENCODING", MPDM_LS(enc));
607 return f->f_read(f, s);
611 static int write_utf16le_bom(struct mpdm_file *f, const wchar_t * str)
613 /* store the LE signature */
614 put_char(0xff, f);
615 put_char(0xfe, f);
617 /* we're 16le from now on */
618 f->f_write = write_utf16le;
620 return f->f_write(f, str);
624 static int write_utf16be_bom(struct mpdm_file *f, const wchar_t * str)
626 /* store the BE signature */
627 put_char(0xfe, f);
628 put_char(0xff, f);
630 /* we're 16be from now on */
631 f->f_write = write_utf16be;
633 return f->f_write(f, str);
637 static wchar_t *read_utf32ae(struct mpdm_file *f, int *s, int le)
638 /* utf32 reader, ANY ending */
640 wchar_t *ptr = NULL;
641 wchar_t wc;
642 int c1, c2, c3, c4;
644 *s = 0;
646 for (;;) {
647 wc = L'\0';
649 if ((c1 = get_char(f)) == EOF)
650 break;
652 if ((c2 = get_char(f)) == EOF)
653 break;
655 if ((c3 = get_char(f)) == EOF)
656 break;
658 if ((c4 = get_char(f)) == EOF)
659 break;
661 if (le)
662 wc = c1 | (c2 << 8) | (c3 << 16) | (c4 << 24);
663 else
664 wc = c4 | (c3 << 8) | (c2 << 16) | (c1 << 24);
666 /* store */
667 if ((ptr = mpdm_poke(ptr, s, &wc, 1, sizeof(wchar_t))) == NULL)
668 break;
670 /* if it's an end of line, finish */
671 if (wc == L'\n')
672 break;
675 if (ptr != NULL) {
676 ptr = mpdm_poke(ptr, s, L"", 1, sizeof(wchar_t));
677 (*s)--;
680 return ptr;
684 static int write_utf32ae(struct mpdm_file *f, const wchar_t * str, int le)
685 /* utf32 writer, ANY ending */
687 int cnt = 0;
688 wchar_t wc;
690 /* convert char by char */
691 for (; (wc = *str) != L'\0'; str++) {
693 if (le) {
694 put_char((wc & 0x000000ff), f);
695 put_char((wc & 0x0000ff00) >> 8, f);
696 put_char((wc & 0x00ff0000) >> 16, f);
697 put_char((wc & 0xff000000) >> 24, f);
699 else {
700 put_char((wc & 0xff000000) >> 24, f);
701 put_char((wc & 0x00ff0000) >> 16, f);
702 put_char((wc & 0x0000ff00) >> 8, f);
703 put_char((wc & 0x000000ff), f);
707 return cnt;
711 static wchar_t *read_utf32le(struct mpdm_file *f, int *s)
713 return read_utf32ae(f, s, 1);
717 static int write_utf32le(struct mpdm_file *f, const wchar_t * str)
719 return write_utf32ae(f, str, 1);
723 static wchar_t *read_utf32be(struct mpdm_file *f, int *s)
725 return read_utf32ae(f, s, 0);
729 static int write_utf32be(struct mpdm_file *f, const wchar_t * str)
731 return write_utf32ae(f, str, 0);
735 static wchar_t *read_utf32(struct mpdm_file *f, int *s)
737 int c1, c2, c3, c4;
738 wchar_t *enc = L"utf-32le";
740 f->f_read = read_utf32le;
742 /* autodetection */
743 c1 = get_char(f);
744 c2 = get_char(f);
745 c3 = get_char(f);
746 c4 = get_char(f);
748 if (c1 == 0 && c2 == 0 && c3 == 0xfe && c4 == 0xff) {
749 enc = L"utf-32be";
750 f->f_read = read_utf32be;
752 if (c1 != 0xff || c2 != 0xfe || c3 != 0 || c4 != 0) {
753 /* no BOM; assume le and hope */
754 fseek(f->in, 0, SEEK_SET);
757 mpdm_hset_s(mpdm_root(), L"DETECTED_ENCODING", MPDM_LS(enc));
759 return f->f_read(f, s);
763 static int write_utf32le_bom(struct mpdm_file *f, const wchar_t * str)
765 /* store the LE signature */
766 put_char(0xff, f);
767 put_char(0xfe, f);
768 put_char(0, f);
769 put_char(0, f);
771 /* we're 32le from now on */
772 f->f_write = write_utf32le;
774 return f->f_write(f, str);
778 static int write_utf32be_bom(struct mpdm_file *f, const wchar_t * str)
780 /* store the BE signature */
781 put_char(0, f);
782 put_char(0, f);
783 put_char(0xfe, f);
784 put_char(0xff, f);
786 /* we're 32be from now on */
787 f->f_write = write_utf32be;
789 return f->f_write(f, str);
793 static wchar_t *read_auto(struct mpdm_file *f, int *s)
794 /* autodetects different encodings based on the BOM */
796 wchar_t *enc = L"";
798 /* by default, multibyte reading */
799 f->f_read = read_mbs;
801 /* ensure seeking is possible */
802 if (f->in != NULL && fseek(f->in, 0, SEEK_CUR) != -1) {
803 int c;
805 c = get_char(f);
807 if (c == 0xff) {
808 /* can be utf32le or utf16le */
809 if (get_char(f) == 0xfe) {
810 /* if next 2 chars are 0x00, it's utf32; otherwise utf16 */
811 if (get_char(f) == 0x00 && get_char(f) == 0x00) {
812 enc = L"utf-32le";
813 f->f_read = read_utf32le;
814 goto got_encoding;
816 else {
817 /* rewind to 3rd character */
818 fseek(f->in, 2, SEEK_SET);
820 enc = L"utf-16le";
821 f->f_read = read_utf16le;
822 goto got_encoding;
826 else
827 if (c == 0x00) {
828 /* can be utf32be */
829 if (get_char(f) == 0x00 && get_char(f) == 0xfe && get_char(f) == 0xff) {
830 enc = L"utf-32be";
831 f->f_read = read_utf32be;
832 goto got_encoding;
835 else
836 if (c == 0xfe) {
837 /* can be utf16be */
838 if (get_char(f) == 0xff) {
839 enc = L"utf-16be";
840 f->f_read = read_utf16be;
841 goto got_encoding;
844 else
845 if (c == 0xef) {
846 /* can be utf8 with BOM */
847 if (get_char(f) == 0xbb && get_char(f) == 0xbf) {
848 enc = L"utf-8bom";
849 f->f_read = read_utf8;
850 goto got_encoding;
853 else {
854 /* try if a first bunch of chars are valid UTF-8 */
855 int p = c;
856 int n = 10000;
857 int u = 0;
859 while (--n && (c = get_char(f)) != EOF) {
860 if ((c & 0xc0) == 0x80) {
861 if ((p & 0xc0) == 0xc0)
862 u++;
863 else
864 if ((p & 0x80) == 0x00) {
865 u = -1;
866 break;
869 else
870 if ((p & 0xc0) == 0xc0) {
871 u = -1;
872 break;
875 p = c;
878 if (u < 0) {
879 /* invalid utf-8; fall back to 8bit */
880 enc = L"8bit";
881 f->f_read = read_iso8859_1;
883 else
884 if (u > 0) {
885 /* utf-8 sequences found */
886 enc = L"utf-8";
887 f->f_read = read_utf8;
890 /* 7 bit ASCII: do nothing */
893 /* none of the above; restart */
894 fseek(f->in, 0, SEEK_SET);
897 got_encoding:
898 mpdm_hset_s(mpdm_root(), L"DETECTED_ENCODING", MPDM_LS(enc));
900 return f->f_read(f, s);
904 static mpdm_t new_mpdm_file(void)
905 /* creates a new file value */
907 mpdm_t v = NULL;
908 struct mpdm_file *fs;
909 mpdm_t e;
911 if ((fs = malloc(sizeof(struct mpdm_file))) == NULL)
912 return NULL;
914 memset(fs, '\0', sizeof(struct mpdm_file));
916 /* default I/O functions */
917 fs->f_read = read_auto;
918 fs->f_write = write_wcs;
920 #ifdef CONFOPT_ICONV
921 /* no iconv encodings by default */
922 fs->ic_enc = fs->ic_dec = (iconv_t) -1;
923 #endif
925 if ((v = mpdm_new(MPDM_FILE | MPDM_FREE, fs, sizeof(struct mpdm_file))) == NULL) {
926 free(fs);
927 return NULL;
930 e = mpdm_hget_s(mpdm_root(), L"ENCODING");
932 if (mpdm_size(e) == 0)
933 e = mpdm_hget_s(mpdm_root(), L"TEMP_ENCODING");
935 if (mpdm_size(e)) {
937 wchar_t *enc = mpdm_string(e);
939 if (wcscmp(enc, L"utf-8") == 0) {
940 fs->f_read = read_utf8_bom;
941 fs->f_write = write_utf8;
943 else
944 if (wcscmp(enc, L"utf-8bom") == 0) {
945 fs->f_read = read_utf8_bom;
946 fs->f_write = write_utf8_bom;
948 else
949 if (wcscmp(enc, L"iso8859-1") == 0 ||
950 wcscmp(enc, L"8bit") == 0) {
951 fs->f_read = read_iso8859_1;
952 fs->f_write = write_iso8859_1;
954 else
955 if (wcscmp(enc, L"utf-16le") == 0) {
956 fs->f_read = read_utf16le;
957 fs->f_write = write_utf16le_bom;
959 else
960 if (wcscmp(enc, L"utf-16be") == 0) {
961 fs->f_read = read_utf16be;
962 fs->f_write = write_utf16be_bom;
964 else
965 if (wcscmp(enc, L"utf-16") == 0) {
966 fs->f_read = read_utf16;
967 fs->f_write = write_utf16le_bom;
969 else
970 if (wcscmp(enc, L"utf-32le") == 0) {
971 fs->f_read = read_utf32le;
972 fs->f_write = write_utf32le_bom;
974 else
975 if (wcscmp(enc, L"utf-32be") == 0) {
976 fs->f_read = read_utf32be;
977 fs->f_write = write_utf32be_bom;
979 else
980 if (wcscmp(enc, L"utf-32") == 0) {
981 fs->f_read = read_utf32;
982 fs->f_write = write_utf32le_bom;
984 else {
985 #ifdef CONFOPT_ICONV
986 mpdm_t cs = MPDM_2MBS(e->data);
988 if ((fs->ic_enc = iconv_open((char *) cs->data, "WCHAR_T")) != (iconv_t) -1 &&
989 (fs->ic_dec = iconv_open("WCHAR_T", (char *) cs->data)) != (iconv_t) -1) {
991 fs->f_read = read_iconv;
992 fs->f_write = write_iconv;
994 #endif /* CONFOPT_ICONV */
997 mpdm_hset_s(mpdm_root(), L"TEMP_ENCODING", NULL);
1000 return v;
1004 static void destroy_mpdm_file(mpdm_t v)
1005 /* destroys and file value */
1007 struct mpdm_file *fs = (struct mpdm_file *)v->data;
1009 if (fs != NULL) {
1010 #ifdef CONFOPT_ICONV
1011 if (fs->ic_enc != (iconv_t) - 1) {
1012 iconv_close(fs->ic_enc);
1013 fs->ic_enc = (iconv_t) - 1;
1016 if (fs->ic_dec != (iconv_t) - 1) {
1017 iconv_close(fs->ic_dec);
1018 fs->ic_dec = (iconv_t) - 1;
1020 #endif
1022 free(fs);
1023 v->data = NULL;
1028 /** interface **/
1030 wchar_t *mpdm_read_mbs(FILE * f, int *s)
1031 /* reads a multibyte string from a stream into a dynamic string */
1033 struct mpdm_file fs;
1035 /* reset the structure */
1036 memset(&fs, '\0', sizeof(fs));
1037 fs.in = f;
1039 return read_mbs(&fs, s);
1043 int mpdm_write_wcs(FILE * f, const wchar_t * str)
1044 /* writes a wide string to a stream */
1046 struct mpdm_file fs;
1048 /* reset the structure */
1049 memset(&fs, '\0', sizeof(fs));
1050 fs.out = f;
1052 return write_wcs(&fs, str);
1056 mpdm_t mpdm_new_f(FILE * f)
1057 /* creates a new file value from a FILE * */
1059 mpdm_t v = NULL;
1061 if (f == NULL)
1062 return NULL;
1064 if ((v = new_mpdm_file()) != NULL) {
1065 struct mpdm_file *fs = (struct mpdm_file *)v->data;
1066 fs->in = fs->out = f;
1069 return v;
1074 * mpdm_open - Opens a file.
1075 * @filename: the file name
1076 * @mode: an fopen-like mode string
1078 * Opens a file. If @filename can be open in the specified @mode, an
1079 * mpdm_t value will be returned containing the file descriptor, or NULL
1080 * otherwise.
1082 * If the file is open for reading, some charset detection methods are
1083 * used. If any of them is successful, its name is stored in the
1084 * DETECTED_ENCODING element of the mpdm_root() hash. This value is
1085 * suitable to be copied over ENCODING or TEMP_ENCODING.
1087 * If the file is open for writing, the encoding to be used is read from
1088 * the ENCODING element of mpdm_root() and, if not set, from the
1089 * TEMP_ENCODING one. The latter will always be deleted afterwards.
1090 * [File Management]
1092 mpdm_t mpdm_open(const mpdm_t filename, const mpdm_t mode)
1094 FILE *f;
1095 mpdm_t fn;
1096 mpdm_t m;
1098 if (filename == NULL || mode == NULL)
1099 return NULL;
1101 /* convert to mbs,s */
1102 fn = mpdm_ref(MPDM_2MBS(filename->data));
1103 m = mpdm_ref(MPDM_2MBS(mode->data));
1105 if ((f = fopen((char *) fn->data, (char *) m->data)) == NULL)
1106 store_syserr();
1107 else {
1108 #if defined(CONFOPT_SYS_STAT_H) && defined(S_ISDIR) && defined(EISDIR)
1109 struct stat s;
1111 /* test if the open file is a directory */
1112 if (fstat(fileno(f), &s) != -1 && S_ISDIR(s.st_mode)) {
1113 /* it's a directory; fail */
1114 errno = EISDIR;
1115 store_syserr();
1116 fclose(f);
1117 f = NULL;
1119 #endif
1122 mpdm_unref(m);
1123 mpdm_unref(fn);
1125 return MPDM_F(f);
1130 * mpdm_close - Closes a file descriptor.
1131 * @fd: the value containing the file descriptor
1133 * Closes the file descriptor.
1134 * [File Management]
1136 mpdm_t mpdm_close(mpdm_t fd)
1138 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1140 if ((fd->flags & MPDM_FILE) && fs != NULL) {
1141 if (fs->in != NULL)
1142 fclose(fs->in);
1144 if (fs->out != fs->in && fs->out != NULL)
1145 fclose(fs->out);
1147 destroy_mpdm_file(fd);
1150 return NULL;
1155 * mpdm_read - Reads a line from a file descriptor.
1156 * @fd: the value containing the file descriptor
1158 * Reads a line from @fd. Returns the line, or NULL on EOF.
1159 * [File Management]
1160 * [Character Set Conversion]
1162 mpdm_t mpdm_read(const mpdm_t fd)
1164 mpdm_t v = NULL;
1165 wchar_t *ptr;
1166 int s;
1167 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1169 if (fs == NULL)
1170 return NULL;
1172 ptr = fs->f_read(fs, &s);
1174 if (ptr != NULL)
1175 v = MPDM_ENS(ptr, s);
1177 return v;
1181 mpdm_t mpdm_getchar(const mpdm_t fd)
1183 int c;
1184 wchar_t tmp[2];
1185 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1187 if (fs == NULL || (c = get_char(fs)) == EOF)
1188 return NULL;
1190 /* get the char as-is */
1191 tmp[0] = (wchar_t) c;
1192 tmp[1] = L'\0';
1194 return MPDM_S(tmp);
1198 mpdm_t mpdm_putchar(const mpdm_t fd, const mpdm_t c)
1200 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1201 const wchar_t *ptr = mpdm_string(c);
1203 if (fs == NULL || put_char(*ptr, fs) == -1)
1204 return NULL;
1206 return c;
1211 * mpdm_write - Writes a value into a file.
1212 * @fd: the file descriptor.
1213 * @v: the value to be written.
1215 * Writes the @v string value into @fd, using the current encoding.
1216 * [File Management]
1217 * [Character Set Conversion]
1219 int mpdm_write(const mpdm_t fd, const mpdm_t v)
1221 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1222 int ret = -1;
1224 if (fs == NULL)
1225 return -1;
1227 ret = fs->f_write(fs, mpdm_string(v));
1229 return ret;
1233 int mpdm_fseek(const mpdm_t fd, long offset, int whence)
1235 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1237 return fseek(fs->in, offset, whence);
1241 long mpdm_ftell(const mpdm_t fd)
1243 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1245 return ftell(fs->in);
1249 FILE * mpdm_get_filehandle(const mpdm_t fd)
1251 FILE * f = NULL;
1253 if (fd->flags & MPDM_FILE) {
1254 struct mpdm_file *fs = (struct mpdm_file *)fd->data;
1255 f = fs->in;
1258 return f;
1263 mpdm_t mpdm_bread(mpdm_t fd, int size)
1268 int mpdm_bwrite(mpdm_tfd, mpdm_t v, int size)
1274 static mpdm_t embedded_encodings(void)
1276 mpdm_t e;
1277 wchar_t *e2e[] = {
1278 L"utf-8", L"utf-8",
1279 L"utf8", NULL,
1280 L"iso8859-1", L"iso8859-1",
1281 L"iso-8859-1", NULL,
1282 L"8bit", NULL,
1283 L"latin1", NULL,
1284 L"latin-1", NULL,
1285 L"utf-16le", L"utf-16le",
1286 L"utf16le", NULL,
1287 L"ucs-2le", NULL,
1288 L"utf-16be", L"utf-16be",
1289 L"utf16be", NULL,
1290 L"ucs-2be", NULL,
1291 L"utf-16", L"utf-16",
1292 L"utf16", NULL,
1293 L"ucs-2", NULL,
1294 L"ucs2", NULL,
1295 L"utf-32le", L"utf-32le",
1296 L"utf32le", NULL,
1297 L"ucs-4le", NULL,
1298 L"utf-32be", L"utf-32be",
1299 L"utf32be", NULL,
1300 L"ucs-4be", NULL,
1301 L"utf-32", L"utf-32",
1302 L"utf32", NULL,
1303 L"ucs-4", NULL,
1304 L"ucs4", NULL,
1305 L"utf-8bom", L"utf-8bom",
1306 L"utf8bom", NULL,
1307 NULL, NULL
1310 if ((e = mpdm_hget_s(mpdm_root(), L"EMBEDDED_ENCODINGS")) == NULL) {
1311 int n;
1312 mpdm_t p = NULL;
1314 e = MPDM_H(0);
1316 for (n = 0; e2e[n] != NULL; n += 2) {
1317 mpdm_t v = MPDM_S(e2e[n]);
1319 if (e2e[n + 1] != NULL)
1320 p = MPDM_S(e2e[n + 1]);
1322 mpdm_hset(e, v, p);
1323 mpdm_hset(e, mpdm_ulc(v, 1), p);
1326 mpdm_hset_s(mpdm_root(), L"EMBEDDED_ENCODINGS", e);
1329 return e;
1334 * mpdm_encoding - Sets the current charset encoding for files.
1335 * @charset: the charset name.
1337 * Sets the current charset encoding for files. Future opened
1338 * files will be assumed to be encoded with @charset, which can
1339 * be any of the supported charset names (utf-8, iso-8859-1, etc.),
1340 * and converted on each read / write. If charset is NULL, it
1341 * is reverted to default charset conversion (i.e. the one defined
1342 * in the locale).
1344 * This function stores the @charset value into the ENCODING item
1345 * of the mpdm_root() hash.
1347 * Returns a negative number if @charset is unsupported, or zero
1348 * if no errors were found.
1349 * [File Management]
1350 * [Character Set Conversion]
1352 int mpdm_encoding(mpdm_t charset)
1354 int ret = -1;
1355 mpdm_t e = embedded_encodings();
1356 mpdm_t v = NULL;
1358 /* NULL encoding? done */
1359 if (mpdm_size(charset) == 0) {
1360 mpdm_hset_s(mpdm_root(), L"ENCODING", NULL);
1361 return 0;
1364 #ifdef CONFOPT_ICONV
1366 iconv_t ic;
1367 mpdm_t cs = mpdm_ref(MPDM_2MBS(charset->data));
1369 /* tries to create an iconv encoder and decoder for this charset */
1371 if ((ic = iconv_open("WCHAR_T", (char *) cs->data)) == (iconv_t) - 1)
1372 ret = -1;
1373 else {
1374 iconv_close(ic);
1376 if ((ic = iconv_open((char *) cs->data, "WCHAR_T")) == (iconv_t) - 1)
1377 ret = -2;
1378 else {
1379 iconv_close(ic);
1381 /* got a valid encoding */
1382 v = charset;
1383 ret = 0;
1387 mpdm_unref(cs);
1389 #endif /* CONFOPT_ICONV */
1391 if (ret != 0 && (v = mpdm_hget(e, charset)) != NULL)
1392 ret = 0;
1394 if (ret == 0)
1395 mpdm_hset_s(mpdm_root(), L"ENCODING", v);
1397 return ret;
1402 * mpdm_unlink - Deletes a file.
1403 * @filename: file name to be deleted
1405 * Deletes a file.
1406 * [File Management]
1408 int mpdm_unlink(const mpdm_t filename)
1410 int ret;
1411 mpdm_t fn;
1413 /* convert to mbs */
1414 fn = mpdm_ref(MPDM_2MBS(filename->data));
1416 if ((ret = unlink((char *) fn->data)) == -1)
1417 store_syserr();
1419 mpdm_unref(fn);
1421 return ret;
1426 * mpdm_stat - Gives status from a file.
1427 * @filename: file name to get the status from
1429 * Returns a 14 element array of the status (permissions, onwer, etc.)
1430 * from the desired @filename, or NULL if the file cannot be accessed.
1431 * (man 2 stat).
1433 * The values are: 0, device number of filesystem; 1, inode number;
1434 * 2, file mode; 3, number of hard links to the file; 4, uid; 5, gid;
1435 * 6, device identifier; 7, total size of file in bytes; 8, atime;
1436 * 9, mtime; 10, ctime; 11, preferred block size for system I/O;
1437 * 12, number of blocks allocated and 13, canonicalized file name.
1438 * Not all elements have necesarily meaningful values, as most are
1439 * system-dependent.
1440 * [File Management]
1442 mpdm_t mpdm_stat(const mpdm_t filename)
1444 mpdm_t r = NULL;
1446 #ifdef CONFOPT_SYS_STAT_H
1447 struct stat s;
1448 mpdm_t fn;
1450 fn = mpdm_ref(MPDM_2MBS(filename->data));
1452 if (stat((char *) fn->data, &s) != -1) {
1453 r = MPDM_A(14);
1455 mpdm_aset(r, MPDM_I(s.st_dev), 0);
1456 mpdm_aset(r, MPDM_I(s.st_ino), 1);
1457 mpdm_aset(r, MPDM_I(s.st_mode), 2);
1458 mpdm_aset(r, MPDM_I(s.st_nlink), 3);
1459 mpdm_aset(r, MPDM_I(s.st_uid), 4);
1460 mpdm_aset(r, MPDM_I(s.st_gid), 5);
1461 mpdm_aset(r, MPDM_I(s.st_rdev), 6);
1462 mpdm_aset(r, MPDM_I(s.st_size), 7);
1463 mpdm_aset(r, MPDM_I(s.st_atime), 8);
1464 mpdm_aset(r, MPDM_I(s.st_mtime), 9);
1465 mpdm_aset(r, MPDM_I(s.st_ctime), 10);
1466 mpdm_aset(r, MPDM_I(0), 11); /* s.st_blksize */
1467 mpdm_aset(r, MPDM_I(0), 12); /* s.st_blocks */
1469 #ifdef CONFOPT_CANONICALIZE_FILE_NAME
1472 char * ptr;
1474 if ((ptr = canonicalize_file_name(
1475 (char *)fn->data)) != NULL) {
1476 mpdm_aset(r, MPDM_MBS(ptr), 13);
1477 free(ptr);
1480 #endif
1482 #ifdef CONFOPT_REALPATH
1484 char tmp[2048];
1486 if (realpath((char *)fn->data, tmp) != NULL)
1487 mpdm_aset(r, MPDM_MBS(tmp), 13);
1489 #endif
1491 #ifdef CONFOPT_FULLPATH
1493 char tmp[_MAX_PATH + 1];
1495 if (_fullpath(tmp, (char *)fn->data, _MAX_PATH) != NULL)
1496 mpdm_aset(r, MPDM_MBS(tmp), 13);
1498 #endif
1500 mpdm_unref(fn);
1502 else
1503 store_syserr();
1505 #endif /* CONFOPT_SYS_STAT_H */
1507 return r;
1512 * mpdm_chmod - Changes a file's permissions.
1513 * @filename: the file name
1514 * @perms: permissions (element 2 from mpdm_stat())
1516 * Changes the permissions for a file.
1517 * [File Management]
1519 int mpdm_chmod(const mpdm_t filename, mpdm_t perms)
1521 int r = -1;
1523 mpdm_t fn = mpdm_ref(MPDM_2MBS(filename->data));
1525 if ((r = chmod((char *) fn->data, mpdm_ival(perms))) == -1)
1526 store_syserr();
1528 mpdm_unref(fn);
1530 return r;
1535 * mpdm_chdir - Changes the working directory
1536 * @dir: the new path
1538 * Changes the working directory
1539 * [File Management]
1541 int mpdm_chdir(const mpdm_t dir)
1543 int r = -1;
1545 mpdm_t fn = mpdm_ref(MPDM_2MBS(dir->data));
1547 if ((r = chdir((char *) fn->data)) == -1)
1548 store_syserr();
1550 mpdm_unref(fn);
1552 return r;
1557 * mpdm_chown - Changes a file's owner.
1558 * @filename: the file name
1559 * @uid: user id (element 4 from mpdm_stat())
1560 * @gid: group id (element 5 from mpdm_stat())
1562 * Changes the owner and group id's for a file.
1563 * [File Management]
1565 int mpdm_chown(const mpdm_t filename, mpdm_t uid, mpdm_t gid)
1567 int r = -1;
1569 #ifdef CONFOPT_CHOWN
1571 mpdm_t fn = mpdm_ref(MPDM_2MBS(filename->data));
1573 if ((r = chown((char *) fn->data, mpdm_ival(uid), mpdm_ival(gid))) == -1)
1574 store_syserr();
1576 mpdm_unref(fn);
1578 #endif /* CONFOPT_CHOWN */
1580 return r;
1585 * mpdm_glob - Executes a file globbing.
1586 * @spec: Globbing spec
1587 * @base: Optional base directory
1589 * Executes a file globbing. @spec is system-dependent, but usually
1590 * the * and ? metacharacters work everywhere. @base can contain a
1591 * directory; if that's the case, the output strings will include it.
1592 * In any case, each returned value will be suitable for a call to
1593 * mpdm_open().
1595 * Returns an array of files that match the globbing (can be an empty
1596 * array if no file matches), or NULL if globbing is unsupported.
1597 * [File Management]
1599 mpdm_t mpdm_glob(const mpdm_t spec, const mpdm_t base)
1601 mpdm_t d = NULL;
1602 mpdm_t f = NULL;
1603 mpdm_t v = NULL;
1605 #ifdef CONFOPT_WIN32
1607 WIN32_FIND_DATA fd;
1608 HANDLE h;
1609 char *ptr;
1610 mpdm_t w;
1611 mpdm_t s = NULL;
1612 mpdm_t sp = NULL;
1614 /* add base */
1615 if (mpdm_size(base))
1616 sp = mpdm_strcat_s(base, L"/");
1618 /* add spec */
1619 w = mpdm_ref(sp);
1621 if (mpdm_size(spec) == 0)
1622 sp = mpdm_strcat_s(w, L"*.*");
1623 else
1624 sp = mpdm_strcat(w, spec);
1626 mpdm_unref(w);
1628 /* delete repeated directory delimiters */
1629 w = mpdm_ref(sp);
1630 sp = mpdm_sregex(MPDM_LS(L"@[\\/]+@g"), w, MPDM_LS(L"/"), 0);
1631 mpdm_unref(w);
1633 w = mpdm_ref(sp);
1634 sp = MPDM_2MBS(w->data);
1635 mpdm_unref(w);
1637 v = MPDM_A(0);
1638 d = mpdm_ref(MPDM_A(0));
1639 f = mpdm_ref(MPDM_A(0));
1640 mpdm_ref(sp);
1642 if ((h = FindFirstFile((char *) sp->data, &fd)) != INVALID_HANDLE_VALUE) {
1643 /* if spec includes a directory, store in s */
1644 if ((ptr = strrchr((char *) sp->data, '/')) != NULL) {
1645 *(ptr + 1) = '\0';
1646 s = MPDM_MBS(sp->data);
1649 do {
1650 /* ignore . and .. */
1651 if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
1652 continue;
1654 /* concat base directory and file names */
1655 w = mpdm_strcat(s, MPDM_MBS(fd.cFileName));
1657 /* if it's a directory, add a / */
1658 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1659 mpdm_t t = mpdm_ref(w);
1660 w = mpdm_strcat_s(t, L"/");
1661 mpdm_unref(t);
1663 mpdm_push(d, w);
1665 else
1666 mpdm_push(f, w);
1668 while (FindNextFile(h, &fd));
1670 FindClose(h);
1673 mpdm_unref(sp);
1675 #endif
1677 #if CONFOPT_GLOB_H
1679 /* glob.h support */
1680 glob_t globbuf;
1681 const char *ptr;
1682 mpdm_t w;
1684 /* build full path */
1685 if (mpdm_size(base))
1686 v = mpdm_strcat_s(base, L"/");
1688 w = mpdm_ref(v);
1690 if (mpdm_size(spec) == 0)
1691 v = mpdm_strcat_s(w, L"*");
1692 else
1693 v = mpdm_strcat(w, spec);
1695 mpdm_unref(w);
1697 /* delete repeated directory delimiters */
1698 w = mpdm_ref(v);
1699 v = mpdm_sregex(MPDM_LS(L"@/{2,}@g"), w, MPDM_LS(L"/"), 0);
1700 mpdm_unref(w);
1702 w = mpdm_ref(v);
1703 v = MPDM_2MBS(w->data);
1704 mpdm_unref(w);
1706 ptr = v->data;
1708 globbuf.gl_offs = 1;
1710 v = MPDM_A(0);
1711 d = mpdm_ref(MPDM_A(0));
1712 f = mpdm_ref(MPDM_A(0));
1714 if (glob(ptr, GLOB_MARK, NULL, &globbuf) == 0) {
1715 int n;
1717 for (n = 0; globbuf.gl_pathv[n] != NULL; n++) {
1718 char *ptr = globbuf.gl_pathv[n];
1719 mpdm_t t = MPDM_MBS(ptr);
1721 /* if last char is /, add to directories */
1722 if (ptr[strlen(ptr) - 1] == '/')
1723 mpdm_push(d, t);
1724 else
1725 mpdm_push(f, t);
1729 globfree(&globbuf);
1731 #else
1733 /* no win32 nor glob.h; try workaround */
1734 /* ... */
1736 #endif
1738 if (v != NULL) {
1739 int n;
1741 mpdm_sort(d, 1);
1742 mpdm_sort(f, 1);
1744 /* transfer all data in d and f */
1745 for (n = 0; n < mpdm_size(d); n++)
1746 mpdm_push(v, mpdm_aget(d, n));
1747 for (n = 0; n < mpdm_size(f); n++)
1748 mpdm_push(v, mpdm_aget(f, n));
1750 mpdm_unref(f);
1751 mpdm_unref(d);
1754 return v;
1758 #ifdef CONFOPT_WIN32
1760 static void win32_pipe(HANDLE * h, int n)
1762 SECURITY_ATTRIBUTES sa;
1763 HANDLE cp, t;
1765 memset(&sa, '\0', sizeof(sa));
1766 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1767 sa.bInheritHandle = TRUE;
1768 sa.lpSecurityDescriptor = NULL;
1770 cp = GetCurrentProcess();
1772 CreatePipe(&h[0], &h[1], &sa, 0);
1773 DuplicateHandle(cp, h[n], cp, &t, 0, FALSE, DUPLICATE_SAME_ACCESS);
1774 CloseHandle(h[n]);
1775 h[n] = t;
1779 static int sysdep_popen(mpdm_t v, char *prg, int rw)
1780 /* win32-style pipe */
1782 HANDLE pr[2];
1783 HANDLE pw[2];
1784 PROCESS_INFORMATION pi;
1785 STARTUPINFO si;
1786 int ret;
1787 struct mpdm_file *fs = (struct mpdm_file *) v->data;
1789 /* init all */
1790 pr[0] = pr[1] = pw[0] = pw[1] = NULL;
1792 if (rw & 0x01)
1793 win32_pipe(pr, 0);
1794 if (rw & 0x02)
1795 win32_pipe(pw, 1);
1797 /* spawn new process */
1798 memset(&pi, '\0', sizeof(pi));
1799 memset(&si, '\0', sizeof(si));
1801 si.cb = sizeof(STARTUPINFO);
1802 si.hStdError = pr[1];
1803 si.hStdOutput = pr[1];
1804 si.hStdInput = pw[0];
1805 si.dwFlags |= STARTF_USESTDHANDLES;
1807 ret = CreateProcess(NULL, prg, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
1809 if (rw & 0x01)
1810 CloseHandle(pr[1]);
1811 if (rw & 0x02)
1812 CloseHandle(pw[0]);
1814 fs->hin = pr[0];
1815 fs->hout = pw[1];
1817 return ret;
1821 static int sysdep_pclose(const mpdm_t v)
1823 struct mpdm_file *fs = (struct mpdm_file *)v->data;
1825 if (fs->hin != NULL)
1826 CloseHandle(fs->hin);
1828 if (fs->hout != NULL)
1829 CloseHandle(fs->hout);
1831 /* how to know process exit code? */
1832 return 0;
1836 #else /* CONFOPT_WIN32 */
1838 static int sysdep_popen(mpdm_t v, char *prg, int rw)
1839 /* unix-style pipe open */
1841 int pr[2], pw[2];
1842 struct mpdm_file *fs = (struct mpdm_file *)v->data;
1844 /* init all */
1845 pr[0] = pr[1] = pw[0] = pw[1] = -1;
1847 if (rw & 0x01)
1848 pipe(pr);
1849 if (rw & 0x02)
1850 pipe(pw);
1852 if (fork() == 0) {
1853 /* child process */
1854 if (rw & 0x01) {
1855 close(1);
1856 dup(pr[1]);
1857 close(pr[0]);
1859 if (rw & 0x02) {
1860 close(0);
1861 dup(pw[0]);
1862 close(pw[1]);
1865 /* redirect stderr to stdout */
1866 close(2);
1867 dup(1);
1869 /* run the program */
1870 execlp("/bin/sh", "/bin/sh", "-c", prg, NULL);
1871 execlp(prg, prg, NULL);
1873 /* still here? exec failed; close pipes and exit */
1874 close(0);
1875 close(1);
1876 exit(0);
1879 /* create the pipes as non-buffered streams */
1880 if (rw & 0x01) {
1881 fs->in = fdopen(pr[0], "r");
1882 setvbuf(fs->in, NULL, _IONBF, 0);
1883 close(pr[1]);
1886 if (rw & 0x02) {
1887 fs->out = fdopen(pw[1], "w");
1888 setvbuf(fs->out, NULL, _IONBF, 0);
1889 close(pw[0]);
1892 return 1;
1896 static int sysdep_pclose(const mpdm_t v)
1897 /* unix-style pipe close */
1899 int s;
1900 struct mpdm_file *fs = (struct mpdm_file *)v->data;
1902 if (fs->in != NULL)
1903 fclose(fs->in);
1905 if (fs->out != fs->in && fs->out != NULL)
1906 fclose(fs->out);
1908 wait(&s);
1910 return s;
1914 #endif /* CONFOPT_WIN32 */
1918 * mpdm_popen - Opens a pipe.
1919 * @prg: the program to pipe
1920 * @mode: an fopen-like mode string
1922 * Opens a pipe to a program. If @prg can be open in the specified @mode, an
1923 * mpdm_t value will be returned containing the file descriptor, or NULL
1924 * otherwise.
1925 * [File Management]
1927 mpdm_t mpdm_popen(const mpdm_t prg, const mpdm_t mode)
1929 mpdm_t v, pr, md;
1930 char *m;
1931 int rw = 0;
1933 if (prg == NULL || mode == NULL)
1934 return NULL;
1936 if ((v = new_mpdm_file()) == NULL)
1937 return NULL;
1939 /* convert to mbs,s */
1940 pr = mpdm_ref(MPDM_2MBS(prg->data));
1941 md = mpdm_ref(MPDM_2MBS(mode->data));
1943 /* get the mode */
1944 m = (char *) md->data;
1946 /* set the mode */
1947 if (m[0] == 'r')
1948 rw = 0x01;
1949 if (m[0] == 'w')
1950 rw = 0x02;
1951 if (m[1] == '+')
1952 rw = 0x03; /* r+ or w+ */
1954 if (!sysdep_popen(v, (char *) pr->data, rw)) {
1955 destroy_mpdm_file(v);
1956 v = NULL;
1959 mpdm_unref(md);
1960 mpdm_unref(pr);
1962 return v;
1967 * mpdm_pclose - Closes a pipe.
1968 * @fd: the value containing the file descriptor
1970 * Closes a pipe.
1971 * [File Management]
1973 mpdm_t mpdm_pclose(mpdm_t fd)
1975 mpdm_t r = NULL;
1977 if ((fd->flags & MPDM_FILE) && fd->data != NULL) {
1978 r = MPDM_I(sysdep_pclose(fd));
1979 destroy_mpdm_file(fd);
1982 return r;
1987 * mpdm_home_dir - Returns the home user directory.
1989 * Returns a system-dependent directory where the user can write
1990 * documents and create subdirectories.
1991 * [File Management]
1993 mpdm_t mpdm_home_dir(void)
1995 mpdm_t r = NULL;
1996 char *ptr;
1997 char tmp[512] = "";
1999 #ifdef CONFOPT_WIN32
2001 LPITEMIDLIST pidl;
2003 /* get the 'My Documents' folder */
2004 SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);
2005 SHGetPathFromIDList(pidl, tmp);
2006 strcat(tmp, "\\");
2008 #endif
2010 #ifdef CONFOPT_PWD_H
2012 struct passwd *p;
2014 /* get home dir from /etc/passwd entry */
2015 if (tmp[0] == '\0' && (p = getpwuid(getpid())) != NULL) {
2016 strcpy(tmp, p->pw_dir);
2017 strcat(tmp, "/");
2020 #endif
2022 /* still none? try the ENV variable $HOME */
2023 if (tmp[0] == '\0' && (ptr = getenv("HOME")) != NULL) {
2024 strcpy(tmp, ptr);
2025 strcat(tmp, "/");
2028 if (tmp[0] != '\0')
2029 r = MPDM_MBS(tmp);
2031 return r;
2036 * mpdm_app_dir - Returns the applications directory.
2038 * Returns a system-dependent directory where the applications store
2039 * their private data, as components or resources.
2040 * [File Management]
2042 mpdm_t mpdm_app_dir(void)
2044 mpdm_t r = NULL;
2046 #ifdef CONFOPT_WIN32
2048 HKEY hkey;
2049 char tmp[MAX_PATH];
2050 LPITEMIDLIST pidl;
2052 /* get the 'Program Files' folder (can fail) */
2053 tmp[0] = '\0';
2054 if (SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl) == S_OK)
2055 SHGetPathFromIDList(pidl, tmp);
2057 /* if it's still empty, get from the registry */
2058 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
2059 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
2060 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) {
2061 int n = sizeof(tmp);
2063 if (RegQueryValueEx(hkey, "ProgramFilesDir",
2064 NULL, NULL, tmp, (LPDWORD) & n) != ERROR_SUCCESS)
2065 tmp[0] = '\0';
2068 if (tmp[0] != '\0') {
2069 strcat(tmp, "\\");
2070 r = MPDM_MBS(tmp);
2072 #endif
2074 /* still none? get the configured directory */
2075 if (r == NULL)
2076 r = MPDM_MBS(CONFOPT_PREFIX "/share/");
2078 return r;