My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*********************************************************************************
* myshell.c, primitive core shall wrapper for Unix
* Copyright (C) 2009 Konstantin Ignatiev, ignatiev@users.sourceforge.net
*
* This program is distributed under the terms of GNU General Public License,
* version 2, as published by the Free Software Foundation.
* See <URL:http://www.fsf.org/licenses/gpl.txt>
*
* ********** Informal subversion log *******************************************
* 138 2009-10-22 Minor improvements
* 137 2009-10-22 Initial version
* *******************************************************************************
*/

/* On Debian, make sure you have package libreadline5-dev installed and do this:
* gcc -g --pedantic -Wall -DRL -o myshell myshell.c -lreadline
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#ifdef RL
#include <readline/readline.h>
#include <readline/history.h>
#endif /* defined(RL) */

#define PATH_SEPARATOR ':'

static char ** split_by_words ( char cmd[] );
static char * get_executable ( char path[], char cmd[] ) ;
static int my_gets (const char prompt[], char line[], int n_alloc);
static char * find_in_path ( char path[], char name[] ) ;

int main ( int argc, char * argv[] )
{
int ii, n_read, status;
pid_t childpid;
char cmd[512], ** args, *executable;

printf ( "MyShell interface, compiled %s%s\n",
__DATE__,
#ifdef RL
", with GNU readline"
#else
""
#endif
);

if (getuid() == geteuid())
printf ("UID = %d (both \"real\" and \"effective\", so setuid mode)\n",
getuid() );
else
printf ("setuid mode active: \"real\" UID = %d, \"effective\" UID = %d\n",
getuid(), geteuid());

printf ("Use Ctrl-D or command \"quit\" to quit\n" );

while ( 1 )
{
n_read = my_gets ("> ", cmd, sizeof cmd - 1);

if (n_read < 0 || strcmp(cmd, "quit") == 0)
{
printf ( "%sBye!\n", (n_read < 0)?"^D\n":"" );
return 0;
}
args = split_by_words ( cmd );
if (NULL == (executable = get_executable (getenv("PATH"), args[0])))
continue;
printf ( "%s(", executable );
for (ii = 1; args[ii] != NULL; ii ++ )
printf ( "%s%s", args[ii], (args[ii + 1] == NULL)?"":"," );
printf ( ")\n" );
if (0 == (childpid = fork()))
{
/* CHILD process */
status = execv(executable, args); /* !!! args[0] passed as EXE name */
fprintf ( stderr, "execv() failed with code %d\n", status );
exit(-1);
}
else if (childpid > 0)
{
/* PARENT, OK */
waitpid (childpid, &status, 0);

printf ("Child process %d finished with status %d\n", childpid, status);
}
else
{
fprintf ( stderr, "Cannot fork()\n" );
}

for (ii = 0; args[ii] != NULL; ii ++ )
free (args[ii]);
}
}

static char * get_executable ( char path[], char cmd[] )
{
struct stat st;
char * exe;
if (strchr(cmd, '/') == NULL)
{
exe = find_in_path (path, cmd);
if (!exe)
{
fprintf ( stderr, "\"%s\" not found in $PATH\n", cmd );
return NULL;
}
}
else
exe = cmd;

if (stat(exe, &st) != 0)
{
fprintf ( stderr, "No such file \"%s\"\n", exe );
return NULL;
}

if ((st.st_mode & S_IFMT) != S_IFREG)
{
fprintf ( stderr, "Not a plain file \"%s\"\n", exe );
return NULL;
}

if ((st.st_mode & S_IXUSR) == 0 &&
(st.st_mode & S_IXGRP) == 0 &&
(st.st_mode & S_IXOTH) == 0 )
{
fprintf ( stderr, "No execution permission \"%s\"\n", exe );
return NULL;
}

return exe;
}

static char * find_in_path ( char path[], char name[] )
{
int ii, idx = 0;
struct stat st;
char test_name[512];

while (path[idx] != '\0')
{
for (; path[idx] == PATH_SEPARATOR; idx ++ );
if (path[idx] != '\0')
{
for (ii = idx + 1; path[ii] != PATH_SEPARATOR && path[ii] != '\0'; ii ++);

strncpy (test_name, path + idx, ii - idx);
test_name[ii - idx] = '/';
strcpy ( test_name + ii - idx + 1, name );

if (0)
printf ( "Trying '%s'\n", test_name );

if (stat(test_name,&st) == 0)
return strdup(test_name);
}
idx = ii;
}
return NULL;
}

static char ** split_by_words ( char cmd[] )
{
int idx, n, ii;
static char * words[512];

idx = n = 0;

while ( cmd[idx] != '\0' )
{
for (; isspace(cmd[idx]); idx ++ );

if (cmd[idx] == '"')
{
for (ii = idx + 1; cmd[ii] != '\0' && cmd[ii] != '"'; ii ++ );
if (cmd[ii] != '"')
{
fprintf ( stderr, "Cannot find matching quote to one in position %d\n", idx + 1 );
exit(-1);
}
cmd[ii] = '\0';
words[n ++] = strdup(cmd + idx + 1);
idx = ii + 1;
}
else if (cmd[idx] != '\0')
{
for(ii = idx + 1; !isspace(cmd[ii]) && cmd[ii] != '\0'; ii ++ );
cmd[ii] = '\0';
words[n ++] = strdup(cmd + idx);
idx = ii + 1;
}
}

words[n] = NULL;

return words;
}

static int my_gets (const char prompt[], char line[], int n_alloc)
{
int n_in;
#ifdef RL
char * rl_in;
rl_in = readline (prompt);
if (!rl_in)
return -1;
n_in = strlen(rl_in);
strncpy ( line, rl_in, n_alloc);
if (rl_in && rl_in[0])
add_history (rl_in);
free (rl_in);
#else
printf ( "%s", prompt );
if (NULL == fgets (line, n_alloc, stdin))
return -1;
n_in = strlen(line) - 1;
line[n_in] = '\0';
#endif
return n_in;
}


Change log

r138 by kostya on Oct 22, 2009   Diff
Minor improvements in Misc/myshell.c
Go to: 
Project members, sign in to write a code review

Older revisions

r137 by kostya on Oct 22, 2009   Diff
Added myshell.c and made svnlist.py
compatible with C-files
All revisions of this file

File info

Size: 5577 bytes, 231 lines
Powered by Google Project Hosting