tabbed

[archived] [fork] xembed tabbing program
git clone https://hhvn.uk/tabbed
git clone git://hhvn.uk/tabbed
Log | Files | Refs | README | LICENSE

xembed.c (694B)


      1 /*
      2  * See LICENSE file for copyright and license details.
      3  */
      4 
      5 #include <fcntl.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <unistd.h>
      9 
     10 int
     11 main(int argc, char *argv[])
     12 {
     13 	char *xembed;
     14 	int tty;
     15 	pid_t pgrp, tcpgrp;
     16 
     17 	if (argc < 3) {
     18 		fprintf(stderr, "usage: %s flag cmd ...\n", argv[0]);
     19 		return 2;
     20 	}
     21 
     22 	if (!(xembed = getenv("XEMBED")))
     23 		goto noembed;
     24 
     25 	if ((tty = open("/dev/tty", O_RDONLY)) < 0)
     26 		goto noembed;
     27 
     28 	pgrp = getpgrp();
     29 	tcpgrp = tcgetpgrp(tty);
     30 
     31 	close(tty);
     32 
     33 	if (pgrp == tcpgrp) { /* in foreground of tty */
     34 		argv[0] = argv[2];
     35 		argv[2] = xembed;
     36 	} else {
     37 noembed:
     38 		argv += 2;
     39 	}
     40 
     41 	execvp(argv[0], argv);
     42 
     43 	perror(argv[0]); /* failed to execute */
     44 	return 1;
     45 }