Thank you very much , of course im not using your code , neither the same logic probably , but it helped me get some idea of the project also , the way you took care of some cases , pretty intresting , now i have a clearer idea
Your code does work fine, but it has a problem. If I put ls -l, and then try to execute ls command, it will execute ls -l, you need to clear the arg[1] before executing the next command.
Miguel Diaz Like when you do ls -l your arg[1] has -l, hence when you execute only ls, the arg[1] still has -l, so it will execute ls -l, so try freeing the arg[1] before sending the arg array to your exec. I'd suggest you to use a lex and yacc to get the inputs in and store them in a command table than this method, it's way more flexible and helps you deal with pipes, grep etc.
@@zihanbu8972 Hi, I don't exactly remember how I solved it but I'm 100% sure that it was about the way we allocated memory for the arrays. When we say *parameters[20], we allocate static memory for our parameters and we cannot free the memory for our next commands. What you should do is that allocate memory dynamically by using malloc function, and maybe reallocate memory by using realloc function to change the memory section reserved for our array. I suggest you to learn about dynamic memory allocation in C, especially malloc and realloc functions.
Btw guys the code is:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "sys/wait.h"
#include "unistd.h"
void read_command(char cmd[], char *par[]) {
char line[1024];
int count = 0, i = 0, j = 0;
char *array[100], *pch;
for (;;) {
int c = fgetc(stdin);
line[count++] = (char) c;
if (c == '
') {
break;
}
if (count == 1) {
return;
}
pch = strtok(line, "
");
while (pch != NULL)
{
array[i++] = strdup(pch);
pch = strtok(NULL, "
");
}
strcpy(cmd, array[0]);
for (int j = 0; j < i; j++) {
par[j] = array[j];
par[i] = NULL;
}
}
}
void type_prompt() {
static int first_time = 1;
if (first_time) {
const char* CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDERR_FILENO, CLEAR_SCREEN_ANSI, 12);
first_time = 0;
}
printf("#");
}
int main() {
char cmd[100], command[100], *parameters[20];
char *envp[] = { (char *) "PATH=C:\\Windows\\System32", 0 };
while(1) {
type_prompt();
read_command(command, parameters);
if (fork() != 0) {
wait(NULL);
} else {
strcpy(cmd, "C:\\Windows\\System32");
strcpy(cmd, command);
execvp(cmd, parameters, envp);
}
if (strcmp(command, "exit") == 0) {
break;
}
}
return 0;
}
thx
Poggers
strcpy(cmd, command);
should be strcat(cmd, command);
Good video; it helped me get pointed in the right direction in regards to my assignment.
you sould initiablize parameters everytime otherwise the parameters stored that last command's para
its good video for a simple shell i could understand , thank you
Thank you very much , of course im not using your code , neither the same logic probably , but it helped me get some idea of the project also , the way you took care of some cases , pretty intresting , now i have a clearer idea
Are you in 379?
You may be interested in ua-cam.com/video/7Mn24vDuPgU/v-deo.html about an outstanding student who quit graduate school!
Thank you. This was very helpful.
This is the code, to help the next group of people who need it.😊
there is nothing, do not make joke when we are serious, this isn't a movie theater
how do I know my shell is working?
test it using commands
Your code does work fine, but it has a problem. If I put ls -l, and then try to execute ls command, it will execute ls -l, you need to clear the arg[1] before executing the next command.
you are right, but what arg[1] we need to clear?
Miguel Diaz Like when you do ls -l your arg[1] has -l, hence when you execute only ls, the arg[1] still has -l, so it will execute ls -l, so try freeing the arg[1] before sending the arg array to your exec. I'd suggest you to use a lex and yacc to get the inputs in and store them in a command table than this method, it's way more flexible and helps you deal with pipes, grep etc.
How exactly are we going to do that in C? I tried everything I found on internet but it didnt work.
@@erenyalcn31 Hi I am facing the same problem now. Did you figure out how to fix this bug?
@@zihanbu8972 Hi, I don't exactly remember how I solved it but I'm 100% sure that it was about the way we allocated memory for the arrays. When we say *parameters[20], we allocate static memory for our parameters and we cannot free the memory for our next commands. What you should do is that allocate memory dynamically by using malloc function, and maybe reallocate memory by using realloc function to change the memory section reserved for our array. I suggest you to learn about dynamic memory allocation in C, especially malloc and realloc functions.
Tks for share the vid. Great job!
please can i get the code
3 years later and nobody got the code.
Lol
lmao just "some headers"
I swear...
his code not working
can you write a link for your code plz,i need it???
Lmao noob.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "sys/wait.h"
#include "unistd.h"
void read_command(char cmd[], char *par[]) {
char line[1024];
int count = 0, i = 0, j = 0;
char *array[100], *pch;
for (;;) {
int c = fgetc(stdin);
line[count++] = (char) c;
if (c == '
') {
break;
}
if (count == 1) {
return;
}
pch = strtok(line, "
");
while (pch != NULL)
{
array[i++] = strdup(pch);
pch = strtok(NULL, "
");
}
strcpy(cmd, array[0]);
for (int j = 0; j < i; j++) {
par[j] = array[j];
par[i] = NULL;
}
}
}
void type_prompt() {
static int first_time = 1;
if (first_time) {
const char* CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDERR_FILENO, CLEAR_SCREEN_ANSI, 12);
first_time = 0;
}
printf("#");
}
int main() {
char cmd[100], command[100], *parameters[20];
char *envp[] = { (char *) "PATH=C:\\Windows\\System32", 0 };
while(1) {
type_prompt();
read_command(command, parameters);
if (fork() != 0) {
wait(NULL);
} else {
strcpy(cmd, "C:\\Windows\\System32");
strcpy(cmd, command);
execvp(cmd, parameters, envp);
}
if (strcmp(command, "exit") == 0) {
break;
}
}
return 0;
}
ls -l does not work
Can you give me the code?
No.
trebla
code plz!
code please
yes
85k views but less 1000 subscribers tells how wicked all these viewers are. Shame on you mean souls.