C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 6th, 2013, 11:36 AM
tinkydwd123 tinkydwd123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 tinkydwd123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 46 sec
Reputation Power: 0
What's wrong with this loop?

I'm writing a Unix shell, and I spent a ton of time on this function but I cannot get it to work.

Here's where it's messing up:

Code:
  // If no slash                                                           
  if(strchr(token, '/') == NULL)
    {
      printf("if no slash token %s\n", token);
            // Get the path                                                
          path = getenv("PATH");
          currentToken = strtok(path, colon);

      // For every possible path                                           
      while(currentToken != NULL)
        {
          printf("path is %s\n", path);
          printf("current token is %s\n", currentToken);
          // Add a / and null to the end of it                             
          stringSize = strlen(currentToken);
          char buffer[stringSize+2+strlen(token)];
          strcpy(buffer, currentToken);
          strcat(buffer, "/");
          strcat(buffer, token);
           strcat(buffer, "\0");
           printf("current token before if %s\n", currentToken);
           printf("buffer is %s\n", buffer);

          // See if this path exists                                       
          if(stat(buffer, &pathExists) == 0)
            {
              printf("if statement");
              return runCommand(buffer, argList);
            }
          currentToken = strtok(NULL, colon);
          printf("current token at end of while: %s\n", currentToken);
        }
    }


I'm trying to get the external commands working, but it only works the first time around, then thinks the currentToken is null after the currentToken = strtok(NULL, colon) statement. Here is sample output:

user@myshell:/home/class/user/course/proj1> ls
token_ptr is ls
if no slash token ls
path is /home/class/user/.bin
current token is /home/class/user/.bin
current token before if /home/class/user/.bin
buffer is /home/class/user/.bin/ls
current token at end of while: /home/class/user/.scripts
path is /home/class/user/.bin
current token is /home/class/user/.scripts
current token before if /home/class/user/.scripts
buffer is /home/class/user/.scripts/ls
current token at end of while: /usr/bin
path is /home/class/user/.bin
current token is /usr/bin
current token before if /usr/bin
buffer is /usr/bin/ls
current token at end of while: /usr/local/java/bin
path is /home/class/user/.bin
current token is /usr/local/java/bin
current token before if /usr/local/java/bin
buffer is /usr/local/java/bin/ls
current token at end of while: /usr/lang
path is /home/class/user/.bin
current token is /usr/lang
current token before if /usr/lang
buffer is /usr/lang/ls
current token at end of while: /bin
path is /home/class/user/.bin
current token is /bin
current token before if /bin
buffer is /bin/ls
if statementtoken is /bin/ls
makefile myshell.c myshell.o proj1submit.sh
makefile~ myshell.c~ myshell.x test

user@myshell:/home/class/user/course/proj1> ls
token_ptr is ls
if no slash token ls
path is /home/class/user/.bin
current token is /home/class/user/.bin
current token before if /home/class/user/.bin
buffer is /home/class/user/.bin/ls
current token at end of while: (null)


What am i doing wrong? It works beautifully the first time!

Reply With Quote
  #2  
Old February 6th, 2013, 12:19 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
> path = getenv("PATH");
> currentToken = strtok(path, colon);
You're getting a pointer to someone else's memory, which you immediately trash with strtok() spraying \0 all through it at the token boundaries.

If you need to do this, make a copy of it first.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 6th, 2013, 01:00 PM
tinkydwd123 tinkydwd123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 tinkydwd123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 46 sec
Reputation Power: 0
Quote:
Originally Posted by salem
> path = getenv("PATH");
> currentToken = strtok(path, colon);
You're getting a pointer to someone else's memory, which you immediately trash with strtok() spraying \0 all through it at the token boundaries.

If you need to do this, make a copy of it first.



Bingo! Thank you!

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > What's wrong with this loop?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap