LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-22-2011, 12:52 AM   #1
olivexer
LQ Newbie
 
Registered: Nov 2011
Posts: 13

Rep: Reputation: Disabled
A serious problem when migrating C programs from Aix to Linux (gcc)


We used to write sprintf(*str, "%03s", *v_str) to output string with '0' left padded to length of 3. This worked fine on Aix, but on Linux gcc (4.1.2), it left pads with space instead. This causes us big trouble as we don't know which programs to check. We have hundreds of programs that may have this!
Is there a 'global' way, say a compiler option, a global modifier etc that can change this behavior?
 
Old 11-22-2011, 05:05 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Buster (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
No, there is no easy way to change this behaviour, which is to do with the library rather than the compiler. This particular usage of leading zero padding with the string format is explicitly undefined in the ANSI standard, so the code is non-portable.

A work-around might be something like:
Code:
int width = 3;
sprintf(str, "%.*d%s", width-strlen(v_str), 0, v_str);
You can use grep '%0[0-9]*s' to search your source files for format strings with this problem.

Last edited by neonsignal; 11-22-2011 at 06:07 AM. Reason: added grep suggestion
 
Old 11-22-2011, 05:47 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
ooops... posted something stupid without proper reading. Deleted.

jlinkels

Last edited by jlinkels; 11-22-2011 at 05:48 AM.
 
Old 11-22-2011, 06:25 AM   #4
olivexer
LQ Newbie
 
Registered: Nov 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thank you. It will be a pain for us to search...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gcc Problem: I Cannot Compile Simple C++ Programs ankurgarg Linux - Software 2 06-13-2008 06:22 AM
how to write c programs in linux using gcc alexander_2007 Programming 2 09-16-2007 06:07 PM
Migrating from gcc 2.96 to 3.3.3 prakashpms Programming 3 02-09-2005 04:08 AM
Migrating from gcc 2.96 to 3.3.3 prakashpms Linux - Software 3 02-08-2005 04:03 PM
Problem compiling programs using ncurses library (gcc 3.2.2 and RH9 default inst.) jclark7777 Linux - Newbie 1 07-20-2004 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration