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