Click here to Skip to main content
15,896,557 members

strcmp is not working the way it should

Revision 3
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 by mimi from Unknown.
Tags: