xembed.c 694 B

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