Character arrays, and pointers how to use strtok and strcmp - c

I'm writing a linux shell for my operating systems class. I've knocked out the majority of it but I'm stuck on simple string comparisons. I've everything I can think of. strcmp should take in \0 terminated strings and return 0 for equal but that doesn't seem to be working and even stepping through the array and checking each char isn't working either. I currently have cmd[0] in the strcmp I know thats not right it needs to be null terminated but I've tried using strcpy and strcat \0 to another string. If someone could point out my mistake it would be much appreciated.
//Matthew Spiers
//CSC306
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
void ckCmd(char dir[]);
int main(){
pid_t pid;
char cdstr[4] = "cd";
char str[50];
char *cmd[3];
char *pstr;
char temp[50];
char dir[50] = "/bin/";
while(1){
pid = fork();
if(pid < 0){
fprintf(stdout, "Fork Failed");
}
else if(pid == 0){
fprintf(stdout, "\e[36m306.SH>\e[0m");
fgets(str, 50, stdin);
for(int i =0; i<50; i++){
if(str[i] == '\n'){
str[i] = '\0';
}
}
strcpy(temp, str); // Make a copy of original string
cmd[0] = strtok(str, " ");
for(int i =1; i<3; i++){
cmd[i] = strtok(NULL, " ");
}
strcat(dir, cmd[0]);
cout << cmd[0];
pstr = strtok(temp, " "); //pull out only first token
//Change Directory
if(!strcmp(pstr, "cd")){ //if first token is cd
//either branch to a routine just change directory
//ie branch and change directory
}
//ckCmd(temp);
execlp(dir, cmd[0], cmd[1], cmd[2], NULL);
_exit(0);
}
else{
wait(NULL);
}
}
}
void ckCmd(char str[]){
char *p;
p = strtok(str, " ");
if(p[0] == 'c'){
chdir("new");
}
}
enter code here

strtok is not reentrant/thread-safe!
You should use the RETURN-value from strtok:
p = strtok(str, " ");
if(p[0] == 'c'){
cmd[0] = strtok(str, " ");
...
if(!strcmp(cmd[0], "cd")){
If p/cmd[0] is NULL, it will crash.

What exactly isn't working? Can you show a smaller code sample?
The line:
strcat(dir, cmd[0]);
Are you aware that dir is the target here and not cmd[0]?
The line: !strcmp(cmd[0], "cd") by itself is correct, but it's unclear what exactly you're trying to do. Could you comment each group of line with your intentions?
Update: I suggest that you're trying too many things at once. To home in on your problem I recommend the following steps:
Understand how strtok works. It isn't very hard - read its manual and then try it in a separate file with some strings. This should give you a good idea.
Break parts of your code out and provide them with pre-set input (not from the user and without the forking). See where the behavior is as expected and where it drifts off.

Related

C strtok() and strcmp() issues

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int args, char* argv[])
{
const int CBUFF = 1024;
char input[CBUFF];
char wkdir[CBUFF];
char* command;
printf("Welcome to MyShell...\n");
while(1)
{
getcwd(wkdir, CBUFF);
printf("%s ? ", wkdir);
fgets(input, CBUFF, stdin);
command = strtok(input, " ");
if(strcmp(command, "cd") == 0)
{
char* path;
path = strtok(NULL, " ");
if(chdir(path) != 0)
{
printf("ERROR: COULD NOT CHANGE DIRECTORY TO SPECIFIED PATH");
}
}
if(strcmp(command, "exit") == 0) break;
}
return 0;
}
I am running into an issue creating a very simple command shell in C. The input is only being read the way I want it too when I add a space after my directive. I know that it has something to do with my improper use of the strtok() function but am not able to figure out what I am doing wrong. I have read the documentation of <string.h> and am turning up blank.
Behavior I want:
Directive "exit" to exit from program.
Current behavior:
Must add space after directive to get it to parse correctly ie. "exit " or "cd " is entered.
You left the trailing newline in the buffer. Get rid of it.
char *got = fgets(input, CBUFF, stdin);
if (!got) return ; /* EOF -- treat like exit */
size_t gotlen = strlen(got);
if (got[gotlen] == '\n') got[gotlen] = 0;

Config file parsing string matching error in c

While I am aware there are libraries for config file parsing I have tried to write my own implementation. The problem is that I can find the config option but comparing the string before the delimeter failes when I try to compare it with the thing I am searching for. I need to comare it with the thing I am searching for becasue my program allows things like Test2 and Test3 because it cannot check if there are characters before or after the word Test. The compare allways failes and I cannot figure out why.
Here is my code:
Main.c
#include <stdio.h>
#include <stdlib.h>
void Parser(char *CONFIG_FILE, int *P_VALUE, char *STRING_TO_LOOK_FOR);
int main(){
int VALUE;
Parser("config.txt", &VALUE, "Test");
printf("%d \n", VALUE);
}
Parser.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Parser(char *CONFIG_FILE, int *P_VALUE, char *STRING_TO_LOOK_FOR){
FILE *FP=fopen(CONFIG_FILE,"a+");
char TMP[256]={0x0};
int i = 1;
while(FP!=NULL && fgets(TMP, sizeof(TMP), FP)!=NULL){ //Loop through every line
i=i+1; //Increment the line number
if (strstr(TMP, STRING_TO_LOOK_FOR)){ //Is the term im looking for in this line
char *NAME_OF_CONFIG_STR = strtok(TMP, "= "); //look for delimiter
char *STRVALUE = strtok(NULL, "= "); //Get everything past the delimiter
char *P_PTR;
char *pos;
if ((pos=strchr(NAME_OF_CONFIG_STR, '\n')) != NULL){ //attempt remove \n doesn't work
*pos = '\0';
}
if(strcmp(STRING_TO_LOOK_FOR, NAME_OF_CONFIG_STR) == 0){ //try to check the two are the same
*P_VALUE = strtol(STRVALUE, &P_PTR, 10); //Returns an integer to main of the value
}
}
}
if(FP != NULL){
fclose(FP);
}
}
config.txt:
Test= 1234
Test2= 5678
Test3= 9012
Thanks to BLUEPIXY and the demo they created this problem has been solved. The issue was in the gcc comiler options I had forgotten -std=99 somehow this caused the program to behave correctly.

C - A simple shell on linux - Some trouble with commands

I wrote this simple shell so far. But I got some trouble with my shell.
For example, when I try to open a pdf file via the command "evince pdffile.pdf", the actual pdfile does not get opened. The pdf viewer runs but the actual file with the whole content never appears.
Or another example is the command "ls -l". I don't get the files and folders listed as it should be, but "ls" is working.
Another example is gedit, and so on.
Also, I should mention. I am not using "system()", because system() would do everything and I would not have something to do. Instead, I am using "execvp()".
Here is the code. I hope, you may find the problem, because I have no clue what the problem is causing.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#define MAX_LENGTH 1024
#define DELIMS " \t\r\n"
void exec_cmd (char *buf);
int main() {
char line[MAX_LENGTH];
char * cmd;
char curDir[100];
while (1) {
getcwd(curDir, 100);
printf("%s#%s$ ", getlogin(), curDir);
if (!fgets(line, MAX_LENGTH, stdin))
break;
if ((cmd = strtok(line, DELIMS))) {
errno = 0;
if (strcmp(cmd, "cd") == 0) {
char *arg = strtok(0, DELIMS);
if (!arg)
fprintf(stderr, "cd: argument is missing.\n");
else chdir(arg);
} else if (strcmp(cmd, "exit") == 0) {
exit(0);
} else exec_cmd(line);
if (errno) perror("Error. Command failure");
}
}
return 0;
}
void exec_cmd (char *buf) {
int status = 0;
char *argv[MAX_LENGTH];
int j=0;
pid_t pid;
argv[j++] = strtok (buf, DELIMS);
while (j<MAX_LENGTH && (argv[j++]=strtok(NULL,DELIMS))!=NULL); // EDIT: " " replaced by DELIMS
pid = fork();
if(pid < 0) {
printf("Error occured");
exit(-1);
} else if(pid == 0) {
execvp(argv[0],argv);
} else if( pid > 0) {
wait(&status);
}
}
The bug is in main().
You are using strtok to find the first word of the line. But strtok modifies the line, making it actually contain just that word (a NUL terminator is written to the string right after it).
You need to make a copy of the line, or use strtok_s, or do something else to avoid modifying the line.
Check the arguments (eg, print them out, each enclosed in []) before you call fork/exec, there's a good chance they're not what you think.
While your first call to strtok uses your full delimiter set, subsequent calls do not. They instead just use a space. That means that the final argument will probably have the newline left on the string by fgets. I'd be using the same delimiter set in the subsequent calls. In other words:
while (j<MAX_LENGTH && (argv[j++]=strtok(NULL,DELIMS))!=NULL);
Having entered your code and done that debugging, I find that the string passed to the function only ever has one word in it. It turns out that's because of the strtok that happened in main to check for cd/exit. That left the nul character at the end of the first word, an effect inherent in the way strtok works.
Probably the quickest fix is to make a copy of the string before the initial strtok in main, then pass that to the function. In other words, use strdup (and, later, free). Now glibc has a strdup but, if you're in an environment that doesn't (it's POSIX rather than ISO), see here.

C program to find individual words in a string using strtok

I am writing a program where I use strtok in order to find each word in a string that I type into the command line, in my example, my code is called command.c so when I type:
./command.out "Hi, there"
I should get as my result:
Arg = "Hi, there"
Next word "Hi,"
Next word "there"
so far my code will complete the arg part of the print statement, but will not use execute the latter part in order to separate the string in question, my code currently is as follows:
#include <stdio.h>
#include <string.h>
void main (int argc, char *argv[]) {
int i;
for(i =1;i< argc; i++)
printf("Arg = %s\n", argv[i]);
char delims[] = " ";
char *word = NULL;
word = strtok(argv[i], delims);
while(word != NULL) {
printf("Next word \"%s\"\n", word);
word = strtok(NULL, delims);
}
}
Where am I going wrong and how can I fix this code? Thanks for all the help
You are missing the curly braces around the for block:
for(i =1;i< argc; i++)
{
printf /* ... and so forth */
}
Your code indentation is wrong, this may cause your problem.
The 'for' statement affects only the next line, the printf one, so variable 'i' increases later to value '2', then when you ask for argv[i], you're asking for argv[2], you should call argv[1].

fgets loops many times before exiting for EOF

I am making a simple shell. It also needs to be able to read text files by lines. This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
// Exit when called, with messages
void my_exit() {
printf("Bye!\n");
exit(0);
}
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
// Char array to store the input
char buff[1024];
// For the fork
int fid;
// Get all the environment variables
char dir[50];
getcwd(dir,50);
char *user = getenv("USER");
char *host = getenv("HOST");
// Issue the prompt here.
printf("%s#%s:%s> ", user, host, dir);
// If not EOF, then do stuff!
while (fgets(buff, 1024, stdin) != NULL) {
// Get rid of the new line character at the end
// We will need more of these for special slash cases
int i = strlen(buff) - 1;
if (buff[i] == '\n') {
buff[i] = 0;
}
// If the text says 'exit', then exit
if (!strcmp(buff,"exit")) {
my_exit();
}
// Start forking!
fid = fork();
// If fid == 0, then we have the child!
if (fid == 0) {
// To keep track of the number of arguments in the buff
int nargs = 0;
// This is a messy function we'll have to change. For now,
// it just counts the number of spaces in the buff and adds
// one. So (ls -a -l) = 3. AKA 2 spaces + 1. Really in the
// end, we should be counting the number of chunks in between
// the spaces.
for (int i = 0; buff[i] != '\0'; i++) {
if (buff[i] == ' ') nargs ++;
}
// Allocate the space for an array of pointers to args the
// size of the number of args, plus one for the NULL pointer.
char **args = malloc((sizeof(char*)*(nargs + 2)));
// Set the last element to NULL
args[nargs+1] = NULL;
// Split string into tokens by space
char *temp = strtok (buff," ");
// Copy each token into the array of args
for (int i = 0; temp != NULL; i++) {
args[i] = malloc (strlen(temp) + 1);
strcpy(args[i], temp);
temp = strtok (NULL, " ");
}
// Run the arguments with execvp
if (execvp(args[0], args)) {
my_exit();
}
}
// If fid !=0 then we still have the parent... Need to
// add specific errors.
else {
wait(NULL);
}
// Issue the prompt again.
printf("%s#%s:%s> ", user, host, dir);
}
// If fgets == NULL, then exit!
my_exit();
return 0;
}
When I run it alone as a shell, it works great. When I run ./myshell < commands.txt, it does not work.
commands.txt is:
ls -l -a
pwd
ls
But the output is:
>Bye!
>Bye!
>Bye!
>Bye!
>Bye!
>Bye!>Bye!
>Bye!
>Bye!
>Bye!
Doesn't even run my commands. Any ideas? I thought my while loop was pretty simple.
I don't know if this is the problem, but you (correctly) mention in a comment that you have to allocate "plus one for the NULL pointer" in the *args array.
However, you don't actually set the last pointer in *args to NULL.
execvp() won't like that.
That doesn't explain why there might be a difference between redirected vs. non-redirected input, other than undefined behavior is a bastard.
Sorry everyone - turns out my text file was in some sort of demented format from Mac's TextEdit GUI. Everything is working great.
I really appreciate all of the helpful responses

Resources