summaryrefslogtreecommitdiff
path: root/src/pdflib/pdcore/pc_file.c
blob: 42a7a022227808e6f18e4e1b14af070388d67e71 (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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
/*---------------------------------------------------------------------------*
 |              PDFlib - A library for generating PDF on the fly             |
 +---------------------------------------------------------------------------+
 | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
 +---------------------------------------------------------------------------+
 |                                                                           |
 |    This software is subject to the PDFlib license. It is NOT in the       |
 |    public domain. Extended versions and commercial licenses are           |
 |    available, please check http://www.pdflib.com.                         |
 |                                                                           |
 *---------------------------------------------------------------------------*/

/* $Id: pc_file.c,v 1.3 2010/01/27 02:27:21 scuri Exp $
 *
 * Various file routines
 *
 */

#include <errno.h>

#include "pc_util.h"
#include "pc_md5.h"
#include "pc_file.h"


/* headers for getpid() or _getpid().
*/
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#include <io.h>
#define PDC_FILENO _fileno
#else
#define PDC_FILENO fileno
#if defined(MAC)
#include <MacErrors.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif
#endif

#ifndef WINCE
#include <time.h>
#else
#include <winbase.h>
#endif

#ifdef VMS
#include <errno.h>
#include <descrip.h>
#include <ssdef.h>
#endif

#if defined(WIN32) && !defined(__BORLANDC__)

#ifndef _IOB_ENTRIES
#define _IOB_ENTRIES 20
#endif

#define PDC_MAXFILEHANDLES 2048

/* set a new maximum for the number of simultaneously open files.
 * if return = -1 error occured
 */
int
pdc_set_maxfilehandles(pdc_core *pdc, int maxfps)
{
    const char *stemp = pdc_errprintf(pdc, "%d", maxfps);

    if (maxfps < _IOB_ENTRIES)
        pdc_error(pdc, PDC_E_IO_TOOFEW_REQFILEHDLS, stemp,
                  pdc_errprintf(pdc, "%d", _IOB_ENTRIES), 0, 0);

    if (maxfps > PDC_MAXFILEHANDLES)
        pdc_error(pdc, PDC_E_IO_TOOMANY_REQFILEHDLS, stemp,
                  pdc_errprintf(pdc, "%d", PDC_MAXFILEHANDLES), 0, 0);

    return _setmaxstdio(maxfps);
}

int
pdc_get_maxfilehandles(void)
{
    return _getmaxstdio();
}

#endif

/* platform independent wrapper functions for 64-bit file handling.
*/
int
pdc__fseek(FILE *fp, pdc_off_t offset, int whence)
{
#if defined(_LARGEFILE_SOURCE)
    #if defined(WIN32)
	switch (whence)
	{
	    case SEEK_SET:
		return fsetpos(fp, &offset);

	    case SEEK_CUR:
	    {
		pdc_off_t pos;

		fgetpos(fp, &pos);
		pos += offset;
		return fsetpos(fp, &pos);
	    }

	    case SEEK_END:
	    {
		pdc_off_t pos, len;

		pos = _telli64(PDC_FILENO(fp));
		_lseeki64(PDC_FILENO(fp), 0, SEEK_END);
		len = _telli64(PDC_FILENO(fp));
		_lseeki64(PDC_FILENO(fp), pos, SEEK_SET);

		len += offset;
		return fsetpos(fp, &len);
	    }

	    default:
		return -1;
	}
    #else
	return fseeko(fp, offset, whence);
    #endif
#else
    return fseek(fp, offset, whence);
#endif
}

pdc_off_t
pdc__ftell(FILE *fp)
{
#if defined(_LARGEFILE_SOURCE)
    #if defined(WIN32)
	pdc_off_t pos;

	fgetpos(fp, &pos);
	return pos;
    #else
	return ftello(fp);
    #endif
#else
    return ftell(fp);
#endif
}


/* we had troubles writing a block of 80 MB via network on a windows platform.
** this could be fixed by breaking down the entire block into 1 MB pieces.
*/
#undef	PDC_BLOCKSIZE
#define	PDC_BLOCKSIZE	1048576

size_t
pdc__fread(void *ptr, size_t size, size_t nmemb, FILE *fp)
{
    char * cp = (char *) ptr;
    size_t total = size * nmemb;
    size_t left = total;
    size_t n, r;

    do
    {
	n = (left < PDC_BLOCKSIZE) ? left : PDC_BLOCKSIZE;
	r = fread(cp, 1, n, fp);

	left -= r;
	cp += r;
    } while (r == n && left != 0);

    return total - left;
}

size_t
pdc__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *fp)
{
    const char *cp = (const char *) ptr;
    size_t total = size * nmemb;
    size_t left = total;
    size_t n, r;

    do
    {
	n = (left < PDC_BLOCKSIZE) ? left : PDC_BLOCKSIZE;
	r = fwrite(cp, 1, n, fp);

	left -= r;
	cp += r;
    } while (r == n && left != 0);

    return total - left;
}

#undef	PDC_BLOCKSIZE


struct pdc_file_s
{
    pdc_core *pdc;         /* pdcore struct */
    char *filename;        /* file name */
    FILE *fp;              /* file struct or NULL. Then data != NULL: */
    pdc_bool wrmode;       /* writing mode */
    pdc_byte *data;        /* file data or NULL. Then fp != NULL */
    pdc_byte *end;         /* first byte above data buffer */
    pdc_byte *pos;         /* current file position in data buffer */
    pdc_byte *limit;       /* limit of file buffer in writing mode */
};

FILE   *
pdc_get_fileptr(pdc_file *sfp)
{
    return sfp->fp;
}

pdc_core   *
pdc_get_pdcptr(pdc_file *sfp)
{
    return sfp->pdc;
}

#if defined(_MSC_VER) && defined(_MANAGED)
#pragma unmanaged
#endif
int
pdc_get_fopen_errnum(pdc_core *pdc, int errnum)
{
    int outnum = errnum, isread;

    (void) pdc;

    isread = (errnum == PDC_E_IO_RDOPEN);

#if defined(VMS)
   /*
    * On the vms system, when a system error occurs which is not
    * mapped into the unix styled errno values, errno is set EVMSERR
    * and a VMS error code is set in vaxc$errno.
    */
    switch (errno)
    {
        case EVMSERR:
        {
            /* unmapped VMS runtime error - check vaxc$errno */
            switch (vaxc$errno)
            {
                case 100052: /* old style RMS file specification syntax error */
                outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_NF;
            }
        }
        return outnum;
    }
#endif /* VMS */

#if defined(MVS)

    switch (errno)
    {
        case 49:
        outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_NF;
    }
    return outnum;

#elif defined(WIN32)
    {
	DWORD lasterror = GetLastError();
        switch (lasterror)
        {
            case ERROR_FILE_NOT_FOUND:
            outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_NF;
            break;

            case ERROR_ACCESS_DENIED:
            case ERROR_INVALID_PASSWORD:
            case ERROR_NETWORK_ACCESS_DENIED:
            outnum = isread ? PDC_E_IO_RDOPEN_PD : PDC_E_IO_WROPEN_PD;
            break;

            case ERROR_INVALID_NAME:
            outnum = isread ? PDC_E_IO_RDOPEN_IS : PDC_E_IO_WROPEN_IS;
            break;

            case ERROR_PATH_NOT_FOUND:
            case ERROR_INVALID_DRIVE:
            case ERROR_BAD_NETPATH:
            case ERROR_BAD_UNIT:
            outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_NP;
            break;

            case ERROR_TOO_MANY_OPEN_FILES:
            case ERROR_SHARING_BUFFER_EXCEEDED:
            outnum = isread ? PDC_E_IO_RDOPEN_TM : PDC_E_IO_WROPEN_TM;
            break;

            case ERROR_BAD_COMMAND:
            outnum = isread ? PDC_E_IO_RDOPEN_BC : PDC_E_IO_WROPEN_BC;
            break;

            case ERROR_FILE_EXISTS:
            outnum = PDC_E_IO_WROPEN_AE;
            break;

            case ERROR_BUFFER_OVERFLOW:
            outnum = PDC_E_IO_WROPEN_TL;
            break;

            case ERROR_WRITE_FAULT:
            case ERROR_CANNOT_MAKE:
            outnum = PDC_E_IO_WROPEN_NC;
            break;

            case ERROR_HANDLE_DISK_FULL:
            case ERROR_DISK_FULL:
            outnum = PDC_E_IO_WROPEN_NS;
            break;

            case ERROR_SHARING_VIOLATION:
            outnum = isread ? PDC_E_IO_RDOPEN_SV : PDC_E_IO_WROPEN_SV;
            break;

            /* This code arises after opening a existing PDF or log file.
             * Because the file could be opened, we can ignore this code.
             */
            case ERROR_ALREADY_EXISTS:
            lasterror = 0;
            outnum = 0;
            break;
        }

        if (lasterror)
        {
            errno = (int) lasterror;
            return outnum;
        }

        /* if lasterror == 0 we must look for errno (see .NET) */
    }

#endif /* WIN32 */

    switch (errno)
    {
#ifdef EACCES
        case EACCES:
        outnum = isread ? PDC_E_IO_RDOPEN_PD : PDC_E_IO_WROPEN_PD;
        break;
#endif
#ifdef EMACOSERR
        case EMACOSERR:
#if defined(MAC) && defined(PDF_ALLOW_MAC_DEPR_FUNCS)

        switch (__MacOSErrNo)
        {
            case fnfErr:
            case dirNFErr:
            case resFNotFound:
            case afpDirNotFound:
            outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_NF;
            break;

            case permErr:
            case wrPermErr:
            case wPrErr:
            case afpAccessDenied:
            case afpVolLocked:
            outnum = isread ? PDC_E_IO_RDOPEN_PD : PDC_E_IO_WROPEN_PD;
            break;

            case nsvErr:
            case afpObjectTypeErr:
            outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_IS;
            break;

            case tmfoErr:
            case afpTooManyFilesOpen:
            outnum = isread ? PDC_E_IO_RDOPEN_TM : PDC_E_IO_WROPEN_TM;
            break;

            case opWrErr:
            outnum = PDC_E_IO_WROPEN_AE;
            break;

            case dirFulErr:
            case dskFulErr:
            case afpDiskFull:
            outnum = PDC_E_IO_WROPEN_NS;
            break;

            case fLckdErr:
            case afpLockErr:
            outnum = isread ? PDC_E_IO_RDOPEN_SV : PDC_E_IO_WROPEN_SV;
            break;

            default:
            break;
        }

        if (__MacOSErrNo)
        {
            return outnum;
        }
#else
        outnum = errnum;
        break;
#endif
        break;
#endif
#ifdef ENOENT
        case ENOENT:
        outnum = isread ? PDC_E_IO_RDOPEN_NF : PDC_E_IO_WROPEN_NF;
        break;
#endif
#ifdef EMFILE
        case EMFILE:
        outnum = isread ? PDC_E_IO_RDOPEN_TM : PDC_E_IO_WROPEN_TM;
        break;
#endif
#ifdef ENFILE
        case ENFILE:
        outnum = isread ? PDC_E_IO_RDOPEN_TM : PDC_E_IO_WROPEN_TM;
        break;
#endif
#ifdef EISDIR
        case EISDIR:
        outnum = isread ? PDC_E_IO_RDOPEN_ID : PDC_E_IO_WROPEN_ID;
        break;
#endif
#ifdef EDQUOT
        case EDQUOT:
        outnum = isread ? PDC_E_IO_RDOPEN_QU : PDC_E_IO_WROPEN_QU;
        break;
#endif
#ifdef EEXIST
        case EEXIST:
        outnum = PDC_E_IO_WROPEN_AE;
        break;
#endif
#ifdef ENAMETOOLONG
        case ENAMETOOLONG:
        outnum = PDC_E_IO_WROPEN_TL;
        break;
#endif
#ifdef ENOSPC
        case ENOSPC:
        outnum = PDC_E_IO_WROPEN_NS;
        break;
#endif
        default:

        /* observed on Solaris because of thread unsafe errno */
        if (errno == 0)
            pdc_error(pdc, PDC_E_INT_BADERRNO, 0, 0, 0, 0);

        outnum = errnum;
        break;
    }

    return outnum;
}
#if defined(_MSC_VER) && defined(_MANAGED)
#pragma managed
#endif

void
pdc_set_fopen_errmsg(pdc_core *pdc, int errnum, const char *qualifier,
                     const char *filename)
{
    const char *stemp1 = NULL;
    const char *stemp2 = NULL;
    int errno1 = errno;

#if defined(EMACOSERR)
#if defined(MAC) && defined(PDF_ALLOW_MAC_DEPR_FUNCS)
    errno1 = (int) __MacOSErrNo;
#endif
#endif

    errnum = pdc_get_fopen_errnum(pdc, errnum);
    if (errnum == PDC_E_IO_RDOPEN)
        errnum = PDC_E_IO_RDOPEN_CODE;
    else if (errnum == PDC_E_IO_WROPEN)
        errnum = PDC_E_IO_WROPEN_CODE;
    if (errnum == PDC_E_IO_RDOPEN_CODE || errnum == PDC_E_IO_WROPEN_CODE)
    {
        stemp1 = pdc_errprintf(pdc, "%d", errno1);

#ifdef PDC_HAS_STRERROR
        stemp2 = strerror(errno1);
        if (stemp2 != NULL)
        {
            if (errnum == PDC_E_IO_RDOPEN_CODE)
                errnum = PDC_E_IO_RDOPEN_CODETEXT;
            else if (errnum == PDC_E_IO_WROPEN_CODE)
                errnum = PDC_E_IO_WROPEN_CODETEXT;
        }
#endif
    }

    pdc_set_errmsg(pdc, errnum, qualifier, filename, stemp1, stemp2);
}

void
pdc_set_fwrite_errmsg(pdc_core *pdc, const char *filename)
{
    const char *stemp1 = NULL;
    const char *stemp2 = NULL;
    int errno1 = errno;
    int errnum = PDC_E_IO_WRITE;

#if defined(EMACOSERR)
#if defined(MAC) && defined(PDF_ALLOW_MAC_DEPR_FUNCS)
    errno1 = (int) __MacOSErrNo;
#endif
#endif

    stemp1 = pdc_errprintf(pdc, "%d", errno1);

#ifdef PDC_HAS_STRERROR
    stemp2 = strerror(errno1);
    if (stemp2 != NULL)
        errnum = PDC_E_IO_WRITE_CODETEXT;
#endif

    pdc_set_errmsg(pdc, errnum, filename, stemp1, stemp2, 0);
}

pdc_bool
pdc_check_fopen_errmsg(pdc_core *pdc, pdc_bool requested)
{
    return (requested || pdc_get_errnum(pdc) != PDC_E_IO_RDOPEN_NF) ?
           pdc_false : pdc_undef;
}

static void
pdc_logg_openclose(pdc_core *pdc, FILE *fp, pdc_bool opened)
{
    int errno1 = errno, errno2 = 0;

    if (pdc_logg_is_enabled(pdc, 3, trc_filesearch))
    {
#if defined(WIN32)
        errno2 = (int) GetLastError();
#elif defined(MAC) && defined(PDF_ALLOW_MAC_DEPR_FUNCS)
        errno2 = __MacOSErrNo;
#endif
        pdc_logg(pdc, "\t%p", fp);
        if (opened)
            pdc_logg(pdc, " opened");
        else
            pdc_logg(pdc, " closed");
#if PDC_FILENO_EXISTS
        if (fp != NULL && opened)
            pdc_logg(pdc, ", fileno=%d", PDC_FILENO(fp));
#endif
        pdc_logg(pdc, ", errno=%d", errno1);
        if (errno2 != 0)
            pdc_logg(pdc, ", errno2=%d", errno2);
        pdc_logg(pdc, "\n");

        /* because of logfile IO */
        if (errno != errno1)
            errno = errno1;
    }
}

void *
pdc_read_file(pdc_core *pdc, FILE *fp, pdc_off_t *o_filelen, int incore)
{
    static const char fn[] = "pdc_read_file";
    pdc_off_t filelen = 0, len = 0;
    char *content = NULL;


#if !defined(MVS) || !defined(I370)

    pdc__fseek(fp, 0, SEEK_END);
    filelen = pdc__ftell(fp);
    pdc__fseek(fp, 0, SEEK_SET);

    if (incore && filelen)
    {
        content = (char *) pdc_malloc(pdc, (size_t) (filelen + 1), fn);
        len = (pdc_off_t) pdc__fread(content, 1, (size_t) filelen, fp);

/* because pdc__ftell lies! */
filelen = len;
        if (!filelen)
        {
            pdc_free(pdc, content);
            filelen = 0;
            content = NULL;
        }
    }

#endif

    if (content) content[filelen] = 0;
    *o_filelen = filelen;
    return (void *) content;
}


