Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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 11-Jan-13 1:07am
Edited 11-Jan-13 20:46pm
v3
Comments
Jochen Arndt at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
strcmp(argument[0], "cd")
will return 0 if the strings are the same, so
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 | :laugh:
 
[edit]C true is non-zero, dunno why I got that wrong...- OriginalGriff[/edit]
  Permalink  
v2
Comments
Jochen Arndt at 11-Jan-13 7:46am
   
?
if (!strcmp(arg, "cd")) will do it when string is identical to "cd"
OriginalGriff at 11-Jan-13 8:00am
   
I blame a lack of caffeine...either that or I'm having a "senior moment". :doh:
mimi from Unknown at 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
 
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!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  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.
  Permalink  
Comments
mimi from Unknown at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 420
1 DamithSL 253
2 Sergey Alexandrovich Kryukov 210
3 Peter Leow 205
4 Maciej Los 195


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid