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
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2007 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
#include <shell.h>
#include <stdio.h>
#include <stdbool.h>
#include <option.h>
#include <stk.h>
#include <tm.h>
#include "name.h"
#undef nv_isnull
#ifndef SH_DICT
# define SH_DICT "libshell"
#endif
#include <poll.h>
#define sh_contexttoshb(context) ((Shbltin_t*)(context))
#define sh_contexttoshell(context) ((context)?(sh_contexttoshb(context)->shp):(NULL))
/*
* time formatting related
*/
struct dctime
{
Namfun_t fun;
Namval_t *format;
char buff[256]; /* Must be large enougth for |tmfmt()| */
};
static char *get_time(Namval_t* np, Namfun_t* nfp)
{
struct dctime *dp = (struct dctime*)nfp;
time_t t = nv_getn(np,nfp);
char *format = nv_getval(dp->format);
tmfmt(dp->buff,sizeof(dp->buff),format,(time_t*)0);
return(dp->buff);
}
static void put_time(Namval_t* np, const char* val, int flag, Namfun_t* nfp)
{
struct dctime *dp = (struct dctime*)nfp;
char *last;
if(val)
{
int32_t t;
if(flag&NV_INTEGER)
{
if(flag&NV_LONG)
t = *(Sfdouble_t*)val;
else
t = *(double*)val;
}
else
{
t = tmdate(val, &last, (time_t*)0);
if(*last)
errormsg(SH_DICT, ERROR_exit(1),"%s: invalid date/time string", val);
}
nv_putv(np, (char*)&t,NV_INTEGER, nfp);
}
else
{
nv_unset(dp->format);
free((void*)dp->format);
nv_putv(np, val, flag, nfp);
}
}
static Namval_t *create_time(Namval_t *np, const char *name, int flags, Namfun_t *nfp)
{
struct dctime *dp = (struct dctime*)nfp;
if(strcmp(name, "format"))
return((Namval_t*)0);
return(dp->format);
}
static const Namdisc_t timedisc =
{
sizeof(struct dctime),
put_time,
get_time,
0,
0,
create_time,
};
static Namval_t *make_time(Namval_t* np)
{
int offset = stktell(stkstd);
char *name = nv_name(np);
struct dctime *dp = newof(NULL,struct dctime,1,0);
if(!dp)
return((Namval_t*)0);
sfprintf(stkstd,"%s.format\0",name);
sfputc(stkstd,0);
dp->format = nv_search(stkptr(stkstd,offset),sh.var_tree,NV_ADD);
dp->fun.disc = &timedisc;
nv_stack(np,&dp->fun);
return(np);
}
/*
* mode formatting related
*/
static char *get_mode(Namval_t* np, Namfun_t* nfp)
{
mode_t mode = nv_getn(np,nfp);
return(fmtperm(mode));
}
static void put_mode(Namval_t* np, const char* val, int flag, Namfun_t* nfp)
{
if(val)
{
int32_t mode;
char *last;
if(flag&NV_INTEGER)
{
if(flag&NV_LONG)
mode = *(Sfdouble_t*)val;
else
mode = *(double*)val;
}
else
{
mode = strperm(val, &last,0);
if(*last)
errormsg(SH_DICT, ERROR_exit(1),"%s: invalid mode string", val);
}
nv_putv(np,(char*)&mode,NV_INTEGER,nfp);
}
else
nv_putv(np,val,flag,nfp);
}
static const Namdisc_t modedisc =
{
0,
put_mode,
get_mode,
};
static Namval_t *make_mode(Namval_t* np)
{
char *name = nv_name(np);
Namfun_t *nfp = newof(NULL,Namfun_t,1,0);
if(!nfp)
return((Namval_t*)0);
nfp->disc = &modedisc;
nv_stack(np,nfp);
return(np);
}
/*
* field related typese and functions
*/
typedef struct _field_
{
char *name; /* field name */
int flags; /* flags */
short offset; /* offset of field into data */
short size; /* size of field */
Namval_t *(*make)(Namval_t*); /* discipline constructor */
} Shfield_t;
/*
* lookup field in field table
*/
static Shfield_t *sh_findfield(Shfield_t *ftable, int nelem, const char *name)
{
Shfield_t *fp = ftable;
register int i,n;
register const char *cp;
for(cp=name; *cp; cp++)
{
if(*cp=='.')
break;
}
n = cp-name;
for(i=0; i < nelem; i++,fp++)
{
if(memcmp(fp->name,name,n)==0 && fp->name[n]==0)
return(fp);
}
return(0);
}
/*
* class types and functions
*/
typedef struct _class_
{
int nelem; /* number of elements */
int dsize; /* size for data structure */
Shfield_t *fields; /* field description table */
} Shclass_t;
struct dcclass
{
Namfun_t fun;
Shclass_t sclass;
};
static Namval_t *sh_newnode(register Shfield_t *fp, Namval_t *np)
{
char *val = np->nvalue + fp->offset;
char *name = nv_name(np);
register Namval_t *nq;
int offset = stktell(stkstd);
sfprintf(stkstd,"%s.%s\0",name,fp->name);
sfputc(stkstd,0);
nq = nv_search(stkptr(stkstd,offset),sh.var_tree,NV_ADD);
if(fp->size<0)
val = *(char**)val;
nv_putval(nq,val,fp->flags|NV_NOFREE);
if(fp->make)
(*fp->make)(nq);
return(nq);
}
static Namval_t *fieldcreate(Namval_t *np, const char *name, int flags, Namfun_t *nfp)
{
struct dcclass *dcp = (struct dcclass*)nfp;
Shclass_t *sp = &dcp->sclass;
Shfield_t *fp = sh_findfield(sp->fields,sp->nelem,name);
Namval_t *nq,**nodes = (Namval_t**)(dcp+1);
int n = fp-sp->fields;
int len = strlen(fp->name);
void *data = (void*)np->nvalue;
if(!(nq=nodes[n]))
{
nodes[n] = nq = sh_newnode(fp,np);
nfp->last = "";
}
if(name[len]==0)
return(nq);
return(nq);
}
static void genvalue(Sfio_t *out, Shclass_t *sp, int indent, Namval_t *npar)
{
Shfield_t *fp = sp->fields;
Namval_t *np, **nodes= (Namval_t**)(sp+1);
register int i,isarray;
if(out)
{
sfwrite(out,"(\n",2);
indent++;
}
for(i=0; i < sp->nelem; i++,fp++)
{
#if 0
/* handle recursive case */
#endif
if(!(np=nodes[i]) && out)
np = sh_newnode(fp,npar);
if(np)
{
isarray=0;
if(nv_isattr(np,NV_ARRAY))
{
isarray=1;
if(array_elem(nv_arrayptr(np))==0)
isarray=2;
else
nv_putsub(np,(char*)0,ARRAY_SCAN);
}
sfnputc(out,'\t',indent);
sfputr(out,fp->name,(isarray==2?'\n':'='));
if(isarray)
{
if(isarray==2)
continue;
sfwrite(out,"(\n",2);
sfnputc(out,'\t',++indent);
}
while(1)
{
char *fmtq;
if(isarray)
{
sfprintf(out,"[%s]",sh_fmtq(nv_getsub(np)));
sfputc(out,'=');
}
if(!(fmtq=nv_getval(np)) || !(fmtq=sh_fmtq(fmtq)))
fmtq = "";
sfputr(out,fmtq,'\n');
if(!nv_nextsub(np))
break;
sfnputc(out,'\t',indent);
}
if(isarray)
{
sfnputc(out,'\t',--indent);
sfwrite(out,")\n",2);
}
}
}
if(out)
{
if(indent>1)
sfnputc(out,'\t',indent-1);
sfputc(out,')');
}
}
static char *walk_class(register Namval_t *np, int dlete, struct dcclass *dcp)
{
static Sfio_t *out;
Sfio_t *outfile;
int savtop = stktell(stkstd);
char *savptr = stkfreeze(stkstd,0);
if(dlete)
outfile = 0;
else if(!(outfile=out))
outfile = out = sfnew((Sfio_t*)0,(char*)0,-1,-1,SF_WRITE|SF_STRING);
else
sfseek(outfile,0L,SEEK_SET);
genvalue(outfile,&dcp->sclass,0,np);
stkset(stkstd,savptr,savtop);
if(!outfile)
return((char*)0);
sfputc(out,0);
return((char*)out->_data);
}
static char *get_classval(Namval_t* np, Namfun_t* nfp)
{
return(walk_class(np,0,(struct dcclass *)nfp));
}
static void put_classval(Namval_t* np, const char* val, int flag, Namfun_t* nfp)
{
walk_class(np,1,(struct dcclass *)nfp);
if(nfp = nv_stack(np,(Namfun_t*)0))
{
free((void*)nfp);
if(np->nvalue && !nv_isattr(np,NV_NOFREE))
free((void*)np->nvalue);
}
if(val)
nv_putval(np,val,flag);
}
static const Namdisc_t classdisc =
{
sizeof(struct dcclass),
put_classval,
get_classval,
0,
0,
fieldcreate
};
static int mkclass(Namval_t *np, Shclass_t *sp)
{
struct dcclass *tcp = newof(NULL,struct dcclass,1,sp->nelem*sizeof(Namval_t*));
if(!tcp)
return(0);
memset((void*)(tcp+1),0,sp->nelem*sizeof(Namval_t*));
tcp->fun.disc = &classdisc;
tcp->sclass = *sp;
np->nvalue = (char*)calloc(sp->dsize,1);
nv_stack(np,&tcp->fun);
return(1);
}
/*
* ====================from here down is file class specific
*/
static struct stat *Sp;
struct filedata
{
struct stat statb;
int fd;
char *name;
};
static Shfield_t filefield[] =
{
{ "atime", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_atime), sizeof(Sp->st_atime), make_time},
{ "ctime", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_ctime), sizeof(Sp->st_ctime), make_time},
{ "dev", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_dev),sizeof(Sp->st_dev)},
{ "fd", NV_INTEGER|NV_RDONLY, offsetof(struct filedata,fd), sizeof(int)},
{ "gid", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_gid), sizeof(Sp->st_gid)},
{ "ino", NV_LONG|NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_ino), sizeof(Sp->st_ino)},
{ "mode", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_mode), sizeof(Sp->st_mode), make_mode},
{ "mtime", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_mtime), sizeof(Sp->st_mtime), make_time},
{ "name", NV_RDONLY, offsetof(struct filedata,name), -1 },
{ "nlink", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_nlink), sizeof(Sp->st_nlink)},
{ "size", NV_LONG|NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_size), sizeof(Sp->st_size)},
{ "uid", NV_INTEGER|NV_RDONLY, offsetof(struct stat,st_uid), sizeof(Sp->st_uid)}
};
static Shclass_t Fileclass =
{
sizeof(filefield)/sizeof(*filefield),
sizeof(struct filedata),
filefield
};
#define letterbit(bit) (1<<((bit)-'a'))
static const char sh_optopen[] =
"[-?\n@(#)$Id: open (AT&T Labs Research) 2007-05-07 $\n]"
"[-author?David Korn <dgk@research.att.com>]"
"[-author?Roland Mainz <roland.mainz@nrubsig.org>]"
"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
"[+NAME? open - create a shell variable correspnding to a file]"
"[+DESCRIPTION?\bopen\b creates the compound variable \avar\a correspinding "
"to the file given by the pathname \afile\a. The elements of \avar\a "
"are the names of elements in the \astat\a structure with the \bst_\b "
"prefix removed.]"
"[+?\afile\a is opened (based on \b-r\b and/or \b-w\b) and the variable "
"\avar\a\b.fd\b is the file descriptor.]"
"[a:append?Open for append.]"
"[b:binary?Open in binary mode"
#ifndef O_BINARY
" (not supported/ignored on this platform)"
#endif
".]"
"[t:text?Open in text mode"
#ifndef O_TEXT
" (not supported/ignored on this platform)"
#endif
".]"
"[c:create?Open for create.]"
"[i:inherit?Open without the close-on-exec bit set.]"
"[I:noinherit?Open with the close-on-exec bit set.]"
"[r:read?Open with read access.]"
"[w:write?Open with write access.]"
"[m:mode]:[mode:=rwrwrw?Open with access mode \amode\a.]"
"[x:exclusive?Open exclusive.]"
"[N:nofollow?If the path names a symbolic link, open fails with ELOOP "
#ifndef O_NOFOLLOW
" (not supported/ignored on this platform)"
#endif
".]"
"[S:sync?Write I/O operations on the file descriptor complete as "
"defined by synchronized I/O file integrity completion"
#ifndef O_SYNC
" (not supported/ignored on this platform)"
#endif
".]"
"[T:trunc?If the file exists and is a regular file, and the file "
"is successfully opened read/write or write-only, its length is "
"truncated to 0 and the mode and owner are unchanged. It "
"has no effect on FIFO special files or terminal device "
"files. Its effect on other file types is "
"implementation-dependent. The result of using -T "
"with read-only files is undefined"
#ifndef O_TRUNC
" (not supported/ignored on this platform)"
#endif
".]"
"\n"
"\nvar file\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Success.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\btmpfile\b(1),\bdup\b(1),\bclose\b(1),\bstat\b(1),\bpoll\b(1),\bstat\b(2)]"
;
extern int b_open(int argc, char *argv[], void *extra)
{
register Namval_t *np;
register int n,oflag=0;
Shell_t *shp = sh_contexttoshell(extra);
struct filedata *fdp;
mode_t mode = 0666;
long flags = 0;
int fd = -1;
char *arg;
while (n = optget(argv, sh_optopen)) switch (n)
{
case 'r':
case 'w':
case 'i':
flags |= letterbit(n);
break;
case 'I':
flags &= ~(letterbit('i'));
break;
case 'b':
#ifdef O_BINARY
oflag |= O_BINARY;
#endif
break;
case 't':
#ifdef O_TEXT
oflag |= O_TEXT;
#endif
break;
case 'N':
#ifdef O_NOFOLLOW
oflag |= O_NOFOLLOW;
#endif
break;
case 'T':
#ifdef O_TRUNC
oflag |= O_TRUNC;
#endif
break;
case 'x':
oflag |= O_EXCL;
break;
case 'c':
oflag |= O_CREAT;
break;
case 'a':
oflag |= O_APPEND;
break;
case 'S':
#ifdef O_SYNC
oflag |= O_SYNC;
#endif
break;
case 'm':
mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
if (*opt_info.arg)
errormsg(SH_DICT, ERROR_system(1), "%s: invalid mode", arg);
break;
case ':':
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(argc!=2 || !(flags&(letterbit('r')|letterbit('w'))))
errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
if(flags&letterbit('r'))
{
if(flags&letterbit('w'))
oflag |= O_RDWR;
else
oflag |= O_RDONLY;
}
else if(flags&letterbit('w'))
oflag |= O_WRONLY;
fd = sh_open(argv[1], oflag, mode);
if(fd<0)
errormsg(SH_DICT, ERROR_system(1), "%s: open failed", argv[1]);
if(!(flags&letterbit('i')))
fcntl(fd, F_SETFL, 0);
np = nv_open(argv[0], shp->var_tree, NV_ARRAY|NV_VARNAME|NV_NOASSIGN);
if(!nv_isnull(np))
nv_unset(np);
mkclass(np, &Fileclass);
fdp = (struct filedata*)np->nvalue;
fstat(fd, &fdp->statb);
fdp->fd = fd;
fdp->name = strdup(argv[1]);
return(0);
}
static const char sh_optclose[] =
"[-?\n@(#)$Id: close (AT&T Labs Research) 2007-04-21 $\n]"
"[-author?Roland Mainz <roland.mainz@nrubsig.org>]"
"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
"[+NAME? close - close a file descriptor]"
"[+DESCRIPTION?\bclose\b closes the file descriptor specified by fd.]"
"\n"
"\nfd\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Success.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bopen\b(1),\bdup\b(1),\btmpfile\b(1),\bpoll\b(1),\bstat\b(1)]"
;
extern int b_close(int argc, char *argv[], void *extra)
{
register int n=0;
int fd = -1;
while (n = optget(argv, sh_optclose)) switch (n)
{
case ':':
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(argc!=1)
errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
errno = 0;
fd = strtol(argv[0], (char **)NULL, 0);
if (errno != 0 || fd < 0)
errormsg(SH_DICT, ERROR_system(1), "%s: invalid descriptor", argv[0]);
n = sh_close(fd);
if (n < 0)
errormsg(SH_DICT, ERROR_system(1), "%s: close error", argv[0]);
return(n==0?0:1);
}
static const char sh_opttmpfile[] =
"[-?\n@(#)$Id: tmpfile (AT&T Labs Research) 2007-05-07 $\n]"
"[-author?Roland Mainz <roland.mainz@nrubsig.org>]"
"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
"[+NAME? tmpfile - create a shell variable correspnding to a temporary file]"
"[+DESCRIPTION?\btmpfile\b creates the compound variable \avar\a correspinding "
"to a temporary file. The elements of \avar\a "
"are the names of elements in the \astat\a structure with the \bst_\b "
"prefix removed.]"
"[i:inherit?Open without the close-on-exec bit set.]"
"[I:noinherit?Open with the close-on-exec bit set.]"
"\n"
"\nvar\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Success.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bopen\b(1),\bdup\b(1),\bclose\b(1),\bstat\b(1),\bstat\b(2)]"
;
extern int b_tmpfile(int argc, char *argv[], void *extra)
{
register Namval_t *np;
register int n;
Shell_t *shp = sh_contexttoshell(extra);
struct filedata *fdp;
bool inherit = false;
FILE *file = NULL;
int ffd, fd = -1;
while (n = optget(argv, sh_opttmpfile)) switch (n)
{
case 'i':
inherit = true;
break;
case 'I':
inherit = false;
break;
case ':':
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(argc!=1)
errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
file = tmpfile();
if(!file)
errormsg(SH_DICT, ERROR_system(1), "%s: tmpfile failed", argv[1]);
ffd = fileno(file);
fd = sh_dup(ffd);
if(fd<0)
errormsg(SH_DICT, ERROR_system(1), "%s: tmpfile failed", argv[1]);
fclose(file);
if(!inherit)
fcntl(fd, F_SETFL, 0);
np = nv_open(argv[0], shp->var_tree, NV_ARRAY|NV_VARNAME|NV_NOASSIGN);
if(!nv_isnull(np))
nv_unset(np);
mkclass(np,&Fileclass);
fdp = (struct filedata*)np->nvalue;
fstat(fd, &fdp->statb);
fdp->fd = fd;
fdp->name = NULL;
return(0);
}
static const char sh_optdup[] =
"[-?\n@(#)$Id: dup (AT&T Labs Research) 2007-05-07 $\n]"
"[-author?Roland Mainz <roland.mainz@nrubsig.org>]"
"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
"[+NAME? dup - duplicate an open file descriptor]"
"[+DESCRIPTION?The \bdup\b commands returns a new file descriptor having the "
"following in common with the original open file descriptor "
"fd: same open file (or pipe), same file pointer (that is, both file descriptors "
"share one file pointer) same access mode (read, write or read/write). "
"The file descriptor returned is the lowest one available.]"
"[i:inherit?Open without the close-on-exec bit set.]"
"[I:noinherit?Open with the close-on-exec bit set.]"
"\n"
"\nvar fd\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Success.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bopen\b(1),\btmpfile\b(1),\bclose\b(1),\bpoll\b(1),\bstat\b(1)]"
;
extern int b_dup(int argc, char *argv[], void *extra)
{
register Namval_t *np;
register int n;
Shell_t *shp = sh_contexttoshell(extra);
struct filedata *fdp;
bool inherit = false;
int ffd, fd = -1;
while (n = optget(argv, sh_optdup)) switch (n)
{
case 'i':
inherit = true;
break;
case 'I':
inherit = false;
break;
case ':':
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(argc!=2)
errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
errno = 0;
ffd = strtol(argv[1], (char **)NULL, 0);
if (errno != 0 || ffd < 0)
errormsg(SH_DICT, ERROR_system(1), "%s: invalid fd", argv[1]);
fd = sh_dup(ffd);
if(fd<0)
errormsg(SH_DICT, ERROR_system(1), "%s: dup failed", argv[1]);
if(!inherit)
fcntl(fd,F_SETFL,0);
np = nv_open(argv[0],shp->var_tree,NV_ARRAY|NV_VARNAME|NV_NOASSIGN);
if(!nv_isnull(np))
nv_unset(np);
mkclass(np, &Fileclass);
fdp = (struct filedata*)np->nvalue;
fstat(fd, &fdp->statb);
fdp->fd = fd;
fdp->name = NULL;
return(0);
}
static const char sh_optstat[] =
"[-?\n@(#)$Id: stat (AT&T Labs Research) 2007-05-07 $\n]"
"[-author?David Korn <dgk@research.att.com>]"
"[-author?Roland Mainz <roland.mainz@nrubsig.org>]"
"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
"[+NAME? stat - get file status]"
"[+DESCRIPTION?\bstat\b creates the compound variable \avar\a correspinding "
"to the file given by the pathname \afile\a. The elements of \avar\a "
"are the names of elements in the \astat\a structure with the \bst_\b "
"prefix removed.]"
"[l:lstat?If the the named file is a symbolic link returns information about "
"the link itself.]"
"\n"
"\nvar file\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Success.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bopen\b(1),\btmpfile\b(1),\bdup\b(1),\bclose\b(1),\bpoll\b(1),\bstat\b(2),\blstat\b(2)]"
;
extern int b_stat(int argc, char *argv[], void *extra)
{
register Namval_t *np;
register int n;
Shell_t *shp = sh_contexttoshell(extra);
struct filedata *fdp;
long flags = 0;
struct stat statb;
while (n = optget(argv, sh_optstat)) switch (n)
{
case 'l':
flags |= letterbit(n);
break;
case ':':
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(argc!=2)
errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
if(flags&letterbit('l'))
{
if(lstat(argv[1], &statb) < 0)
errormsg(SH_DICT, ERROR_system(1), "%s: stat failed", argv[1]);
}
else
{
if(stat(argv[1], &statb) < 0)
errormsg(SH_DICT, ERROR_system(1), "%s: stat failed", argv[1]);
}
np = nv_open(argv[0],shp->var_tree,NV_ARRAY|NV_VARNAME|NV_NOASSIGN);
if(!nv_isnull(np))
nv_unset(np);
mkclass(np,&Fileclass);
fdp = (struct filedata*)np->nvalue;
fdp->statb = statb;
fdp->fd = -1;
fdp->name = strdup(argv[1]);
return(0);
}
static const char sh_optrewind[] =
"[-?\n@(#)$Id: rewind (AT&T Labs Research) 2007-05-07 $\n]"
"[-author?Roland Mainz <roland.mainz@nrubsig.org>]"
"[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
"[+NAME? rewind - reset file position indicator in a stream]"
"[+DESCRIPTION?The \brewind\b command will move the file pointer of fd to position 0.]"
"\n"
"\nfd\n"
"\n"
"[+EXIT STATUS?]{"
"[+0?Success.]"
"[+>0?An error occurred.]"
"}"
"[+SEE ALSO?\bopen\b(1),\btmpfile\b(1),\bdup\b(1),\bclose\b(1),\bstat\b(1),\bstat\b(2)]"
;
extern int b_rewind(int argc, char *argv[], void *extra)
{
Shell_t *shp = sh_contexttoshell(extra);
int fd = -1;
register int n;
while (n = optget(argv, sh_optrewind)) switch (n)
{
case ':':
errormsg(SH_DICT, 2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(argc!=1)
errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));
errno = 0;
fd = strtol(argv[0], (char **)NULL, 0);
if (errno != 0 || fd < 0)
errormsg(SH_DICT, ERROR_system(1), "%s: invalid fd", argv[0]);
if (sh_seek(fd, 0, SEEK_SET) == (off_t)-1)
errormsg(SH_DICT, ERROR_system(1), "seek error");
return(0);
}
|