Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Basic Shell change Directory
New Posts  All Forums:Forum Nav:

Basic Shell change Directory

post #1 of 3
Thread Starter 
I am coding a basic shell and my first requirement is to test for cd, I have all my conditions for the possible cd commands, after which, I will handing commands like ls. As for now I am really confused about one block of code.
Code:
if(strcmp(tokens[0], cd) == 1){
        printf("Changing to the Home Directory");
        success = chdir(home);
        printf("%d\n",success);
    }

    if(strcmp(tokens[0], cd) == 0){
        if(NUM_TOKENS > 2){
            printf("error: Too many arguments\n");
        }
        else{
            printf("Changing to new directory %s ",tokens[1]);
            char *string = tokens[0];
            printf("%s\n", tokens[1]);
            success = chdir(string);
            printf("%d\n",success);
        }
    }

When hardcoding chdir("test") the code runs fine, if a user on the command line enters "cd test" tokens[0] is cd and tokens[1] is the string "test" but chdir(tokens[1]) fails and I don't understand why.

I was hoping someone could give me a heads up as to why my strings are wrong, or not being read.
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #2 of 3
Are you absolutely sure that you're splitting the string correctly? If hardcoding chdir("test") works but it fails when you pass the parameters from your shells user input, then the most obvious reason would be that tokens[1] doesn't contain the expected string.

Have you tried running a debugger against it or strace?

Also, is this intended to be a working replacement for your *nix shell or just a pet project? I ask because ls is actually an ELF binary so you wouldn't need to hardcode your own version:
Code:
$ file `which ls`
/usr/bin/ls: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=e4f0290ed393213e25e62f4c9be1ac6c628545bb, stripped
post #3 of 3
You are doing chdir("cd"):

char *string = tokens[0]; // <-- cd
success = chdir(string);

But you should just use a debugger next time when you're stuck. Sometimes you mentally skip the problem since you wrote the code, and the debugger will just show you what's wrong. In this case you could've looked at the value of string.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Basic Shell change Directory