/*
 * In the case of systems which are not capable of Unicode file names:
 *
 * File name can be converted to Latin-1: The incoming char pointer
 * will be deleted and a new char pointer to the Latin-1 string will
 * be returned. Otherwise an execption will be thrown.
 *
 * Returned string is temporary allocated.
 */
char *
pdc_check_filename(pdc_core *pdc, char *filename)
{
#if !defined(PDC_UNICODE_FILENAME)
    char *ffname = pdc_utf8_to_hostbytes(pdc, pdc->honorlang, filename);

    pdc_free_tmp(pdc, filename);
    if (ffname == NULL)
        pdc_error(pdc, PDC_E_IO_UNSUPP_UNINAME, 0, 0, 0, 0);
    filename = (char *) ffname;
#endif

    return filename;
}

/*
 * Returned string is temporary allocated.
 */
char *
pdc_get_filename(pdc_core *pdc, char *filename)
{
    char *ffname;

#if defined(PDC_UNICODE_FILENAME)
    static const char fn[] = "pdc_get_filename";

    ffname = pdc_strdup_ext(pdc, filename, PDC_CONV_TMPALLOC, fn);
#else
    ffname = pdc_hostbytes_to_utf8(pdc, pdc->honorlang, filename);
#endif

    return ffname;
}

/*
 * pdc_convert_filename_ext converts a file name as string of name type
 * (see function pdc_convert_name_ext) to a [EBCDIC-]UTF-8 string with or
 * without a BOM. If the compiler doesn't allow Unicode filenames
 * (see define PDC_UNICODE_FILENAME) the filename is Latin-1 encoded
 * if possible or an exception will be thrown.
 *
 * Returned string is temporary allocated.
 *
 */
const char *
pdc_convert_filename_ext(pdc_core *pdc, const char *filename, int len,
                         const char *paramname, pdc_encoding enc, int codepage,
                         int flags)
{
    char *fname = NULL;
    const char *outfilename = NULL;
    int i = 0;

    if (filename == NULL)
        pdc_error(pdc, PDC_E_ILLARG_EMPTY, paramname, 0, 0, 0);

    /* temporary allocation will be enforced */
    flags |= PDC_CONV_TMPALLOC;

    fname = pdc_convert_name_ext(pdc, filename, len, enc, codepage, flags);

    if (fname == NULL || *fname == '\0')
        pdc_error(pdc, PDC_E_ILLARG_EMPTY, paramname, 0, 0, 0);

    if (pdc_is_utf8_bytecode(fname))
    {
#if defined(PDC_UNICODE_FILENAME)
        i = 3;
#else
        fname = pdc_check_filename(pdc, fname);
#endif
    }

    outfilename = &fname[i];
    return outfilename;
}

/*
 * Returned string is temporary allocated.
 */
const char *
pdc_convert_filename(pdc_core *pdc, const char *filename, int len,
                     const char *paramname, pdc_bool withbom)
{
    int flags = PDC_CONV_EBCDIC;

    if (withbom)
        flags |= PDC_CONV_WITHBOM;

    return pdc_convert_filename_ext(pdc, filename, len, paramname,
                                    pdc_invalidenc, 0, flags);
}

/*
 * pdc_fopen_logg opens a file. The function expects a UTF-8 encoded file name.
 * (see function pdc_convert_filename), if define PDC_UNICODE_FILENAME is set.
 *
 */
FILE *
pdc_fopen_logg(pdc_core *pdc, const char *filename, const char *mode)
{
    FILE *fp = NULL;
    int i = 0;


#if defined(PDC_UNICODE_FILENAME)

    pdc_byte *outfilename = NULL;
    pdc_text_format nameformat = PDC_UTF8;
    pdc_text_format targetnameformat = pdc_utf16;
    int len = (int) pdc_strlen(filename);
    int outlen = 0;

    if (pdc_is_utf16be_unicode(filename))
        nameformat = pdc_utf16be;

    /* convert filename from UTF-8 / UTF-16BE to UTF-16 or Latin-1 */
    pdc_convert_string(pdc, nameformat, 0, NULL, (pdc_byte *) filename, len,
                       &targetnameformat, NULL, &outfilename, &outlen,
                       PDC_CONV_TRYBYTES | PDC_CONV_NOBOM, pdc_true);

    if (targetnameformat == pdc_bytes)
    {
        fp = fopen((const char *) outfilename, mode);
    }
    else
    {
        wchar_t wmode[8];

        len = (int) strlen(mode);
        for (i = 0; i < len; i++)
            wmode[i] = (wchar_t) mode[i];
        wmode[len] = 0;

        fp = _wfopen((wchar_t *) outfilename, wmode);
    }

    pdc_free(pdc, outfilename);

#else
    (void) pdc;

    /* due to honorlang, codeset of LANG: UTF-8 */
    if (pdc_is_utf8_bytecode(filename))
        i = 3;

    fp = fopen(&filename[i], mode);
#endif

    pdc_logg_openclose(pdc, fp, pdc_true);



    return fp;
}

pdc_file *
pdc_fopen(pdc_core *pdc, const char *filename, const char *qualifier,
          const pdc_byte *data, size_t size, int flags)
{
    static const char fn[] = "pdc_fopen";
    pdc_file *sfp;

    /* reset error number */
    pdc_set_errmsg(pdc, 0, 0, 0, 0, 0);

    sfp = (pdc_file *) pdc_calloc(pdc, sizeof(pdc_file), fn);

    /* initialize */
    sfp->pdc = pdc;
    sfp->filename = pdc_strdup_ext(pdc, filename, 0, fn);

    if (flags & PDC_FILE_WRITEMODE || flags & PDC_FILE_APPENDMODE)
        sfp->wrmode = pdc_true;


    if (data != NULL || size > 0)
    {
        /* virtual file */
        if (sfp->wrmode)
        {
            sfp->data = (pdc_byte *) pdc_calloc(pdc, size, fn);
            if (data != NULL)
            {
                /* append mode */
                memcpy(sfp->data, data, size);
                sfp->pos = sfp->data + size;
            }
            else
            {
                sfp->pos = sfp->data;
            }
            sfp->end = sfp->pos;
            sfp->limit = sfp->data + size;
        }
        else
        {
            sfp->data = (pdc_byte *) data;
            sfp->pos = sfp->data;
            sfp->end = sfp->data + size;
        }
    }
    else
    {
        const char *mode;


        /* disk file */
        if (flags & PDC_FILE_BINARY)
            mode = READBMODE;
        else
            mode = READTMODE;
        if (flags & PDC_FILE_APPENDMODE)
            mode = APPENDMODE;
        else if (flags & PDC_FILE_WRITEMODE)
            mode = WRITEMODE;

        sfp->fp = pdc_fopen_logg(pdc, filename, mode);
        if (sfp->fp == NULL)
        {
            pdc_fclose(sfp);

            if (qualifier == NULL)
                qualifier = "";
            pdc_set_fopen_errmsg(pdc, PDC_E_IO_RDOPEN, qualifier, filename);
            return NULL;
        }
    }

    return sfp;
}

pdc_bool
pdc_file_isvirtual(pdc_file *sfp)
{
    return sfp->fp ? pdc_false : pdc_true;
}

char *
pdc_file_name(pdc_file *sfp)
{
    return sfp->filename;
}

pdc_core *
pdc_file_getpdc(pdc_file *sfp)
{
    return sfp->pdc;
}

pdc_off_t
pdc_file_size(pdc_file *sfp)
{
    pdc_off_t filelen;

    if (sfp->fp)
    {
        pdc_off_t pos = pdc__ftell(sfp->fp);

        pdc_read_file(sfp->pdc, sfp->fp, &filelen, 0);
        pdc__fseek(sfp->fp, pos, SEEK_SET);
    }
    else
        filelen = (pdc_off_t) (sfp->end - sfp->data);

    return filelen;
}

const void *
pdc_freadall(pdc_file *sfp, size_t *filelen, pdc_bool *ismem)
{
    const void *result = NULL;

    *filelen = 0;

    pdc_logg_cond(sfp->pdc, 1, trc_filesearch,
        "\tAttempting to read whole file \"%s\"\n", sfp->filename);

    if (sfp->fp)
    {
        pdc_off_t flen;     /* TODO2GB: >2GB on 32-bit platforms? */

        result = pdc_read_file(sfp->pdc, sfp->fp, &flen, 1);

        if (ismem)
            *ismem = pdc_false;
	*filelen = (size_t) flen;
    }
    else
    {
        result = sfp->data;

        if (ismem)
            *ismem = pdc_true;
        *filelen = (size_t) (sfp->end - sfp->data);
    }

    pdc_logg_cond(sfp->pdc, 1, trc_filesearch,
        "\t%d bytes read from %s file, contents=%p\n",
        (int) (*filelen),
        (sfp->fp) ? "disk" : "memory",
        result);

    return result;
}

char *
pdc_fgetline(char *s, int size, pdc_file *sfp)
{
    int i, c;


    c = pdc_fgetc(sfp);
    if (c == EOF)
        return NULL;


    size--;
    for (i = 0; i < size; i++)
    {
        if (c == '\n' || c == '\r')
            break;
        s[i] = (char) c;

        c = pdc_fgetc(sfp);
        if (c == EOF)
        {
            i++;
            break;
        }

    }
    s[i] = 0;

    /* Skip windows line end \r\n */
    if (c == '\r')
    {
        c = pdc_fgetc(sfp);
        if (c != EOF)
        {
            if (c != '\n')
            {
                if (sfp->fp)
                {

                    ungetc(c, sfp->fp);
                }
                else
                {
                    pdc_fseek(sfp, -1, SEEK_CUR);
                }
            }
        }
    }
    return s;
}

/*
 * Emulation of C file functions - relevant for PDFlib
 */

pdc_off_t
pdc_ftell(pdc_file *sfp)
{
    if (sfp->fp)
        return pdc__ftell(sfp->fp);

    return (pdc_off_t) (sfp->pos - sfp->data);
}

int
pdc_fseek(pdc_file *sfp, pdc_off_t offset, int whence)
{
    static const char fn[] = "pdc_fseek";

    if (sfp->fp)
        return pdc__fseek(sfp->fp, offset, whence);

    switch (whence)
    {
        case SEEK_SET:
        sfp->pos = sfp->data + offset;
        break;

        case SEEK_CUR:
        sfp->pos += offset;
        break;

        case SEEK_END:
        sfp->pos = sfp->end;
        break;
    }

    if (sfp->pos > sfp->end)
    {
        /* extend file in writing mode */
        if (sfp->wrmode)
        {
            size_t nbytes = (size_t) (sfp->pos - sfp->end);

            if (sfp->pos > sfp->limit)
            {
                size_t size = (size_t) (sfp->pos - sfp->data);

                sfp->data = (pdc_byte *) pdc_realloc(sfp->pdc, sfp->data, size,
                                                     fn);
                sfp->end = sfp->data + size;
                sfp->pos = sfp->end;
                sfp->limit = sfp->end;
            }

            memset(sfp->pos - nbytes, 0, nbytes);
        }
        else
        {
            return -1;
        }
    }
    else if (sfp->pos < sfp->data)
    {
        return -1;
    }

    return 0;
}

size_t
pdc_fread(void *ptr, size_t size, size_t nmemb, pdc_file *sfp)
{
    size_t nbytes = 0;

    if (sfp->fp)
        return pdc__fread(ptr, size, nmemb, sfp->fp);

    nbytes = size * nmemb;
    if (sfp->pos + nbytes > sfp->end)
    {
        nbytes = (size_t) (sfp->end - sfp->pos);
        nmemb = nbytes / size;
        nbytes = nmemb *size;
    }

    if (nbytes)
        memcpy(ptr, sfp->pos, nbytes);
    sfp->pos += nbytes;

    return nmemb;
}

size_t
pdc_fwrite(const void *ptr, size_t size, size_t nmemb, pdc_file *sfp)
{
    static const char fn[] = "pdc_fwrite";

    if (sfp->wrmode)
    {
        size_t poslen, nbytes = 0;

        if (sfp->fp)
        {
            size_t total = pdc__fwrite(ptr, size, nmemb, sfp->fp);

            if (total < size * nmemb)
            {
                pdc_set_fwrite_errmsg(sfp->pdc, sfp->filename);
                PDC_RETHROW(sfp->pdc);
            }

            return total;
        }

        nbytes = size * nmemb;
        if (sfp->pos + nbytes > sfp->limit)
        {
            poslen = (size_t) (sfp->pos - sfp->data);
            size = poslen + nbytes;

            sfp->data = (pdc_byte *) pdc_realloc(sfp->pdc, sfp->data, size, fn);
            sfp->pos = sfp->data + poslen;
            sfp->end = sfp->data + size;
            sfp->limit = sfp->end;
        }
        memcpy(sfp->pos, ptr, nbytes);
        sfp->pos += nbytes;
        if (sfp->pos > sfp->end)
            sfp->end = sfp->pos;
    }
    else
    {
        nmemb = 0;
    }

    return nmemb;
}

void
pdc_freset(pdc_file *sfp, size_t size)
{
    static const char fn[] = "pdc_freset";

    if (sfp->wrmode && !sfp->fp)
    {
        if (size > (size_t) (sfp->limit - sfp->data))
        {
            sfp->data = (pdc_byte *) pdc_realloc(sfp->pdc, sfp->data, size, fn);
            sfp->limit = sfp->data + size;
        }

        sfp->pos = sfp->data;
        sfp->end = sfp->data;
    }
}

int
pdc_fgetc(pdc_file *sfp)
{
    int ch = 0;

    if (sfp->fp)
        return pdc__fgetc(sfp->fp);

    if (sfp->pos < sfp->end)
    {
        ch = (int) *sfp->pos;
        sfp->pos++;
    }
    else
    {
        ch = EOF;
    }

    return ch;
}

int
pdc_feof(pdc_file *sfp)
{
    if (sfp->fp)
        return pdc__feof(sfp->fp);

    return (sfp->pos >= sfp->end) ? 1 : 0;
}

void
pdc_fclose_logg(pdc_core *pdc, FILE *fp)
{
    fclose(fp);
    pdc_logg_openclose(pdc, fp, pdc_false);
}

void
pdc_fclose(pdc_file *sfp)
{
    if (sfp)
    {
        if (sfp->fp)
        {
            pdc_fclose_logg(sfp->pdc, sfp->fp);
            sfp->fp = NULL;
        }
        else if (sfp->wrmode)
        {
            if (sfp->data)
            {
                pdc_free(sfp->pdc, sfp->data);
                sfp->data = NULL;
            }
        }

        if (sfp->filename)
        {
            pdc_free(sfp->pdc, sfp->filename);
            sfp->filename = NULL;
        }

        pdc_free(sfp->pdc, sfp);
    }
}

/*
 * Concatenating a directory name with a file base name to a full valid
 * file name of maximal length PDC_FILENAMELEN - 1.
 *
 * On MVS platforms an extension at the end of basename will be discarded.
 */
void
pdc_file_fullname(pdc_core *pdc, const char *dirname, const char *basename,
                  char *fullname)
{
    const char *pathsep = PDC_PATHSEP;
    const char *stemp = NULL;
    size_t len = 0;

#ifdef MVS
    pdc_bool lastterm = pdc_false;
#endif

    if (!dirname || !dirname[0])
    {
        len += strlen(basename);
        if (len >= PDC_FILENAMELEN)
            goto PDC_FILE_ERROR;

        strcpy(fullname, basename);
    }
    else
    {
        fullname[0] = 0;

#ifdef MVS
        if (strncmp(dirname, PDC_FILEQUOT, 1))
            strcat(fullname, PDC_FILEQUOT);
#endif

        len += strlen(dirname);
        if (len >= PDC_FILENAMELEN)
            goto PDC_FILE_ERROR;

        strcat(fullname, dirname);

#ifdef VMS
        /* look for logical name or whose result */
        if(getenv(dirname))
            pathsep = PDC_PATHSEP_LOG;
        else if (fullname[strlen(fullname)-1] == ']')
            pathsep = "";
#endif

        len += strlen(pathsep) + strlen(basename);
        if (len >= PDC_FILENAMELEN)
            goto PDC_FILE_ERROR;

        strcat(fullname, pathsep);
        strcat(fullname, basename);

#ifdef MVS
        lastterm = pdc_true;
#endif
    }

#ifdef MVS
    {
        int ie, len;

        len = strlen(fullname);
        for (ie = len - 1; ie >= 0; ie--)
        {
            if (fullname[ie] == pathsep[0])
                break;

            if (fullname[ie] == '.')
            {
                fullname[ie] = 0;
                break;
            }
        }
        if (lastterm)
        {
            len += strlen(PDC_PATHTERM) + strlen(PDC_FILEQUOT);
            if (len >= PDC_FILENAMELEN)
                goto PDC_FILE_ERROR;

            strcat(fullname, PDC_PATHTERM);
            strcat(fullname, PDC_FILEQUOT);
        }
    }
#endif

    return;

    PDC_FILE_ERROR:

    if (!dirname || !dirname[0])
        stemp = pdc_errprintf(pdc, "%s", basename);
    else
        stemp = pdc_errprintf(pdc, "%s%s%s", dirname, pathsep, basename);
    pdc_error(pdc, PDC_E_IO_TOOLONG_FULLNAME, stemp, 0, 0, 0);
}

#define EXTRA_SPACE     32     /* extra space for separators, FILEQUOT etc. */

char *
pdc_file_fullname_mem(pdc_core *pdc, const char *dirname, const char *basename)
{
    static const char fn[] = "pdc_file_fullname_mem";
    char *fullname;
    size_t len;

    len = strlen(basename);
    if (dirname && dirname[0])
        len += strlen(dirname);
    len += EXTRA_SPACE;
    fullname = (char *) pdc_malloc(pdc, len, fn);

    pdc_file_fullname(pdc, dirname, basename, fullname);

    return fullname;
}

/*
 * Returns the full specified path name in a new memory.
 * The full path name can be concatened by a path name,
 * file name and extension (incl. dot).
 */
char *
pdc_file_concat(pdc_core *pdc, const char *dirname, const char *basename,
                const char *extension)
{
    static const char fn[] = "pdc_file_concat";
    char *pathname = pdc_file_fullname_mem(pdc, dirname, basename);
    size_t len = strlen(pathname) + 1;

    if (extension != NULL)
        len += strlen(extension);

    pathname = (char *) pdc_realloc(pdc, pathname, len, fn);

    if (extension != NULL)
        strcat(pathname, extension);

    return pathname;
}

/*
 * Returns the file basename of a full specified path name in the same memory.
 */
const char *
pdc_file_strip_dirs(const char *pathname)
{
    const char *scan = pathname + strlen(pathname);

    while (pathname <= --scan)
    {
        if (*scan == PDC_PATHSEP[0] || *scan == PDC_PATHSEP_ALT[0])
            return scan + 1;
    }

    return pathname;
}


/*
 * Returns the file path of a full specified path name in the same memory.
 */
char *
pdc_file_strip_name(char *pathname)
{
    char *scan = pathname + strlen(pathname);

    while (pathname <= --scan)
    {
        if (*scan == PDC_PATHSEP[0] || *scan == PDC_PATHSEP_ALT[0])
        {
            *scan = 0;
            break;
        }
    }

    return pathname;
}


/*
 * Returns the file extension of a path name in the same memory.
 */
char *
pdc_file_strip_ext(char *pathname)
{
    char *scan = pathname + strlen(pathname);

    while (pathname <= --scan)
    {
        if (*scan == '.')
        {
            *scan = 0;
            break;
        }
    }

    return pathname;
}


/*
 * Function reads a text file and creates a string list
 * of all no-empty and no-comment lines. The strings are stripped
 * by leading and trailing white space characters.
 *
 * The caller is responsible for freeing the resultated string list
 * by calling the function pdc_cleanup_stringlist.
 *
 * Not for unicode strings.
 *
 * Return value: Number of strings
 */

#define PDC_ARGV_CHUNKSIZE 256

