diff options
| author | tgoodwin <tgoodwin> | 1998-10-21 11:00:16 +0000 |
|---|---|---|
| committer | Toby Goodwin <toby@paccrat.org> | 2015-05-16 21:23:11 +0100 |
| commit | a988abd0be551c0c37fc20b49b233c8a22ebfe7a (patch) | |
| tree | a2f82e4e3bc1c466a8380f779790297fb214775d | |
| parent | 1a7f00653697b147d3be949c448b820f1e5fddbf (diff) | |
Initial revision
| -rw-r--r-- | open.c | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ +/* open.c: to insulate <fcntl.h> from the rest of rc. */ + +#include "rc.h" +#include <fcntl.h> + +/* + Opens a file with the necessary flags. Assumes the following + declaration for redirtype: + + enum redirtype { + rFrom, rCreate, rAppend, rHeredoc, rHerestring + }; +*/ + +static const int mode_masks[] = { + /* rFrom */ O_RDONLY, + /* rCreate */ O_TRUNC | O_CREAT | O_WRONLY, + /* rAppend */ O_APPEND | O_CREAT | O_WRONLY +}; + +extern int rc_open(const char *name, redirtype m) { + if ((unsigned) m >= arraysize(mode_masks)) + panic("bad mode passed to rc_open"); + return open(name, mode_masks[m], 0666); +} + +/* make a file descriptor non blocking. must only be called after read() +has returned EAGAIN. */ + +extern void makenonblock(int fd) { + int flags; + + if ((flags = fcntl(fd, F_GETFL)) == -1) { + uerror("fcntl"); + exit(1); + } + if (! (flags & O_NONBLOCK)) + panic("not O_NONBLOCK"); + flags &= ~O_NONBLOCK; + if (fcntl(fd, F_SETFL, (long) flags) == -1) { + uerror("fcntl"); + exit(1); + } +} + |