int slave;
int child;
int subchild;
+int childstatus;
char *fname;
struct termios tt;
#endif
int aflg = 0;
char *cflg = NULL;
+int eflg = 0;
int fflg = 0;
int qflg = 0;
int tflg = 0;
int
main(int argc, char **argv) {
+ sigset_t block_mask, unblock_mask;
struct sigaction sa;
extern int optind;
char *p;
}
}
- while ((ch = getopt(argc, argv, "ac:fqt")) != -1)
+ while ((ch = getopt(argc, argv, "ac:efqt")) != -1)
switch((char)ch) {
case 'a':
aflg++;
case 'c':
cflg = optarg;
break;
+ case 'e':
+ eflg++;
+ break;
case 'f':
fflg++;
break;
case '?':
default:
fprintf(stderr,
- _("usage: script [-a] [-f] [-q] [-t] [file]\n"));
+ _("usage: script [-a] [-e] [-f] [-q] [-t] [file]\n"));
exit(1);
}
argc -= optind;
printf(_("Script started, file is %s\n"), fname);
fixtty();
+ /* setup SIGCHLD handler */
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
-
sa.sa_handler = finish;
sigaction(SIGCHLD, &sa, NULL);
+ /* init mask for SIGCHLD */
+ sigprocmask(SIG_SETMASK, NULL, &block_mask);
+ sigaddset(&block_mask, SIGCHLD);
+
+ sigprocmask(SIG_SETMASK, &block_mask, &unblock_mask);
child = fork();
+ sigprocmask(SIG_SETMASK, &unblock_mask, NULL);
+
if (child < 0) {
perror("fork");
fail();
}
if (child == 0) {
+
+ sigprocmask(SIG_SETMASK, &block_mask, NULL);
subchild = child = fork();
+ sigprocmask(SIG_SETMASK, &unblock_mask, NULL);
+
if (child < 0) {
perror("fork");
fail();
(void) fclose(fscript);
- if (die == 0 && child && kill(child, 0) == -1 && errno == ESRCH)
- die = 1;
-
while (die == 0) {
if ((cc = read(0, ibuf, BUFSIZ)) > 0) {
ssize_t wrt = write(master, ibuf, cc);
register int pid;
while ((pid = wait3(&status, WNOHANG, 0)) > 0)
- if (pid == child)
+ if (pid == child) {
+ childstatus = status;
die = 1;
+ }
}
void
my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec));
fprintf(fscript, _("Script started on %s"), obuf);
- if (die == 0 && child && kill(child, 0) == -1 && errno == ESRCH)
- /*
- * the SIGCHLD handler could be executed when the "child"
- * variable is not set yet. It means that the "die" is zero
- * althought the child process is already done. We have to
- * check this thing now. Now we have the "child" variable
- * already initialized. For more details see main() and
- * finish(). --kzak 07-Aug-2007
- */
- die = 1;
-
do {
if (die && flgs == 0) {
/* ..child is dead, but it doesn't mean that there is
if (!qflg)
printf(_("Script done, file is %s\n"), fname);
}
+
+ if(eflg) {
+ if (WIFSIGNALED(childstatus))
+ exit(WTERMSIG(childstatus) + 0x80);
+ else
+ exit(WEXITSTATUS(childstatus));
+ }
exit(0);
}