-4

I am really frusted with my program right now. I expect that the strcat function would simply concatenate the desired strings together but apparently the strcat function (when uncommented) doesn't get the concatenation correct, and somehow changes the value of my variables. As is, the correct result if printed, for example:

/usr/local/sbin
/usr/local/sbin
/usr/local/bin
/usr/local/bin
/usr/sbin
/usr/sbin
... (etc)

But when these two lines are uncommented

//strcat(file_loc, slash);
//strcat(file_loc, temp);

then here is the bizzare result I get:

/usr/local/sbin
/usr/local/sbin/ls
ls
ls/ls
/usr/sbin
/usr/sbin/ls
... (etc)

Here is the function that I am working on. Help would be very much appreciated.

void step_four (void) {
    int n=0;
    int has_slash=0;
    const char * slash="/";
    char * aa;
    char bb[64];
    int cc=0;
    int len;
    char * file_loc = malloc(100);
    char * temp= malloc(100);
    char * temp2 = malloc(100);
    //char [] f="FOO";


    printf("XXXXXXXXXXXXXXXXXXXXXXXX\n");
    printf("Made it to step 4\n");
    printf("First word of command is: %s\n", words[0]);

    aa = words[0];
    len = strlen(aa);

    while (cc < len) {
        bb[cc] = *(aa + cc);
        cc++;
    }

    while(n < len) {
        if (bb[n++] == '/') {
            //printf("HELLO !!\n");
            has_slash=1;
            break;
        }
    }


    //printf("has_slash=%d\n", has_slash);

    n=0;

    while (paths[n] != NULL) {
        //printf("%s\n", paths[n]);
        file_loc = paths[n];
        //file_loc[strlen(file_loc)]='\0';
        temp = words[0];
        if (has_slash) {
            //do stuff for slash

        }
        else {  
            printf("%s\n", file_loc);
            //strcat(temp2, "a");
            //strcat(file_loc, slash);
            //strcat(file_loc, temp);
            //file_loc[strlen(file_loc)]='/';
            //file_loc[strlen(file_loc) + 1]='/0';
            printf("%s\n", file_loc);
        //  f = file_loc;

        }
        n++;
    }

}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111

1 Answers1

0

http://www.cplusplus.com/reference/cstring/strcat/

char * strcat ( char * destination, const char * source );

"Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination."

The above quote is from cplusplus.

swtdrgn
  • 1,154
  • 4
  • 17
  • 49