1
- /* $NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $ */
1
+ /* $NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $ */
2
2
3
3
/*-
4
4
* Copyright (c) 1991, 1993, 1994
38
38
#if 0
39
39
static char sccsid [] = "@(#)args.c 8.3 (Berkeley) 4/2/94" ;
40
40
#else
41
- __RCSID ("$NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $" );
41
+ __RCSID ("$NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $" );
42
42
#endif
43
43
#endif /* not lint */
44
44
45
45
#include <sys/types.h>
46
46
#include <sys/time.h>
47
47
48
+ #ifndef NO_IOFLAG
49
+ #include <fcntl.h>
50
+ #endif /* NO_IOFLAG */
48
51
#include <err.h>
49
52
#include <errno.h>
50
53
#include <limits.h>
@@ -70,6 +73,16 @@ static void f_conv(char *);
70
73
static int c_conv (const void * , const void * );
71
74
#endif /* NO_CONV */
72
75
76
+ #ifdef NO_IOFLAG
77
+ static void f_iflag (char * ) __dead ;
78
+ static void f_oflag (char * ) __dead ;
79
+ #else
80
+ static void f_iflag (char * );
81
+ static void f_oflag (char * );
82
+ static u_int f_ioflag (char * , u_int );
83
+ static int c_ioflag (const void * , const void * );
84
+ #endif /* NO_IOFLAG */
85
+
73
86
static void f_bs (char * );
74
87
static void f_cbs (char * );
75
88
static void f_count (char * );
@@ -96,10 +109,12 @@ static const struct arg {
96
109
{ "files" , f_files , C_FILES , C_FILES },
97
110
{ "ibs" , f_ibs , C_IBS , C_BS |C_IBS },
98
111
{ "if" , f_if , C_IF , C_IF },
112
+ { "iflag" , f_iflag , C_IFLAG , C_IFLAG },
99
113
{ "iseek" , f_skip , C_SKIP , C_SKIP },
100
114
{ "msgfmt" , f_msgfmt , 0 , 0 },
101
115
{ "obs" , f_obs , C_OBS , C_BS |C_OBS },
102
116
{ "of" , f_of , C_OF , C_OF },
117
+ { "oflag" , f_oflag , C_OFLAG , C_OFLAG },
103
118
{ "oseek" , f_seek , C_SEEK , C_SEEK },
104
119
{ "progress" , f_progress , 0 , 0 },
105
120
{ "seek" , f_seek , C_SEEK , C_SEEK },
@@ -389,3 +404,102 @@ c_conv(const void *a, const void *b)
389
404
}
390
405
391
406
#endif /* NO_CONV */
407
+
408
+ static void
409
+ f_iflag (char * arg )
410
+ {
411
+ /* Build a small version (i.e. for a ramdisk root) */
412
+ #ifdef NO_IOFLAG
413
+ errx (EXIT_FAILURE , "iflag option disabled" );
414
+ /* NOTREACHED */
415
+ #else
416
+ iflag = f_ioflag (arg , C_IFLAG );
417
+ return ;
418
+ #endif
419
+ }
420
+
421
+ static void
422
+ f_oflag (char * arg )
423
+ {
424
+ /* Build a small version (i.e. for a ramdisk root) */
425
+ #ifdef NO_IOFLAG
426
+ errx (EXIT_FAILURE , "oflag option disabled" );
427
+ /* NOTREACHED */
428
+ #else
429
+ oflag = f_ioflag (arg , C_OFLAG );
430
+ return ;
431
+ #endif
432
+ }
433
+
434
+ #ifndef NO_IOFLAG
435
+ static const struct ioflag {
436
+ const char * name ;
437
+ u_int set ;
438
+ u_int allowed ;
439
+ } olist [] = {
440
+ /* the array needs to be sorted by the first column so
441
+ bsearch() can be used to find commands quickly */
442
+ { "alt_io" , O_ALT_IO , C_IFLAG |C_OFLAG },
443
+ { "append" , O_APPEND , C_OFLAG },
444
+ { "async" , O_ASYNC , C_IFLAG |C_OFLAG },
445
+ { "cloexec" , O_CLOEXEC , C_IFLAG |C_OFLAG },
446
+ { "creat" , O_CREAT , C_OFLAG },
447
+ { "direct" , O_DIRECT , C_IFLAG |C_OFLAG },
448
+ { "directory" , O_DIRECTORY , C_NONE },
449
+ { "dsync" , O_DSYNC , C_OFLAG },
450
+ { "excl" , O_EXCL , C_IFLAG |C_OFLAG },
451
+ { "exlock" , O_EXLOCK , C_IFLAG |C_OFLAG },
452
+ { "noctty" , O_NOCTTY , C_IFLAG |C_OFLAG },
453
+ { "nofollow" , O_NOFOLLOW , C_IFLAG |C_OFLAG },
454
+ { "nonblock" , O_NONBLOCK , C_IFLAG |C_OFLAG },
455
+ { "nosigpipe" , O_NOSIGPIPE , C_IFLAG |C_OFLAG },
456
+ { "rdonly" , O_RDONLY , C_IFLAG },
457
+ { "rdwr" , O_RDWR , C_IFLAG },
458
+ { "rsync" , O_RSYNC , C_IFLAG },
459
+ { "search" , O_SEARCH , C_IFLAG |C_OFLAG },
460
+ { "shlock" , O_SHLOCK , C_IFLAG |C_OFLAG },
461
+ { "sync" , O_SYNC , C_IFLAG |C_OFLAG },
462
+ { "trunc" , O_TRUNC , C_IFLAG |C_OFLAG },
463
+ { "wronly" , O_WRONLY , C_NONE },
464
+ };
465
+
466
+ static u_int
467
+ f_ioflag (char * arg , u_int flagtype )
468
+ {
469
+ u_int ioflag = 0 ;
470
+ struct ioflag * cp , tmp ;
471
+ const char * flagstr = (flagtype == C_IFLAG ) ? "iflag" : "oflag" ;
472
+
473
+ while (arg != NULL ) {
474
+ tmp .name = strsep (& arg , "," );
475
+ if (!(cp = bsearch (& tmp , olist ,
476
+ __arraycount (olist ), sizeof (* olist ), c_ioflag ))) {
477
+ errx (EXIT_FAILURE , "unknown %s %s" , flagstr , tmp .name );
478
+ /* NOTREACHED */
479
+ }
480
+
481
+ if ((cp -> set & O_ACCMODE ) && (flagtype == C_OFLAG )) {
482
+ warnx ("rdonly, rdwr and wronly are ignored for oflag" );
483
+ continue ;
484
+ }
485
+
486
+ if ((cp -> allowed & flagtype ) == 0 ) {
487
+ warnx ("%s set for %s but makes no sense" ,
488
+ cp -> name , flagstr );
489
+ }
490
+
491
+ ioflag |= cp -> set ;
492
+ }
493
+
494
+
495
+ return ioflag ;
496
+ }
497
+
498
+ static int
499
+ c_ioflag (const void * a , const void * b )
500
+ {
501
+
502
+ return (strcmp (((const struct ioflag * )a )-> name ,
503
+ ((const struct ioflag * )b )-> name ));
504
+ }
505
+ #endif /* NO_IOFLAG */
0 commit comments