Click here to Skip to main content
15,897,167 members

strcmp is not working the way it should

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

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

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.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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