2011-09-14 14:34:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Marian Ohligs, Chair for Operating Systems,
|
|
|
|
* RWTH Aachen University
|
2011-09-15 21:17:25 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
2011-09-14 14:34:10 +02:00
|
|
|
*/
|
|
|
|
|
2011-05-24 00:48:56 +02:00
|
|
|
#include <stdlib.h>
|
2011-05-24 17:23:10 +02:00
|
|
|
#include <stdio.h>
|
2011-05-24 00:48:56 +02:00
|
|
|
#include <string.h>
|
2011-05-24 17:23:10 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-09-22 15:39:20 +02:00
|
|
|
#include <ctype.h>
|
2011-11-09 12:14:31 +01:00
|
|
|
#include <dirent.h>
|
2011-05-24 17:23:10 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2011-09-23 15:31:44 +02:00
|
|
|
#include <sys/stat.h>
|
2011-05-24 00:48:56 +02:00
|
|
|
|
|
|
|
void showlogo() {
|
|
|
|
printf("\n\n");
|
|
|
|
printf("================================================================================\n");
|
|
|
|
printf(" m(etalsvm)shell\n\n");
|
|
|
|
printf("================================================================================\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void help() {
|
2011-09-23 15:31:44 +02:00
|
|
|
printf("shell builtin commands: \n");
|
2011-09-24 20:13:16 +02:00
|
|
|
printf("cd <path> :\t change current working directory to path\n");
|
|
|
|
printf("pwd :\t show current working directory\n");
|
|
|
|
printf("cat <path>:\t print files\n");
|
2011-11-09 13:13:41 +01:00
|
|
|
printf("ls :\t list directory entrys in pwd\n");
|
2011-09-24 20:13:16 +02:00
|
|
|
printf("help :\t this short help\n");
|
|
|
|
printf("exit :\t exit the shell \n");
|
2011-05-24 00:48:56 +02:00
|
|
|
}
|
|
|
|
|
2011-09-23 15:31:44 +02:00
|
|
|
#define MAXARGS 16
|
|
|
|
#define MAXPATH 128
|
|
|
|
#define MAXCMDLINE 1024
|
2011-09-22 15:39:20 +02:00
|
|
|
|
|
|
|
#define ST_WHITE 0
|
2011-09-23 15:31:44 +02:00
|
|
|
#define ST_ARG 1
|
|
|
|
#define ST_QUOTE 2
|
|
|
|
|
2011-10-12 11:26:24 +02:00
|
|
|
#define USE_STAT 1
|
2011-09-22 15:39:20 +02:00
|
|
|
|
|
|
|
char** splitargs(char* args) {
|
|
|
|
static char* ret[MAXARGS+1];
|
|
|
|
int i = 0;
|
|
|
|
int state = ST_WHITE;
|
|
|
|
while (*args != '\0') {
|
|
|
|
if (state == ST_WHITE) {
|
|
|
|
if (!isspace(*args)) {
|
2011-09-23 15:31:44 +02:00
|
|
|
if(*args == '"') {
|
|
|
|
args++;
|
|
|
|
state = ST_QUOTE;
|
|
|
|
if (*args == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state = ST_ARG;
|
|
|
|
}
|
2011-09-22 15:39:20 +02:00
|
|
|
ret[i++] = args;
|
|
|
|
if (i == MAXARGS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-09-23 15:31:44 +02:00
|
|
|
if ((state == ST_QUOTE && *args == '"') || isspace(*args)) {
|
2011-09-22 15:39:20 +02:00
|
|
|
*args = '\0';
|
|
|
|
state = ST_WHITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args++;
|
|
|
|
}
|
|
|
|
ret[i] = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-23 15:31:44 +02:00
|
|
|
char* ms_getcwd() {
|
|
|
|
return getenv("PWD");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ms_setcwd(char* path) {
|
|
|
|
static char newpath[MAXPATH];
|
|
|
|
static struct stat stats;
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*path == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*path == '/') {
|
|
|
|
strcpy(newpath, path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char* oldpath = ms_getcwd();
|
|
|
|
|
|
|
|
if(oldpath[strlen(oldpath)-1] == '/')
|
|
|
|
sprintf(newpath,"%s%s", oldpath, path);
|
|
|
|
else
|
|
|
|
sprintf(newpath,"%s/%s", oldpath, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_STAT
|
|
|
|
if (stat(newpath, &stats)) {
|
2011-10-12 11:26:24 +02:00
|
|
|
printf("mshell: %s is not a valid path\n XY", newpath);
|
2011-09-23 15:31:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(stats.st_mode)) {
|
|
|
|
printf("mshell: %s is not a directory\n", newpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
setenv("PWD", newpath, 1);
|
|
|
|
}
|
|
|
|
|
2011-09-24 20:13:16 +02:00
|
|
|
int ms_cat(char* filename)
|
|
|
|
{
|
|
|
|
int fd, r = 0;
|
|
|
|
char* buffer = malloc(1024*sizeof(char));
|
|
|
|
|
|
|
|
fd = open(filename, 0, "wr");
|
|
|
|
|
2011-09-25 12:59:38 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
printf("cat: No such file or directory");
|
|
|
|
printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-24 20:13:16 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
r = read(fd, buffer, 1024);
|
|
|
|
write(1, buffer, r);
|
|
|
|
} while (r > 0);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-09 13:13:41 +01:00
|
|
|
int ms_ls()
|
2011-11-09 12:14:31 +01:00
|
|
|
{
|
|
|
|
DIR* dir;
|
|
|
|
dirent_t* dirent;
|
2011-11-09 13:13:41 +01:00
|
|
|
char* dirname = malloc(sizeof(char)*1024);
|
|
|
|
|
|
|
|
strcpy(dirname, ms_getcwd());
|
2011-11-09 12:14:31 +01:00
|
|
|
|
|
|
|
dir = opendir(dirname);
|
|
|
|
if (dir < 1) {
|
|
|
|
printf("ls: No such file or directory");
|
|
|
|
printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
dirent = readdir(dir);
|
|
|
|
if (dirent == NULL)
|
|
|
|
break;
|
|
|
|
printf("%s", dirent->d_name);
|
|
|
|
printf("\n");
|
|
|
|
} while (dirent != NULL);
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-24 00:48:56 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2011-09-23 15:31:44 +02:00
|
|
|
char commandline[MAXCMDLINE];
|
2011-09-22 15:39:20 +02:00
|
|
|
char* command;
|
2011-07-27 17:17:40 +02:00
|
|
|
|
2011-09-23 15:31:44 +02:00
|
|
|
int status = 0;
|
2011-05-24 17:23:10 +02:00
|
|
|
pid_t pid;
|
2011-05-24 00:48:56 +02:00
|
|
|
system("clear");
|
|
|
|
showlogo();
|
|
|
|
while(1) {
|
2011-09-22 15:39:20 +02:00
|
|
|
char** newargv;
|
|
|
|
|
|
|
|
printf("mshell> ");
|
|
|
|
gets(commandline);
|
|
|
|
|
|
|
|
newargv = splitargs(commandline);
|
|
|
|
command = newargv[0];
|
|
|
|
|
|
|
|
if(command == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-24 00:48:56 +02:00
|
|
|
if(!strcmp(command, "exit")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!strcmp(command, "help")) {
|
|
|
|
help();
|
2011-09-23 15:31:44 +02:00
|
|
|
} else if (!strcmp(command, "pwd")) {
|
|
|
|
printf("%s\n", ms_getcwd());
|
|
|
|
} else if (!strcmp(command, "cd")) {
|
|
|
|
ms_setcwd(newargv[1]);
|
2011-09-24 20:13:16 +02:00
|
|
|
} else if (!strcmp(command, "cat")) {
|
|
|
|
ms_cat(newargv[1]);
|
2011-11-09 12:14:31 +01:00
|
|
|
} else if (!strcmp(command, "ls")) {
|
2011-11-09 13:13:41 +01:00
|
|
|
ms_ls();
|
2011-05-24 17:23:10 +02:00
|
|
|
} else {
|
2011-09-23 15:31:44 +02:00
|
|
|
char path[MAXPATH];
|
2011-09-22 15:39:20 +02:00
|
|
|
sprintf(path, "/bin/%s", command);
|
2011-05-24 18:54:53 +02:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) { //child
|
2011-09-23 15:31:44 +02:00
|
|
|
status = execve(path, newargv, environ);
|
2011-09-22 15:39:20 +02:00
|
|
|
if (status)
|
|
|
|
printf("mshell: %s: command not found\n", path);
|
2011-05-24 17:23:10 +02:00
|
|
|
return errno;
|
2011-05-24 18:54:53 +02:00
|
|
|
} else {
|
|
|
|
wait(&status);
|
|
|
|
}
|
2011-05-24 00:48:56 +02:00
|
|
|
}
|
2011-07-30 19:48:44 +02:00
|
|
|
}
|
2011-05-24 17:23:10 +02:00
|
|
|
return errno;
|
2011-05-24 00:48:56 +02:00
|
|
|
}
|
|
|
|
|