-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnl-filesys.c
3281 lines (2688 loc) · 70.4 KB
/
nl-filesys.c
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
/*
Copyright (C) 2020 Lutz Mueller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "newlisp.h"
#include <errno.h>
#include "protos.h"
#define AF_UNSPEC 0 /* from socket.h or winsock2.h */
#if defined(SOLARIS) || defined(TRU64) || defined(AIX)
#include <stropts.h>
#endif
#ifdef SOLARIS
#define FIONREAD I_NREAD
#endif
#ifndef WINDOWS
#include <sys/types.h>
#ifndef ANDROID
#ifndef EMSCRIPTEN
#include <sys/ipc.h>
#include <sys/sem.h>
#endif
#endif
#include <sys/mman.h>
#include <sys/ioctl.h>
#endif
int init_argv(char * ptr, char *argv[]);
char * getUUID(char * str, char * node);
#ifdef OS2
#include <conio.h>
int semctl(int semid, int semnum, int cmd, ...);
#endif
#if defined(LINUX) || defined(KFREEBSD)
union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
#ifdef LINUX
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
#endif
};
#endif /* LINUX || KFREEBSD */
#ifndef TRU64
extern char ** environ;
#endif
#ifdef WINDOWS
#define fgetc win_fgetc
#define realpath win_realpath
#include <conio.h>
#include <io.h>
#include <direct.h>
#define pclose _pclose
#define pipe _pipe
/*
Set binary as default file mode for Windows.
See also http://www.mingw.org/MinGWiki/index.php/binary
*/
unsigned int _CRT_fmode = _O_BINARY;
int setenv (const char *name, const char *value, int replace);
#endif /* Win32 */
#ifndef WINDOWS
#include <sys/socket.h>
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif
#if defined(LINUX) || defined(KFREEBSD) || defined(CYGWIN)
char * strptime(const char * str, const char * fmt, struct tm * ttm);
#endif
time_t calcDateValue(int year, int month, int day, int hour, int min, int sec);
ssize_t currentDateValue(void);
extern STREAM readLineStream;
extern FILE * IOchannel;
extern int pagesize;
extern char * errorMessage[];
extern STREAM errorStream;
extern UINT netErrorIdx;
extern int newlispLibConsoleFlag;
/* semaphore() function type */
#ifndef NO_SEMAPHORE
#define SEM_CREATE 0
#define SEM_STATUS 1
#define SEM_SIGNAL 2
#endif
/* used in fork and spawn */
int parentPid = 0;
/* share, message */
CELL * readWriteShared(UINT * address, CELL * params, int flag);
CELL * readWriteSocket(int socket, CELL * params);
CELL * readWriteSharedExpression(UINT * adress, CELL * params);
void checkDeleteShareFile(UINT * address);
CELL * p_isFile(CELL * params) /* includes dev,socket,dir,file etc. */
{
char * fileName;
int flag;
params = getString(params, &fileName);
flag = getFlag(params);
return(isFile(fileName, flag) ? nilCell : flag ? stuffString(fileName) : trueCell);
}
int isFile(char * fileName, int flag)
{
struct stat fileInfo;
int result;
#ifdef WINDOWS
char slash;
size_t len;
len = strlen(fileName);
slash = *(fileName + len - 1);
if((slash == '\\' || slash == '/') && (!(len >= 2 && *(fileName + len - 2) == ':')))
*(fileName + len - 1) = 0;
#ifdef USE_WIN_UTF16PATH
result = stat_utf16(fileName, &fileInfo);
#else
result = stat(fileName, &fileInfo);
#endif
if(slash == '\\' || slash == '/')
*(fileName + len - 1) = slash;
#else /* not WINDOWS */
result = stat(fileName, &fileInfo);
#endif
if(result == 0)
{
if(flag)
result = ! S_ISREG(fileInfo.st_mode);
}
return(result);
}
CELL * p_isDirectory(CELL * params)
{
char * fileName;
getString(params, &fileName);
return(isDir(fileName) ? trueCell : nilCell);
}
int isDir(char * fileName)
{
struct stat fileInfo;
#ifdef WINDOWS
char slash;
size_t len;
len = strlen(fileName);
slash = *(fileName + len - 1);
if((slash == '\\' || slash == '/') && (!(len >= 2 && *(fileName + len - 2) == ':')))
*(fileName + len - 1) = 0;
#endif
#ifdef USE_WIN_UTF16PATH
if(stat_utf16(fileName, &fileInfo) != 0)
#else
if(stat(fileName, &fileInfo) != 0)
#endif
{
#ifdef WINDOWS
*(fileName + len - 1) = slash;
#endif
return(0);
}
#ifdef WINDOWS
*(fileName + len - 1) = slash;
#endif
if(S_ISDIR(fileInfo.st_mode))
return(1);
return(0);
}
CELL * p_open(CELL * params)
{
char * fileName;
char * accessMode;
char * option = NULL;
int handle;
IO_SESSION * session;
params = getString(params, &fileName);
params = getString(params, &accessMode);
if(params != nilCell)
getString(params, &option);
if( (handle = openFile(fileName, accessMode, option)) == (int)-1)
return(nilCell);
session = createIOsession(handle, AF_UNSPEC);
if(*accessMode == 'r')
session->stream = fdopen(handle, "r");
else if(*accessMode == 'w')
session->stream = fdopen(handle, "w");
else if(*accessMode == 'u')
session->stream = fdopen(handle, "r+");
else if(*accessMode == 'a')
session->stream = fdopen(handle, "a+");
return(stuffInteger((UINT)handle));
}
CELL * p_close(CELL * params)
{
UINT handle;
getInteger(params, &handle);
if(handle == 0) return(nilCell);
if(handle == printDevice) printDevice = 0;
if(deleteIOsession(handle)) return(trueCell);
return(nilCell);
}
CELL * p_readChar(CELL * params)
{
UINT handle;
unsigned char chr;
if(params != nilCell)
getInteger(params, &handle);
else
handle = printDevice;
#ifdef WINDOWS
/* make it work as on Unix */
if(printDevice == 1 || printDevice == 2) handle = 0;
#endif
if(read((int)handle, &chr, 1) <= 0) return(nilCell);
return(stuffInteger((UINT)chr));
}
CELL * p_readBuffer(CELL * params)
{
UINT handle;
size_t size, length;
ssize_t bytesRead = 0;
char * waitFor;
STREAM stream = {NULL, NULL, 0, 0, 0};
CELL * strCell;
SYMBOL * readSptr;
int found = 0;
char chr;
params = getInteger(params, &handle);
params = getEvalDefault(params, &strCell);
if(!symbolCheck || symbolCheck->contents != (UINT)strCell)
return(errorProc(ERR_IS_NOT_REFERENCED));
if(isProtected(symbolCheck->flags))
return(errorProcExt2(ERR_SYMBOL_PROTECTED, stuffSymbol(symbolCheck)));
readSptr = symbolCheck;
params = getInteger(params, (UINT *)&size);
if(params == nilCell)
{
openStrStream(&stream, size, 0);
found = 1;
if((bytesRead = read(handle, stream.buffer, size)) == -1)
{
closeStrStream(&stream);
return(nilCell);
}
}
else
{
getString(params, &waitFor);
openStrStream(&stream, MAX_LINE, 0);
length = strlen(waitFor);
while(bytesRead < size)
{
if(read(handle, &chr, 1) <= 0)
break;
writeStreamChar(&stream, chr);
if(++bytesRead < length) continue;
if(strcmp(waitFor, stream.ptr - length) == 0)
{
found = 1;
break;
}
}
}
deleteList(strCell);
if(bytesRead == 0)
{
readSptr->contents = (UINT)copyCell(nilCell);
closeStrStream(&stream);
return(nilCell);
}
/*
#ifndef WINDOWS
if((fstream = getIOstream(handle)) != NULL)
{
newPosition = lseek(handle, 0, SEEK_CUR);
fseek(fstream, newPosition, 0);
}
#endif
*/
if(stream.size > bytesRead)
stream.buffer = reallocMemory(stream.buffer, bytesRead + 1);
readSptr->contents = (UINT)makeStringCell(stream.buffer, bytesRead);
if(found) return(stuffInteger(bytesRead));
return(nilCell);
}
CELL * p_readFile(CELL * params)
{
char * fileName;
char * buffer = NULL;
ssize_t size;
#ifndef EMSCRIPTEN
CELL * result;
#endif
params = getString(params, &fileName);
#ifndef EMSCRIPTEN
if(my_strnicmp(fileName, "http://", 7) == 0)
{
result = getPutPostDeleteUrl(fileName, params, HTTP_GET, CONNECT_TIMEOUT);
return((my_strnicmp((char *)result->contents, (char *)"ERR:", 4) == 0) && netErrorIdx ? nilCell : result);
}
#endif
if((size = readFile(fileName, &buffer)) == -1)
return(nilCell);
return(makeStringCell(buffer, size));
}
/* allocates a buffer and reads a file into it */
ssize_t readFile(char * fileName, char * * buffer)
{
int handle;
off_t size;
struct stat fileInfo;
fileName = getLocalPath(fileName);
#ifdef USE_WIN_UTF16PATH
if(stat_utf16(fileName, &fileInfo) != 0)
#else
if(stat(fileName, &fileInfo) != 0)
#endif
return(-1);
size = fileInfo.st_size;
if( (handle = openFile(fileName, "r", NULL)) == (int)-1)
return(-1);
*buffer = callocMemory(size+1);
if(read(handle, *buffer, size) == -1)
{
freeMemory(*buffer);
close(handle);
*buffer = NULL;
return(-1);
}
close(handle);
return(size);
}
CELL * p_writeChar(CELL * params)
{
UINT handle;
UINT data;
size_t count;
unsigned char chr;
params = getInteger(params, &handle);
count = 0;
while(params != nilCell)
{
params = getInteger(params, &data);
chr = (unsigned char)data;
if(write((int)handle, (void *)&chr, 1) == -1)
return(nilCell);
++count;
}
return(stuffInteger(count));
}
size_t appendCellString(CELL * cell, char * buffer, size_t size)
{
cell->contents = (UINT)reallocMemory((char *)cell->contents, cell->aux + size);
memcpy((char *)cell->contents + cell->aux - 1, buffer, size);
cell->aux += size;
*((char *)cell->contents + cell->aux - 1) = 0;
return(size);
}
CELL * p_appendFile(CELL * params)
{
return(appendWriteFile(params, "a"));
}
CELL * p_writeFile(CELL * params)
{
return(appendWriteFile(params, "w"));
}
int writeFile(char * fileName, char * buffer, size_t size, char * type)
{
int handle;
if( (handle = openFile(fileName, type, NULL)) == (int)-1)
return(-1);
if(write(handle, buffer, size) == (int)-1)
return(-1);
close(handle);
return(0);
}
CELL * appendWriteFile(CELL * params, char * type)
{
char * fileName;
char * buffer;
size_t size;
#ifndef EMSCRIPTEN
CELL * result;
#endif
params = getString(params, &fileName);
#ifndef EMSCRIPTEN
if(my_strnicmp(fileName, "http://", 7) == 0)
{
result = getPutPostDeleteUrl(fileName, params,
(*type == 'w') ? HTTP_PUT : HTTP_PUT_APPEND, CONNECT_TIMEOUT);
return((my_strnicmp((char *)result->contents, (char *)"ERR:", 4) == 0) && netErrorIdx ? nilCell : result);
}
#endif
getStringSize(params, &buffer, &size, TRUE);
if(writeFile(fileName, buffer, size, type) == (int)-1)
return(nilCell);
return(stuffInteger(size));
}
CELL * writeBuffer(CELL * params, int lineFeed);
CELL * p_writeBuffer(CELL * params)
{
return(writeBuffer(params, FALSE));
}
CELL * p_writeLine(CELL * params)
{
return(writeBuffer(params, TRUE));
}
CELL * writeBuffer(CELL * params, int lineFeed)
{
CELL * device;
UINT handle;
SYMBOL * symbolRef;
char * buffer;
size_t size, userSize;
if(params == nilCell)
{
varPrintf(OUT_DEVICE, "%s", readLineStream.buffer);
if(lineFeed) varPrintf(OUT_DEVICE, LINE_FEED);
size = readLineStream.ptr - readLineStream.buffer;
goto RETURN_WRITE_BUFFER;
}
params = getEvalDefault(params, &device);
symbolRef = symbolCheck;
if(params == nilCell)
{
buffer = readLineStream.buffer;
size = readLineStream.ptr - readLineStream.buffer;
}
else
params = getStringSize(params, &buffer, &size, TRUE);
if(!lineFeed)
{
if(params != nilCell)
{
getInteger(params, (UINT *)&userSize);
size = (userSize > size) ? size : userSize;
}
}
if(isNumber(device->type))
{
getIntegerExt(device, &handle, FALSE);
if(write((int)handle, buffer, size) == -1) return(nilCell);
if(lineFeed)
if(write((int)handle, LINE_FEED, LINE_FEED_LEN) == -1) return(nilCell);
}
else if(device->type == CELL_STRING)
{
if(symbolRef && isProtected(symbolRef->flags))
return(errorProcExt2(ERR_SYMBOL_PROTECTED, stuffSymbol(symbolRef)));
appendCellString(device, buffer, size);
if(lineFeed)
appendCellString(device, LINE_FEED, LINE_FEED_LEN);
}
else
return(errorProcExt(ERR_INVALID_PARAMETER, device));
RETURN_WRITE_BUFFER:
return(stuffInteger(size + (lineFeed ? LINE_FEED_LEN : 0)));
}
CELL * p_seek(CELL * params)
{
UINT handle;
FILE * fstream;
#ifdef LFS
INT64 paramPosition;
off_t newPosition;
#else
off_t paramPosition;
off_t newPosition;
#endif
params = getInteger(params, &handle);
if(params == nilCell)
{
if(handle == 0)
newPosition = ftell(stdout);
else if((fstream = getIOstream(handle)) != NULL)
newPosition = ftell(fstream);
else if( (newPosition = lseek(handle, 0, SEEK_CUR)) == -1)
return(nilCell);
}
else
{
#ifdef LFS
getInteger64Ext(params, ¶mPosition, TRUE);
#else
getInteger(params, (UINT *)¶mPosition);
#endif
newPosition = paramPosition;
if(newPosition == -1)
{
if( (newPosition = lseek((int)handle, 0, SEEK_END)) == -1)
return(nilCell);
}
else
{
if( lseek((int)handle, newPosition, SEEK_SET) == -1)
return(nilCell);
}
}
paramPosition = newPosition;
#ifdef LFS
return(stuffInteger64(paramPosition));
#else
return(stuffInteger(paramPosition));
#endif
}
char * readStreamLine(STREAM * stream, FILE * inStream)
{
#ifdef OLD_READ_STREAM /* pre 10.5.8 */
int chr;
#else
char buff[MAX_STRING];
size_t l;
#endif
openStrStream(stream, MAX_STRING, 1);
#ifdef TRU64
do {
errno = 0;
#endif
#ifdef TRUE64 /* pre 10.5.8 also all other OS */
while((chr = fgetc(inStream)) != EOF)
{
if(chr == '\n') break;
if(chr == '\r')
{
chr = fgetc(inStream);
if(chr == '\n' || chr == EOF) break;
}
writeStreamChar(stream, chr);
}
#else
while(fgets(buff, MAX_STRING, inStream) != NULL)
{
l=strlen(buff);
if(buff[l-1] == 0x0A)
{
buff[--l] = 0;
if(buff[l-1] == 0x0D)
buff[--l] = 0;
writeStreamStr(stream, buff, l);
break;
}
writeStreamStr(stream, buff, l);
}
#endif /* pre 10.5.8 also all other OS */
#ifdef TRU64
} while (errno == EINTR);
#endif
#ifdef TRU64 /* and pre 10.5.8 on all other OS */
if(chr == EOF && stream->position == 0) return(NULL);
#else
if(feof(inStream))
{
clearerr(inStream);
if(stream->position == 0) return(NULL);
}
#endif
return(stream->buffer);
}
CELL * p_readLine(CELL * params)
{
UINT handle;
unsigned char chr;
char * line;
int bytesRead;
FILE * fstream;
if(params != nilCell)
getInteger(params, &handle);
else
handle = printDevice;
#ifdef WINDOWS
/* make it work as on Unix */
if(printDevice == 1 || printDevice == 2) handle = 0;
#endif
/* check if stream input can be done */
fstream = (handle == 0) ? IOchannel : getIOstream(handle);
#ifdef LIBRARY
if(!newlispLibConsoleFlag && fstream == stdin)
return(nilCell);
#endif
if(fstream != NULL)
{
if((line = readStreamLine(&readLineStream, fstream)) == NULL)
return(nilCell);
return(stuffString(line));
}
/* do raw handle input, only happens when using read-line on
sockets on UNIX and pipes on Windows */
openStrStream(&readLineStream, MAX_STRING, 1);
while(TRUE)
{
if((bytesRead = read((int)handle, &chr, 1)) <= 0) break;
if(chr == '\n') break;
if(chr == '\r')
{
if(read((int)handle, &chr, 1) < 0) break;
if(chr == '\n') break;
}
writeStreamChar(&readLineStream, chr);
}
if(bytesRead <= 0 && readLineStream.position == 0)
return(nilCell);
return(stuffStringN(readLineStream.buffer, readLineStream.position));;
}
CELL * p_currentLine(CELL * params)
{
return(stuffString(readLineStream.buffer));
}
char * getLocalPath(char * fileName)
{
if(my_strnicmp(fileName, "file://", 7) == 0)
fileName = fileName + 7;
#ifdef WINDOWS
if(*fileName == '/' && *(fileName + 2) == ':')
fileName = fileName + 1;
#endif
return(fileName);
}
int openFile(char * fileName, char * accessMode, char * option)
{
int blocking = 0;
#ifndef WINDOWS
int handle;
#endif
fileName = getLocalPath(fileName);
#ifndef WINDOWS
if(option != NULL && *option == 'n')
blocking = O_NONBLOCK;
#endif
if(*accessMode == 'r')
#ifdef USE_WIN_UTF16PATH
return(open_utf16(fileName, O_RDONLY | O_BINARY | blocking, 0));
#else
return(open(fileName, O_RDONLY | O_BINARY | blocking, 0));
#endif
else if(*accessMode == 'w')
#ifdef WINDOWS
#ifdef USE_WIN_UTF16PATH
return(open_utf16(fileName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE));
#else
return(open (fileName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE));
#endif /* UTF16 */
#else
return(open(fileName,O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | blocking,
S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)); /* rw-rw-rw */
#endif
else if(*accessMode == 'u')
return(open(fileName, O_RDWR | O_BINARY, 0));
else if(*accessMode == 'a')
{
#ifdef WINDOWS
#ifdef USE_WIN_UTF16PATH
return(open_utf16(fileName, O_RDWR | O_APPEND | O_BINARY | O_CREAT, S_IREAD | S_IWRITE));
#else
return(open(fileName, O_RDWR | O_APPEND | O_BINARY | O_CREAT, S_IREAD | S_IWRITE));
#endif /* UTF 16 */
#else
handle = open(fileName, O_RDWR | O_APPEND | O_BINARY | O_CREAT,
S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH); /* rw-rw-rw */
#ifdef EMSCRIPTEN
/* oppen append is broken on Emscripten, open for update but filepointer
stays at the beginning and old contents is overwritten */
if(lseek(handle, 0, SEEK_END) != -1)
return(handle);
#else
return(handle);
#endif
#endif
}
return(-1);
}
/* ------------------------- directory management ------------------------- */
CELL * p_copyFile(CELL * params)
{
char * fromName;
char * toName;
int fromHandle, toHandle;
unsigned char * copyBuffer;
UINT bytesRead;
params = getString(params, &fromName);
getString(params, &toName);
if((fromHandle = openFile(fromName, "read", NULL)) < 0)
return(nilCell);
if((toHandle = openFile(toName,"write", NULL)) < 0)
return(nilCell);
copyBuffer = allocMemory(MAX_FILE_BUFFER);
do
{
bytesRead = read(fromHandle, copyBuffer, MAX_FILE_BUFFER);
if(write(toHandle, copyBuffer, (int)bytesRead) < 0)
fatalError(ERR_IO_ERROR, 0, 0);
} while (bytesRead == MAX_FILE_BUFFER);
free(copyBuffer);
close(fromHandle);
close(toHandle);
return(trueCell);
}
CELL * p_renameFile(CELL * params)
{
char *oldName;
char *newName;
params = getString(params, &oldName);
getString(params, &newName);
#ifdef USE_WIN_UTF16PATH
return(rename_utf16(oldName, newName) == 0 ? trueCell : nilCell);
#else
return(rename(oldName, newName) == 0 ? trueCell : nilCell);
#endif
}
CELL * p_deleteFile(CELL * params)
{
char * fileName;
#ifndef EMSCRIPTEN
CELL * result;
#endif
params = getString(params, &fileName);
#ifndef EMSCRIPTEN
if(my_strnicmp(fileName, "http://", 7) == 0)
{
result = getPutPostDeleteUrl(fileName, params, HTTP_DELETE, CONNECT_TIMEOUT);
return((my_strnicmp((char *)result->contents, (char *)"ERR:", 4) == 0) && netErrorIdx ? nilCell : result);
}
#endif
fileName = getLocalPath(fileName);
#ifdef USE_WIN_UTF16PATH
return(unlink_utf16(fileName) == 0 ? trueCell : nilCell);
#else
return(unlink(fileName) == 0 ? trueCell : nilCell);
#endif
}
CELL * p_makeDir(CELL * params)
{
char * dirString;
UINT mode = 0777; /* drwxrwxrwx gets user masked to drwxr-xr-x on most UNIX */
/* consume param regardless of OS */
params = getString(params, &dirString);
if(params != nilCell)
{
getInteger(params, &mode);
mode = mode > 0xfff ? 0xfff : mode;
}
#ifdef WINDOWS
#ifdef USE_WIN_UTF16PATH
return(mkdir_utf16(dirString) == 0 ? trueCell : nilCell);
#else
return(mkdir(dirString) == 0 ? trueCell : nilCell);
#endif /* UTF16 */
#else
return(mkdir(dirString, (mode_t)mode) == 0 ? trueCell : nilCell);
#endif
}
CELL * p_removeDir(CELL * params)
{
char * dirString;
getString(params, &dirString);
#ifdef USE_WIN_UTF16PATH
return(rmdir_utf16(dirString) == 0 ? trueCell : nilCell);
#else
return(rmdir(dirString) == 0 ? trueCell : nilCell);
#endif
}
CELL * p_changeDir(CELL * params)
{
char * newDir;
getString(params, &newDir);
#ifdef USE_WIN_UTF16PATH
return(chdir_utf16(newDir) == 0 ? trueCell : nilCell);
#else
return(chdir(newDir) == 0 ? trueCell : nilCell);
#endif
}
CELL * p_directory(CELL * params)
{
CELL * dirList;
char * dirPath;
char * fileName;
char * pattern = NULL;
INT options = 0;
DIR * dir;
struct dirent * dEnt;
if(params != nilCell)
{
params = getString(params, &dirPath);
if(params != nilCell)
{
params = getString(params, &pattern);
if(params != nilCell)
/* 10.6.1 also accept string for options */
parseRegexOptions(params, (UINT *)&options, TRUE);
}
}
else dirPath = ".";
dirList = getCell(CELL_EXPRESSION);
dir = opendir(dirPath);
if(dir == NULL) return(nilCell);
while((dEnt = readdir(dir)) != NULL)
{
#ifdef USE_WIN_UTF16PATH
fileName = utf16_to_utf8(dEnt->d_name);
#else
fileName = dEnt->d_name;
#endif
if(!pattern || searchBufferRegex(fileName, 0, pattern, strlen(fileName), options, NULL) != -1)
addList(dirList, stuffString(fileName));
#ifdef USE_WIN_UTF16PATH
free(fileName);
#endif
}
closedir(dir);
return(dirList);
}