File: open.c

package info (click to toggle)
rc 1.6c6-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 736 kB
  • ctags: 836
  • sloc: ansic: 7,124; sh: 325; yacc: 124; makefile: 95
file content (63 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download | duplicates (6)
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
/* 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 blocking. return value indicates whether
the desciptor was previously set to non-blocking. */

extern bool makeblocking(int fd) {
	int flags;

	if ((flags = fcntl(fd, F_GETFL)) == -1) {
		uerror("fcntl");
		rc_error(NULL);
	}
	if (! (flags & O_NONBLOCK))
		return FALSE;
	flags &= ~O_NONBLOCK;
	if (fcntl(fd, F_SETFL, (long) flags) == -1) {
		uerror("fcntl");
		rc_error(NULL);
	}
	return TRUE;
}

/* make a file descriptor the same pgrp as us.  Returns TRUE if
it changes anything. */

extern bool makesamepgrp(int fd) {
	pid_t grp;

	grp = getpgrp();

	if (tcgetpgrp(fd) == grp)
		return FALSE;

	if (tcsetpgrp(fd, grp) < 0) {
		uerror("tcsetgrp");
		return FALSE;
	}
	return TRUE;
}