forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.c
2065 lines (1728 loc) · 56.6 KB
/
files.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 and directory functions and efuns.
*
*---------------------------------------------------------------------------
*/
#include "driver.h"
#include "typedefs.h"
#include "my-alloca.h"
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#define generic_dirent dirent
#define DIRENT_NLENGTH(dirent) (strlen((dirent)->d_name))
#if defined(CYGWIN)
extern int lstat(const char *, struct stat *);
#endif
/*-------------------------------------------------------------------------*/
#include "files.h"
#include "array.h"
#include "comm.h"
#include "filestat.h"
#include "interpret.h"
#include "lex.h" /* lex_close() */
#include "main.h"
#include "mempools.h"
#include "mstrings.h"
#include "simulate.h"
#include "stdstrings.h"
#include "strfuns.h"
#include "svalue.h"
#include "xalloc.h"
#include "../mudlib/sys/driver_hook.h"
#include "../mudlib/sys/files.h"
/*-------------------------------------------------------------------------*/
static Bool
isdir (const char *path)
/* Helper function for copy and move: test if <path> is a directory.
*/
{
struct stat stats;
return ixstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
} /* isdir() */
/*-------------------------------------------------------------------------*/
static void
strip_trailing_slashes (char *path)
/* Strip trailing slashed from <path>, which is modified in-place.
*/
{
int last;
last = strlen (path) - 1;
while (last > 0 && path[last] == '/')
path[last--] = '\0';
} /* strip_trailing_slashes() */
/*-------------------------------------------------------------------------*/
static int
copy_file (const char *from, const char *to, int mode)
/* Copy the file <from> to <to> with access <mode>.
* Return 0 on success, 1 or errno on failure.
*/
{
int fromfd, tofd;
ssize_t count;
char buf[4096];
fromfd = ixopen(from, O_RDONLY);
if (fromfd < 0)
{
debug_message("copy_file(): can't open '%s': %s\n", from, strerror(errno));
return 1;
}
/* We have to unlink 'to', because it may be a symlink.
O_CREAT won't remove that. */
if (unlink(to) < 0 && errno != ENOENT)
{
debug_message("copy_file(): can't unlink '%s': %s\n", to, strerror(errno));
close(fromfd);
return 1;
}
tofd = ixopen3(to, O_WRONLY|O_CREAT|O_TRUNC, mode);
if (tofd < 0)
{
debug_message("copy_file(): can't open '%s': %s\n", to, strerror(errno));
close(fromfd);
return 1;
}
#ifdef HAVE_FCHMOD
/* We have given the file mode to ixopen3, this is just to counter umask.
So don't worry if this fchmod fails. */
fchmod(tofd, mode);
#endif
do
{
ssize_t written;
count = read(fromfd, buf, sizeof(buf));
if (count < 0)
{
debug_message("copy_file(): can't read from '%s': %s\n", from, strerror(errno));
close(fromfd);
close(tofd);
unlink(to);
return 1;
}
written = 0;
while (written < count)
{
ssize_t len;
len = write(tofd, buf + written, count - written);
if (len <= 0)
{
debug_message("copy_file(): can't write to '%s': %s\n", to, strerror(errno));
close(fromfd);
close(tofd);
unlink(to);
return 1;
}
written += len;
}
} while (count > 0);
FCOUNT_READ(from);
FCOUNT_WRITE(to);
close(fromfd);
close(tofd);
#ifndef HAVE_FCHMOD
chmod(to, mode);
#endif
return 0;
} /* copy_file() */
/*-------------------------------------------------------------------------*/
static int
move_file (const char *from, const char *to)
/* Move the file or directory <from> to <to>, copying it if necessary.
* Result is 0 on success, 1 or errno on failure.
*/
{
struct stat fromstat, tostat;
if (lstat(from, &fromstat) < 0)
{
debug_message("move_file(): can't lstat '%s': %s\n", from, strerror(errno));
return 1;
}
if (!lstat(to, &tostat))
{
if (fromstat.st_dev == tostat.st_dev
&& fromstat.st_ino == tostat.st_ino)
{
/* Same file. */
debug_message("move_file(): '%s' and '%s' are the same.\n", from, to);
return 1;
}
if (S_ISDIR(tostat.st_mode))
{
debug_message("move_file(): destination '%s' is a directory.\n", to);
return 1;
}
}
if (rename(from, to))
{
if (errno == EXDEV)
{
if (!S_ISREG(fromstat.st_mode))
{
debug_message("move_file(): can't move '%s' across filesystems: Not a regular file.\n", to);
return 1;
}
if (copy_file(from, to, fromstat.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)))
return 1;
unlink(from);
}
else
{
debug_message("move_file(): can't rename '%s' to '%s': %s\n", to, from, strerror(errno));
return 1;
}
}
FCOUNT_DEL(from);
return 0;
} /* move_file() */
/*-------------------------------------------------------------------------*/
/* If your system's native methods can provide a speedier directory access
* than opendir/readdir/closedir(), implement them as xopendir() and friends,
* and define XDIR to the datastructure you need.
*/
#ifndef XDIR
/* Use the normal Posix calls */
struct xdirect
{
/* inode and position in directory aren't usable in a portable way,
* so why support them anyway?
*/
short d_namlen;
char *d_name;
int size;
int time;
int atime;
int mode;
};
#define XOPENDIR(dest, path) (\
(!chdir(path) &&\
NULL != ((dest) = opendir("."))) ||\
(chdir(mud_lib),MY_FALSE)\
)
#define xclosedir(dir_ptr) (chdir(mud_lib),closedir(dir_ptr))
#define xrewinddir(dir_ptr) rewinddir(dir_ptr)
#define XDIR DIR
/*-------------------------------------------------------------------------*/
static struct xdirect *
xreaddir (XDIR *dir_ptr, int mask)
/* Read the next entry from <dir_ptr> and return it via a pointer
* to a static xdirect structure.
* <mask> is tested for GETDIR_SIZES, GETDIR_DATES, GETDIR_ACCESS,
* GETDIR_MODES - only the data for requested items is returned.
*/
{
static struct xdirect xde;
struct generic_dirent *de;
int namelen;
struct stat st;
de = readdir(dir_ptr);
if (!de)
return NULL;
namelen = DIRENT_NLENGTH(de);
xde.d_namlen = namelen;
xde.d_name = de->d_name;
if (mask & (GETDIR_SIZES|GETDIR_DATES|GETDIR_ACCESS|GETDIR_MODES) )
{
if (ixstat(xde.d_name, &st) == -1) /* who knows... */
{
xde.size = FSIZE_NOFILE;
xde.time = 0;
xde.atime = 0;
xde.mode = 0;
}
else
{
if (S_IFDIR & st.st_mode)
xde.size = FSIZE_DIR;
else
xde.size = st.st_size;
xde.time = st.st_mtime;
xde.atime = st.st_atime;
xde.mode = st.st_mode;
}
}
return &xde;
} /* xreaddir() */
#endif /* XDIR */
/*-------------------------------------------------------------------------*/
static int
pstrcmp (const void *p1, const void *p2)
/* qsort() comparison function: strcmp() on two svalue-strings.
*/
{
return mstrcmp(((svalue_t*)p1)->u.str, ((svalue_t*)p2)->u.str);
} /* pstrcmp() */
/*-------------------------------------------------------------------------*/
struct get_dir_error_context
{
error_handler_t head;
XDIR *dirp;
vector_t *v;
};
/*-------------------------------------------------------------------------*/
static void
get_dir_error_handler (error_handler_t *arg)
/* T_ERROR_HANDLER function: <arg> is a (struct get_dir_error_context*)
* with the directory which needs to be closed.
*/
{
struct get_dir_error_context *ecp;
ecp = (struct get_dir_error_context *)arg;
xclosedir(ecp->dirp);
if (ecp->v)
free_array(ecp->v);
} /* get_dir_error_handler() */
/*-------------------------------------------------------------------------*/
/* Error handling structure to close a iconv conversion descriptor.
*/
struct iconv_error_context
{
error_handler_t head;
iconv_t cd;
};
/*-------------------------------------------------------------------------*/
static void
iconv_error_handler (error_handler_t *arg)
/* T_ERROR_HANDLER function: <arg> is a (struct iconv_error_context*)
* with a conversion descriptor that needs to be closed.
* If the conversion descriptor is -1, then nothing needs to be done.
*/
{
struct iconv_error_context *ecp = (struct iconv_error_context *)arg;
if (iconv_valid(ecp->cd))
iconv_close(ecp->cd);
xfree(ecp);
} /* iconv_error_handler() */
/*-------------------------------------------------------------------------*/
svalue_t *
f_copy_file (svalue_t *sp)
/* EFUN copy_file()
*
* int copy_file(string from, string to)
*
* The efun rename() will copy the file <from> to the new name <to>.
* If <to> is a directory, then <from> will be placed in that
* directory and keep its original name.
*
* You must have read permission for <from> and write permission
* for the target name to copy the file.
*
* On successfull completion copy_file() will return 0. If any error
* occurs, a non-0 value is returned.
*
* TODO: Add two more args: start, length to implement slicing?
* TODO:: See f-981229-10 "truncate_file()".
* TODO: Return useful error messages.
*/
{
struct stat to_stats, from_stats;
string_t *path;
int result;
/* Check the arguments */
do
{
char fromB[MAXPATHLEN+1];
char toB[MAXPATHLEN+1];
result = 1; /* Default: failure */
path = check_valid_path(sp[-1].u.str, current_object, STR_COPY_FILE
, MY_FALSE);
if (!path)
break;
if (!convert_path_to_native_buf(get_txt(path), mstrsize(path), fromB, sizeof(fromB)))
{
push_string(inter_sp, path);
errorf("Could not encode path '%s'.\n", get_txt(path));
}
free_mstring(path);
if (isdir(fromB))
break;
path = check_valid_path(sp->u.str, current_object, STR_COPY_FILE
, MY_TRUE);
if (!path)
break;
if (!mstrsize(path) && mstreq(sp->u.str, STR_SLASH))
{
strcpy(toB, "./");
}
else if (!convert_path_to_native_buf(get_txt(path), mstrsize(path), toB, sizeof(toB)))
{
push_string(inter_sp, path);
errorf("Could not encode path '%s'.\n", get_txt(path));
}
free_mstring(path);
strip_trailing_slashes(fromB);
if (isdir(toB))
{
/* Target is a directory; build full target filename. */
size_t tolen = strlen(toB);
char *cp = strrchr(fromB, '/');
if (cp)
cp++;
else
cp = fromB;
if (tolen + strlen(cp) + 2 > sizeof(toB))
errorf("Target file name too long.\n");
toB[tolen++] = '/';
strcpy(toB + tolen, cp);
}
/* Now copy the file */
if (lstat(fromB, &from_stats) != 0)
{
char *strFromB = convert_path_from_native(fromB, strlen(fromB));
errorf("%s: lstat failed\n", strFromB == NULL ? get_txt(sp[-1].u.str) : strFromB);
break;
}
if (lstat(toB, &to_stats) == 0)
{
if (from_stats.st_dev == to_stats.st_dev
&& from_stats.st_ino == to_stats.st_ino)
{
char tmp[MAXPATHLEN];
size_t fromLen = convert_path_from_native_buf(fromB, strlen(fromB), tmp, sizeof(tmp));
char *strToB = convert_path_from_native(toB, strlen(toB));
errorf("'%s' and '%s' are the same file\n",
fromLen ? tmp : get_txt(sp[-1].u.str),
strToB == NULL ? get_txt(sp[0].u.str) : strToB);
break;
}
if (S_ISDIR(to_stats.st_mode))
{
char *strToB = convert_path_from_native(toB, strlen(toB));
errorf("%s: cannot overwrite directory\n", strToB == NULL ? get_txt(sp[0].u.str) : strToB);
break;
}
}
else if (errno != ENOENT)
{
char *strToB = convert_path_from_native(toB, strlen(toB));
perror("copy_file");
errorf("%s: unknown error\n", strToB == NULL ? get_txt(sp[0].u.str) : strToB);
break;
}
if (!S_ISREG(from_stats.st_mode))
{
char *strFromB = convert_path_from_native(fromB, strlen(fromB));
errorf("cannot copy '%s': Not a regular file\n", strFromB == NULL ? get_txt(sp[-1].u.str) : strFromB);
break;
}
result = copy_file(fromB, toB, from_stats.st_mode & 0777);
}while(0);
/* Clean up the stack and return the result */
free_svalue(sp);
free_svalue(sp-1);
put_number(sp-1, result);
return sp-1;
} /* f_copy_file() */
/*-------------------------------------------------------------------------*/
svalue_t *
f_file_size (svalue_t *sp)
/* EFUN file_size()
*
* int file_size(string file)
*
* Returns the size of the file in bytes.
*
* Size FSIZE_NOFILE (-1) indicates that the file either does not
* exist, or that it is not readable for the calling object/user.
* Size FSIZE_DIR (-2) indicates that it is a directory.
*/
{
long len;
struct stat st;
string_t * file;
st.st_mode = 0; /* Silences ZeroFault/AIX under high optimizations */
file = check_valid_path(sp->u.str, current_object, STR_FILE_SIZE, MY_FALSE);
if (!file)
len = FSIZE_NOFILE;
else
{
char *native = convert_path_to_native(get_txt(file), mstrsize(file));
if (!native || ixstat(native, &st) == -1)
len = FSIZE_NOFILE;
else if (S_IFDIR & st.st_mode)
len = FSIZE_DIR;
else
len = (long)st.st_size;
}
free_mstring(file);
free_svalue(sp);
put_number(sp, len);
return sp;
} /* f_file_size() */
/*-------------------------------------------------------------------------*/
svalue_t *
f_get_dir (svalue_t *sp)
/* EFUN get_dir()
*
* string *get_dir(string str)
* string *get_dir(string str, int mask)
*
* This function takes a path as argument and returns an array of file
* names and attributes in that directory.
*
* Returns 0 if the directory to search in does not exist.
*
* The filename part of the path may contain '*' or '?' as wildcards:
* every '*' matches an arbitrary amount of characters (or just itself).
* Thus get_dir("/path/ *") would return an alphabetically sorted array
* of all files in directory "/path/", or just ({ "/path/ *" }) if this
* file happens to exist.
*
* To query the content of a directory, use the directory name with a
* trailing '/' or '/.', for example get_dir("/path/."). Use the
* directory name as it is to get information about the directory itself.
*
* The optional second argument mask can be used to get
* information about the specified files.
*
* GETDIR_EMPTY (0x00) get_dir returns an empty array (not very
* useful).
* GETDIR_NAMES (0x01) put the alphabetically sorted file names into
* the returned array.
* GETDIR_SIZES (0x02) put the file sizes unsorted into the returned
* array. directories have size FSIZE_DIR (-2).
* GETDIR_DATES (0x04) put the file modification dates unsorted into
* the returned array.
* GETDIR_ACCESS (0x40) put the file access dates unsorted into
* the returned array.
* GETDIR_MODES (0x80) put the unix file modes unsorted into
* the returned array.
*
* GETDIR_ALL (0xDF) Return all.
*
* GETDIR_PATH (0x10) if this mask bit is set, the filenames with
* the full path will be returned
* (GETDIR_NAMES is implied).
* GETDIR_UNSORTED (0x20) if this mask bit is set, the result of will
* _not_ be sorted.
*
* Note: You should use GETDIR_NAMES|GETDIR_UNSORTED to get the entries
* in the same order as with GETDIR_SIZES and GETDIR_DATES.
*
* The values of mask can be added together.
*/
{
vector_t *v;
char path[MAXPATHLEN+1];
int mask;
string_t *fpath = NULL;
mask = sp->u.number;
v = NULL;
do {
static struct get_dir_error_context ec; /* must survive errors */
vector_t *w;
int i, j, count = 0;
XDIR *dirp;
int namelen;
Bool do_match = MY_FALSE;
size_t pathlen;
Bool in_top_dir = MY_FALSE;
struct xdirect *de;
struct stat st;
char *p;
char *regexpr = 0;
int nqueries;
/* Adjust the mask for implied bits */
if (mask & GETDIR_PATH)
mask |= GETDIR_NAMES;
if (!sp[-1].u.str)
break;
fpath = check_valid_path(sp[-1].u.str, current_object, STR_GET_DIR, MY_FALSE);
if (fpath == NULL)
break;
/* We need to modify the returned path, and thus to make a
* writeable copy.
*/
convert_path_to_native_buf(get_txt(fpath), mstrsize(fpath), path, sizeof(path));
free_mstring(fpath);
/* Convert the empty path to '.' */
if (strlen(path) < 2)
{
path[0] = path[0] ? path[0] : '.';
path[1] = '\0';
p = path;
in_top_dir = MY_TRUE;
}
else
{
/* If path ends with '/' or "/." remove it
*/
if ((p = strrchr(path, '/')) == NULL)
p = path;
if ((p[0] == '/' && p[1] == '.' && p[2] == '\0')
|| (p[0] == '/' && p[1] == '\0')
)
*p = '\0';
in_top_dir = (p == path);
}
/* Number of data items per file */
nqueries = ((mask & GETDIR_NAMES) != 0)
+ ((mask & GETDIR_SIZES) != 0)
+ ((mask & GETDIR_DATES) != 0)
+ ((mask & GETDIR_ACCESS) != 0)
+ ((mask & GETDIR_MODES) != 0)
;
if (strchr(p, '*') || ixstat(path, &st) < 0)
{
/* We got a wildcard and/or a directory:
* prepare to match.
*/
if (*p == '\0')
break;
regexpr = alloca(strlen(p)+2);
if (p != path)
{
strcpy(regexpr, p + 1);
*p = '\0';
}
else
{
strcpy(regexpr, p);
strcpy(path, ".");
in_top_dir = MY_TRUE;
}
do_match = MY_TRUE;
}
else if (*p != '\0' && strcmp(path, "."))
{
/* We matched a single file */
svalue_t *stmp;
if (*p == '/' && *(p + 1) != '\0')
p++;
v = allocate_array(nqueries);
stmp = v->item;
if (mask & GETDIR_NAMES)
{
if (mask & GETDIR_PATH)
{
char *native = convert_path_from_native(path, strlen(path));
if (!native)
{
/* If we can't encode it, it doesn't exist... */
free_array(v);
v = ref_array(&null_vector);
break;
}
put_c_string(stmp, native);
if (!compat_mode)
{
string_t *tmp = stmp->u.str;
stmp->u.str = add_slash(tmp);
free_mstring(tmp);
}
}
else
{
char *native = convert_path_from_native(p, strlen(p));
if (!native)
{
/* If we can't encode it, it doesn't exist... */
free_array(v);
v = ref_array(&null_vector);
break;
}
put_c_string(stmp, native);
}
stmp++;
}
if (mask & GETDIR_SIZES){
put_number(stmp, (S_IFDIR & st.st_mode) ? FSIZE_DIR : st.st_size);
stmp++;
}
if (mask & GETDIR_DATES)
{
put_number(stmp, st.st_mtime);
stmp++;
}
if (mask & GETDIR_ACCESS)
{
put_number(stmp, st.st_atime);
stmp++;
}
if (mask & GETDIR_MODES)
{
put_number(stmp, st.st_mode);
stmp++;
}
break;
}
if ( XOPENDIR(dirp, path) == 0)
break;
/* Prepare the error handler to do clean up.
*/
ec.dirp = dirp;
ec.v = NULL;
inter_sp = sp+1;
push_error_handler(get_dir_error_handler, &ec.head);
/* Count files
*/
for (de = xreaddir(dirp, 1); de; de = xreaddir(dirp, 1))
{
namelen = de->d_namlen;
if (do_match)
{
if ( !match_string(regexpr, de->d_name, namelen) )
continue;
}
else
{
if (namelen <= 2 && *de->d_name == '.'
&& (namelen == 1 || de->d_name[1] == '.' ) )
continue;
}
count += nqueries;
if (max_array_size && count >= (long)max_array_size)
break;
}
if (nqueries)
count /= nqueries;
/* Make array and put files on it.
*/
v = allocate_array(count * nqueries);
if (count == 0)
{
/* This is the easy case :-) */
inter_sp--;
xclosedir(dirp);
break;
}
ec.v = v;
xrewinddir(dirp);
w = v;
j = 0;
// do not prepend the path for the mudlib root directory. (effect is
// in the loop below if GETDIR_PATH == true).
if (in_top_dir)
pathlen = 0;
else
pathlen = strlen(path);
/* Taken into account that files might be added/deleted from outside. */
for(i = 0, de = xreaddir(dirp,mask); de; de = xreaddir(dirp,mask))
{
namelen = de->d_namlen;
if (do_match)
{
if ( !match_string(regexpr, de->d_name, namelen) )
continue;
}
else
{
if (namelen <= 2 && *de->d_name == '.'
&& (namelen == 1 || de->d_name[1] == '.' ) )
continue;
}
if (i >= count)
{
/* New file. Don't need efficience here, but consistence. */
vector_t *tmp, *new;
count++;
tmp = allocate_array(nqueries);
new = add_array(v, tmp);
free_array(v);
free_array(tmp);
ec.v = v = new;
w = v;
}
if (mask & GETDIR_NAMES)
{
char result[MAXPATHLEN];
size_t respos = 0;
if (mask & GETDIR_PATH)
{
size_t reslen;
if (!compat_mode)
{
result[0] = '/';
respos++;
}
if (pathlen)
{
reslen = convert_path_from_native_buf(path, pathlen, result + respos, sizeof(result) - respos - 1);
if (!reslen)
continue;
respos += reslen;
result[respos++] = '/';
}
if (namelen)
{
reslen = convert_path_from_native_buf(de->d_name, namelen, result + respos, sizeof(result) - respos);
if (!reslen)
continue;
respos += reslen;
}
}
else
{
respos = convert_path_from_native_buf(de->d_name, namelen, result, sizeof(result));
if (!respos)
continue;
}
put_c_n_string(w->item+j, result, respos);
j++;
}
if (mask & GETDIR_SIZES)
{
put_number(w->item + j, de->size);
j++;
}
if (mask & GETDIR_DATES)
{
put_number(w->item + j, de->time);
j++;
}
if (mask & GETDIR_ACCESS)
{
put_number(w->item + j, de->atime);
j++;
}
if (mask & GETDIR_MODES)
{
put_number(w->item + j, de->mode);
j++;
}
i++;
}
xclosedir(dirp);
if (i < count)
{
/* Some files vanished... */
vector_t *tmp = allocate_array(j);
svalue_t *old = v->item, *new = tmp->item;
while (j--)
*new++ = *old++;
free_empty_vector(v);
v = tmp;
}
inter_sp--;
if ( !((mask ^ 1) & (GETDIR_NAMES|GETDIR_UNSORTED)) )
{
/* Sort by names. */
qsort(v->item, i, sizeof v->item[0] * nqueries, pstrcmp);
}
}while(0);
sp--; /* just an int */
free_string_svalue(sp);
if (v)
put_array(sp, v);
else
put_number(sp, 0);
return sp;
} /* f_get_dir() */
/*-------------------------------------------------------------------------*/
svalue_t *
f_mkdir (svalue_t *sp)
/* EFUN mkdir()
*
* int mkdir(string path)
*
* Make a directory named path. Return 1 for success and 0 for
* failure.
*/
{
int i = 0;
string_t *path;
path = check_valid_path(sp->u.str, current_object, STR_MKDIR, MY_TRUE);
if (path != NULL)
i = (mkdir(convert_path_str_to_native_or_throw(path), 0775) != -1);
free_svalue(sp);
put_number(sp, i);
return sp;
} /* f_mkdir() */
/*-------------------------------------------------------------------------*/
svalue_t *
v_read_bytes (svalue_t *sp, int num_arg)
/* EFUN read_bytes()
*
* string read_bytes (string file, int start, int number)
*
* Reads a given amount of bytes from file.
* If <start> is not given or 0, the file is read from the
* beginning, else from the <start>th byte on. If <start> is
* negative, it is counted from the end of the file.