Disappearing Strings in C
I'm working on a simple shell program as part of a class project and I've hit a dead end. I'm new to C programming, so I could be missing something pretty obvious here, but I have pinpointed it to a certain chunk of my code. In the following snippet, the first for loop prints off all of the paths in the user's ENV path variable. In the second loop, it does the same only this time it tacks on a "/" character.
---snippet--- char *lookupPath(char **argv, char **dir) { char *temp; /* Not sure if the following line is necessary... */ temp = (char *) malloc(MAX_PATH_LEN * 2); /* Print user's path variables */ for (i = 0; i < 8; i++) printf("%d= %s\n", i, dir[i]); /* Look in PATH directories */ /* Use access() to see if the file is in a dir */ for (i = 1; i < MAX_PATHS; i++) { temp = NULL; if (dir[i] == NULL) continue; temp = dir[i]; strncat(dir[i], "/", MAX_PATH_LEN); printf("Looking %d in %s\n", i, temp); . . . } ---end snippet--- This is what prints: ---script--- $ ./shell # ls 0= (null) 1= /usr/kerberos/bin 2= /usr/lib/ccache 3= /usr/local/bin 4= /usr/bin 5= /bin 6= /usr/X11R6/bin 7= /home/adam/bin Looking 1 in /usr/kerberos/bin/ Looking 2 in / Looking 3 in /usr/local/bin/ Looking 4 in / Looking 5 in /bin/ Looking 6 in / Looking 7 in /home/adam/bin/ ---end script--- For reasons unbeknowest to me, only the odd numbered paths are printing. One second dir[i] contains a path, the next moment it's null. Where are my paths going?? Thanks in advance! |
Hi,
#1, Your first loop and second loop, initial value and loop condition are not same. #2. It seems that you want to add "/" at the end of dir[i], (issue hiding here, potentially HUGE.) and screwing up succeeding member. Your penguin will do what you ask... Adding "/" to dir[n] is DANGEROUS, unless you know you have allocated free space after dir[n], which Code:
char *lookupPath(char **argv, char **dir) Happy Penguins! |
Quote:
Code:
temp = (char *) malloc(MAX_PATH_LEN * 2); Thanks again |
I think that your problem lies with the adding the / to dir rather than another variable you so carefully set up and forgot to use.
dir is an array of strings and the memory will look as follows: Code:
0/usr/kerberos/bin0/usr/lib/ccache0/usr/local/bin0/usr/bin0/bin0/usr/X11R6/bin0/home/adam/bin Code:
0/usr/kerberos/bin/0usr/lib/ccache0/usr/local/bin0/usr/bin0/bin0/usr/X11R6/bin0/home/adam/bin |
I think dir is an array of pointers to strings. We don't see all the code in the second loop - are you incrementing i within the loop as well as in the for line?
|
dir is essentially argc
|
The entire function is as follows:
Code:
char *lookupPath(char **argv, char **dir) { |
Okay, that is because you do temp = dir[i], so temp points to where dir[i] points to, you need to copy dir[i] into temp then you should be alright.
|
Quote:
Code:
for (i = 0; i < MAX_PATHS; i++) { Thank you all for your help! |
Great stuff :)
|
<curmudgeon>
It's problems like this -- memory allocation and pointers -- that made me decide, many years ago when I was a programming group manager, that the first programming course any programmer should list in their resume had to be an assembly language course. Anyone without that qualification had to work really hard to convince me to hire them. I sometimes, but rarely, made exceptions for programmers with bcpl experience for non-trivial applications. </curmudgeon> |
It seems that adymroxx learned step ZERO in C programming. Now, adymroxx cannot use "I am new in C" excuse any longer. ;)
|
Quote:
|
All times are GMT -5. The time now is 08:47 PM. |