2

Hopefully this is easy to do. I am writing a program in C that needs to run on Windows and Linux. On Linux it works great, but on Windows (through Cygwin) it screws up because the environment variables have backslashes rather than slashes in the path. Is there a way to replace these? I tried the following but it didn't change anything, probably because it thinks the backslash is escaping the next char which obviously isn't the case. Here's what I tried:

char* fixPath(char *env)
{
    char *val[100];
    strcpy(val, getenv(env));
    int index = 0;
    while(val[index])
    {     
         if(val[index] == '\\')
            val[index] = '/';
         else
            index++;
    }
    printf("\n***%s",val);
    return val;
};

int main(int argc, char *argv[])
{
    char *test1;
    test1 = fixPath("SERVER1");
    printf("\n*****%s",test1);
...
}

What is the right way to do this?

Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86
jensengar
  • 6,117
  • 17
  • 58
  • 90
  • 1
    Backslashes are escape characters when they appear in string literals in the source code. Backslashes in strings in memory do not act as escape characters, and replacing them with another character probably won't solve your problem. – Mark Byers Jun 25 '12 at 18:22
  • Maybe try a Perl regular expression? perl -pi -e 's/\//\\/g' This will directly edit the file so you may want to test it out first. – squiguy Jun 25 '12 at 18:23
  • I would try to use the given path, but if i try and execute the command (ant -f PATH) then returns "ant -f C:servernewfolderjboss". there are no slashes forward or backward in it. – jensengar Jun 25 '12 at 19:25

5 Answers5

4

Major problems:

(1)

char *val[100];

should be:

char val[100];

(2)

You can not return a local variable (val) as a function result, as it will be out of scope.

Minor problem:

The logic is wrong here:

while(val[index])
{     
     if(val[index] == '\\')
        val[index] = '/';
     else
        index++;
}

it should be:

while(val[index])
{     
     if(val[index] == '\\')
        val[index] = '/';
     index++;
}

although this is not fatal.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    The old logic should work though? It's just that it goes another time through the loop every time it needs to replace a character. – Laserallan Jun 25 '12 at 18:24
2

Your memory management is flawed. First, you declare a char *val[100] instead of char val[100]. Then, you try to return that very local address by reference, which is undefined behavior. You'd better pass the buffer as an argument to the function, and make it an array of chars. Not an array of pointers.

Oh, and you're casting a string literal ("SERVER1") into a char*, which is also bad. Make it a const char*.

void fixPath(const char* env, char* fixed)
{
//...
}

int main(int argc, char *argv[])
{
    char test1[100];
    fixPath("SERVER1", test1);
    printf("\n*****%s",test1);
...
}

Now, using a fixed buffer size of 100 is dangerous, of course, but there's only so many issues one can refer to in an answer...

Eran
  • 21,632
  • 6
  • 56
  • 89
0
char *val[100];
strcpy(val, getenv(env));
int index = 0;
while(val[index])
{     
     if(val[index] == '\\')
        val[index] = '/';
     else
        index++;
}

You have declared val as an array of pointers here. It should be an array of char.

So, you loop through your array of pointers while val[index] is non-null. That is a problem. You are not looping through a string and comparing characters, you are comparing pointers to \\.

You should also increment index each iteration, not only when you don't find a match, though this is not the source of your problem..

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

There's a problem with the memory management on your function. You are returning a pointer to locally allocated memory. Try with this generic string replacing function: What is the function to replace string in C?

Community
  • 1
  • 1
alf
  • 18,372
  • 10
  • 61
  • 92
0

I know this doesn't help your program but for functionality's sake you could try this Perl one-liner.

perl -pi -e 's/\//\\/g' filename(s) 

This can work on multiple files and will replace all forward slashes with backslashes.

squiguy
  • 32,370
  • 6
  • 56
  • 63