2019-02-11 12:27:35 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2019-02-11 21:05:34 +01:00
|
|
|
#define BUFFERSIZE 255
|
|
|
|
#define BLU "\x1B[34m"
|
|
|
|
#define GRN "\x1B[32m"
|
|
|
|
#define YEL "\x1B[33m"
|
|
|
|
#define RESET "\x1B[0m"
|
2019-02-11 12:27:35 +01:00
|
|
|
|
|
|
|
void help(){
|
2019-02-11 21:05:34 +01:00
|
|
|
printf(BLU " Krystian Janowicz \n Dostepne polecenia: \n echo [arg] \n pwd \n help \n exit \n ls \n" RESET);
|
2019-02-11 12:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void pwd(){
|
2019-02-11 21:05:34 +01:00
|
|
|
char path[BUFFERSIZE];
|
|
|
|
getcwd(path,BUFFERSIZE);
|
|
|
|
printf(GRN " %s" RESET,path);
|
2019-02-11 12:27:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void prompt(){
|
|
|
|
char *user;
|
|
|
|
char *path;
|
2019-02-11 21:05:34 +01:00
|
|
|
char hostname[BUFFERSIZE];
|
2019-02-11 12:27:35 +01:00
|
|
|
|
|
|
|
user = getenv( "USER" );
|
2019-02-11 21:05:34 +01:00
|
|
|
memset(hostname, 0, BUFFERSIZE);
|
|
|
|
gethostname(hostname, BUFFERSIZE);
|
|
|
|
printf(YEL "%s@" RESET, user);
|
|
|
|
printf(YEL "%s" RESET, hostname);
|
|
|
|
pwd();
|
2019-02-11 12:27:35 +01:00
|
|
|
printf(" $ ");
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:05:34 +01:00
|
|
|
int echo(char komenda[BUFFERSIZE]){
|
2019-02-11 12:27:35 +01:00
|
|
|
char *sprawdzczy = " ";
|
|
|
|
int dlugosc=strlen(komenda);
|
|
|
|
for(int i=0;i<=dlugosc-1; i++){
|
|
|
|
|
|
|
|
char temp;
|
|
|
|
temp=komenda[i];
|
|
|
|
|
|
|
|
if(temp==' '){
|
|
|
|
char *reszta = strpbrk(komenda, sprawdzczy);
|
2019-02-11 21:05:34 +01:00
|
|
|
printf("%s", reszta);
|
|
|
|
return 0;
|
2019-02-11 12:27:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:05:34 +01:00
|
|
|
int exxit(char komenda[BUFFERSIZE]){
|
2019-02-11 12:27:35 +01:00
|
|
|
char *sprawdzczy = " ";
|
|
|
|
int dlugosc=strlen(komenda);
|
|
|
|
for(int i=0;i<=dlugosc-1; i++)
|
|
|
|
{
|
|
|
|
|
|
|
|
char temp;
|
|
|
|
temp=komenda[i];
|
|
|
|
char *sprawdzczy = " ";
|
|
|
|
if(temp==' '){
|
|
|
|
|
|
|
|
char *reszta = strpbrk(komenda, sprawdzczy);
|
|
|
|
int dlugoscReszty=strlen(reszta)-1;
|
|
|
|
printf("%d\n", dlugoscReszty);
|
|
|
|
int zwrocik=reszta[1];
|
|
|
|
return zwrocik-48; // -48 bo ASCII, nie wiedzialem jak to przekonwertowac "normalnie"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2019-02-11 21:05:34 +01:00
|
|
|
void cd(char komenda[BUFFERSIZE]){
|
|
|
|
char *tok;
|
|
|
|
tok = strchr(komenda,' ');
|
|
|
|
|
|
|
|
char *tempTok = tok + 1;
|
|
|
|
tok = tempTok;
|
|
|
|
char *locationOfNewLine = strchr(tok, '\n');
|
|
|
|
if(locationOfNewLine) {
|
|
|
|
*locationOfNewLine = '\0';
|
|
|
|
}
|
|
|
|
chdir(tok);
|
|
|
|
}
|
2019-02-11 12:27:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
void list_dir(char *name) {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dp;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
2019-02-11 21:05:34 +01:00
|
|
|
|
2019-02-11 12:27:35 +01:00
|
|
|
|
|
|
|
if ((dir = opendir(name)) == NULL) {
|
|
|
|
perror("Blad");
|
|
|
|
}
|
|
|
|
while ((dp = readdir(dir)) != NULL) {
|
|
|
|
if (stat(dp->d_name, &statbuf) == -1) continue;
|
|
|
|
if (dp->d_name[0] == '.') continue;
|
2019-02-11 21:05:34 +01:00
|
|
|
|
|
|
|
printf( "%s ", dp->d_name);
|
2019-02-11 12:27:35 +01:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
2019-02-11 21:05:34 +01:00
|
|
|
char komenda[BUFFERSIZE];
|
2019-02-11 12:27:35 +01:00
|
|
|
|
|
|
|
do{
|
2019-02-11 21:05:34 +01:00
|
|
|
prompt();
|
|
|
|
bzero(komenda, BUFFERSIZE);
|
|
|
|
fgets(komenda, BUFFERSIZE, stdin);
|
2019-02-11 12:27:35 +01:00
|
|
|
|
2019-02-11 21:05:34 +01:00
|
|
|
if (strcmp(komenda, "help\n") == 0){
|
2019-02-11 12:27:35 +01:00
|
|
|
help();
|
|
|
|
}
|
2019-02-11 21:05:34 +01:00
|
|
|
else if (strcmp(komenda, "ls\n") == 0){
|
2019-02-11 12:27:35 +01:00
|
|
|
chdir(*(++argv));
|
|
|
|
list_dir(".");
|
2019-02-11 21:05:34 +01:00
|
|
|
printf("\n");
|
2019-02-11 12:27:35 +01:00
|
|
|
}
|
2019-02-11 21:05:34 +01:00
|
|
|
else if (strcmp(komenda, "pwd\n") == 0){
|
2019-02-11 12:27:35 +01:00
|
|
|
pwd() ;
|
2019-02-11 21:05:34 +01:00
|
|
|
printf("\n");
|
2019-02-11 12:27:35 +01:00
|
|
|
}
|
2019-02-11 21:05:34 +01:00
|
|
|
else if((komenda[0]=='c') && (komenda[1]=='d')) {
|
|
|
|
cd(komenda);
|
|
|
|
|
|
|
|
}else if ((komenda[0]=='e') && (komenda[1]=='c') && (komenda[2]=='h') && (komenda[3]=='o')){
|
2019-02-11 12:27:35 +01:00
|
|
|
|
|
|
|
echo(komenda);
|
|
|
|
}
|
|
|
|
else if ((komenda[0]=='e') && (komenda[1]=='x') && (komenda[2]=='i') && (komenda[3]=='t')){
|
|
|
|
int zwrot=exxit(komenda);
|
|
|
|
return zwrot;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
printf ("nie znaleziono polecenia. uzyj: help \n" ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
}while(1);
|
|
|
|
|
|
|
|
}
|