Click here to Skip to main content

mimi from Unknown asked:

Open original thread
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???
Tags: C

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid