R
rabbits77
x-no-archive: yes
I am playing around with some code trying to make a toy
command shell.
What is below is very much a work in progress but what I am finding
is that the strcmp()s are not behaving as I expected.
That is if I enter a command line like
run something
the command "run" is correctly parsed by strtok
but the
if (!strcmp(command, "run"))
does not print the line saying it was recognized.
Why is that?
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#define MAXSIZE 4096
#define ONEBYTE 1
#define NUMBYTES(x) x*2
void execute_command();
int main(){
int n;
int num_chars=-1;
char char_buf[1];
char* command_line;
command_line=(char*)malloc(MAXSIZE);
bzero(command_line, MAXSIZE);
fcntl(STDIN_FILENO, F_SETFL, O_ASYNC); // set to
asynchronous I/O
while((n=read(STDIN_FILENO, char_buf,
ONEBYTE))==ONEBYTE){//read one byte at a time
num_chars++;
if(char_buf[0]=='\n'){
/*write(STDOUT_FILENO, command_line, NUMBYTES(num_chars));*/
execute_command(command_line);
bzero(command_line, MAXSIZE);
num_chars=-1;
}
else{
strcat(command_line,char_buf);
}
}
}
void execute_command(char* command_line){
char* command = strtok(command_line, " ");
char* argument = strtok(NULL,"");
printf("command: \"%s\"\n",command);
printf("argument: \"%s\"\n",argument);
command[3]='\0';
if (!strcmp(command, "run")) {
printf("run: \"%s\"\n",command);
}
if (strcmp(command, "show")) {
printf("show: \"%s\"\n",command);
}
if (strcmp(command, "kill")) {
printf("kill: \"%s\"\n",command);
}
}
I am playing around with some code trying to make a toy
command shell.
What is below is very much a work in progress but what I am finding
is that the strcmp()s are not behaving as I expected.
That is if I enter a command line like
run something
the command "run" is correctly parsed by strtok
but the
if (!strcmp(command, "run"))
does not print the line saying it was recognized.
Why is that?
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#define MAXSIZE 4096
#define ONEBYTE 1
#define NUMBYTES(x) x*2
void execute_command();
int main(){
int n;
int num_chars=-1;
char char_buf[1];
char* command_line;
command_line=(char*)malloc(MAXSIZE);
bzero(command_line, MAXSIZE);
fcntl(STDIN_FILENO, F_SETFL, O_ASYNC); // set to
asynchronous I/O
while((n=read(STDIN_FILENO, char_buf,
ONEBYTE))==ONEBYTE){//read one byte at a time
num_chars++;
if(char_buf[0]=='\n'){
/*write(STDOUT_FILENO, command_line, NUMBYTES(num_chars));*/
execute_command(command_line);
bzero(command_line, MAXSIZE);
num_chars=-1;
}
else{
strcat(command_line,char_buf);
}
}
}
void execute_command(char* command_line){
char* command = strtok(command_line, " ");
char* argument = strtok(NULL,"");
printf("command: \"%s\"\n",command);
printf("argument: \"%s\"\n",argument);
command[3]='\0';
if (!strcmp(command, "run")) {
printf("run: \"%s\"\n",command);
}
if (strcmp(command, "show")) {
printf("show: \"%s\"\n",command);
}
if (strcmp(command, "kill")) {
printf("kill: \"%s\"\n",command);
}
}