Initial import.
authorfredrik <fredrik@959494ce-11ee-0310-bf91-de5d638817bd>
Sun, 23 Apr 2006 01:56:26 +0000 (01:56 +0000)
committerfredrik <fredrik@959494ce-11ee-0310-bf91-de5d638817bd>
Sun, 23 Apr 2006 01:56:26 +0000 (01:56 +0000)
git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/utils@597 959494ce-11ee-0310-bf91-de5d638817bd

notty.c [new file with mode: 0644]

diff --git a/notty.c b/notty.c
new file mode 100644 (file)
index 0000000..3595811
--- /dev/null
+++ b/notty.c
@@ -0,0 +1,53 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+void usage(void)
+{
+    fprintf(stderr, "usage: notty PROGRAM [-r] [ARGS...]\n");
+}
+
+int main(int argc, char **argv)
+{
+    int tty, retain;
+    int c;
+    
+    retain = 0;
+    while((c = getopt(argc, argv, "r")) >= 0) {
+       switch(c) {
+       case 'r':
+           retain = 1;
+           break;
+       case '?':
+       case ':':
+       default:
+           usage();
+           exit(1);
+       }
+    }
+    if(argc - optind < 1) {
+       usage();
+       exit(1);
+    }
+    if((tty = open("/dev/tty", O_RDWR)) < 0) {
+       perror("/dev/tty");
+       exit(1);
+    }
+    if(ioctl(tty, TIOCNOTTY)) {
+       perror("TIOCNOTTY");
+       exit(1);
+    }
+    if(retain) {
+       if(tty != 3) {
+           dup2(tty, 3);
+           close(tty);
+       }
+    } else {
+       close(tty);
+    }
+    execvp(argv[optind], argv + optind);
+    perror(argv[optind]);
+    return(127);
+}