- WARNSfy

- KNF, ANSIfy, misc cosmetics

note: this is small shell, not secure shell.
This commit is contained in:
tsutsui 2011-05-19 22:12:35 +00:00
parent f893ffa835
commit 84af88e97a
2 changed files with 69 additions and 80 deletions

View File

@ -1,6 +1,7 @@
# $NetBSD: Makefile,v 1.7 2001/12/12 00:05:10 tv Exp $ # $NetBSD: Makefile,v 1.8 2011/05/19 22:12:35 tsutsui Exp $
# Small Shell (i.e. for boot media) # Small Shell (i.e. for boot media)
WARNS?= 4
PROG= ssh PROG= ssh
NOMAN= # defined NOMAN= # defined

View File

@ -1,4 +1,4 @@
/* $NetBSD: ssh.c,v 1.3 2009/10/21 23:12:09 snj Exp $ */ /* $NetBSD: ssh.c,v 1.4 2011/05/19 22:12:35 tsutsui Exp $ */
/* /*
* Copyright (c) 1995 Gordon W. Ross * Copyright (c) 1995 Gordon W. Ross
@ -61,7 +61,7 @@ extern int optind, opterr;
char cur_path[MAXPATH] = "PATH=/bin:/usr/bin"; char cur_path[MAXPATH] = "PATH=/bin:/usr/bin";
char rc_name[] = ".sshrc"; char rc_name[] = ".sshrc";
char *prompt = "ssh: "; const char *prompt = "ssh: ";
int eflag; /* exit on cmd failure */ int eflag; /* exit on cmd failure */
int iflag; /* interactive mode (catch interrupts) */ int iflag; /* interactive mode (catch interrupts) */
@ -69,7 +69,7 @@ int sflag; /* read from stdin (ignore file arg) */
int xflag; /* execution trace */ int xflag; /* execution trace */
/* Command file: name, line number, arg count, arg vector */ /* Command file: name, line number, arg count, arg vector */
char *cf_name; const char *cf_name;
int cf_line; int cf_line;
int cf_argc; int cf_argc;
char **cf_argv; char **cf_argv;
@ -79,16 +79,23 @@ int run_bg_pid;
jmp_buf next_cmd; jmp_buf next_cmd;
void catchsig __P((int sig)); int main(int, char *[]);
void child_newfd __P((int setfd, char *file, int otype)); void catchsig(int sig);
int find_in_path __P((char *cmd, char *filebuf)); void child_newfd(int setfd, char *file, int otype);
void print_termsig __P((FILE *fp, int cstat)); int find_in_path(char *cmd, char *filebuf);
int runfile __P((FILE *fp)); void print_termsig(FILE *fp, int cstat);
int runfile(FILE *fp);
int cmd_eval(int, char *[]);
int cmd_cd(int, char *[]);
int cmd_exit(int, char *[]);
int cmd_help(int, char *[]);
int cmd_path(int, char *[]);
int cmd_run(int, char *[]);
main(argc, argv) int
int argc; main(int argc, char *argv[])
char **argv;
{ {
struct sigaction sa; struct sigaction sa;
FILE *cfp; /* command file ptr */ FILE *cfp; /* command file ptr */
@ -116,7 +123,7 @@ main(argc, argv)
} }
if (error) { if (error) {
fprintf(stderr, "usage: ssh [-eisx] [cmd_file [...]]\n"); fprintf(stderr, "usage: ssh [-eisx] [cmd_file [...]]\n");
exit(1); exit(EXIT_FAILURE);
} }
cf_argc = argc - optind; cf_argc = argc - optind;
cf_argv = &argv[optind]; cf_argv = &argv[optind];
@ -145,7 +152,7 @@ main(argc, argv)
cfp = fopen(cf_name, "r"); cfp = fopen(cf_name, "r");
if (cfp == NULL) { if (cfp == NULL) {
perror(cf_name); perror(cf_name);
exit(1); exit(EXIT_FAILURE);
} }
error = runfile(cfp); error = runfile(cfp);
fclose(cfp); fclose(cfp);
@ -174,13 +181,13 @@ main(argc, argv)
} }
} }
error = runfile(stdin); error = runfile(stdin);
exit (error); exit(error);
} }
void void
catchsig(sig) catchsig(int sig)
int sig;
{ {
longjmp(next_cmd, sig); longjmp(next_cmd, sig);
} }
@ -189,8 +196,7 @@ catchsig(sig)
* Returns exit status. * Returns exit status.
*/ */
int int
runfile(cfp) runfile(FILE *cfp)
FILE *cfp;
{ {
char ibuf[MAXLINE]; char ibuf[MAXLINE];
char *argv[MAXARGS]; char *argv[MAXARGS];
@ -269,7 +275,7 @@ runfile(cfp)
break; break;
} }
/* return status of last command */ /* return status of last command */
return (exitcode); return exitcode;
} }
@ -279,9 +285,9 @@ runfile(cfp)
****************************************************************/ ****************************************************************/
struct cmd { struct cmd {
char *name; const char *name;
int (*func)(); int (*func)(int, char *[]);
char *help; const char *help;
}; };
struct cmd cmd_table[]; struct cmd cmd_table[];
@ -291,9 +297,7 @@ struct cmd cmd_table[];
* Returns exit status. * Returns exit status.
*/ */
int int
cmd_eval(argc, argv) cmd_eval(int argc, char *argv[])
int argc;
char **argv;
{ {
struct cmd *cp; struct cmd *cp;
@ -305,7 +309,7 @@ cmd_eval(argc, argv)
if (!strcmp(cp->name, argv[0])) { if (!strcmp(cp->name, argv[0])) {
/* Pass only args to builtin. */ /* Pass only args to builtin. */
--argc; argv++; --argc; argv++;
return (cp->func(argc, argv)); return cp->func(argc, argv);
} }
} }
@ -313,7 +317,7 @@ cmd_eval(argc, argv)
* If no matching builtin, let "run ..." * If no matching builtin, let "run ..."
* have a chance to try an external. * have a chance to try an external.
*/ */
return (cmd_run(argc, argv)); return cmd_run(argc, argv);
} }
/***************************************************************** /*****************************************************************
@ -323,15 +327,12 @@ cmd_eval(argc, argv)
* All return an exit status. * All return an exit status.
****************************************************************/ ****************************************************************/
char help_cd[] = "cd [dir]"; const char help_cd[] = "cd [dir]";
int int
cmd_cd(argc, argv) cmd_cd(int argc, char *argv[])
int argc;
char **argv;
{ {
char *dir; const char *dir;
int err;
if (argc > 0) if (argc > 0)
dir = argv[0]; dir = argv[0];
@ -342,17 +343,15 @@ cmd_cd(argc, argv)
} }
if (chdir(dir)) { if (chdir(dir)) {
perror(dir); perror(dir);
return (1); return 1;
} }
return(0); return 0;
} }
char help_exit[] = "exit [n]"; const char help_exit[] = "exit [n]";
int int
cmd_exit(argc, argv) cmd_exit(int argc, char **argv)
int argc;
char **argv;
{ {
int val = 0; int val = 0;
@ -361,12 +360,10 @@ cmd_exit(argc, argv)
exit(val); exit(val);
} }
char help_help[] = "help [command]"; const char help_help[] = "help [command]";
int int
cmd_help(argc, argv) cmd_help(int argc, char *argv[])
int argc;
char **argv;
{ {
struct cmd *cp; struct cmd *cp;
@ -374,7 +371,7 @@ cmd_help(argc, argv)
for (cp = cmd_table; cp->name; cp++) { for (cp = cmd_table; cp->name; cp++) {
if (!strcmp(cp->name, argv[0])) { if (!strcmp(cp->name, argv[0])) {
printf("usage: %s\n", cp->help); printf("usage: %s\n", cp->help);
return (0); return 0;
} }
} }
printf("%s: no such command\n", argv[0]); printf("%s: no such command\n", argv[0]);
@ -385,27 +382,24 @@ cmd_help(argc, argv)
printf(" %s", cp->name); printf(" %s", cp->name);
} }
printf("\nFor specific usage: help [command]\n"); printf("\nFor specific usage: help [command]\n");
return (0); return 0;
} }
char help_path[] = "path [dir1:dir2:...]"; const char help_path[] = "path [dir1:dir2:...]";
int int
cmd_path(argc, argv) cmd_path(int argc, char *argv[])
int argc;
char **argv;
{ {
int i;
if (argc <= 0) { if (argc <= 0) {
printf("%s\n", cur_path); printf("%s\n", cur_path);
return(0); return 0;
} }
strncpy(cur_path+5, argv[0], MAXPATH-6); strncpy(cur_path+5, argv[0], MAXPATH-6);
putenv(cur_path); putenv(cur_path);
return (0); return 0;
} }
/***************************************************************** /*****************************************************************
@ -415,17 +409,15 @@ cmd_path(argc, argv)
* (or zero for a background job) * (or zero for a background job)
****************************************************************/ ****************************************************************/
char help_run[] = "\ const char help_run[] = "\
run [-bg] [-i ifile] [-o ofile] [-e efile] program [args...]\n\ run [-bg] [-i ifile] [-o ofile] [-e efile] program [args...]\n\
or simply: program [args...]"; or simply: program [args...]";
int int
cmd_run(argc, argv) cmd_run(int argc, char *argv[])
int argc;
char **argv;
{ {
struct sigaction sa; struct sigaction sa;
int pid, err, cstat, fd; int pid, err, cstat;
char file[MAXPATHLEN]; char file[MAXPATHLEN];
int background; int background;
char *opt, *ifile, *ofile, *efile; char *opt, *ifile, *ofile, *efile;
@ -458,7 +450,7 @@ cmd_run(argc, argv)
goto shift; goto shift;
default: default:
fprintf(stderr, "run %s: bad option\n", opt); fprintf(stderr, "run %s: bad option\n", opt);
return (1); return 1;
shift: shift:
--argc; argv++; --argc; argv++;
} }
@ -467,7 +459,7 @@ cmd_run(argc, argv)
if (argc <= 0) { if (argc <= 0) {
fprintf(stderr, "%s:%d run: missing command\n", fprintf(stderr, "%s:%d run: missing command\n",
cf_name, cf_line); cf_name, cf_line);
return (1); return 1;
} }
/* Commands containing '/' get no path search. */ /* Commands containing '/' get no path search. */
@ -475,12 +467,12 @@ cmd_run(argc, argv)
strncpy(file, argv[0], sizeof(file)-1); strncpy(file, argv[0], sizeof(file)-1);
if (access(file, X_OK)) { if (access(file, X_OK)) {
perror(file); perror(file);
return (1); return 1;
} }
} else { } else {
if (find_in_path(argv[0], file)) { if (find_in_path(argv[0], file)) {
fprintf(stderr, "%s: command not found\n", argv[0]); fprintf(stderr, "%s: command not found\n", argv[0]);
return (1); return 1;
} }
} }
@ -504,23 +496,23 @@ cmd_run(argc, argv)
} }
err = execve(file, argv, environ); err = execve(file, argv, environ);
perror(argv[0]); perror(argv[0]);
return (1); return 1;
} }
/* parent */ /* parent */
/* Handle background option... */ /* Handle background option... */
if (background) { if (background) {
fprintf(stderr, "[%d]\n", pid); fprintf(stderr, "[%d]\n", pid);
run_bg_pid = pid; run_bg_pid = pid;
return (0); return 0;
} }
if (waitpid(pid, &cstat, 0) < 0) { if (waitpid(pid, &cstat, 0) < 0) {
perror("waitpid"); perror("waitpid");
return (1); return 1;
} }
if (WTERMSIG(cstat)) { if (WTERMSIG(cstat)) {
print_termsig(stderr, cstat); print_termsig(stderr, cstat);
} }
return (WEXITSTATUS(cstat)); return WEXITSTATUS(cstat);
} }
/***************************************************************** /*****************************************************************
@ -532,7 +524,7 @@ struct cmd cmd_table[] = {
{ "help", cmd_help, help_help }, { "help", cmd_help, help_help },
{ "path", cmd_path, help_path }, { "path", cmd_path, help_path },
{ "run", cmd_run, help_run }, { "run", cmd_run, help_run },
{ 0 }, { NULL, NULL, NULL },
}; };
/***************************************************************** /*****************************************************************
@ -540,9 +532,7 @@ struct cmd cmd_table[] = {
****************************************************************/ ****************************************************************/
int int
find_in_path(cmd, filebuf) find_in_path(char *cmd, char *filebuf)
char *cmd;
char *filebuf;
{ {
char *dirp, *endp, *bufp; /* dir, end */ char *dirp, *endp, *bufp; /* dir, end */
@ -555,12 +545,12 @@ find_in_path(cmd, filebuf)
*bufp++ = '/'; *bufp++ = '/';
strcpy(bufp, cmd); strcpy(bufp, cmd);
if (access(filebuf, X_OK) == 0) if (access(filebuf, X_OK) == 0)
return (0); return 0;
if (*endp == ':') if (*endp == ':')
endp++; endp++;
dirp = endp; /* next dir */ dirp = endp; /* next dir */
} }
return (-1); return -1;
} }
/* /*
@ -568,17 +558,17 @@ find_in_path(cmd, filebuf)
* which was opened with OTYPE and MODE. * which was opened with OTYPE and MODE.
*/ */
void void
child_newfd(setfd, file, otype) child_newfd(int setfd, char *file, int otype)
int setfd; /* what to set (i.e. 0,1,2) */ /* int setfd; what to set (i.e. 0,1,2) */
char *file; /* char *file; */
int otype; /* O_RDONLY, etc. */ /* int otype; O_RDONLY, etc. */
{ {
int newfd; int newfd;
close(setfd); close(setfd);
if ((newfd = open(file, otype, def_omode)) < 0) { if ((newfd = open(file, otype, def_omode)) < 0) {
perror(file); perror(file);
exit(1); exit(EXIT_FAILURE);
} }
if (newfd != setfd) { if (newfd != setfd) {
dup2(newfd, setfd); dup2(newfd, setfd);
@ -587,9 +577,7 @@ child_newfd(setfd, file, otype)
} }
void void
print_termsig(fp, cstat) print_termsig(FILE *fp, int cstat)
FILE *fp;
int cstat;
{ {
fprintf(fp, "Terminated, signal %d", fprintf(fp, "Terminated, signal %d",
WTERMSIG(cstat)); WTERMSIG(cstat));