int
pdc_read_textfile(pdc_core *pdc, pdc_file *sfp, int flags, char ***linelist)
{
    static const char fn[] = "pdc_read_textfile";
    char buf[PDC_BUFSIZE];
    char *content = NULL;
    char **strlist = NULL;
    int nlines = 0;
    pdc_off_t filelen;
    size_t len = 0, sumlen = 0, maxl = 0;
    pdc_bool tocont = pdc_false;
    int i, nbs, is = -1;

    /* get file length */
    filelen = pdc_file_size(sfp);
    if (filelen)
    {
        /* allocate content array */
        content = (char *) pdc_calloc(pdc, (size_t) filelen, fn);

        /* read loop */
        while (pdc_fgetline(buf, PDC_BUFSIZE, sfp) != NULL)
        {
            /* trim white spaces */
            if (tocont)
                pdc_strtrim(buf);
            else
                pdc_str2trim(buf);

            /* skip blank and comment lines */
            if (buf[0] == 0 || buf[0] == '%')
            {
                tocont = pdc_false;
                continue;
            }

            /* register new line */
            if (!tocont)
            {
                if (nlines)
                    pdc_logg_cond(pdc, 2, trc_filesearch,
                        "\t\tLine %d; \"%s\"\n", nlines, strlist[nlines - 1]);

                if (nlines >= (int) maxl)
                {
                    maxl += PDC_ARGV_CHUNKSIZE;
                    strlist = (strlist == NULL) ?
                            (char **)pdc_malloc(pdc, maxl * sizeof(char *), fn):
                            (char **)pdc_realloc(pdc, strlist, maxl *
                                                 sizeof(char *), fn);
                }

                is += sumlen + 1;
                strlist[nlines] = &content[is];
                nlines++;
                sumlen = 0;
            }

            /* process new line */
            nbs = 0;
            len = strlen(buf);
            for (i = 0; i < (int) len; i++)
            {
                /* backslash found */
                if (buf[i] == '\\')
                {
                    nbs++;
                }
                else
                {
                    /* comment sign found */
                    if (buf[i] == '%')
                    {
                        if (nbs % 2)
                        {
                            /* masked */
                            memmove(&buf[i-1], &buf[i], (size_t) (len-i));
                            len--;
                            buf[len] = 0;
                        }
                        else
                        {
                            buf[i] = 0;
                            len = strlen(buf);
                        }
                    }
                    nbs = 0;
                }
            }

            /* continuation line */
            tocont = (nbs % 2) ? pdc_true : pdc_false;
            if (tocont)
            {
                if (flags & PDC_FILE_KEEPLF)
                    buf[len - 1] = '\n';
                else
                    len--;
            }
            buf[len] = '\0';

            /* backslash substitution */
            if (flags & PDC_FILE_BSSUBST)
            {
                len = (size_t) pdc_subst_backslash(pdc, (pdc_byte *) buf,
                                          (int) len, NULL, pdc_bytes, pdc_true);
            }

            /* concatenate line */
            strcat(&content[is], buf);

            sumlen += len;
        }

        if (!strlist) pdc_free(pdc, content);
    }

    if (nlines)
        pdc_logg_cond(pdc, 2, trc_filesearch,
            "\t\tLine %d; \"%s\"\n", nlines, strlist[nlines - 1]);

    *linelist = strlist;
    return nlines;
}


/* generate a temporary file name from the current time, pid, 'dirname',
** and the data in 'inbuf' using MD5. prepend 'dirname' to the file name.
** the result is written to 'outbuf'. if 'outbuf' is NULL, memory will be
** allocated and must be freed by the caller. otherwise, 'pdc' can be set
** to NULL.
**
** if 'dirname' isn't specified the function looks for an environment
** variable via the define PDC_TMPDIR_ENV. This define is set in
** pc_config.h. If the environment variable has a value and if the
** directory exists (check with the temporary file together) the
** directory will be used.
*/

#ifdef MVS
#define TMP_NAME_LEN	9
#define TMP_SUFFIX	""
#define TMP_SUFF_LEN	0
#else
#define TMP_NAME_LEN	14
#define TMP_SUFFIX	".TMP"
#define TMP_SUFF_LEN	4
#endif

char *
pdc_temppath(
    pdc_core *pdc,
    char *outbuf,
    const char *inbuf,
    size_t inlen,
    const char *dirname)
{
    char		name[TMP_NAME_LEN + TMP_SUFF_LEN + 1];
    MD5_CTX             md5;
    time_t              timer;
    unsigned char       digest[MD5_DIGEST_LENGTH];
    int                 i;
    size_t              dirlen;
#ifdef VMS
    char               *tmpdir = NULL;
#endif /* VMS */

#if defined(WIN32)
#if defined(__BORLANDC__)
    int pid = getpid();
#else
    int pid = _getpid();
#endif
#else
#if !defined(MAC)
    pid_t pid = getpid();
#endif
#endif

#ifdef PDC_TMPDIR_ENV
    if (!dirname)
    {
        dirname = (char *) getenv(PDC_TMPDIR_ENV);
    }
#endif /* !PDC_TMPDIR_ENV */

    time(&timer);

    MD5_Init(&md5);
#if !defined(MAC)
    MD5_Update(&md5, (unsigned char *) &pid, sizeof pid);
#endif
    MD5_Update(&md5, (unsigned char *) &timer, sizeof timer);

    if (inlen == 0 && inbuf != (const char *) 0)
	inlen = strlen(inbuf);

    if (inlen != 0)
	MD5_Update(&md5, (unsigned char *) inbuf, inlen);

    dirlen = dirname ? strlen(dirname) : 0;
    if (dirlen)
	MD5_Update(&md5, (const unsigned char *) dirname, dirlen);

    MD5_Final(digest, &md5);

    for (i = 0; i < TMP_NAME_LEN - 1; ++i)
        name[i] = (char) (PDF_A + digest[i % MD5_DIGEST_LENGTH] % 26);

    name[i] = 0;
    strcat(name, TMP_SUFFIX);

    if (!outbuf)
        outbuf = pdc_file_fullname_mem(pdc, dirname, name);
    else
        pdc_file_fullname(pdc, dirname, name, outbuf);
    return outbuf;
}

/* Write function depending on pdc->asciifile.
 */
size_t
pdc_fwrite_ascii(pdc_core *pdc, const char *str, size_t len, FILE *fp)
{

    (void) pdc;
    len = pdc__fwrite(str, 1, len, fp);


    return len;
}

/* Creates a file depending on PDC_FILE_ASCII and pdc->asciifile.
*/
size_t
pdc_write_file(
    pdc_core *pdc,
    const char *filename,
    const char *qualifier,
    const char *content,
    size_t len,
    int flags)
{
    size_t wlen = 0;
    pdc_file *sfp;


    sfp = pdc_fopen(pdc, filename, qualifier, NULL, 0, flags);
    if (sfp != NULL)
    {
        wlen = pdc_fwrite_ascii(pdc, content, len, sfp->fp);
        if (wlen < len)
        {
            pdc_set_fwrite_errmsg(pdc, filename);
            PDC_RETHROW(pdc);
        }
        pdc_fclose(sfp);
    }


    return wlen;
}