2
* uinbg.c -- $Id: uinbg.c 685 2003-03-08 15:26:51Z travo $
3
* foreground/background detection function (UNIX arcana)
5
* Copyright (c) 1998. See accompanying LEGAL file for details.
12
#define _POSIX_SOURCE 1
15
#include <sys/types.h>
16
extern pid_t tcgetpgrp(int fd);
17
/* POSIX and SYSV getpgrp prototype is: pid_t getpgrp(void)
18
* BSD4.3 is more like 2nd branch, where the parameter is the pid of the
19
* process whose process group is returned (pid=0 means this process)
20
* - passing the parameter to the POSIX/SYSV function is harmless,
21
* while omitting the paramter to the BSD function is disastrous
22
* - the only reason for the first branch is if sys/types.h has the
23
* POSIX prototype for getpgrp */
24
#ifdef USE_POSIX_GETPGRP
25
#undef USE_POSIX_GETPGRP
26
#define USE_POSIX_GETPGRP
27
extern pid_t getpgrp(void);
29
#define USE_POSIX_GETPGRP 0
30
extern pid_t getpgrp(pid_t);
36
/* if the process group of the controlling terminal for stdin (fd 0)
37
* does not match our process group, we are in the background, and
38
* any attempt to read stdin will generate a SIGTTIN signal
39
* read the intro(2) and termio(4) man pages for more
40
* (possibly easier to just handle SIGTTIN if it happens...) */
41
pid_t tgid = tcgetpgrp(0); /* controlling terminal process group */
43
pid_t pgid = getpgrp(USE_POSIX_GETPGRP); /* get our process group */
49
#ifdef USE_TIOCGPGRP_IOCTL
50
#include USE_TIOCGPGRP_IOCTL
55
if (ioctl(fd, TIOCGPGRP, &tgid)<0) return -1;