/
fsys.c
1404 lines (1185 loc) · 35 KB
/
fsys.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 2010 by Sean Conner. All Rights Reserved.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
*********************************************************************/
#ifdef __linux
# define _DEFAULT_SOURCE
# define _BSD_SOURCE
# define _POSIX_SOURCE
#endif
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <luaconf.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#if !defined(__APPLE__)
# include <sys/sysmacros.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <libgen.h>
#include <utime.h>
#include <fnmatch.h>
#include <glob.h>
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
# error You need to compile against Lua 5.1 or higher
#endif
#define TYPE_DIR "org.conman.fsys:dir"
#define TYPE_EXPAND "org.conman.fsys:expand"
#if LUA_VERSION_NUM == 501
# define luaL_setfuncs(L,reg,up) luaI_openlib((L),NULL,(reg),(up))
#endif
/*************************************************************************/
static void fsysL_pushperm(lua_State *L,mode_t mode)
{
char perms[12];
perms[ 0] = (mode & S_ISUID) ? 'u' : '-';
perms[ 1] = (mode & S_ISGID) ? 'g' : '-';
perms[ 2] = (mode & S_ISVTX) ? 's' : '-';
perms[ 3] = (mode & S_IRUSR) ? 'r' : '-';
perms[ 4] = (mode & S_IWUSR) ? 'w' : '-';
perms[ 5] = (mode & S_IXUSR) ? 'x' : '-';
perms[ 6] = (mode & S_IRGRP) ? 'r' : '-';
perms[ 7] = (mode & S_IWGRP) ? 'w' : '-';
perms[ 8] = (mode & S_IXGRP) ? 'x' : '-';
perms[ 9] = (mode & S_IROTH) ? 'r' : '-';
perms[10] = (mode & S_IWOTH) ? 'w' : '-';
perms[11] = (mode & S_IXOTH) ? 'x' : '-';
lua_pushlstring(L,perms,sizeof(perms));
}
/*************************************************************************/
static void fsysL_pushmode(lua_State *L,mode_t mode)
{
lua_createtable(L,0,5);
fsysL_pushperm(L,mode); lua_setfield(L,-2,"perms");
lua_pushboolean(L,mode & S_ISUID); lua_setfield(L,-2,"setuid");
lua_pushboolean(L,mode & S_ISGID); lua_setfield(L,-2,"getgid");
lua_pushboolean(L,mode & S_ISVTX); lua_setfield(L,-2,"sticky");
if (S_ISREG(mode))
lua_pushliteral(L,"file");
else if (S_ISDIR(mode))
lua_pushliteral(L,"dir");
else if (S_ISCHR(mode))
lua_pushliteral(L,"chardev");
else if (S_ISBLK(mode))
lua_pushliteral(L,"blockdev");
else if (S_ISFIFO(mode))
lua_pushliteral(L,"pipe");
else if (S_ISLNK(mode))
lua_pushliteral(L,"link");
else if (S_ISSOCK(mode))
lua_pushliteral(L,"socket");
else
lua_pushliteral(L,"?");
lua_setfield(L,-2,"type");
}
/*************************************************************************/
static void fsysL_pushdev(lua_State *L,dev_t dev)
{
lua_createtable(L,0,2);
lua_pushinteger(L,major(dev));
lua_setfield(L,-2,"major");
lua_pushinteger(L,minor(dev));
lua_setfield(L,-2,"minor");
}
/*************************************************************************/
static void fsysL_pushstat(lua_State *L,struct stat const *status)
{
assert(L != NULL);
assert(status != NULL);
lua_createtable(L,0,13);
fsysL_pushdev (L,status->st_dev); lua_setfield(L,-2,"dev");
lua_pushinteger(L,status->st_ino); lua_setfield(L,-2,"inode");
fsysL_pushmode (L,status->st_mode); lua_setfield(L,-2,"mode");
lua_pushinteger(L,status->st_nlink); lua_setfield(L,-2,"nlink");
lua_pushinteger(L,status->st_uid); lua_setfield(L,-2,"uid");
lua_pushinteger(L,status->st_gid); lua_setfield(L,-2,"gid");
fsysL_pushdev (L,status->st_rdev); lua_setfield(L,-2,"rdev");
lua_pushinteger(L,status->st_size); lua_setfield(L,-2,"size");
lua_pushinteger(L,status->st_blksize); lua_setfield(L,-2,"blocksize");
lua_pushinteger(L,status->st_blocks); lua_setfield(L,-2,"blocks");
lua_pushinteger(L,status->st_atime); lua_setfield(L,-2,"atime");
lua_pushinteger(L,status->st_mtime); lua_setfield(L,-2,"mtime");
lua_pushinteger(L,status->st_ctime); lua_setfield(L,-2,"ctime");
}
/*************************************************************************/
static mode_t fsysL_checkmode(lua_State *L,int idx,mode_t def)
{
size_t len;
char const *txt;
mode_t mode;
if (lua_isnoneornil(L,idx))
return def;
mode = 0;
txt = luaL_checklstring(L,idx,&len);
if (len == 3)
{
if (*txt++ == 'r')
mode |= S_IRUSR | S_IRGRP | S_IROTH;
if (*txt++ == 'w')
mode |= S_IWUSR | S_IWGRP | S_IWOTH;
if (*txt++ == 'x')
mode |= S_IXUSR | S_IXGRP | S_IXOTH;
return mode;
}
if (len == 12)
{
if (*txt++ == 'u')
mode |= S_ISUID;
if (*txt++ == 'g')
mode |= S_ISGID;
if (*txt++ == 's')
mode |= S_ISVTX;
len -= 3;
}
if (len >= 9)
{
mode |= (*txt++ == 'r') ? S_IRUSR : 0;
mode |= (*txt++ == 'w') ? S_IWUSR : 0;
mode |= (*txt++ == 'x') ? S_IXUSR : 0;
mode |= (*txt++ == 'r') ? S_IRGRP : 0;
mode |= (*txt++ == 'w') ? S_IWGRP : 0;
mode |= (*txt++ == 'x') ? S_IXGRP : 0;
mode |= (*txt++ == 'r') ? S_IROTH : 0;
mode |= (*txt++ == 'w') ? S_IWOTH : 0;
mode |= (*txt++ == 'x') ? S_IXOTH : 0;
}
return mode;
}
/*************************************************************************/
static int fsys_stat(lua_State *L)
{
struct stat status;
if (lua_isstring(L,1))
{
if (stat(lua_tostring(L,1),&status) < 0)
goto fsys_stat_err;
}
else if (lua_isuserdata(L,1))
{
if (!luaL_callmeta(L,1,"_tofd"))
goto fsys_stat_err;
if (fstat(luaL_checkinteger(L,-1),&status) < 0)
goto fsys_stat_err;
}
fsysL_pushstat(L,&status);
lua_pushinteger(L,0);
return 2;
fsys_stat_err:
lua_pushnil(L);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_lstat(lua_State *L)
{
struct stat status;
if (lstat(luaL_checkstring(L,1),&status) < 0)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
}
else
{
fsysL_pushstat(L,&status);
lua_pushinteger(L,0);
}
return 2;
}
/*************************************************************************/
static int fsys_chmod(lua_State *L)
{
errno = 0;
if (lua_isstring(L,1))
chmod(luaL_checkstring(L,1),fsysL_checkmode(L,2,0666));
else if (luaL_callmeta(L,1,"_tofd"))
fchmod(luaL_checkinteger(L,-1),fsysL_checkmode(L,2,0664));
else
errno = EINVAL;
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_umask(lua_State *L)
{
fsysL_pushperm(L,~umask(~fsysL_checkmode(L,1,0777)) & 0777);
return 1;
}
/*************************************************************************/
static int fsys_mkfifo(lua_State *L)
{
errno = 0;
mkfifo(luaL_checkstring(L,1),fsysL_checkmode(L,2,0666));
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_mkdir(lua_State *L)
{
errno = 0;
mkdir(luaL_checkstring(L,1),fsysL_checkmode(L,2,0777));
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_rmdir(lua_State *L)
{
errno = 0;
rmdir(luaL_checkstring(L,1));
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_redirect(lua_State *L)
{
if (luaL_callmeta(L,1,"_tofd") && luaL_callmeta(L,2,"_tofd"))
{
int orig = luaL_checkinteger(L,-2);
int copy = luaL_checkinteger(L,-1);
errno = 0;
dup2(orig,copy);
}
else
errno = EINVAL;
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_chroot(lua_State *L)
{
if (chroot(luaL_checkstring(L,1)) < 0)
{
lua_pushboolean(L,false);
lua_pushinteger(L,errno);
}
else
{
chdir("/");
lua_pushboolean(L,true);
lua_pushinteger(L,0);
}
return 2;
}
/*************************************************************************/
static int fsys_chdir(lua_State *L)
{
errno = 0;
chdir(luaL_checkstring(L,1));
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_symlink(lua_State *L)
{
char const *old = luaL_checkstring(L,1);
char const *new = luaL_checkstring(L,2);
errno = 0;
symlink(old,new);
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/*************************************************************************/
static int fsys_link(lua_State *L)
{
char const *old = luaL_checkstring(L,1);
char const *new = luaL_checkstring(L,2);
errno = 0;
link(old,new);
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/**********************************************************************/
static int fsys_readlink(lua_State *L)
{
char buffer[FILENAME_MAX];
int size;
size = readlink(luaL_checkstring(L,1),buffer,sizeof(buffer));
if (size == -1)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
}
else
{
lua_pushlstring(L,buffer,size);
lua_pushinteger(L,0);
}
return 2;
}
/*********************************************************************/
static int fsys_utime(lua_State *L)
{
char const *path;
struct utimbuf when;
path = luaL_checkstring(L,1);
when.modtime = luaL_checknumber(L,2);
when.actime = luaL_optnumber(L,3,when.modtime);
errno = 0;
utime(path,&when);
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/**********************************************************************/
static int fsys_access(lua_State *L)
{
char const *fname = luaL_checkstring(L,1);
char const *tmode = luaL_checkstring(L,2);
int mode = 0;
for (int i = 0 ; tmode[i] != '\0'; i++)
{
switch(toupper(tmode[i]))
{
case 'R': mode |= R_OK; break;
case 'W': mode |= W_OK; break;
case 'X': mode |= X_OK; break;
case 'F': mode |= F_OK; break;
default: break;
}
}
errno = 0;
access(fname,mode);
lua_pushboolean(L,errno == 0);
lua_pushinteger(L,errno);
return 2;
}
/************************************************************************/
static int fsys_getcwd(lua_State *L)
{
char cwd[FILENAME_MAX];
errno = 0;
lua_pushstring(L,getcwd(cwd,sizeof(cwd)));
lua_pushinteger(L,errno);
return 2;
}
/************************************************************************/
static int dir_meta___tostring(lua_State *L)
{
lua_pushfstring(L,"directory (%p)",lua_touserdata(L,1));
return 1;
}
/*************************************************************************/
static int dir_meta___gc(lua_State *L)
{
DIR **dir = luaL_checkudata(L,1,TYPE_DIR);
if (*dir != NULL)
{
closedir(*dir);
*dir = NULL;
}
return 0;
}
/*************************************************************************/
static int dir_meta__tofd(lua_State *L)
{
#if defined(__SunOS_5_10)
lua_pushinteger(L,-1);
#else
lua_pushinteger(L,dirfd(*(DIR **)luaL_checkudata(L,1,TYPE_DIR)));
#endif
return 1;
}
/*************************************************************************/
static int dir_meta_rewind(lua_State *L)
{
rewinddir(*(DIR **)luaL_checkudata(L,1,TYPE_DIR));
return 0;
}
/*************************************************************************
*
* Usage: entry = dir:read()
*
* Desc: Return the next filename in the directory
*
* Return: entry (string) filename, nil when at end of directory
*
* Note: This function will return "." (current directory) and
* ".." (parent) directory. Also, this function, upon end-
* of-directory, will *NOT* close the directory.
*
**************************************************************************/
static int dir_meta_read(lua_State *L)
{
struct dirent *entry = readdir(*(DIR **)luaL_checkudata(L,1,TYPE_DIR));
if (entry)
lua_pushstring(L,entry->d_name);
else
lua_pushnil(L);
return 1;
}
/*************************************************************************
*
* Usage: entry = dir:next()
*
* Desc: Return the next filename in the directory.
*
* Return: entry (string) filename, nil when at end of directory
*
* Note: This function will *NOT* return "." (current directory)
* or ".." (parent directory). Also, this function *WILL*
* close the directory when it reaches the end.
*
* The intent of this function is to be used as an iterator,
* where getting "." or ".." is most likely *not* something
* that is wanted.
*
**************************************************************************/
static int dir_meta_nil(lua_State *L)
{
/*--------------------------------------------------------------------
; This function is used for our iterator function when we can't open a
; directory for reading.
;---------------------------------------------------------------------*/
(void)L;
return 0;
}
static int dir_meta_next(lua_State *L)
{
struct dirent *entry;
DIR **dir = luaL_checkudata(L,1,TYPE_DIR);
while(true)
{
entry = readdir(*dir);
if (entry == NULL)
{
dir_meta___gc(L);
lua_pushnil(L);
return 1;
}
else
{
if (
(strcmp(entry->d_name,".") != 0)
&& (strcmp(entry->d_name,"..") != 0)
)
{
lua_pushstring(L,entry->d_name);
return 1;
}
}
}
}
/*************************************************************************/
static int fsys_opendir(lua_State *L)
{
char const *dname;
DIR *dir;
DIR **pdir;
dname = luaL_optstring(L,1,".");
dir = opendir(dname);
if (dir == NULL)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
return 2;
}
pdir = lua_newuserdata(L,sizeof(DIR *));
*pdir = dir;
luaL_getmetatable(L,TYPE_DIR);
lua_setmetatable(L,-2);
lua_pushinteger(L,0);
return 2;
}
/*************************************************************************/
static int fsys_dir(lua_State *L)
{
fsys_opendir(L);
lua_pop(L,1); /* remove errno from stack */
if (lua_isnil(L,-1))
lua_pushcfunction(L,dir_meta_nil);
else
lua_pushcfunction(L,dir_meta_next);
lua_insert(L,-2);
lua_pushnil(L); /* no initial value */
lua_pushvalue(L,-2); /* Lua 5.4 toclose variable, safe to set pre 5.4 */
return 4;
}
/*************************************************************************/
static int fsys__safename(lua_State *L)
{
char const *old = luaL_checkstring(L,1);
luaL_Buffer buf;
luaL_buffinit(L,&buf);
while(*old)
{
switch(*old)
{
case '/': luaL_addstring(&buf,"%2F"); break;
case '%': luaL_addstring(&buf,"%25"); break;
default: luaL_addchar (&buf,*old); break;
}
old++;
}
luaL_pushresult(&buf);
return 1;
}
/**********************************************************************
* Usage: base = fsys.basename(path)
* Desc: Return the last segment from a pathname.
* Input: path (string)
* Return: base (string)
*
* Note: The original pathname can be reconstructed by the following
* code:
*
* path = "/home/spc/spc"
* base = fsys.basename(path)
* dir = fsys.dirname(path)
* full = dir .. "/" .. base
* assert(full == path)
*
**********************************************************************/
static int fsys_basename(lua_State *L)
{
char const *name = luaL_checkstring(L,1);
char const *slash = strrchr(name,*LUA_DIRSEP);
if (slash)
lua_pushstring(L,slash + 1);
else
lua_pushvalue(L,1);
return 1;
}
/**********************************************************************
* Usage: dir = fsys.dirname(path)
* Desc: Return the path of a filename (path minus the last segment)
* Input: path (string)
* Return: dir (string)
*
* Note: See note for fsys.basename().
**********************************************************************/
static int fsys_dirname(lua_State *L)
{
char const *name = luaL_checkstring(L,1);
char const *slash = strrchr(name,*LUA_DIRSEP);
if (slash)
lua_pushlstring(L,name,(size_t)(slash - name));
else
lua_pushstring(L,".");
return 1;
}
/***********************************************************************
* Usage: ext = fsys.extension(path)
* Desc: Return the extension (if any) of a filename
* Input: path (string)
* Return: ext (string)
*
* Note: Returns an empty string if no extension exists.
**********************************************************************/
static int fsys_extension(lua_State *L)
{
char const *name = luaL_checkstring(L,1);
char const *slash = strrchr(name,*LUA_DIRSEP);
char const *dot = strrchr(name,'.');
if (dot && (dot > slash))
lua_pushstring(L,dot + 1);
else
lua_pushliteral(L,"");
return 1;
}
/************************************************************************/
static int fsys_filename(lua_State *L)
{
char const *name = luaL_checkstring(L,1);
char const *slash = strrchr(name,*LUA_DIRSEP);
char const *dot = strrchr(name,'.');
if (dot && (dot > slash))
lua_pushlstring(L,name,(size_t)(dot - name));
else
lua_pushvalue(L,1);
return 1;
}
/************************************************************************/
static int fsyslib_close(lua_State *L)
{
#if LUA_VERSION_NUM == 501
FILE **pfp = luaL_checkudata(L,1,LUA_FILEHANDLE);
fclose(*pfp);
*pfp = NULL;
lua_pushboolean(L,1);
return 1;
#else
luaL_Stream *pfp = luaL_checkudata(L,1,LUA_FILEHANDLE);
fclose(pfp->f);
pfp->closef = NULL;
lua_pushboolean(L,1);
return 1;
#endif
}
/***********************************************************************
* Usage: pipe = fsys.pipe([binary])
* Desc: Create a Unix pipe
* Input: binary (boolean) true if creating a binary stream
* Return: pipe (table)
* * read = read stream
* * wrote = write stream
************************************************************************/
static int fsys_pipe(lua_State *L)
{
#if LUA_VERSION_NUM == 501
FILE **pfpread;
FILE **pfpwrite;
FILE *fpr;
FILE *fpw;
int fh[2];
char const *rm;
char const *wm;
/*------------------------------------------------------------------------
; This is done first because there may not be a paramter, and if we create
; the return data, we might get behavior we weren't expecting.
;-------------------------------------------------------------------------*/
if (lua_isboolean(L,1))
{
rm = "rb";
wm = "wb";
}
else
{
rm = "r";
wm = "w";
}
/*---------------------------------------------------------------------
; Create our return table. We initialize the FILE pointers to NULL to
; signify a "closed" file. We do this all up front first before anything
; else in case things go wrong to the calls in Lua. By the time we get
; through this code, we should be okay to continue without worry of losing
; an open file.
;-------------------------------------------------------------------------*/
lua_createtable(L,0,2);
pfpread = lua_newuserdata(L,sizeof(FILE *));
*pfpread = NULL;
luaL_getmetatable(L,LUA_FILEHANDLE);
lua_setmetatable(L,-2);
lua_createtable(L,0,0);
lua_pushcfunction(L,fsyslib_close);
lua_setfield(L,-2,"__close");
lua_setfenv(L,-2);
lua_setfield(L,-2,"read");
pfpwrite = lua_newuserdata(L,sizeof(FILE *));
*pfpwrite = NULL;
luaL_getmetatable(L,LUA_FILEHANDLE);
lua_setmetatable(L,-2);
lua_createtable(L,0,0);
lua_pushcfunction(L,fsyslib_close);
lua_setfield(L,-2,"__close");
lua_setfenv(L,-2);
lua_setfield(L,-2,"write");
/*---------------------------------------------------------------------
; now we can create our pipe and reopen them as FILE*s. If these fail,
; the table and userdata structures we created will be reclaimed properly
; in time (since to Lua, they're closed files).
;-----------------------------------------------------------------------*/
if (pipe(fh) < 0)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
return 2;
}
fpr = fdopen(fh[0],rm);
if (fpr == NULL)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
close(fh[1]);
close(fh[0]);
return 2;
}
fpw = fdopen(fh[1],wm);
if (fpw == NULL)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
close(fh[1]);
fclose(fpr);
return 2;
}
/*-------------------------------------------------------------------
; everything has gone as planned. Set the open files and return it
; back to our Lua script.
;-------------------------------------------------------------------*/
*pfpread = fpr;
*pfpwrite = fpw;
lua_pushinteger(L,0);
return 2;
#else
luaL_Stream *readp;
luaL_Stream *writep;
FILE *fpr;
FILE *fpw;
int fh[2];
char const *rm;
char const *wm;
if (lua_isboolean(L,1))
{
rm = "rb";
wm = "wb";
}
else
{
rm = "r";
wm = "w";
}
lua_createtable(L,0,2);
readp = lua_newuserdata(L,sizeof(luaL_Stream));
readp->f = NULL;
readp->closef = NULL;
luaL_getmetatable(L,LUA_FILEHANDLE);
lua_setmetatable(L,-2);
lua_setfield(L,-2,"read");
writep = lua_newuserdata(L,sizeof(luaL_Stream));
writep->f = NULL;
writep->closef = NULL;
luaL_getmetatable(L,LUA_FILEHANDLE);
lua_setmetatable(L,-2);
lua_setfield(L,-2,"write");
if (pipe(fh) < 0)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
return 2;
}
fpr = fdopen(fh[0],rm);
if (fpr == NULL)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
close(fh[1]);
close(fh[0]);
return 2;
}
fpw = fdopen(fh[1],wm);
if (fpw == NULL)
{
lua_pushnil(L);
lua_pushinteger(L,errno);
close(fh[1]);
fclose(fpr);
return 2;
}
readp->f = fpr;
readp->closef = fsyslib_close;
writep->f = fpw;
writep->closef = fsyslib_close;
lua_pushinteger(L,0);
return 2;
#endif
}
/***********************************************************************/
static int fsys_isatty(lua_State *L)
{
if (!luaL_callmeta(L,1,"_tofd"))
lua_pushboolean(L,false);
else
lua_pushboolean(L,isatty(luaL_checkinteger(L,-1)));
return 1;
}
/************************************************************************/
static int fsys_fnmatch(lua_State *L)
{
char const *pattern = luaL_checkstring(L,1);
char const *filename = luaL_checkstring(L,2);
char const *tflags = luaL_optstring(L,3,"");
int flags = 0;
for ( ; *tflags ; tflags++)
{
switch(*tflags)
{
case '\\': flags |= FNM_NOESCAPE; break;
case '/' : flags |= FNM_PATHNAME; break;
case '.' : flags |= FNM_PERIOD; break;
}
}
lua_pushboolean(L,fnmatch(pattern,filename,flags) == 0);
return 1;
}
/************************************************************************/
int fsys_expand(lua_State *L)
{
char const *pattern = luaL_checkstring(L,1);
glob_t gbuf;
int rc;
memset(&gbuf,0,sizeof(gbuf));
rc = glob(pattern,0,NULL,&gbuf);
if (rc != 0)
{
lua_pushnil(L);
switch(rc)
{