472,119 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

need to parse CL input using strtok()

Hi everybody. I haven't programmed anything in about 8 years, I've read up a little bit on C and need to write a shell in C. I want to use strtok() to take an input from a user and parse it into the command and its arguments.

for example: copy <file1> <file2> will copy file 2 to file 1, del <file1> will delete a file, etc. The exit command is all I've implemented right now, but even that produces an error when executed...I'm sure I've got a problem with how I've implemented strtok().

I've tried to implement it below, but when I compile it using gcc (no errors/warnings) and try to execute it I get the following output:

com = exit
Segmentation fault (core dumped)

Here is the code snippet:

char command[50]; //Command line input

char com[10]; //Primary command
char file1[20]; //File affected by command (if necessary)
char file2[20]; //2nd file affected by command (if necessary)

char *token; //Used for strtok() function


printf("o-shell>"); //user prompts for o-shell

scanf("%s", command); //command line input from user

//tokenize the command line input using strtok() function
//this produces the command as well as any command line arguments

while(token != NULL)
token = strtok(command, " ");
strcpy(com, token);

printf("com = %s\n", com);

token = strtok(NULL, " ");
strcpy(file1, token);

printf("file1 = %s\n", file1);

token = strtok(NULL, " ");
strcpy(file2, token);

printf("file2 = %s\n", file2);

When I run the program, I get a segmentation fault. Will this code run OK if there are no arguments, just a command? I'm sure the problem is with strtok() either way. thanks!
Jun 30 '07 #1
4 4547
7,435 Expert 4TB
What happens if one of the first calls to strtok returns NULL? Could this be causing the fault?
Jun 30 '07 #2
9,208 Expert Mod 8TB
When you drop to your while loop the first time, the token has garbage in it.
char *token; //Used for strtok() function
If the garbage is not null, you go inside the loop, corrupt memory and that produces your segmentation fault.

You must always initize pointers before using them.
Jun 30 '07 #3
I redid my code and it partially works right now. I can use strtok to find the first input in the command line, but cannot get the arguments.

The format is <command> <arg1> <arg2>

So far, I've only implemented 'exit' and 'help'. When I run this code, it compiles fine but I get a segmentation fault. I've placed a couple printf to help debug. One of the problems is that the arguments are not detected/counted.

Here is the output:

************************************************** *
Welcome to O-Shell! A UNIX shell by O Haqqi

Type 'help' for list of commands, 'exit' to exit
************************************************** *

exit was entered
There are 0 arguments
Command is exit
Segmentation fault (core dumped)

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

int main(int argc, char *argv[])
char command[50]; //Command line input

//tokens for strtok() funtion
char * com; //Primary command
char * arg1; //First argument of command (usually a file name)
char * arg2; //2nd argument of command (usually a file name)

int args; //Number of arguments entered

printf("****************************************** *********\n");
printf("Welcome to O-Shell! A UNIX shell by O Haqqi\n\n");
printf("Type 'help' for list of commands, 'exit' to exit\n");
printf("****************************************** *********\n\n");


printf("o-shell>"); //user prompts for o-shell

scanf("%s", command); //command line input from user

printf("%s was entered\n", command);

com = strtok(command, " \r\n\t");

args = 0;
arg1 = strtok(NULL, " \r\n\t");
if(arg1) args++; //to see if arguments being tracked
arg2 = strtok(NULL, " \r\n\t");
if(arg2) args++; //to see if arguments being tracked

printf("There are %d arguments\n", args);
printf("Command is %s\n", com);
printf("File1 is %s\n", arg1);
printf("File2 is %s\n", arg2);

if ((strcmp(com,"help")) ==0 || (strcmp(com,"Help")) ==0)
printf("\nO-Shell Help Menu:\n");
printf("\nhelp - Print help menu\n");
printf("\nexit - Exit O-Shell\n");
printf("\ntype <file> - Print contents of <file>\n");
printf("\ncopy <file1> <file2> - Copy contents of <file1> to <file2>.");
printf("\n <file2> must be a non-existent file and must be created\n");
printf("\ndelete <file> - Delete <file>\n\n");

else if ((strcmp(com,"exit")) ==0 || (strcmp(com,"Exit")) ==0)
printf("Exiting O-Shell...Goodbye!\n");

printf("Invalid command!!! No operation performed...\n");


return 0;
Jul 1 '07 #4
9,208 Expert Mod 8TB
How are these commands being entered???

What does one look like??

Your scanf("%s", command) is going to stop at the first whitespace character. In effect, you will get one word. There will be no \r \n \t in the command becuse they will be eaten by scanf().

Did to mean to use getch() inside a loop??

Please give me an example of usage.
Jul 1 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by Johnny Google | last post: by
10 posts views Thread by Matt | last post: by
7 posts views Thread by Steven Woody | last post: by
46 posts views Thread by vvk4 | last post: by
3 posts views Thread by federico_bertola | last post: by
6 posts views Thread by Richard | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.