-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathargs.h
100 lines (82 loc) · 2.38 KB
/
args.h
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
/* vim: expandtab:tw=68:ts=4:sw=4:
*
* args.h
*
* Copyright (c) 2018 Sudhi Herle <sw at herle.net>
*
* Licensing Terms: GPLv2
*
* If you need a commercial license for this work, please contact
* the author.
*
* This software does not come with any express or implied
* warranty; it is provided "as is". No claim is made to its
* suitability for any purpose.
*/
#ifndef ___ARGS_H__PjHOay0xLBQkDp5s___
#define ___ARGS_H__PjHOay0xLBQkDp5s___ 1
/* Provide C linkage for symbols declared here .. */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdint.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
/*
* This represents a parsed set of "dd" args.
*
* After a successful parse, the file(s) are opened, and the
* corresponding stat structs are filled-in.
*
* For block devices, the stat::st_size is filled in by
* querying the appropriate blkdev iotcl(2).
*/
struct Args
{
// Input and output fds
int ifd,
ofd;
int ipipe, // bool flag: set if ifd is a pipe
opipe; // bool flag: set if ofd is a pipe
uint64_t bs; // TYP_SZ; block size in bytes
uint64_t skip; // TYP_I; in units of blocks
uint64_t seek; // TYP_I; in units of blocks
uint64_t count; // TYP_SZ; in units of blocks
uint64_t iosize; // TYP_SZ; if we are doing mmap - then this is the map chunk size
char infile[PATH_MAX];
char outfile[PATH_MAX];
int iflag; // O_xxx flags
int oflag; // O_xxx flags
// If this is 0, it means read till EOF.
uint64_t insize;
struct stat ist,
ost;
};
typedef struct Args Args;
// predicate that returns true if an fd is a pipe
// XXX This destroys the current offset!
#define ispipe(fd) ({\
int zr = 0;\
if (lseek(fd, 0, SEEK_CUR) != 0) { \
if (errno == ESPIPE) zr = 1; \
}\
zr; \
})
/*
* Parse command line args and populate the Args structure.
*/
int Parse_args(Args *aa, int argc, char * const argv[]);
/*
* Convert args to a string
*/
char* Args_string(char *buf, size_t sz, const Args *a);
/*
* Convert generic flags to string.
*/
char * Flag2str(char *buf, size_t bufsz, int flag);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* ! ___ARGS_H__PjHOay0xLBQkDp5s___ */
/* EOF */