Perl script to search sprintf and replace with snprintf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to search sprintf and replace with snprintf
# 1  
Old 03-22-2009
Perl script to search sprintf and replace with snprintf

Dear all,

I am new to perl script and would need some help for my 1st script. I wrote a script to search sprintf(buf,"%s", sourcestring) and replace with snprintf(buf, sizeof(buf),"%s", sourcestring). As snprintf() requires an extra argument, so it is not a simple search-and-replace. I need to match sprintf(), grab the first argument and use it for the second argument. This script also need to check and ensure that the destination string, the first argument to sprintf(), is a characeter array and not a pointer before doing the replacement.

My code is as follows:
#!/usr/bin/perl
# Search and Replace
if ($ARGV[0] eq "")
{
print "usage: file1 file2 etc. or wildcards (replaces originals in place\n)";
}
else
{
$totrep = 0;
$totfil = 0;
foreach $filename (@ARGV)
{
if(-T $filename)
{
$totrep += &process($filename);
$totfil++;
}
}
print "$totfil files, $totrep replacements\n";
}
sub process
{
$fn = $_[0];
undef $/; #to grab entire file at once
print STDERR "$fn"; #show currnt file name
open (INFILE, $fn);
$q = <INFILE>;
close INFILE;
$/ = "\n";
$sum = 0;

if ($q =~ m/char\s*([\w]+)/i)
{

$sum = ($q =~ s/\bsprintf\(([\w]+),/snprintf\(\1,sizeof\(\1\),/ig);
}
if ($sum > 0)
{
open (OUTFILE,">$fn");
print OUTFILE $q;
close OUTFILE;
print " $sum replacement(s)";
}
print "\n";
return $sum;
}

However, there are some error for checking the char string (once the 1st occurence of char string passed the check, it will do search-and-replace for all sprintf() to snprintf() in the file). I would need help to identify what's wrong with the script. Thanks all.
# 2  
Old 03-22-2009
lets look at the regexp:

$sum = ($q =~ s/\bsprintf\(([\w]+),/snprintf\(\1,sizeof\(\1\),/ig);

you have [\w]+ which is the same as \w+ but the problem is that you are also looking for non-word characters in the string, but \w only matches a-zA-Z0-9_ so characters like % will never be found in the search string. Or have I misunderstood what you are doing?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace (perl)

How can I achieve this? Perl would be awesome. Input string a_b c //Note there is a blank here Needed Output a_b_c Thanks (4 Replies)
Discussion started by: dragonpoint
4 Replies

2. Shell Programming and Scripting

Search and Replace in Perl

I am trying to write a simple perl script to run on a FreeBSD machine. There are alot of posts here, and I have read so many, yet can not get this script to run. #!/usr/bin/perl -e 's/\r\n/~/' infile.txt outfile.txt I am trying to take a windows text file, move it into Unix, run a script on... (1 Reply)
Discussion started by: mach1
1 Replies

3. Shell Programming and Scripting

printf vs sprintf - perl

I would like to assign the output of printf to a variable in perl , it give me back a "1" instead of the time. How can I stuff the variable with what printf returns? Here is my code: #!/usr/bin/perl $time = localtime(time);... (3 Replies)
Discussion started by: Dabheeruz
3 Replies

4. Shell Programming and Scripting

search sprintf and replace with snprintf

Hi, anyone knows the perl search-and-replace expression for strcpy (char * destination, const char * source ); to strncpy ( char * destination, const char * source, size_t num ); ? the first and second arguments are the same (destination and source), the difference being that strncpy... (1 Reply)
Discussion started by: ChaMeN
1 Replies

5. Shell Programming and Scripting

sprintf result on perl

Hello, In perl lang, I have create a string (@str) by sprintf but unfortunately when program printed it out, only I could saw a number like 1. Certainly printf doesn't problem. How I can convert a string that are result of sprintf to a common string format??! Thanks in advance. PLEASE HELP ME. (2 Replies)
Discussion started by: Zaxon
2 Replies

6. UNIX for Dummies Questions & Answers

Perl search and replace not working in csh script

I am using perl to perform a search and replace. It works at the command line, but not in the csh shell script perl -pi -e 's@/Pattern@@g' $path/$file I used the @ as my delimiter because the pattern contains "/" (3 Replies)
Discussion started by: NobluesFDT
3 Replies

7. Shell Programming and Scripting

Search and replace in Perl

Hello, I have a Perl script that reads in an Excel spread sheet and formats the values into a text file. I am having trouble with one column that can have numbers or letters. Excel left justifies the values that start with a letter and right justifies the values that contain only a... (2 Replies)
Discussion started by: jyoung
2 Replies

8. Shell Programming and Scripting

search & replace password perl script

I wanted a perl script to be done for Password search & replace in two files. For Example: Example 1)--i am having a file such as cat /opt/customer/Ariba/UAT/ariba/app/buyer/Server/config/Parameters.table Example 2)--and i am having a other file in other location such as cat... (4 Replies)
Discussion started by: shellscript22
4 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Solaris

problem with sprintf snprintf in Solaris

******************************** Following is not the real issue. The issue is with popen. Plz continue forward with the thread to get a better picture. ******************************** Hi, I am working on a customised ftp application. In it we have used sprintf to store a UNIX command... (7 Replies)
Discussion started by: diganta
7 Replies
Login or Register to Ask a Question