/* * NAME * if - conditional command * * SYNOPSIS * if [expression [command [arg ...]]] * * DESCRIPTION * See the if(1) manual page for full details. */ int main(int argc, char **argv) { bool re; /* return value of expr() */ setmyerrexit(&ut_errexit); setmyname(argv[0]); setmypid(getpid()); ifeuid = geteuid(); /* * Set-ID execution is not supported. */ if (ifeuid != getuid() || getegid() != getgid()) err(FC_ERR, FMT2S, getmyname(), ERR_SETID); (void)sasignal(SIGCHLD, SIG_DFL); if (argc > 1) { ac = argc; av = argv; ap = 1; re = expr(); if (re && ap < ac) doex(!FORKED); } else re = false; return re ? SH_TRUE : SH_FALSE; }
/* * Initialize the shell. */ static void sh_init(const char *av0p) { struct stat sb; int i; setmyerrexit(&sh_errexit); setmyname(av0p); setmypid(getpid()); /* * Set-ID execution is not supported. */ if (geteuid() != getuid() || getegid() != getgid()) err(SH_ERR, FMT1S, ERR_SETID); /* * Save the process ID of the shell as a 5-digit ASCII * string (apid). Each occurrence of `$$' in a command * line is substituted w/ the value of apid. */ i = snprintf(apid, sizeof(apid), "%05u", (unsigned)getmypid()); if (i < 0 || i >= (int)sizeof(apid)) *apid = EOS; /* * Fail if any of the descriptors 0, 1, or 2 is not open. */ for (i = 0; i < 3; i++) if (fstat(i, &sb) == -1) err(SH_ERR, "%u: %s\n", (unsigned)i, strerror(errno)); /* * Set the SIGCHLD signal to its default action. * Correct operation of the shell requires that zombies * be created for its children when they terminate. */ (void)sasignal(SIGCHLD, SIG_DFL); }
/* * NAME * fd2 - redirect from/to file descriptor 2 * * SYNOPSIS * fd2 [-e] [-f file] [--] command [arg ...] * * DESCRIPTION * See the fd2(1) manual page for full details. */ int main(int argc, char **argv) { bool eopt; int efd, fd, nfd, ofd, opt; char *file; setmyerrexit(&ut_errexit); setmyname(argv[0]); setmypid(getpid()); /* * Set-ID execution is not supported. */ if (geteuid() != getuid() || getegid() != getgid()) err(FC_ERR, FMT2S, getmyname(), ERR_SETID); /* * File descriptors 0, 1, and 2 must be open. */ for (fd = 0; fd < 3; fd++) if (!fd_isopen(fd)) err(FC_ERR, "%s: %u: %s\n", getmyname(), (unsigned)fd, strerror(errno)); ofd = FD1, efd = FD2; eopt = false, file = NULL; while ((opt = getopt(argc, argv, ":ef:")) != -1) switch (opt) { case 'e': eopt = true; break; case 'f': file = optarg; break; default: usage(); } argc -= optind; argv += optind; if (argc < 1) usage(); if (file != NULL) { if ((nfd = open(file, O_WRONLY|O_APPEND|O_CREAT, 0666)) == -1) err(FC_ERR, FMT3S, getmyname(), file, ERR_CREATE); if (dup2(nfd, efd) == -1) err(FC_ERR, FMT2S, getmyname(), strerror(errno)); if (eopt && dup2(efd, ofd) == -1) err(FC_ERR, FMT2S, getmyname(), strerror(errno)); (void)close(nfd); } else { if (eopt) ofd = FD2, efd = FD1; if (dup2(ofd, efd) == -1) err(FC_ERR, FMT2S, getmyname(), strerror(errno)); } /* * Try to execute the specified command. */ (void)err_pexec(argv[0], argv); /*NOTREACHED*/ return FC_ERR; }
/* * NAME * glob - global command (file name generation) * * SYNOPSIS * glob command [arg ...] * * DESCRIPTION * See glob(1) the manual page for full details. */ int main(int argc, char **argv) { const char **gav; /* points to generated argument vector */ int pmc = 0; /* pattern-match count */ int i; const char **tav; const char *cmd; setmyerrexit(&ut_errexit); setmyname(argv[0]); setmypid(getpid()); /* * Set-ID execution is not supported. */ if (geteuid() != getuid() || getegid() != getgid()) err(SH_ERR, FMT1S, ERR_SETID); if (argc < 2) err(SH_ERR, FMT1S, ERR_GARGCOUNT); if ((gav = malloc(GAVNEW * sizeof(char *))) == NULL) { err(SH_ERR, FMT1S, ERR_NOMEM); /*NOTREACHED*/ } *gav = NULL; gavp = gav; gave = &gav[GAVNEW - 1]; while (*++argv != NULL) gav = glob1(gav, *argv, &pmc); gavp = NULL; if (pmc == 0) { err(SH_ERR, FMT1S, ERR_NOMATCH); /*NOTREACHED*/ } cmd = gav[0]; if (IS_LIBEXEC(EQUAL)) { for (i = 0; gav[i] != NULL; i++) ; /* nothing */ if ((tav = malloc((i + 1) * sizeof(char *))) == NULL) { err(SH_ERR, FMT1S, ERR_NOMEM); /*NOTREACHED*/ } if (EQUAL(cmd, "fd2")) tav[0] = FD2_PATH; else if (EQUAL(cmd, "goto")) tav[0] = GOTO_PATH; else tav[0] = IF_PATH; cmd = tav[0]; (void)memcpy(&tav[1], &gav[1], i * sizeof(char *)); (void)err_pexec(cmd, (char *const *)tav); } else (void)err_pexec(cmd, (char *const *)gav); /*NOTREACHED*/ return SH_ERR; }