rc

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

system-bsd.c (1045B)


      1 /* signal-safe read and write (for BSD slow devices). writeall() also
      2 allows partial writes */
      3 
      4 #include "rc.h"
      5 
      6 #include <errno.h>
      7 
      8 #include "jbwrap.h"
      9 #include "wait.h"
     10 
     11 Jbwrap slowbuf;
     12 volatile sig_atomic_t slow;
     13 
     14 static char *safe_buf;
     15 static size_t safe_remain;
     16 
     17 extern void writeall(int fd, char *buf, size_t remain) {
     18 	int i;
     19 
     20 	safe_buf = buf;
     21 	safe_remain = remain;
     22 	for (i = 0; safe_remain > 0; buf += i, safe_remain -= i) {
     23 		if (sigsetjmp(slowbuf.j, 1) == 0) {
     24 			slow = TRUE;
     25 			if ((i = write(fd, safe_buf, safe_remain)) <= 0)
     26 				break; /* abort silently on errors in write() */
     27 		} else
     28 			break;
     29 	}
     30 	slow = FALSE;
     31 	sigchk();
     32 }
     33 
     34 extern int rc_read(int fd, char *buf, size_t n) {
     35 	ssize_t r;
     36 
     37 	if (sigsetjmp(slowbuf.j, 1) == 0) {
     38 		slow = TRUE;
     39 		r = read(fd, buf, n);
     40 	} else {
     41 		errno = EINTR;
     42 		r = -1;
     43 	}
     44 	slow = FALSE;
     45 
     46 	return r;
     47 }
     48 
     49 static int r = -1;
     50 extern pid_t rc_wait(int *stat) {
     51 	if (sigsetjmp(slowbuf.j, 1) == 0) {
     52 		slow = TRUE;
     53 		r = wait(stat);
     54 	} else {
     55 		errno = EINTR;
     56 		r = -1;
     57 	}
     58 	slow = FALSE;
     59 
     60 	return r;
     61 }