Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the return value of strcmp in c.I wrote the following code in c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include<stdarg.h>
#define MAX 128
char *path = "/bin/";
char *argument[MAX];
char buffer[MAX];
int numberOfArguments;
pid_t pid;
  int rv ;    
char prog[128];

char parse(char buffer[],int i)
{

             buffer[i] = '\0';
             argument[0] = strtok(buffer, " ");
             numberOfArguments = 1;
             argument[numberOfArguments] = strtok(NULL, " ");
             while (argument[numberOfArguments] != NULL) {
             numberOfArguments++;
             argument[numberOfArguments] = strtok(NULL, " ");
                           }  
                                
  return  numberOfArguments;                               
}
int changeDirectory( ){
   int result = 0;  
    if (numberOfArguments > 2) {
              printf("%s: Too many operands \nUsage: %s <pathname>\n", (char *) argument[0], (char *) argument[0]);
                 }
   if(numberOfArguments == 1) {
               
                const char* home = getenv("HOME");
                int i = chdir(home);
                if(i < 0)
                       printf("directory couldn't be changed\n");
                else{
                        printf("directory changed\n");
                        printf("home = %s\n", home);
                    }
         }
   else {
             result = chdir(argument[1]);
              if(result == 0){
                      printf("directory changed\n");
                      exit(0);
                     }
              else{
                       printf("Couldn't change directory to %4s", (char *)argument[1] ); 
          }
       }
return 0;
}
void forground(char buffer[], int i){ 
                         parse(buffer,i);         
                                 char prog[128];
                                  strcpy(prog, path);
                                  strcat(prog,argument[0]);
                                  printf("%s\n",prog);  
                                 rv = execv(prog, argument);
                                /* int l=0;
                                   while(l<numberOfArguments+1){
                                   printf("argument [%d] is %s\n",l,argument[l]);
                                     l++;
                                        } */
}
int main(){
char *s;
while(1)
       {
            printf("myshell>> ");  
            int i = 0; 
         
             while ((i < MAX) && ((buffer[i] = getchar()) != '\n') && buffer[i] != EOF)
                i++;
               if (i == MAX) {
                          buffer[MAX-1] = '\0';
                          while (getchar() != '\n');
                          printf("argument too long\n");
                       }
              else {
                        pid = fork();
                        if (pid != 0){
                                  wait(NULL);
                               }
                        if (pid != 0) {            
                                
                             forground(buffer,i);
                           }
             
          parse(buffer,i); 
     
          if (!strcmp(argument[0], "cd"))
          changeDirectory();
               else  if (!strcmp(argument[0], "exit")) {
      	                int ret;         
                        ret = execl ("/bin/bash", "bash",(char *)0);
                          }  
          
process();    
            }
 }
return 0;  
}

the out put is always Null after argv[0].That means some how it is loosing the input after "cd".Why is that does strcmp turn the other valus after cd to null???
Posted
Updated 11-Jan-13 20:46pm
v3
Comments
Jochen Arndt 11-Jan-13 7:23am    
strcmp() will probably work as it should. If the result is not the expected one, there must be some error in your code. The posted code snippet is incomplete. So we can't really help. You are accessing argument[0] but did not show us how that variable is set.

Working just from your code fragment, it is difficult to work out what your problem is: the only reference to strcmp you have does not use argv at all - it uses an array of strings you create and fill.

But are you sure you are using it right?
C++
strcmp(argument[0], "cd")

will return 0 if the strings are the same, so
C++
if (!strcmp(argument[0], "cd"))
  changeDirectory(buffer,i);
will change the directory if the arguement is not "cd"

I don't think that is quite what you intended...

Use the debugger - step through you code and see what is going on. It's a lot easier and quicker that asking here - particularly if you don't give us the right info! :laugh:

[edit]C true is non-zero, dunno why I got that wrong...- OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Jochen Arndt 11-Jan-13 7:46am    
?if (!strcmp(arg, "cd")) will do it when string is identical to "cd"
OriginalGriff 11-Jan-13 8:00am    
I blame a lack of caffeine...either that or I'm having a "senior moment". :doh:
mimi from Unknown 12-Jan-13 2:58am    
there i have updated the code.this is exactly what i wrote.i was expecting when i do
cd /Desktop
sld change the directory to Desktop and
cd
should chang the directory to Home
But the problem is that it tells me that it changed the directory to home.i have tried to display the argument array elements in the changeDirectory function
like
<pre>
int changeDirectory( ){
int l=0;
while(l<numberofarguments+1){
printf("argument="" [%d]="" is="" %s\n",l,argument[l]);
="" l++;
="" }=""
}
&lt;="" pre="">

it just makes them NUll after cd for example
if <pre>#cd /tmp</pre>
the out put is
<pre>argument [0] is cd
argument [1] is NULL
</pre> and if i type
<pre>#cd </pre>
the out put is
<pre>argument [0] is cd
argument [1] is NULL
</pre> So why is it all the same why doesnt it print the argument after cd???hope this is discriptive enough!!

  1. You should always check the documentation[^] first. As you can see, the strcmp() function returns an integer value not a boolean as your code assumes.
  2. There is no definition of the variables (buffer, argument) in your code; where are they defined?
  3. You are calling the strcmp() function before you call your parse() function.
 
Share this answer
 
Comments
mimi from Unknown 12-Jan-13 3:03am    
there i have updated the code.this is exactly what i wrote.i was expecting when i do cd /Desktop sld change the directory to Desktop and cd should chang the directory to Home But the problem is that it tells me that it changed the directory to home.i have tried to display the argument array elements in the changeDirectory function like

int changeDirectory( ){
int l=0;
while(l
Richard MacCutchan 12-Jan-13 5:34am    
Unfortunately I do not have access to a UNIX system to check the code for you. I can only suggest that you use your debugger to step through it and see the exact values of all your variables at every stage.
Richard MacCutchan 12-Jan-13 7:01am    
I have just run your code (with all the Linux and forked parts removed) and it seems to work. If I type "cd /Directory", then it follows the code path to change to that directory. If I type "cd" then it follows the code path to change to the HOME directory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900