-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
2303 lines (1971 loc) · 43.7 KB
/
file.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.c -
$Author$
$Date$
created at: Mon Nov 15 12:24:34 JST 1993
Copyright (C) 1993-2000 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#ifdef NT
#include "missing/file.h"
#endif
#include "ruby.h"
#include "rubyio.h"
#include "rubysig.h"
#include "dln.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#else
int flock _((int, int));
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#else
# define MAXPATHLEN 1024
#endif
#include <time.h>
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
#ifndef NT
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
#endif /* NT */
#endif
VALUE rb_time_new _((time_t, time_t));
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifndef HAVE_STRING_H
char *strrchr _((const char*,const char));
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifndef HAVE_LSTAT
#define lstat stat
#endif
VALUE rb_cFile;
VALUE rb_mFileTest;
static VALUE rb_cStat;
static int
apply2files(func, vargs, arg)
int (*func)();
VALUE vargs;
void *arg;
{
int i;
VALUE path;
struct RArray *args = RARRAY(vargs);
for (i=0; i<args->len; i++) {
Check_SafeStr(args->ptr[i]);
}
for (i=0; i<args->len; i++) {
path = args->ptr[i];
if ((*func)(RSTRING(path)->ptr, arg) < 0)
rb_sys_fail(RSTRING(path)->ptr);
}
return args->len;
}
static VALUE
rb_file_path(obj)
VALUE obj;
{
OpenFile *fptr;
GetOpenFile(obj, fptr);
if (!fptr->path) return Qnil;
return rb_str_new2(fptr->path);
}
#ifdef NT
#include "missing/file.h"
#endif
static VALUE
stat_new(st)
struct stat *st;
{
struct stat *nst;
if (!st) rb_bug("stat_new() called with bad value");
nst = ALLOC(struct stat);
*nst = *st;
return Data_Wrap_Struct(rb_cStat, NULL, free, nst);
}
static struct stat*
get_stat(self)
VALUE self;
{
struct stat* st;
Data_Get_Struct(self, struct stat, st);
if (!st) rb_bug("collapsed File::Stat");
return st;
}
static VALUE
rb_stat_cmp(self, other)
VALUE self, other;
{
time_t t1 = get_stat(self)->st_mtime;
time_t t2 = get_stat(other)->st_mtime;
if (t1 == t2)
return INT2FIX(0);
else if (t1 < t2)
return INT2FIX(-1);
else
return INT2FIX(1);
}
static VALUE
rb_stat_dev(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_dev);
}
static VALUE
rb_stat_ino(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_ino);
}
static VALUE
rb_stat_mode(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_mode);
}
static VALUE
rb_stat_nlink(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_nlink);
}
static VALUE
rb_stat_uid(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_uid);
}
static VALUE
rb_stat_gid(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_gid);
}
static VALUE
rb_stat_rdev(self)
VALUE self;
{
#ifdef HAVE_ST_RDEV
return INT2FIX((int)get_stat(self)->st_rdev);
#else
return INT2FIX(0);
#endif
}
static VALUE
rb_stat_size(self)
VALUE self;
{
return INT2FIX((int)get_stat(self)->st_size);
}
static VALUE
rb_stat_blksize(self)
VALUE self;
{
#ifdef HAVE_ST_BLKSIZE
return INT2FIX((int)get_stat(self)->st_blksize);
#else
return INT2FIX(0);
#endif
}
static VALUE
rb_stat_blocks(self)
VALUE self;
{
#ifdef HAVE_ST_BLOCKS
return INT2FIX((int)get_stat(self)->st_blocks);
#else
return INT2FIX(0);
#endif
}
static VALUE
rb_stat_atime(self)
VALUE self;
{
return rb_time_new(get_stat(self)->st_atime, 0);
}
static VALUE
rb_stat_mtime(self)
VALUE self;
{
return rb_time_new(get_stat(self)->st_mtime, 0);
}
static VALUE
rb_stat_ctime(self)
VALUE self;
{
return rb_time_new(get_stat(self)->st_ctime, 0);
}
static VALUE
rb_stat_inspect(self)
VALUE self;
{
VALUE str;
int i;
struct {
char *name;
VALUE (*func)();
} member[] = {
{"dev", rb_stat_dev},
{"ino", rb_stat_ino},
{"mode", rb_stat_mode},
{"nlink", rb_stat_nlink},
{"uid", rb_stat_uid},
{"gid", rb_stat_gid},
{"rdev", rb_stat_rdev},
{"size", rb_stat_size},
{"blksize", rb_stat_blksize},
{"blocks", rb_stat_blocks},
{"atime", rb_stat_atime},
{"mtime", rb_stat_mtime},
{"ctime", rb_stat_ctime},
};
str = rb_str_new2("#<");
rb_str_cat2(str, rb_class2name(CLASS_OF(self)));
rb_str_cat2(str, " ");
for (i = 0; i < sizeof(member)/sizeof(member[0]); i++) {
VALUE str2;
char *p;
if (i > 0) {
rb_str_cat2(str, ", ");
}
rb_str_cat2(str, member[i].name);
rb_str_cat2(str, "=");
str2 = rb_inspect((*member[i].func)(self));
rb_str_append(str, str2);
}
rb_str_cat2(str, ">");
OBJ_INFECT(str, self);
return str;
}
static int
rb_stat(file, st)
VALUE file;
struct stat *st;
{
if (TYPE(file) == T_FILE) {
OpenFile *fptr;
rb_secure(2);
GetOpenFile(file, fptr);
return fstat(fileno(fptr->f), st);
}
Check_SafeStr(file);
#if defined DJGPP
if (RSTRING(file)->len == 0) return -1;
#endif
return stat(RSTRING(file)->ptr, st);
}
static VALUE
rb_file_s_stat(obj, fname)
VALUE obj, fname;
{
struct stat st;
Check_SafeStr(fname);
if (stat(RSTRING(fname)->ptr, &st) == -1) {
rb_sys_fail(RSTRING(fname)->ptr);
}
return stat_new(&st);
}
static VALUE
rb_io_stat(obj)
VALUE obj;
{
OpenFile *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fileno(fptr->f), &st) == -1) {
rb_sys_fail(fptr->path);
}
return stat_new(&st);
}
static VALUE
rb_file_s_lstat(obj, fname)
VALUE obj, fname;
{
#ifdef HAVE_LSTAT
struct stat st;
Check_SafeStr(fname);
if (lstat(RSTRING(fname)->ptr, &st) == -1) {
rb_sys_fail(RSTRING(fname)->ptr);
}
return stat_new(&st);
#else
return rb_file_s_stat(obj, fname);
#endif
}
static VALUE
rb_file_lstat(obj)
VALUE obj;
{
#ifdef HAVE_LSTAT
OpenFile *fptr;
struct stat st;
rb_secure(2);
GetOpenFile(obj, fptr);
if (!fptr->path) return Qnil;
if (lstat(fptr->path, &st) == -1) {
rb_sys_fail(fptr->path);
}
return stat_new(&st);
#else
return rb_io_stat(obj);
#endif
}
static int
group_member(gid)
GETGROUPS_T gid;
{
#if !defined(NT)
if (getgid() == gid || getegid() == gid)
return Qtrue;
# ifdef HAVE_GETGROUPS
# ifndef NGROUPS
# define NGROUPS 32
# endif
{
GETGROUPS_T gary[NGROUPS];
int anum;
anum = getgroups(NGROUPS, gary);
while (--anum >= 0)
if (gary[anum] == gid)
return Qtrue;
}
# endif
#endif
return Qfalse;
}
#ifndef S_IXUGO
# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
#endif
int
eaccess(path, mode)
const char *path;
int mode;
{
#ifdef S_IXGRP
struct stat st;
static int euid = -1;
if (stat(path, &st) < 0) return (-1);
if (euid == -1)
euid = geteuid ();
if (euid == 0)
{
/* Root can read or write any file. */
if (mode != X_OK)
return 0;
/* Root can execute any file that has any one of the execute
bits set. */
if (st.st_mode & S_IXUGO)
return 0;
}
if (st.st_uid == euid) /* owner */
mode <<= 6;
else if (group_member (st.st_gid))
mode <<= 3;
if (st.st_mode & mode) return 0;
return -1;
#else /* !NT */
return access(path, mode);
#endif
}
static VALUE
test_d(obj, fname)
VALUE obj, fname;
{
#ifndef S_ISDIR
# define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
#endif
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISDIR(st.st_mode)) return Qtrue;
return Qfalse;
}
static VALUE
test_p(obj, fname)
VALUE obj, fname;
{
#ifdef S_IFIFO
# ifndef S_ISFIFO
# define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
# endif
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISFIFO(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
static VALUE
test_l(obj, fname)
VALUE obj, fname;
{
#ifndef S_ISLNK
# ifdef _S_ISLNK
# define S_ISLNK(m) _S_ISLNK(m)
# else
# ifdef _S_IFLNK
# define S_ISLNK(m) ((m & S_IFMT) == _S_IFLNK)
# else
# ifdef S_IFLNK
# define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
# endif
# endif
# endif
#endif
#ifdef S_ISLNK
struct stat st;
Check_SafeStr(fname);
if (lstat(RSTRING(fname)->ptr, &st) < 0) return Qfalse;
if (S_ISLNK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
static VALUE
test_S(obj, fname)
VALUE obj, fname;
{
#ifndef S_ISSOCK
# ifdef _S_ISSOCK
# define S_ISSOCK(m) _S_ISSOCK(m)
# else
# ifdef _S_IFSOCK
# define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
# else
# ifdef S_IFSOCK
# define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
# endif
# endif
# endif
#endif
#ifdef S_ISSOCK
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISSOCK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
static VALUE
test_b(obj, fname)
VALUE obj, fname;
{
#ifndef S_ISBLK
# ifdef S_IFBLK
# define S_ISBLK(m) ((m & S_IFMT) == S_IFBLK)
# else
# define S_ISBLK(m) (0) /* anytime false */
# endif
#endif
#ifdef S_ISBLK
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISBLK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
static VALUE
test_c(obj, fname)
VALUE obj, fname;
{
#ifndef S_ISCHR
# define S_ISCHR(m) ((m & S_IFMT) == S_IFCHR)
#endif
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISCHR(st.st_mode)) return Qtrue;
return Qfalse;
}
static VALUE
test_e(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
return Qtrue;
}
static VALUE
test_r(obj, fname)
VALUE obj, fname;
{
Check_SafeStr(fname);
if (eaccess(RSTRING(fname)->ptr, R_OK) < 0) return Qfalse;
return Qtrue;
}
static VALUE
test_R(obj, fname)
VALUE obj, fname;
{
Check_SafeStr(fname);
if (access(RSTRING(fname)->ptr, R_OK) < 0) return Qfalse;
return Qtrue;
}
static VALUE
test_w(obj, fname)
VALUE obj, fname;
{
Check_SafeStr(fname);
if (eaccess(RSTRING(fname)->ptr, W_OK) < 0) return Qfalse;
return Qtrue;
}
static VALUE
test_W(obj, fname)
VALUE obj, fname;
{
Check_SafeStr(fname);
if (access(RSTRING(fname)->ptr, W_OK) < 0) return Qfalse;
return Qtrue;
}
static VALUE
test_x(obj, fname)
VALUE obj, fname;
{
Check_SafeStr(fname);
if (eaccess(RSTRING(fname)->ptr, X_OK) < 0) return Qfalse;
return Qtrue;
}
static VALUE
test_X(obj, fname)
VALUE obj, fname;
{
Check_SafeStr(fname);
if (access(RSTRING(fname)->ptr, X_OK) < 0) return Qfalse;
return Qtrue;
}
#ifndef S_ISREG
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
#endif
static VALUE
test_f(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISREG(st.st_mode)) return Qtrue;
return Qfalse;
}
static VALUE
test_z(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (st.st_size == 0) return Qtrue;
return Qfalse;
}
static VALUE
test_s(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qnil;
if (st.st_size == 0) return Qnil;
return rb_int2inum(st.st_size);
}
static VALUE
test_owned(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (st.st_uid == geteuid()) return Qtrue;
return Qfalse;
}
static VALUE
test_rowned(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (st.st_uid == getuid()) return Qtrue;
return Qfalse;
}
static VALUE
test_grpowned(obj, fname)
VALUE obj, fname;
{
#ifndef NT
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (st.st_gid == getegid()) return Qtrue;
#endif
return Qfalse;
}
#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
static VALUE
check3rdbyte(file, mode)
const char *file;
int mode;
{
struct stat st;
if (stat(file, &st) < 0) return Qfalse;
if (st.st_mode & mode) return Qtrue;
return Qfalse;
}
#endif
static VALUE
test_suid(obj, fname)
VALUE obj, fname;
{
#ifdef S_ISUID
Check_SafeStr(fname);
return check3rdbyte(RSTRING(fname)->ptr, S_ISUID);
#else
return Qfalse;
#endif
}
static VALUE
test_sgid(obj, fname)
VALUE obj, fname;
{
#ifdef S_ISGID
Check_SafeStr(fname);
return check3rdbyte(RSTRING(fname)->ptr, S_ISGID);
#else
return Qfalse;
#endif
}
static VALUE
test_sticky(obj, fname)
VALUE obj, fname;
{
#ifdef S_ISVTX
return check3rdbyte(STR2CSTR(fname), S_ISVTX);
#else
return Qnil;
#endif
}
static VALUE
rb_file_s_size(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0)
rb_sys_fail(RSTRING(fname)->ptr);
return rb_int2inum(st.st_size);
}
static VALUE
rb_file_ftype(st)
struct stat *st;
{
char *t;
if (S_ISREG(st->st_mode)) {
t = "file";
} else if (S_ISDIR(st->st_mode)) {
t = "directory";
} else if (S_ISCHR(st->st_mode)) {
t = "characterSpecial";
}
#ifdef S_ISBLK
else if (S_ISBLK(st->st_mode)) {
t = "blockSpecial";
}
#endif
#ifdef S_ISFIFO
else if (S_ISFIFO(st->st_mode)) {
t = "fifo";
}
#endif
#ifdef S_ISLNK
else if (S_ISLNK(st->st_mode)) {
t = "link";
}
#endif
#ifdef S_ISSOCK
else if (S_ISSOCK(st->st_mode)) {
t = "socket";
}
#endif
else {
t = "unknown";
}
return rb_str_new2(t);
}
static VALUE
rb_file_s_ftype(obj, fname)
VALUE obj, fname;
{
struct stat st;
Check_SafeStr(fname);
if (lstat(RSTRING(fname)->ptr, &st) == -1) {
rb_sys_fail(RSTRING(fname)->ptr);
}
return rb_file_ftype(&st);
}
static VALUE
rb_file_s_atime(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0)
rb_sys_fail(RSTRING(fname)->ptr);
return rb_time_new(st.st_atime, 0);
}
static VALUE
rb_file_atime(obj)
VALUE obj;
{
OpenFile *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fileno(fptr->f), &st) == -1) {
rb_sys_fail(fptr->path);
}
return rb_time_new(st.st_atime, 0);
}
static VALUE
rb_file_s_mtime(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0)
rb_sys_fail(RSTRING(fname)->ptr);
return rb_time_new(st.st_mtime, 0);
}
static VALUE
rb_file_mtime(obj)
VALUE obj;
{
OpenFile *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fileno(fptr->f), &st) == -1) {
rb_sys_fail(fptr->path);
}
return rb_time_new(st.st_mtime, 0);
}
static VALUE
rb_file_s_ctime(obj, fname)
VALUE obj, fname;
{
struct stat st;
if (rb_stat(fname, &st) < 0)
rb_sys_fail(RSTRING(fname)->ptr);
return rb_time_new(st.st_ctime, 0);
}
static VALUE
rb_file_ctime(obj)
VALUE obj;
{
OpenFile *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fileno(fptr->f), &st) == -1) {
rb_sys_fail(fptr->path);
}
return rb_time_new(st.st_ctime, 0);
}
static void
chmod_internal(path, mode)
const char *path;
int mode;
{
if (chmod(path, mode) == -1)
rb_sys_fail(path);
}
static VALUE
rb_file_s_chmod(argc, argv)
int argc;
VALUE *argv;
{
VALUE vmode;
VALUE rest;
int mode, n;
rb_secure(2);
rb_scan_args(argc, argv, "1*", &vmode, &rest);
mode = NUM2INT(vmode);
n = apply2files(chmod_internal, rest, mode);
return INT2FIX(n);
}
static VALUE
rb_file_chmod(obj, vmode)
VALUE obj, vmode;
{
OpenFile *fptr;
int mode;
rb_secure(2);
mode = NUM2INT(vmode);
GetOpenFile(obj, fptr);
#if defined(DJGPP) || defined(NT) || defined(__BEOS__) || defined(__EMX__)
if (!fptr->path) return Qnil;
if (chmod(fptr->path, mode) == -1)
rb_sys_fail(fptr->path);
#else
if (fchmod(fileno(fptr->f), mode) == -1)
rb_sys_fail(fptr->path);
#endif
return INT2FIX(0);
}
struct chown_args {
int owner, group;
};
static void
chown_internal(path, args)
const char *path;
struct chown_args *args;
{
if (chown(path, args->owner, args->group) < 0)
rb_sys_fail(path);
}
static VALUE
rb_file_s_chown(argc, argv)
int argc;
VALUE *argv;
{
VALUE o, g, rest;
struct chown_args arg;
int n;
rb_secure(2);
rb_scan_args(argc, argv, "2*", &o, &g, &rest);
if (NIL_P(o)) {
arg.owner = -1;
}
else {
arg.owner = NUM2INT(o);
}
if (NIL_P(g)) {
arg.group = -1;
}
else {
arg.group = NUM2INT(g);
}
n = apply2files(chown_internal, rest, &arg);
return INT2FIX(n);
}
static VALUE
rb_file_chown(obj, owner, group)
VALUE obj, owner, group;
{
OpenFile *fptr;