TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:6767] Re: [SCALUG] Re: [TCLUG:6728] csh question



Thank you very much John, you are like a human brain for those who do
without! :-)

I was able to construct this almost useless (except for my purposes)
program with your pointers (I actually read APUE, and the section the
section I needed to read, but didn't grok what I needed for some reason
(I'm being nice and attributing it to caffeine deficiency)). 

Troy

// isbg.c

// isbg
// Troy Johnson
// 19990629

// A program for reporting if the current process is running in
//   the foreground, background, or without a controlling terminal.
//   1 == back, 0 == fore, -1 == no ctty

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

main()
{
	int fd;		// File descriptor.
	int p_stat = 0;	// Process status flag.
	pid_t my_pgid;	// My process group id.
	pid_t f_pgid;	// Foreground process group id.

	if ((fd = open("/dev/tty", O_RDONLY)) >= 0)
	{
		my_pgid = getpgrp();
		f_pgid = tcgetpgrp(fd);
		close(fd);
		if (my_pgid == f_pgid)
		{	// If foreground process, set return value to 0.
			p_stat = 0;
		}
		else
		{	// If background process, set return value to 1.
			p_stat = 1;
		}
	}
	else
	{	// If no controlling tty, set return value to -1.
		p_stat = -1;
	}

	// Return value to user.
	printf("%d\n", p_stat);
	exit(0);
}