Help - I don't know what I am doing with pointers

Hi,

I am having trouble with pointers.

I have a custom shell program for class I am making. I am having a problem with what I thought would be the easiest part. I have a variable to hold the path and I need to add and remove from it. The addPath function I have it working fine. For some reason I can't get the removeFromPath function working correctly. All's it is add and removing from a char array. Please let me know what you think I am doing wrong.



This is my output. As you can see the removeFromPath function is working to a point, but as you can see if I keep using path - <path> I am getting this as the output: PATH:/usr/sbin:/usr/local/bin:/usr/X11/bin:usr/X11/bin

I am assuming it has something to do with the loop and the dirPath size...but I am not sure what is wrong.


(custom shell)> path
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
(custom shell)> path - /bin
PATH:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
(custom shell)> path
/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
(custom shell)> path - /usr/bin
PATH:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
(custom shell)> path
/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
(custom shell)> path - /sbin
PATH:/usr/sbin:/usr/local/bin:/usr/X11/bin:usr/X11/bin     
(custom shell)>



------

the path is defined as

strcpy(dirPath,"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void removeFromPath()
{
        char field [32];
        char *stringPtr;
        char *pathPtr;
        char firstpass = 'y';
        int n;

        strcpy(pathPtr,dirPath);
        stringPtr=pathPtr;

        char *tempBuf=(char *)malloc((strlen(dirPath)+1));
        strcpy(dirPath,tempBuf);

        while ( sscanf(stringPtr, "%31[^:]%n", field, &n) == 1 )
        {
                //need to build new path variable removing the path specified
                if(strcmp((const char *)field,(const char *)cmds[2])==0)
                {
                        //do nothing, to drop off addition
                }
                else
                {
                        if ( firstpass == 'y')
                        {
                                strcat(tempBuf,field);
                                firstpass = 'n';
                        }
                        else
                        {
                                strcat(tempBuf,":");
                                strcat(tempBuf,field);
                        }
                }
                stringPtr +=n;
                ++stringPtr;
        }//end while()

        //clear variables
        strcpy(cmds[0],"");
        strcpy(cmds[1],"");
        strcpy(cmds[2],"");
        strcpy(dirPath,tempBuf);
        tempBuf="";
        field[0]scanf('\0';
        //firstpass='';
        printf("PATH:%s\n",dirPath);
}//end removeFromPath() 


If you need more information let me know.

Thanks.
You can't strcpy into pathPtr until you give it some memory to which it should point.

How have you defined dirPath? How large is it? It looks to me like you're going to quickly overrun dirPath with your strcat() calls.
Topic archived. No new replies allowed.