Pliki_SOP/Microshell/microshell.c

321 lines
6.3 KiB
C
Raw Normal View History

2019-01-10 12:56:56 +01:00
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
2019-01-10 14:17:29 +01:00
#include <sys/types.h>
#include <sys/wait.h>
2019-01-11 15:48:26 +01:00
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <readline/readline.h>
#include <readline/history.h>
2019-01-17 13:21:50 +01:00
#include <fcntl.h>
2019-01-18 18:20:25 +01:00
#include <ctype.h>
2019-01-10 12:56:56 +01:00
const int max = 1000;
2019-01-10 19:52:29 +01:00
void cut(char **argv, char *command){
int w = 0;
char sep[] = " ";
char *ptr = strtok(command, sep);
while (ptr != NULL){
argv[w]=ptr;
w+=1;
ptr = strtok(NULL, sep);
2019-01-10 13:13:37 +01:00
}
argv[w]=NULL;
2019-01-10 12:56:56 +01:00
}
2019-01-17 13:21:50 +01:00
void rmChar(char *str, char garbage) { /*przesledzic jak to dziala!!!!! w opisie sobie napisac*/
char *src, *dst;
for (src = dst = str; *src != '\0'; src++) {
*dst = *src;
if (*dst != garbage) dst++;
}
*dst = '\0';
}
2019-01-11 15:48:26 +01:00
void cd (char **argv){
if(argv[1] == NULL){
printf("Add arguments\n");
return;
}
else if(argv[2] != NULL){
printf("Too many arguments\n");
return;
}
else{
if (chdir(argv[1]) != 0)
perror("chdir error");
return;
}
}
void spathf (char **argv, int *spath){
int l;
if (argv[1] == NULL){
printf("Add arguments\n");
return;
}
else if (argv[2] != NULL){
printf("Too many arguments\n");
return;
}
else if (!strcmp(argv[1], "help")){
printf("Arguments:\n0 turns off path showing\n1 turns on path showing\n");
return;
}
l = argv[1][0] - '0';
if (argv[1][1] != 0 || l < 0 || l > 1){
printf("Wrong number\n");
return;
}
else
*spath = l;
}
2019-01-13 12:07:28 +01:00
void help (char **argv){
if (fork() == 0){
execlp("less", "less", "help", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
else
wait(NULL);
}
void exe (char **argv){
if (fork() == 0){
execvp(argv[0], argv);
perror("execvp");
exit(EXIT_FAILURE);
}
else
wait(NULL);
}
2019-01-14 12:54:39 +01:00
void ptab (int ile){
int i;
for (i=0; i<ile; i++)
printf(" |");
}
2019-01-13 12:07:28 +01:00
void tree (char **argv, char * directory){
if (argv[1] != NULL && argv[2] != NULL){
printf("Too many arguments\nshowtree takes no arguments \nor exacly 1 argument for desired directory\n");
return;
}
else{
if (fork() == 0){
DIR *d;
struct dirent *ent;
2019-01-13 16:47:33 +01:00
char ndr[max];
2019-01-14 12:54:39 +01:00
int tab = 0;
2019-01-17 13:21:50 +01:00
if (argv[1] != NULL && argv[2] == NULL){ /*jeden argument*/
cd(argv);
getcwd(ndr, sizeof(ndr));
}
else
strcpy(ndr, directory);
printf("Content of: %s\n", ndr);
2019-01-13 12:07:28 +01:00
2019-01-13 16:47:33 +01:00
if ((d = opendir(ndr)) != NULL){ /*wchodzimy do katalogu*/
while ((ent = readdir(d)) != NULL){ /*czytamy zawartosc*/
char *name = ent->d_name;
if (!(!strcmp(name, ".") || !strcmp(name, ".."))){ /*jesli nazwa rozna od . lub .. */
if (fork()==0){
2019-01-13 12:07:28 +01:00
if (chdir(name) == 0){ /*udalo nam sie wejsc do katalogu*/
closedir(d);
2019-01-14 12:54:39 +01:00
ptab(tab);
printf("\033[1;34m%s\033[0m\n", name);
2019-01-13 12:07:28 +01:00
getcwd(ndr, sizeof(ndr)); /*zmieniamy directory*/
2019-01-13 16:47:33 +01:00
d = opendir(ndr);
2019-01-14 12:54:39 +01:00
tab++;
2019-01-13 12:07:28 +01:00
}
2019-01-13 16:47:33 +01:00
else{
2019-01-14 12:54:39 +01:00
ptab(tab);
printf("%s\n", name);
2019-01-13 16:47:33 +01:00
exit(0);
}
2019-01-13 12:07:28 +01:00
}
2019-01-13 16:47:33 +01:00
else
wait(NULL);
2019-01-13 12:07:28 +01:00
}
}
2019-01-13 16:47:33 +01:00
closedir(d);
2019-01-13 12:07:28 +01:00
}
exit(0);
}
else{
wait(NULL);
return;
}
}
}
2019-01-17 13:21:50 +01:00
void mycp (char **argv){
if (argv[1] == NULL || argv[2] == NULL){
printf("Add arguments\n");
return;
}
else if (argv[3] != NULL){
printf("Too many arguments\n");
return;
}
else{
int file1 = open(argv[1], O_RDONLY);
if (file1 == -1){
perror("open error");
return;
}
if (open(argv[2], O_RDONLY) != -1){
printf("%s already exists!\n", argv[2]);
return;
}
int newfile = creat(argv[2], 0666);
if (newfile == -1){
perror("create error");
return;
}
char bufor[1024];
int r;
while ((r = read(file1, bufor, sizeof(bufor))) > 0)
write(newfile, bufor, r);
close(file1);
close(newfile);
}
}
2019-01-18 18:20:25 +01:00
void myps (char **argv){
if (fork() == 0){
if (chdir("/proc") != 0){
perror("cant open /proc");
exit(0);
}
else{ /*weszlismy w 1 fork do katalogu proc */
DIR *d;
struct dirent *ent;
int i, p;
if ((d = opendir("/proc")) != NULL){ /*otwieramy proc*/
while ((ent = readdir(d)) != NULL){ /*czytamy zawartosc*/
char *name = ent->d_name;
p=0;
for (i=0; i<strlen(name); i++)
if (!isdigit(name[i])){
p=1;
break;
}
if(p)
continue; /*jesli nie jest liczba to idziemy dalej*/
char *direc;
direc = (char*)malloc((strlen(name) + 10) * sizeof(char));
direc[0] = '\0';
strcat(direc, "./");
strcat(direc, name);
strcat(direc, "/status"); /*tworzenie sciezki*/
2019-01-18 19:43:13 +01:00
if (fork() == 0){
printf("Pid: %s ", name);
exit(0);
}
2019-01-18 18:20:25 +01:00
else
wait(NULL);
2019-01-18 19:43:13 +01:00
if (fork() == 0){
execlp("sed", "sed", "-n", "1p", direc, NULL);
}
2019-01-18 18:20:25 +01:00
else
wait(NULL);
}
}
}
exit(0);
}
else
wait(NULL);
}
2019-01-10 12:56:56 +01:00
int main (){
2019-01-17 13:21:50 +01:00
int spath = 1, i=0;
using_history();
2019-01-13 12:07:28 +01:00
struct passwd *p = getpwuid(getuid());
2019-01-10 13:13:37 +01:00
while (1){
2019-01-13 12:07:28 +01:00
char *argv[100];
char *komenda, *prompt, directory[max];
2019-01-13 12:07:28 +01:00
if (getcwd(directory, sizeof(directory)) == NULL){
2019-01-10 13:13:37 +01:00
perror("getcwd() error");
2019-01-11 15:48:26 +01:00
return -1;
2019-01-10 13:13:37 +01:00
}
if (spath){
prompt = (char*)malloc(strlen(directory)+strlen(p->pw_name)+22 *sizeof(char));
prompt[0]='\0';
strcat(prompt, "\033[1;32m");
strcat(prompt, "[");
strcat(prompt, directory);
strcat(prompt, "]\033[0m");
strcat(prompt, p->pw_name);
}
else{
prompt = (char*)malloc(strlen(p->pw_name)+3 *sizeof(char));
strcat(prompt, p->pw_name);
}
strcat(prompt, "$ ");
if (!(komenda = readline(prompt)))
break;
if (komenda[0] == 0)
continue;
add_history(komenda);
cut(argv, komenda);
2019-01-17 13:21:50 +01:00
while (argv[i] != NULL){
rmChar(argv[i], 34);
i+=1;
}
i=0;
2019-01-11 15:48:26 +01:00
2019-01-10 13:59:45 +01:00
if (argv[0]==NULL)
continue;
2019-01-11 15:48:26 +01:00
else if (!strcmp(argv[0], "spath")){
spathf(argv, &spath);
continue;
}
else if (!strcmp(argv[0], "cd")){
cd(argv);
continue;
}
2019-01-10 23:53:16 +01:00
else if (!strcmp(argv[0], "help")){
2019-01-13 12:07:28 +01:00
help(argv);
continue;
}
else if (!strcmp(argv[0], "showtree")){
tree(argv, directory);
continue;
2019-01-10 23:53:16 +01:00
}
2019-01-17 13:21:50 +01:00
else if (!strcmp(argv[0], "mycp")){
mycp(argv);
continue;
}
2019-01-18 18:20:25 +01:00
else if (!strcmp(argv[0], "showps")){
myps(argv);
continue;
}
2019-01-10 15:05:24 +01:00
else if (!strcmp(argv[0], "exit"))
2019-01-10 14:57:55 +01:00
break;
2019-01-10 13:17:30 +01:00
else
2019-01-13 12:07:28 +01:00
exe(argv);
2019-01-10 14:36:35 +01:00
free(komenda);
free(prompt);
2019-01-10 13:13:37 +01:00
}
return 0;
2019-01-10 12:56:56 +01:00
}