ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
/* 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??
#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)
implies "NO, you do not". You may be overwriting first byte of next member or something else. (it depends on complier implementation.) C compiler usually does not detect it, as it trusts you. This may end up with run time error "Segmentation Fault"
#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], (another issue hiding here, potentially HUGE), and printing out temp.
These two issues are a kind of forgettable, tiny and benign. 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)
implies "NO, you do not". You may be overwriting first byte of next member or something else. (it depends on complier implementation.) C compiler usually does not detect it, as it trusts you. This may end up with run time error "Segmentation Fault"
Happy Penguins!
Shouldn't this line
Code:
temp = (char *) malloc(MAX_PATH_LEN * 2);
give enough room to add "/" to the previous string? Keep in mind that I'm pretty new to C... should I call malloc once in each loop?
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:
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?
char *lookupPath(char **argv, char **dir) {
/* This function searched the directories identified by the dir
* argument to see if argv[0] (the file name) appears there.
* Allocate a new string, place the full path name in it, then
* return a string. */
char *result;
char *temp;
int i;
/* Check to seee if file name is already an absolute path */
if (*argv[0] == '/') {
result = argv[0];
return result;
}
temp = (char *) malloc(MAX_PATH_LEN * 2);
for (i = 0; i < MAX_PATHS; 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 = 0; i < MAX_PATHS; i++) {
temp = dir[i];
if (dir[i] == NULL) continue;
temp = dir[i];
strncat(temp, "/", MAX_PATH_LEN);
//strncat(temp, argv[0], MAX_PATH_LEN);
printf("Looking %d in %s\n", i, temp2);
//if (access(dir[i], X_OK) == 0) {
// result = temp;
// return result;
//}
}
/* File name not found in any path variable */
fprintf(stderr, "%s: command not found\n", argv[0]);
return NULL;
}
I've changed the strncat to add to the temp variable rather than dir[i] and the problem persists.
<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>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.