writing a shell in C++, Cannot use Unix system() library routine to execute the command line











up vote
-3
down vote

favorite












I have a project that I am currently stuck on. Here is what I got so far.



#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>

#define newsh_buffSize 1024
#define newsh_tok_buff 64
#define newsh_tok_delim " trna"

// Reading from standard input and return //
void *newsh_read_line(void){
char *line = NULL;
ssize_t bufsize = 0;
getline(&line, &bufsize, stdin);
return line;
}
char** newsh_split_line(char *line){
int bufsize = newsh_tok_buff;
int place = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;

if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}

token = strtok(line, newsh_tok_delim);
while(token != NULL) {
tokens[place] = token;
place++;

if(place <= bufsize){
bufsize += newsh_tok_buff;
tokens = realloc(tokens, bufsize * sizeof(char*));
if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, newsh_tok_delim);
}
tokens[place] = NULL;
return tokens;
}
void newsh_loop(void){
char *line;
char **args;
int status;

do{
printf(">>");
line = newsh_read_line();
args = newsh_split_line(line);
status = newsh_execute(args);

free(line);
free(args);
}while(status);
}

int newsh_launch(char **args) {
pid_t pid, wpid;
int status;

pid = fork();
if (pid == 0) {
if(execvp(args[0], args) == -1) {
perror("newsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0) {
perror("newsh");
}
else {
do{
wpid = waitpid(pid, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}

// Declaring Built in Commands //
int newsh_cd(char **args);
int newsh_bp(char **args);
int newsh_var(char **args);
int newsh_quit(char **args);

char *builtin_str = {
"cd",
"quit"
};

int(*builtin_func)(char **) = {
&newsh_cd,
&newsh_quit,
&newsh_var,
&newsh_bp
};

int newsh_num_builtin(){
return sizeof(builtin_str)/sizeof(char*);
}

// Command Implementation //
int newsh_cd(char **args) {

}

int newsh_quit(char **args){
return 0;
}

int newsh_execute(char **args) {
int j;

if(args[0] == NULL) {
return 1;
}
for(j = 0; j < newsh_num_builtin(); j++) {
if(strcmp(args[0], builtin_str[j]) == 0) {
return(*builtin_func[j])(args);
}
}
return newsh_launch(args);
}

// MAin function Implementation //
int main(int args, char **argv) {
newsh_loop();
EXIT_SUCCESS;
}


I have to write a shell in C/C++ without using Unix system() library routine to execute the command line. The shell is called newsh and must be able to handle the following built-in commands:
1) set variable value : variable being any reasonable variable name and value being a number / a token. Keep in mind variables are case sensitive.
2) cd directoryName: change the directory to the "directoryName" which can be either absolute (starting with /) or relative (no /). cd must also have a single parameter.
3)bp, list all processes running in the background, in any format but shouldn't rely on external programs like ps to create its output
4) quit, exits with exit status 0, should also accept on the input stream and treat it as if the user has typed quit.



In my code there's some program control commands, but for right now I would like to be able to just get these 4 builtin commands going. Basically I need to readline from standard input, break the lines into tokens, and check for correctness. In the end I would like to be to test my program by using a testfile by envoking newsh < testfile . Any help or input on these 4 is greatly appreciated, thanks!










share|improve this question






















  • "in C/C++" nice new language.
    – Swordfish
    Nov 10 at 19:54










  • This code comes across as strongly C. Why is it tagged C++? In C++ you'd do a lot of this very different.
    – Jesper Juhl
    Nov 10 at 20:37








  • 2




    "Any help or input on these 4" - A question should ask one question. Not four. Four questions should be four questions.
    – Jesper Juhl
    Nov 10 at 20:42

















up vote
-3
down vote

favorite












I have a project that I am currently stuck on. Here is what I got so far.



#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>

#define newsh_buffSize 1024
#define newsh_tok_buff 64
#define newsh_tok_delim " trna"

// Reading from standard input and return //
void *newsh_read_line(void){
char *line = NULL;
ssize_t bufsize = 0;
getline(&line, &bufsize, stdin);
return line;
}
char** newsh_split_line(char *line){
int bufsize = newsh_tok_buff;
int place = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;

if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}

token = strtok(line, newsh_tok_delim);
while(token != NULL) {
tokens[place] = token;
place++;

if(place <= bufsize){
bufsize += newsh_tok_buff;
tokens = realloc(tokens, bufsize * sizeof(char*));
if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, newsh_tok_delim);
}
tokens[place] = NULL;
return tokens;
}
void newsh_loop(void){
char *line;
char **args;
int status;

do{
printf(">>");
line = newsh_read_line();
args = newsh_split_line(line);
status = newsh_execute(args);

free(line);
free(args);
}while(status);
}

