Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site umcp-cs.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: setenv from c Message-ID: <1741@umcp-cs.UUCP> Date: Wed, 2-Oct-85 15:53:59 EDT Article-I.D.: umcp-cs.1741 Posted: Wed Oct 2 15:53:59 1985 Date-Received: Wed, 9-Oct-85 06:45:08 EDT References: <2936@ncsu.UUCP> <6000007@mirror.UUCP> <1355@teddy.UUCP> Organization: U of Maryland, Computer Science Dept., College Park, MD Lines: 69 TIOCSTI works, but is unweildy and not very general. In addition, you must be careful not to overflow the TTY input queue. Here is a program I wrote long ago for saving and restoring C shell history in, well, an `unusual fasion' which works around the latter problem. #include #include #include main(argc, argv) int argc; register char **argv; { register char *hp, *cp; register FILE *in; register total = 0; static char histbuf[BUFSIZ], cmd[BUFSIZ + 5] = "!#:p "; struct sgttyb tty, otty; if (argc != 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); exit(1); } if ((in = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]); exit(1); } if (ioctl(0, TIOCGETP, &tty) < 0) { fprintf(stderr, "%s: not connected to a tty\n", argv[0]); exit(1); } switch (fork()) { case -1: fprintf(stderr, "%s: fork failed\n", argv[0]); exit(1); case 0: break; default: exit(0); } otty = tty; tty.sg_flags &= ~ECHO; (void) ioctl(0, TIOCSETN, &tty); while (fgets(histbuf, sizeof histbuf, in)) { if (cmd[5]) { for (cp = cmd; *cp; cp++) (void) ioctl(0, TIOCSTI, cp); if ((total += cp - cmd) >= 100) {/* fudge factor */ sleep(1); total = 0; } } cp = cmd + 5; hp = histbuf; if (hp[6] == '\t' && isdigit(hp[5])) hp += 7; while (*cp++ = *hp++); } /* * Note: drop last command as it was the one that dumped the history * list. */ (void) ioctl(0, TIOCSETN, &otty); exit(0); } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251) UUCP: seismo!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@mimsy.umd.edu