setenv from c
Chris Torek
chris at umcp-cs.UUCP
Thu Oct 3 05:53:59 AEST 1985
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 <stdio.h>
#include <sgtty.h>
#include <ctype.h>
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 at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list