int newsh_launch(char **args) {
pid_t pid, wpid;
int status;

pid = fork();
if (pid == 0) {
if(execvp(args[0], args) == -1) {
perror("newsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0) {
perror("newsh");
}
else {
do{
wpid = waitpid(pid, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}

// Declaring Built in Commands //
int newsh_cd(char **args);
int newsh_bp(char **args);
int newsh_var(char **args);
int newsh_quit(char **args);

char *builtin_str = {
"cd",
"quit"
};

int(*builtin_func)(char **) = {
&newsh_cd,
&newsh_quit,
&newsh_var,
&newsh_bp
};

int newsh_num_builtin(){
return sizeof(builtin_str)/sizeof(char*);
}

// Command Implementation //
int newsh_cd(char **args) {

}

int newsh_quit(char **args){
return 0;
}

int newsh_execute(char **args) {
int j;

if(args[0] == NULL) {
return 1;
}
for(j = 0; j < newsh_num_builtin(); j++) {
if(strcmp(args[0], builtin_str[j]) == 0) {
return(*builtin_func[j])(args);
}
}
return newsh_launch(args);
}

// MAin function Implementation //
int main(int args, char **argv) {
newsh_loop();
EXIT_SUCCESS;
}


I have to write a shell in C/C++ without using Unix system() library routine to execute the command line. The shell is called newsh and must be able to handle the following built-in commands:
1) set variable value : variable being any reasonable variable name and value being a number / a token. Keep in mind variables are case sensitive.
2) cd directoryName: change the directory to the "directoryName" which can be either absolute (starting with /) or relative (no /). cd must also have a single parameter.
3)bp, list all processes running in the background, in any format but shouldn't rely on external programs like ps to create its output
4) quit, exits with exit status 0, should also accept on the input stream and treat it as if the user has typed quit.



In my code there's some program control commands, but for right now I would like to be able to just get these 4 builtin commands going. Basically I need to readline from standard input, break the lines into tokens, and check for correctness. In the end I would like to be to test my program by using a testfile by envoking newsh < testfile . Any help or input on these 4 is greatly appreciated, thanks!










share|improve this question






















  • "in C/C++" nice new language.
    – Swordfish
    Nov 10 at 19:54










  • This code comes across as strongly C. Why is it tagged C++? In C++ you'd do a lot of this very different.
    – Jesper Juhl
    Nov 10 at 20:37








  • 2




    "Any help or input on these 4" - A question should ask one question. Not four. Four questions should be four questions.
    – Jesper Juhl
    Nov 10 at 20:42















up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I have a project that I am currently stuck on. Here is what I got so far.



#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>

#define newsh_buffSize 1024
#define newsh_tok_buff 64
#define newsh_tok_delim " trna"

// Reading from standard input and return //
void *newsh_read_line(void){
char *line = NULL;
ssize_t bufsize = 0;
getline(&line, &bufsize, stdin);
return line;
}
char** newsh_split_line(char *line){
int bufsize = newsh_tok_buff;
int place = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;

if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}

token = strtok(line, newsh_tok_delim);
while(token != NULL) {
tokens[place] = token;
place++;

if(place <= bufsize){
bufsize += newsh_tok_buff;
tokens = realloc(tokens, bufsize * sizeof(char*));
if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, newsh_tok_delim);
}
tokens[place] = NULL;
return tokens;
}
void newsh_loop(void){
char *line;
char **args;
int status;

do{
printf(">>");
line = newsh_read_line();
args = newsh_split_line(line);
status = newsh_execute(args);

free(line);
free(args);
}while(status);
}

