void setjobctl(int on) { int fd; int pgrp; if (on == jobctl || rootshell == 0) return; if (on) { int ofd; ofd = fd = open(_PATH_TTY, O_RDWR); if (fd < 0) { fd += 3; while (!isatty(fd) && --fd >= 0) ; } fd = fcntl(fd, F_DUPFD, 10); close(ofd); if (fd < 0) goto out; fcntl(fd, F_SETFD, FD_CLOEXEC); do { /* while we are in the background */ if ((pgrp = tcgetpgrp(fd)) < 0) { out: sh_warnx("can't access tty; job control turned off"); mflag = on = 0; goto close; } if (pgrp == getpgrp()) break; killpg(0, SIGTTIN); } while (1); initialpgrp = pgrp; setsignal(SIGTSTP); setsignal(SIGTTOU); setsignal(SIGTTIN); pgrp = rootpid; setpgid(0, pgrp); xtcsetpgrp(fd, pgrp); } else { /* turning job control off */ fd = ttyfd; pgrp = initialpgrp; xtcsetpgrp(fd, pgrp); setpgid(0, pgrp); setsignal(SIGTSTP); setsignal(SIGTTOU); setsignal(SIGTTIN); close: close(fd); fd = -1; } ttyfd = fd; jobctl = on; }
/* * Find out what the current directory is. If we already know the current * directory, this routine returns immediately. */ inline STATIC char * getpwd(void) { char *buf; buf = malloc(PATH_MAX); if (buf != NULL) { if(getcwd(buf, sizeof(buf))) { return buf; } else { free(buf); } } sh_warnx("getcwd() failed: %s", strerror(errno)); return NULL; }