-
Notifications
You must be signed in to change notification settings - Fork 4
/
filesys.c
3001 lines (2653 loc) · 81.2 KB
/
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
/*
FILE: filesys.c
HEADER: filesys.h
--GNU LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
TO_HEADER:
#ifdef WIN32
#include <windows.h>
#include <winbase.h>
#ifndef _WIN32_WCE
#include <io.h>
#include <direct.h>
#include <lm.h>
#include <lmaccess.h>
#endif
#else
#ifdef _WIN32_WCE
#include <windows.h>
#include <winbase.h>
#include <io.h>
#include <direct.h>
#include <lm.h>
#include <lmaccess.h>
#else
#ifdef __MACOS__
#include <unistd.h>
#include <dirent.h>
#else
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#endif
#ifndef TYPE_SOCKET
typedef int SOCKET;
#endif
#endif
#endif
#ifndef LOCK_SH
#define LOCK_SH 1
#endif
#ifndef LOCK_EX
#define LOCK_EX 2
#endif
#ifndef LOCK_NB
#define LOCK_NB 4
#endif
#ifndef LOCK_UN
#define LOCK_UN 8
#endif
*/
#ifdef _WIN32_WCE
#else
#include <fcntl.h>
#ifndef __MACOS__
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <errno.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef WIN32
#ifndef _WIN32_WCE
#include <winsock2.h>
#include <aclapi.h>
#endif
#define SHUT_RDWR SD_BOTH
#else
#ifndef __MACOS__
#include <sys/socket.h>
#endif
#include <utime.h>
#include <signal.h>
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
#ifndef SD_BOTH
#define SD_BOTH 2
#endif
#endif
#ifdef vms
#ifndef VMS
#define VMS
#endif
#endif
#include "filesys.h"
#include "errcodes.h"
/*POD
@c Handling system specific file operations
=abstract
The file T<filesys.h> contains file handling primitive functions. The reason for this module
is to have all system specific file handling functions to be separated in a single file.
All other modules use these functions that behave the same on Win32 platform as well as on UNIX.
=end
These functions are to be used by other parts of the program. They implement system
specific operations, and other levels need not care about these system specific stuff.
The function names are prefixed usually with T<file_>, some are prefixed with T<sys_>.
=toc
CUT*/
/*POD
=H file_fopen
@c Open a file
This is same as fopen.
VMS has some specialities when writing a file.
/*FUNCTION*/
FILE *file_fopen(
char *pszFileName,
char *pszOpenMode
){
/*noverbatim
CUT*/
#ifdef VMS
/* it is presented here to ease porting to VMS, but it was never tested. */
if( *pszOpenMode == "w" )
return fopen(pszFileName,pszOpenMode,"rat=cr","rfm=var");
else
return fopen(pszFileName,pszOpenMode);
#else
return fopen(pszFileName,pszOpenMode);
#endif
}
/*POD
=H file_fclose
@c Close a file
This is same as fclose. Nothing special. This is just a placeholder.
/*FUNCTION*/
void file_fclose(FILE *fp
){
/*noverbatim
CUT*/
fclose(fp);
}
/*POD
=H file_size
@c return the size of a file
/*FUNCTION*/
long file_size(char *pszFileName
){
/*noverbatim
CUT*/
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' )pszFileName[i] = LastChar;
#endif
if( st == -1 )return 0;
return buf.st_size;
}
#ifdef WIN32
/* This is a Windows specific routine that converts normal UNIX
time value (seconds since the epoch) to Windows FILETIME structure
*/
#define MSEPOCH 116444736000000000L
static void Utime2Filetime(long lTime,
PFILETIME pFileTime){
union myuft {
LONGLONG llTime;
FILETIME FT;
} *p;
p = (union myuft *)pFileTime;
p->llTime = lTime;
p->llTime *= 10000000; /* convert from seconds to 100nsecs */
/* This is the file time value of January 1, 1970. 00:00 */
p->llTime += MSEPOCH;
return;
}
static void Filetime2Utime(long *plTime,
PFILETIME pFileTime){
LONGLONG llTime;
memcpy(&llTime,pFileTime,sizeof(llTime));
/* This is the file time value of January 1, 1970. 00:00 */
llTime -= MSEPOCH;
llTime /= 10000000; /* convert from 100nsecs to seconds */
*plTime = (long)llTime;
return;
}
#endif
/*POD
=H file_time_accessed
@c return the time the file was last accessed
/*FUNCTION*/
long file_time_accessed(char *pszFileName
){
/*noverbatim
CUT*/
#ifdef WIN32
FILETIME FileTime;
long lTime;
HANDLE hFile;
int LastChar,i;
i = strlen(pszFileName);
if( i == 0 )return 0;
i--;
LastChar = pszFileName[i];
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
hFile = CreateFile(pszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
if( hFile == INVALID_HANDLE_VALUE )return 0;
if( !GetFileTime(hFile,NULL,&FileTime,NULL) ){
CloseHandle(hFile);
return 0;
}
CloseHandle(hFile);
Filetime2Utime(&lTime,&FileTime);
return lTime;
#else
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' )pszFileName[i] = LastChar;
#endif
if( st == -1 )return 0;
return buf.st_atime;
#endif
}
/*POD
=H file_time_modified
@c return the time the file was modified
/*FUNCTION*/
long file_time_modified(char *pszFileName
){
/*noverbatim
CUT*/
#ifdef WIN32
FILETIME FileTime;
unsigned long lTime;
HANDLE hFile;
SYSTEMTIME Syst;
int LastChar,i;
i = strlen(pszFileName);
if( i == 0 )return 0;
i--;
LastChar = pszFileName[i];
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
hFile = CreateFile(pszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
if( hFile == INVALID_HANDLE_VALUE )return 0;
if( !GetFileTime(hFile,NULL,NULL,&FileTime) ){
CloseHandle(hFile);
return 0;
}
CloseHandle(hFile);
FileTimeToSystemTime(&FileTime,&Syst);
Filetime2Utime(&lTime,&FileTime);
return lTime;
#else
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' )pszFileName[i] = LastChar;
#endif
if( st == -1 )return 0;
return buf.st_mtime;
#endif
}
/*POD
=H file_time_created
@c return the time the file was created
/*FUNCTION*/
long file_time_created(char *pszFileName
){
/*noverbatim
CUT*/
#ifdef WIN32
FILETIME FileTime;
long lTime;
HANDLE hFile;
int LastChar,i;
i = strlen(pszFileName);
if( i == 0 )return 0;
i--;
LastChar = pszFileName[i];
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
hFile = CreateFile(pszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
if( hFile == INVALID_HANDLE_VALUE )return 0;
if( !GetFileTime(hFile,&FileTime,NULL,NULL) ){
CloseHandle(hFile);
return 0;
}
CloseHandle(hFile);
Filetime2Utime(&lTime,&FileTime);
return lTime;
#else
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' )pszFileName[i] = LastChar;
#endif
if( st == -1 )return 0;
return buf.st_ctime;
#endif
}
/*POD
=H file_isdir
@c return true if the file is a directory
/*FUNCTION*/
int file_isdir(char *pszFileName
){
/*noverbatim
CUT*/
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
#endif
if( st == -1 )return 0;
return (buf.st_mode&S_IFDIR) ? -1 : 0;
}
/*POD
=H file_isreg
@c return true if the file is a regular file (not directory)
/*FUNCTION*/
int file_isreg(char *pszFileName
){
/*noverbatim
CUT*/
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
#endif
if( st == -1 )return 0;
return (buf.st_mode&S_IFREG) ? -1 : 0;
}
/*POD
=H file_exists
@c return true if the file exists
/*FUNCTION*/
int file_exists(char *pszFileName
){
/*noverbatim
CUT*/
struct stat buf;
int LastChar,i,st;
i = strlen(pszFileName);
if( i == 0 )return 0;
#ifdef __MACOS__
st = stat(pszFileName,&buf);
#else
i--;
LastChar = pszFileName[i];
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = (char)0;
st = stat(pszFileName,&buf);
if( LastChar == '/' || LastChar == '\\' )pszFileName[i] = LastChar;
#endif
if( st == -1 )
return 0;
else
return -1;
}
/*POD
=H file_truncate
@c truncate a file to a given length
It return 0 on success and -1 on error.
/*FUNCTION*/
int file_truncate(FILE *fp,
long lNewFileSize
){
/*noverbatim
CUT*/
#if (defined(WIN32) || defined(__MACOS__))
#if BCC32
#define _chsize chsize
#endif
return _chsize(_fileno(fp),lNewFileSize);
#else
return ftruncate(fileno(fp),lNewFileSize);
#endif
}
/*POD
=H file_fgetc
@c Get a single character from a file
Nothing special, it is just a placeholder.
/*FUNCTION*/
int file_fgetc(FILE *fp
){
/*noverbatim
CUT*/
return fgetc(fp);
}
/*POD
=H file_ferror
@c ferror
Nothing special, it is just a placeholder.
/*FUNCTION*/
int file_ferror(FILE *fp
){
/*noverbatim
CUT*/
return ferror(fp);
}
/*POD
=H file_fread
@c fread
Nothing special, it is just a placeholder.
/*FUNCTION*/
int file_fread(char *buf,
int size,
int count,
FILE *fp
){
/*noverbatim
CUT*/
return fread(buf,size,count,fp);
}
/*POD
=H file_fwrite
@c fwrite
Nothing special, it is just a placeholder.
/*FUNCTION*/
int file_fwrite(char *buf,
int size,
int count,
FILE *fp
){
/*noverbatim
CUT*/
return fwrite(buf,size,count,fp);
}
/*POD
=H file_fputc
@c Get a single character from a file
Nothing special, it is just a placeholder.
/*FUNCTION*/
int file_fputc(int c, FILE *fp
){
/*noverbatim
CUT*/
return fputc(c,fp);
}
/*POD
=H file_setmode
@c Set the mode of a file stream to binary or to ASCII
Nothing special, it is just a placeholder. On UNIX this is doing
nothing transparently.
/*FUNCTION*/
void file_setmode(FILE *fp,
int mode
){
/*noverbatim
CUT*/
#ifdef WIN32
#if BCC32
#define _setmode setmode
#endif
_setmode(_fileno(fp),mode);
#endif
return;
}
/*POD
=H file_binmode
@c Set a file stream to binary mode
/*FUNCTION*/
void file_binmode(FILE *fp
){
/*noverbatim
CUT*/
#ifdef WIN32
file_setmode(fp,_O_BINARY);
#endif
return;
}
/*POD
=H file_textmode
@c Set a file stream to text mode
/*FUNCTION*/
void file_textmode(FILE *fp
){
/*noverbatim
CUT*/
#ifdef WIN32
file_setmode(fp,_O_TEXT);
#endif
return;
}
/*POD
=H file_flock
@c Lock a file
/*FUNCTION*/
int file_flock(FILE *fp,
int iLockType
){
/*noverbatim
CUT*/
#define LK_LEN 0xffff0000
#ifdef WIN32
#define LK_ERR(f,i) ((f) ? (i = 0) : 0 )
OVERLAPPED o;
int i = -1;
HANDLE fh;
fh = (HANDLE)_get_osfhandle(_fileno(fp));
memset(&o, 0, sizeof(o));
switch(iLockType) {
case LOCK_SH: /* shared lock */
LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
break;
case LOCK_EX: /* exclusive lock */
LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
break;
case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
break;
case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
LK_ERR(LockFileEx(fh,
LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
0, LK_LEN, 0, &o),i);
break;
case LOCK_UN: /* unlock lock */
LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
break;
default: /* unknown */
/*errno = EINVAL;*/
break;
}
return i;
#elif defined(__MACOS__)
return 0;
#else
/* return flock(fileno(fp),iLockType); */
struct flock fl;
int WaitOption;
switch(iLockType) {
case LOCK_SH: /* shared lock */
iLockType = F_RDLCK;
WaitOption = F_SETLKW;
break;
case LOCK_EX: /* exclusive lock */
iLockType = F_WRLCK;
WaitOption = F_SETLKW;
break;
case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
iLockType = F_RDLCK;
WaitOption = F_SETLK;
break;
case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
iLockType = F_WRLCK;
WaitOption = F_SETLK;
break;
case LOCK_UN: /* unlock lock */
iLockType = F_UNLCK;
WaitOption = F_SETLKW;
break;
default: /* unknown */
return -1;
}
fl.l_type = iLockType;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = LK_LEN;
return fcntl( fileno( fp ), F_SETLKW , &fl );
#endif
}
/*POD
=H file_lock
@c Lock a range of a file
/*FUNCTION*/
int file_lock(FILE *fp,
int iLockType,
long lStart,
long lLength
){
/*noverbatim
CUT*/
#ifdef WIN32
#undef LK_ERR
#undef LK_LEN
#define LK_ERR(f,i) return ((f) ? 0 : -1)
#define LK_LEN 0xffff0000
#define dwReserved 0L
OVERLAPPED o;
int i = -1;
HANDLE fh;
fh = (HANDLE)_get_osfhandle(_fileno(fp));
memset(&o, 0, sizeof(o));
o.Offset = (DWORD)lStart;
switch(iLockType) {
case LOCK_SH: /* shared lock */
LK_ERR(LockFileEx(fh, 0, dwReserved, lLength, 0, &o),i);
case LOCK_EX: /* exclusive lock */
LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, dwReserved, lLength, 0, &o),i);
case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, dwReserved, lLength, 0, &o),i);
case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, dwReserved, lLength, 0, &o),i);
case LOCK_UN: /* unlock lock */
LK_ERR(UnlockFileEx(fh, 0, lLength, 0, &o),i);
default: /* unknown */
return -1;
}
#elif defined(__MACOS__)
return 0;
#else
struct flock fl;
int WaitOption;
switch(iLockType) {
case LOCK_SH: /* shared lock */
iLockType = F_RDLCK;
WaitOption = F_SETLKW;
break;
case LOCK_EX: /* exclusive lock */
iLockType = F_WRLCK;
WaitOption = F_SETLKW;
break;
case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
iLockType = F_RDLCK;
WaitOption = F_SETLK;
break;
case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
iLockType = F_WRLCK;
WaitOption = F_SETLK;
break;
case LOCK_UN: /* unlock lock */
iLockType = F_UNLCK;
WaitOption = F_SETLKW;
break;
default: /* unknown */
return -1;
}
fl.l_type = iLockType;
fl.l_whence = SEEK_SET;
fl.l_start = lStart;
fl.l_len = lLength;
return fcntl( fileno( fp ), F_SETLKW , &fl );
#endif
}
/*POD
=H file_feof
@c Check end of file condition
Nothing special, it is just a placeholder.
/*FUNCTION*/
int file_feof(FILE *fp
){
/*noverbatim
CUT*/
return feof(fp);
}
/*POD
=H file_mkdir
@c Create a directory
This is the usual UNIX mkdir function. The difference is that the access code is always 0777 on UNIX
which means that the user, group and others can read, write and execute the directory. If the permission
needed is different from that you have to call the T<file_chmod> function as soon as it becomes available.
The argument of the function is the name of the desired directory.
/*FUNCTION*/
int file_mkdir(char *pszDirectoryName
){
/*noverbatim
CUT*/
char *s;
#ifndef __MACOS__
for( s = pszDirectoryName ; *s ; s++ )if( *s == '\\' )*s= '/';
#endif
#ifdef WIN32
return _mkdir(pszDirectoryName);
#else
return mkdir(pszDirectoryName,0777);
#endif
}
/*POD
=H file_rmdir
@c Remove a directory
This is the usual UNIX rmdir function.
The argument of the function is the name of the directory to be deleted.
/*FUNCTION*/
int file_rmdir(char *pszDirectoryName
){
/*noverbatim
CUT*/
char *s;
#ifndef __MACOS__
for( s = pszDirectoryName ; *s ; s++ )if( *s == '\\' )*s= '/';
#endif
#ifdef WIN32
/* Note that there is no need to convert the / characters to \
because / is a valid separator under Windows NT just as \ is. */
return _rmdir(pszDirectoryName);
#else
return rmdir(pszDirectoryName);
#endif
}
/*POD
=H file_remove
@c Remove a file
Nothing special, it is just a placeholder. This function performs the UNIX T<remove> functionality. This
function also exists under WIN32, therefore this function is only a placeholder.
/*FUNCTION*/
int file_remove(char *pszFileName
){
/*noverbatim
CUT*/
char *s;
#ifndef __MACOS__
for( s = pszFileName ; *s ; s++ )if( *s == '\\' )*s= '/';
#endif
return remove(pszFileName);
}
/* Note that the recursive calls can use the same file name buffer and
still the different concurrently running threads use different buffers.
This would NOT be the case if buffer is static inside the recursive function.
We could use an auto buffer but that is waste of stack space.
*/
#define MAX_FNLEN 1024
int file_deltree_r(char * buffer){
tDIR DirList;
DIR*pDirList;
struct dirent *pD;
int dirlen;
dirlen=strlen(buffer);
#ifdef __MACOS__
if( buffer[dirlen-1] != ':' ){
dirlen++;
if( dirlen >= MAX_FNLEN )return -1;
strcpy(buffer+dirlen-1,":");
}
#else
if( buffer[dirlen-1] != '/' ){
dirlen++;
if( dirlen >= MAX_FNLEN )return -1;
strcpy(buffer+dirlen-1,"/");
}
#endif
pDirList = file_opendir(buffer,&DirList);
if( pDirList == NULL )return -1;
while( pD = file_readdir(pDirList) ){
/* skip . and .. directories */
if( pD->d_name[0] == '.' &&
( pD->d_name[1] == (char)0 ||
( pD->d_name[1] == '.' && pD->d_name[2] == (char)0 ) ) )continue;
if( dirlen+strlen(pD->d_name) >= MAX_FNLEN )return -1;
strcpy(buffer+dirlen,pD->d_name);
if( file_isdir(buffer) )
file_deltree_r(buffer);
else
file_remove(buffer);
}
file_closedir(pDirList);
dirlen--;
buffer[dirlen] = (char)0;
return file_rmdir(buffer);
}
/*POD
=H file_deltree
@c Delete a directory tree
/*FUNCTION*/
int file_deltree(char *pszDirectoryName
){
/*noverbatim
CUT*/
char buffer[MAX_FNLEN];
#ifndef __MACOS__
char *s;
for( s = pszDirectoryName ; *s ; s++ )if( *s == '\\' )*s= '/';
#endif
if( ! file_exists(pszDirectoryName) )return -1;
if( ! file_isdir(pszDirectoryName) )return -1;
/* does not fit into the buffer ? */
if( strlen(pszDirectoryName) >= MAX_FNLEN )return -1;
strcpy(buffer,pszDirectoryName);
return file_deltree_r(buffer);
}
/*POD
=H file_MakeDirectory
This function is a bit out of the line of the other functions in this module. This function uses the
T<file_mkdir> function to create a directory. The difference is that this function tries to create a
directory recursively. For example you can create the directory
T</usr/bin/scriba>
with a simple call and the function will create the directories T</usr> if it did not exist, then
T</usr/bin> and finally T</usr/bin/scriba> The function fails if the directory can not be created
because of access restrictions or because the directory path or a sub path already exists, and is not
a directory.
The argument of the function is the name of the desired directory.
The function alters the argument replacing each \ character to /
The argument may end with / since v1.0b30
If the argument is a Windows full path including the drive letter, like
'C:' the function tries to create the directory 'C:', which fails, but
ignores this error because only the last creation in the line down the
directory path is significant.
In case of error, the argument may totally be destroyed.
/*FUNCTION*/
int file_MakeDirectory(char *pszDirectoryName
){
/*noverbatim
CUT*/
char *s;
int iDirNameLen,i,iResult;
#ifndef __MACOS__
for( s=pszDirectoryName ; *s ; s++ )
if( *s == '\\' )*s = '/';
#endif
iDirNameLen = strlen(pszDirectoryName);
i = 0;
iResult = 0;
while( i < iDirNameLen ){
#ifdef __MACOS__
while( pszDirectoryName[i] && pszDirectoryName[i] != ':' )i++;
#else
while( pszDirectoryName[i] && pszDirectoryName[i] != '/' )i++;
#endif