int newsh_launch(char **args) {
pid_t pid, wpid;
int status;

pid = fork();
if (pid == 0) {
if(execvp(args[0], args) == -1) {
perror("newsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0) {
perror("newsh");
}
else {
do{
wpid = waitpid(pid, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}

// Declaring Built in Commands //
int newsh_cd(char **args);
int newsh_bp(char **args);
int newsh_var(char **args);
int newsh_quit(char **args);

char *builtin_str = {
"cd",
"quit"
};

int(*builtin_func)(char **) = {
&newsh_cd,
&newsh_quit,
&newsh_var,
&newsh_bp
};

int newsh_num_builtin(){
return sizeof(builtin_str)/sizeof(char*);
}

// Command Implementation //
int newsh_cd(char **args) {

}

int newsh_quit(char **args){
return 0;
}

int newsh_execute(char **args) {
int j;

if(args[0] == NULL) {
return 1;
}
for(j = 0; j < newsh_num_builtin(); j++) {
if(strcmp(args[0], builtin_str[j]) == 0) {
return(*builtin_func[j])(args);
}
}
return newsh_launch(args);
}

// MAin function Implementation //
int main(int args, char **argv) {
newsh_loop();
EXIT_SUCCESS;
}


I have to write a shell in C/C++ without using Unix system() library routine to execute the command line. The shell is called newsh and must be able to handle the following built-in commands:
1) set variable value : variable being any reasonable variable name and value being a number / a token. Keep in mind variables are case sensitive.
2) cd directoryName: change the directory to the "directoryName" which can be either absolute (starting with /) or relative (no /). cd must also have a single parameter.
3)bp, list all processes running in the background, in any format but shouldn't rely on external programs like ps to create its output
4) quit, exits with exit status 0, should also accept on the input stream and treat it as if the user has typed quit.



In my code there's some program control commands, but for right now I would like to be able to just get these 4 builtin commands going. Basically I need to readline from standard input, break the lines into tokens, and check for correctness. In the end I would like to be to test my program by using a testfile by envoking newsh < testfile . Any help or input on these 4 is greatly appreciated, thanks!










share|improve this question













I have a project that I am currently stuck on. Here is what I got so far.



#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/wait.h>

#define newsh_buffSize 1024
#define newsh_tok_buff 64
#define newsh_tok_delim " trna"

// Reading from standard input and return //
void *newsh_read_line(void){
char *line = NULL;
ssize_t bufsize = 0;
getline(&line, &bufsize, stdin);
return line;
}
char** newsh_split_line(char *line){
int bufsize = newsh_tok_buff;
int place = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;

if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}

token = strtok(line, newsh_tok_delim);
while(token != NULL) {
tokens[place] = token;
place++;

if(place <= bufsize){
bufsize += newsh_tok_buff;
tokens = realloc(tokens, bufsize * sizeof(char*));
if(!tokens) {
fprintf(stderr, "Allocation Errorn");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, newsh_tok_delim);
}
tokens[place] = NULL;
return tokens;
}
void newsh_loop(void){
char *line;
char **args;
int status;

do{
printf(">>");
line = newsh_read_line();
args = newsh_split_line(line);
status = newsh_execute(args);

free(line);
free(args);
}while(status);
}

int newsh_launch(char **args) {
pid_t pid, wpid;
int status;

pid = fork();
if (pid == 0) {
if(execvp(args[0], args) == -1) {
perror("newsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0) {
perror("newsh");
}
else {
do{
wpid = waitpid(pid, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}

// Declaring Built in Commands //
int newsh_cd(char **args);
int newsh_bp(char **args);
int newsh_var(char **args);
int newsh_quit(char **args);

char *builtin_str = {
"cd",
"quit"
};

int(*builtin_func)(char **) = {
&newsh_cd,
&newsh_quit,
&newsh_var,
&newsh_bp
};

int newsh_num_builtin(){
return sizeof(builtin_str)/sizeof(char*);
}

// Command Implementation //
int newsh_cd(char **args) {

}

int newsh_quit(char **args){
return 0;
}

int newsh_execute(char **args) {
int j;

if(args[0] == NULL) {
return 1;
}
for(j = 0; j < newsh_num_builtin(); j++) {
if(strcmp(args[0], builtin_str[j]) == 0) {
return(*builtin_func[j])(args);
}
}
return newsh_launch(args);
}

// MAin function Implementation //
int main(int args, char **argv) {
newsh_loop();
EXIT_SUCCESS;
}


I have to write a shell in C/C++ without using Unix system() library routine to execute the command line. The shell is called newsh and must be able to handle the following built-in commands:
1) set variable value : variable being any reasonable variable name and value being a number / a token. Keep in mind variables are case sensitive.
2) cd directoryName: change the directory to the "directoryName" which can be either absolute (starting with /) or relative (no /). cd must also have a single parameter.
3)bp, list all processes running in the background, in any format but shouldn't rely on external programs like ps to create its output
4) quit, exits with exit status 0, should also accept on the input stream and treat it as if the user has typed quit.



In my code there's some program control commands, but for right now I would like to be able to just get these 4 builtin commands going. Basically I need to readline from standard input, break the lines into tokens, and check for correctness. In the end I would like to be to test my program by using a testfile by envoking newsh < testfile . Any help or input on these 4 is greatly appreciated, thanks!







c++ shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 19:47









Jermane Robinson

1




1












  • "in C/C++" nice new language.
    – Swordfish
    Nov 10 at 19:54










  • This code comes across as strongly C. Why is it tagged C++? In C++ you'd do a lot of this very different.
    – Jesper Juhl
    Nov 10 at 20:37








  • 2




    "Any help or input on these 4" - A question should ask one question. Not four. Four questions should be four questions.
    – Jesper Juhl
    Nov 10 at 20:42




















  • "in C/C++" nice new language.
    – Swordfish
    Nov 10 at 19:54










  • This code comes across as strongly C. Why is it tagged C++? In C++ you'd do a lot of this very different.
    – Jesper Juhl
    Nov 10 at 20:37








  • 2




    "Any help or input on these 4" - A question should ask one question. Not four. Four questions should be four questions.
    – Jesper Juhl
    Nov 10 at 20:42


















"in C/C++" nice new language.
– Swordfish
Nov 10 at 19:54




"in C/C++" nice new language.
– Swordfish
Nov 10 at 19:54












This code comes across as strongly C. Why is it tagged C++? In C++ you'd do a lot of this very different.
– Jesper Juhl
Nov 10 at 20:37






This code comes across as strongly C. Why is it tagged C++? In C++ you'd do a lot of this very different.
– Jesper Juhl
Nov 10 at 20:37






2




2




"Any help or input on these 4" - A question should ask one question. Not four. Four questions should be four questions.
– Jesper Juhl
Nov 10 at 20:42






"Any help or input on these 4" - A question should ask one question. Not four. Four questions should be four questions.
– Jesper Juhl
Nov 10 at 20:42



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242785%2fwriting-a-shell-in-c-cannot-use-unix-system-library-routine-to-execute-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242785%2fwriting-a-shell-in-c-cannot-use-unix-system-library-routine-to-execute-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values