rc

[fork] interactive rc shell
git clone https://hhvn.uk/rc
git clone git://hhvn.uk/rc
Log | Files | Refs | README | LICENSE

open.c (1310B)


      1 /* open.c: to insulate <fcntl.h> from the rest of rc. */
      2 
      3 #include "rc.h"
      4 #include <fcntl.h>
      5 
      6 /*
      7    Opens a file with the necessary flags. Assumes the following
      8    declaration for redirtype:
      9 
     10 	enum redirtype {
     11 		rFrom, rCreate, rAppend, rHeredoc, rHerestring
     12 	};
     13 */
     14 
     15 static const int mode_masks[] = {
     16 	/* rFrom */	O_RDONLY,
     17 	/* rCreate */	O_TRUNC | O_CREAT | O_WRONLY,
     18 	/* rAppend */	O_APPEND | O_CREAT | O_WRONLY
     19 };
     20 
     21 extern int rc_open(const char *name, redirtype m) {
     22 	if ((unsigned) m >= arraysize(mode_masks))
     23 		panic("bad mode passed to rc_open");
     24 	return open(name, mode_masks[m], 0666);
     25 }
     26 
     27 /* make a file descriptor blocking. return value indicates whether
     28 the descriptor was previously set to non-blocking. */
     29 
     30 extern bool makeblocking(int fd) {
     31 	int flags;
     32 
     33 	if ((flags = fcntl(fd, F_GETFL)) == -1) {
     34 		uerror("fcntl");
     35 		rc_error(NULL);
     36 	}
     37 	if (! (flags & O_NONBLOCK))
     38 		return FALSE;
     39 	flags &= ~O_NONBLOCK;
     40 	if (fcntl(fd, F_SETFL, (long) flags) == -1) {
     41 		uerror("fcntl");
     42 		rc_error(NULL);
     43 	}
     44 	return TRUE;
     45 }
     46 
     47 /* make a file descriptor the same pgrp as us.  Returns TRUE if
     48 it changes anything. */
     49 
     50 extern bool makesamepgrp(int fd) {
     51 	pid_t grp;
     52 
     53 	grp = getpgrp();
     54 
     55 	if (tcgetpgrp(fd) == grp)
     56 		return FALSE;
     57 
     58 	if (tcsetpgrp(fd, grp) < 0) {
     59 		uerror("tcsetgrp");
     60 		return FALSE;
     61 	}
     62 	return TRUE;
     63 }