root / prex-0.9.0 / usr / bin / sh / cmds.c @ 03e9c04a
History | View | Annotate | Download (3.26 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2005-2006, Kohsuke Ohtani
|
3 |
* All rights reserved.
|
4 |
*
|
5 |
* Redistribution and use in source and binary forms, with or without
|
6 |
* modification, are permitted provided that the following conditions
|
7 |
* are met:
|
8 |
* 1. Redistributions of source code must retain the above copyright
|
9 |
* notice, this list of conditions and the following disclaimer.
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
12 |
* documentation and/or other materials provided with the distribution.
|
13 |
* 3. Neither the name of the author nor the names of any co-contributors
|
14 |
* may be used to endorse or promote products derived from this software
|
15 |
* without specific prior written permission.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 |
* SUCH DAMAGE.
|
28 |
*/
|
29 |
|
30 |
#include <sys/fcntl.h> |
31 |
#include <sys/prex.h> |
32 |
|
33 |
#include <unistd.h> |
34 |
#include <errno.h> |
35 |
#include <stdlib.h> |
36 |
#include <stdio.h> |
37 |
#include <string.h> |
38 |
#include <setjmp.h> |
39 |
|
40 |
#include "sh.h" |
41 |
|
42 |
extern char **environ; |
43 |
|
44 |
static int cmd_null(int argc, char *argv[]); |
45 |
static int cmd_cd(int argc, char *argv[]); |
46 |
static int cmd_mem(int argc, char *argv[]); |
47 |
static int cmd_exec(int argc, char *argv[]); |
48 |
static int cmd_exit(int argc, char *argv[]); |
49 |
|
50 |
/*
|
51 |
* Internal shell commands
|
52 |
*/
|
53 |
const struct cmdentry shell_cmds[] = { |
54 |
{ "cd" ,cmd_cd },
|
55 |
{ "exec" ,cmd_exec },
|
56 |
{ "exit" ,cmd_exit },
|
57 |
{ "export" ,cmd_export },
|
58 |
{ "mem" ,cmd_mem },
|
59 |
{ "set" ,cmd_showvars },
|
60 |
{ "unset" ,cmd_unsetvar },
|
61 |
{ NULL ,cmd_null },
|
62 |
}; |
63 |
|
64 |
|
65 |
static int |
66 |
cmd_null(int argc, char *argv[]) |
67 |
{ |
68 |
return 0; |
69 |
} |
70 |
|
71 |
static int |
72 |
cmd_cd(int argc, char *argv[]) |
73 |
{ |
74 |
char *p;
|
75 |
|
76 |
if (argc > 2) { |
77 |
fprintf(stderr, "usage: cd [dir]\n");
|
78 |
return 0; |
79 |
} |
80 |
if (argc == 1) { |
81 |
p = getenv("HOME");
|
82 |
if (p == NULL) |
83 |
p = "/";
|
84 |
} else
|
85 |
p = argv[1];
|
86 |
|
87 |
if (chdir(p) < 0) |
88 |
return errno;
|
89 |
return 0; |
90 |
} |
91 |
|
92 |
static int |
93 |
cmd_exec(int argc, char *argv[]) |
94 |
{ |
95 |
char **envp = environ;
|
96 |
|
97 |
if (argc < 2) { |
98 |
fprintf(stderr, "usage: exec command\n");
|
99 |
return 0; |
100 |
} |
101 |
|
102 |
close(0);
|
103 |
open("/dev/tty", O_RDONLY);
|
104 |
|
105 |
/*
|
106 |
* Memory size optimization for 'exec sh'.
|
107 |
*/
|
108 |
if (!strcmp(argv[1], "sh")) { |
109 |
longjmp(jmpbuf, 1);
|
110 |
/* NOTREACHED */
|
111 |
} |
112 |
return execve(argv[1], &argv[2], envp); |
113 |
} |
114 |
|
115 |
static int |
116 |
cmd_mem(int argc, char *argv[]) |
117 |
{ |
118 |
struct meminfo info;
|
119 |
|
120 |
sys_info(INFO_MEMORY, &info); |
121 |
|
122 |
/* UNIX v7 style... */
|
123 |
printf("mem = %d\n", (u_int)info.total);
|
124 |
return 0; |
125 |
} |
126 |
|
127 |
static int |
128 |
cmd_exit(int argc, char *argv[]) |
129 |
{ |
130 |
|
131 |
exit(0);
|
132 |
/* NOTREACHED */
|
133 |
} |