Code:
strncpy(new_filename, old_filename, argv[j]-ext+1);
What value does the expression "argv[j]-ext+1" have?
And to expand a bit on my warning earlier about strncpy(): From what I'm guessing you're trying to copy, from old_filename, everything up to the extension. Thus there will never be a null character in the source and so a string will never be created (we'll assume that your strrchr() call always returns a non-null pointer).
If this is indeed the case, then I'd highly recommend memcpy() instead of strncpy(). strncpy() really was designed to be use in a specific case in unix, where an array of char was usually null terminated (thus a string) except in the case where the length of the string (not counting the null character) was exactly the length of the array; then the entire array was filled and no null character stored.
Its name, unfortunately, tags it as a string handling function which it is only sporadically. By using memcpy() you're not tricking yourself into thinking you're dealing with a string. If you do decide to use strncpy(), there are a couple of things to keep in mind:
The third argument (the size argument) must, absolutely must be no larger than the size of the array you're passing in as the first argument. Otherwise you might overrun the array, which is no good business. Generally the third argument to strncpy() is used to give the size of the first array, much the way snprintf() is used (though snprintf() is smarter than strncpy()).
Once you've called strncpy(), and you want a string (which is almost always, in those rare cases that you should use strncpy()), then you must null-terminate it yourself. The general call to strncpy() should look something like:
Code:
strncpy(target, source, n);
target[n] = 0;
In this case, if target is 100 bytes, then n should be 99. By doing this, you're guaranteeing a string has been created.
Of course, the way you're using strncpy() is not the normal method (from what I can tell), but as I mentioned, a method for which memcpy() is probably better; you're essentially using strncpy() as a poor man's memcpy(). You are limiting the source length, not the target length (which you hope is long enough to hold what you're copying).
I'm not certain this whole rambling post is very useful to you, so the two things I urge you to keep in mind are: strncpy() is a function usually best left alone, but if you must use it, remember to null terminate your strings manually.