forked from bonsai/harakit
dj(1): remove Io_fdopen
This commit is contained in:
parent
2b593559af
commit
5b1d4fef88
47
src/dj.c
47
src/dj.c
@ -62,26 +62,6 @@ static int write_flags = O_WRONLY | O_CREAT;
|
|||||||
/* Macro to check if fd is stdin or stdout */
|
/* Macro to check if fd is stdin or stdout */
|
||||||
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
|
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
|
||||||
|
|
||||||
/* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd,
|
|
||||||
* which will be -1 if an error occured. */
|
|
||||||
static int
|
|
||||||
Io_fdopen(struct Io *io, char *fn){
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
assert(io != NULL);
|
|
||||||
|
|
||||||
if((fd = open(fn, io->fl,
|
|
||||||
/* these are the flags used by touch(1p) */
|
|
||||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))
|
|
||||||
!= -1
|
|
||||||
&& (fdisstd(io->fd) || close(io->fd) == 0)){
|
|
||||||
io->fd = fd;
|
|
||||||
io->fn = fn;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Reads io->bs bytes from *io's file descriptor into io->buf, storing the
|
/* Reads io->bs bytes from *io's file descriptor into io->buf, storing the
|
||||||
* number of read bytes in io->bufuse and updating io->bytes. If io->bufuse is
|
* number of read bytes in io->bufuse and updating io->bytes. If io->bufuse is
|
||||||
* 0, errno will probably be set. Returns io. */
|
* 0, errno will probably be set. Returns io. */
|
||||||
@ -155,12 +135,12 @@ usage(char *s){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int align; /* low 8b used, negative if no alignment is being done */
|
int align; /* low 8b used, negative if no alignment is being done */
|
||||||
int count; /* 0 if dj(1) runs until no more reads are possible */
|
int count; /* 0 if dj(1) runs until no more reads are possible */
|
||||||
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
|
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
|
||||||
size_t i; /* side of io being modified */
|
size_t i; /* side of io being modified */
|
||||||
struct Io io[2];
|
|
||||||
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
|
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
|
||||||
|
struct Io io[2];
|
||||||
|
|
||||||
/* Set defaults. */
|
/* Set defaults. */
|
||||||
align = -1;
|
align = -1;
|
||||||
@ -190,10 +170,21 @@ int main(int argc, char *argv[]){
|
|||||||
io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO;
|
io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO;
|
||||||
io[i].fn = i == 0 ? stdin_name : stdout_name;
|
io[i].fn = i == 0 ? stdin_name : stdout_name;
|
||||||
break;
|
break;
|
||||||
}else if(Io_fdopen(&io[i], optarg) != -1)
|
}else{
|
||||||
break;
|
int fd;
|
||||||
|
|
||||||
|
if((fd = open(optarg, io[i].fl, /* touch(1p) flags */
|
||||||
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
|
||||||
|
| S_IROTH | S_IWOTH))
|
||||||
|
!= -1
|
||||||
|
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)){
|
||||||
|
io[i].fd = fd;
|
||||||
|
io[i].fn = optarg;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
return oserr(optarg);
|
return oserr(optarg);
|
||||||
case 'n': noerror = 1; break;
|
case 'n': noerror = 1; break;
|
||||||
case 'H': fmt = fmt_human; break;
|
case 'H': fmt = fmt_human; break;
|
||||||
case 'a':
|
case 'a':
|
||||||
if(optarg[0] == '\0' || optarg[1] == '\0'){
|
if(optarg[0] == '\0' || optarg[1] == '\0'){
|
||||||
|
Loading…
Reference in New Issue
Block a user