Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to replace this line...
strcpy(deletefile_name, string);
with this one ...
strlcpy(deletefile_name,string,strlen(deletefile_name);
Basically, what I am doing is replacing the calls to strcpy with strlcpy (more secure). I have thousands of lines of code and going through them line by line is getting too tedious.
Would someone tell me how I could do this line replacement using Sed, Awk, or Vi? I do not understand regular expressions enough to figure it out.
Any help is greatly appreciated. Thanks in advance!
Sam
(ignore below)
a
ab
abc
abcd
abcde

sed 's/strcpy(deletefile_name, string);/strlcpy(deletefile_name,string,strlen(deletefile_name);/g' inputfile
Jean-Pierre.

Thanks for the quick response Jean-Pierre...
Could you tell me what the syntax would be if the deletefile_name and filename strings were variable? Trying to substitute based on any given instance of strcpy over to strlcpy syntax...
strcpy(variable1,variable2);
to
strlcpy(variable1,variable2,strlen(variable1);Thank you.
Sam

This is what I'm tying to do, based on your previous instructions...
sed 's/strcpy($destination,$source);/strlcpy($destination,$source,strlen($destination);/g' test1.dat
I'm stuck on how I make it process every instance of strcpy. The variables are above... I think I need a regular expression, just not sure. Any assistance is greatly appreciated.
Sam

Try this :
sed 's/strcpy(\([^,]\+\),\([^)]\+\));/strlcpy(\1,\2,strlen(\1));/g' input_file
Jean-Pierre.

Thanks again for your quick response...
That syntax seems to list out the file's content verbatim with no changes. Any ideas?Thanks.
Sam

Verify the command you typed.
The command works fine for me :/home/jp> sed 's/strcpy(\([^,]\+\),\([^)]\+\));/strlcpy(\1,\2,strlen(\1));/g'
strcpy(a,b); strcpy(xx,yy);
strlcpy(a,b,strlen(a)); strlcpy(xx,yy,strlen(xx))
^D
/home/hp>
Jean-Pierre.

you did not indicate the number of files you want to modify. Simplest way is
#define strcpy(deletefile_name, string) strlcpy(deletefile_name,string,strlen(deletefile_name)
once at the beginning of each file.Even manually, this should be fast. If there happens to be a common "include.h", almost instantaneous.

Jean-Pierre:
I'm not sure what I'm doing wrong, but it doesn't work out for me...
The data file ...
[comp%] cat test1.dat
data before 1245
strcpy(procfile_name,UPASS_CFG_ENV);
strcpy(deletefile_name, string);
data after 12345[comp%] sed 's/strcpy(\([^,]\+\),\([^)]\+\));/strlcpy(\1,\2,strlen(\1));/g' test1.dat
data before 12345
strcpy(procfile_name,UPASS_CFG_ENV);
strcpy(deletefile_name, string);
data after 12345Dlonra:
Thanks for your response. I want to modify one file at a time. In this case, I don't think that the #define approach will work, because the content of the function's parameters are unknown. I would have to manually go through the code, find them all and paste them at the top... redefining them.
It seems to me that I could save time, by modifying them while I am there. I was trying to avoid a manual process. This approach wouldn't be very "fast" in the 15k+ lines of code in each file. I know there is a way in sed or awk to do this in probably less than a line of code, with minimal effort. Still working on that.Thank you both for your suggestions.
#define strcpy(deletefile_name, string) strlcpy(deletefile_name,string,strlen(deletefile_name)
once at the beginning of each file.
Even manually, this should be fast. If there happens to be a common "include.h", almost instantaneous.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |