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: o-shell>exit
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
for(;;)
{
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!
4 4733
What happens if one of the first calls to strtok returns NULL? Could this be causing the fault?
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.
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
************************************************** *
o-shell>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");
for(;;)
{
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");
break;
}
else
printf("Invalid command!!! No operation performed...\n");
}
return 0;
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Johnny Google |
last post by:
Here is an example of the type of data from a file I will have:
Apple,4322,3435,4653,6543,4652
Banana,6934,5423,6753,6531
Carrot,3454,4534,3434,1111,9120,5453
Cheese,4411,5522,6622,6641
The...
|
by: Matt |
last post by:
Ok, so the exercise is to:
Write a program that prints its input one word per line.
Does this mean so that when I prest ^Z(ctrl-z) to exit the program if I
were to do this:
Hey you
It...
|
by: Steven Woody |
last post by:
in C, is there any function can be used to decompose tokens from a string? if
not, can i find it in CPP? thanks!
--
steven woody (id: narke)
Celine: Well, who says relationships have to last...
|
by: vvk4 |
last post by:
I have an excel spreadsheet that I need to parse. I was thinking of saving
this as a CSV file. And then reading the file using C. The actual in EXCEL
looks like:
a,b a"b a","b a,",b
In CSV...
|
by: Dadio |
last post by:
Hi!
I have to take some strings from a file and put them in a record...
The various strings in the file are written on this way:
string1|string2|string3|string4|string5|
This is the program...
|
by: federico_bertola |
last post by:
I have this code:
int RESULT_OF_BLACKLIST = 0;
int BlackListMethod(
{
FILE *blacklist;
char String = "I.am.a.dotted.string";
char Word;
if ((blacklist = fopen("blacklist.dat", "r")) == NULL)...
|
by: Richard |
last post by:
Which way would you guys recommened to best parse a multiline file which contains
two fields seperated by a tab. In this case its the
linux/proc/filesystems file a sample of which I have included...
|
by: adolghiu |
last post by:
Hi,
I have a text file as input which has odd chars like: - // "
Bellow is a sample:
Jul 6 00:00:13 proxyhost httpd: 172.26.74.212 - "GET http://www.google.com/gwt/i?i=08...
|
by: arnuld |
last post by:
I searched the c.l.c archives provided by Google as Google Groups with
"word input" as the key words and did not come up with anything good.
C++ has std::string for taking a word as input from...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |