Sponsored Content
Top Forums Programming does snprintf guarantee null termination? Post 302195761 by jim mcnamara on Thursday 15th of May 2008 11:21:39 PM
Old 05-16-2008
What the OP is on about:

from the same C-99 standard (what POSIX uses)
Quote:
The snprintf function returns the number of characters that would have been written
had n been sufficiently large, not counting the terminating null character, or a neg ative
value if an encoding error occurred. Thus, the null-terminated output has been
completely written if and only if the returned value is nonnegative and less than n.
So you have to check the return code of snprintf to know if it worked correctly.
You should check return codes no matter how stupid it seems. If you use gcc then
Code:
gcc -Wall <filename.c>

or use lint on your code. The only acceptable compile is a completely clean one.
 

7 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

random script termination

I'm writing a script to archive data. First, the files are all rsync'd to the archive directory via NFS mounts(I know not the most efficient, but the only choice in this situation), then I use md5sum to validate the transfers. During execution of the script, it will exit for no apparent reason. It... (6 Replies)
Discussion started by: mph
6 Replies

3. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: ChaMeN
1 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

guarantee to start before restart...

Hi All, is there a way or script that i can check my AIX 5.3 OS will restart before i made restart? is there a script that can check all the startup files are ok before restarting. it is because i was stuck last time when i restart my PC because some startup files were missing:o. (2 Replies)
Discussion started by: malcomex999
2 Replies

6. Shell Programming and Scripting

Problem with ssh termination...

hi all, i have a situation where i run ssh command from a unix machine to execute few scripts on 2 other unix machines. my problem is, the scripts that i run will start few commands on the 2 servers and will quit....i am able to quit from the script but i have to give ctrl+c (on the... (10 Replies)
Discussion started by: niteesh_!7
10 Replies

7. AIX

A question about scsi termination

http://ep.yimg.com/ay/iercomputer/ibm-39j5022-ultra320-scsi-adapter-dual-channel-pci-x-fc5736-3.gif I have bought this controller. Simple and fast question: I will put on this controller a external LTO tape,which is terminated with a terminator. I have to put another terminator on PCI-controller... (1 Reply)
Discussion started by: Linusolaradm1
1 Replies
WPRINTF(3)						     Linux Programmer's Manual							WPRINTF(3)

NAME
wprintf, fwprintf, swprintf, vwprintf, vfwprintf, vswprintf - formatted wide character output conversion SYNOPSIS
#include <stdio.h> #include <wchar.h> int wprintf(const wchar_t *format, ...); int fwprintf(FILE *stream, const wchar_t *format, ...); int swprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, ...); #include <stdarg.h> int vwprintf(const wchar_t *format, va_list args); int vfwprintf(FILE *stream, const wchar_t *format, va_list args); int vswprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, va_list args); DESCRIPTION
The wprintf family of functions is the wide-character equivalent of the printf family of functions. It performs formatted output of wide characters. The wprintf and vwprintf functions perform wide character output to stdout. stdout must not be byte oriented; see function fwide for more information. The fwprintf and vfwprintf functions perform wide character output to stream. stream must not be byte oriented; see function fwide for more information. The swprintf and vswprintf functions perform wide character output to an array of wide characters. The programmer must ensure that there is room for at least maxlen wide characters at wcs. These functions are like the printf, vprintf, fprintf, vfprintf, sprintf, vsprintf functions except for the following differences: o The format string is a wide character string. o The output consists of wide characters, not bytes. o swprintf and vswprintf take a maxlen argument, sprintf and vsprintf do not. (snprintf and vsnprintf take a maxlen argument, but these functions do not return -1 upon buffer overflow on Linux.) The treatment of the conversion characters c and s is different: c If no l modifier is present, the int argument is converted to a wide character by a call to the btowc function, and the resulting wide character is written. If an l modifier is present, the wint_t (wide character) argument is written. s If no l modifier is present: The ``const char *'' argument is expected to be a pointer to an array of character type (pointer to a string) containing a multibyte character sequence beginning in the initial shift state. Characters from the array are converted to wide characters (each by a call to the mbrtowc function with a conversion state starting in the initial state before the first byte). The resulting wide characters are written up to (but not including) the terminating null wide character. If a precision is specified, no more wide characters than the number specified are written. Note that the precision determines the number of wide characters written, not the number of bytes or screen positions. The array must contain a terminating null byte, unless a precision is given and it is so small that the number of converted wide characters reaches it before the end of the array is reached. -- If an l modifier is present: The ``const wchar_t *'' argument is expected to be a pointer to an array of wide characters. Wide characters from the array are written up to (but not including) a terminating null wide character. If a precision is specified, no more than the number specified are written. The array must contain a terminating null wide character, unless a precision is given and it is smaller than or equal to the number of wide characters in the array. RETURN VALUE
The functions return the number of wide characters written, excluding the terminating null wide character in case of the functions swprintf and vswprintf. They return -1 when an error occurs. CONFORMING TO
ISO/ANSI C, UNIX98 SEE ALSO
printf(3), fprintf(3), snprintf(3), fputwc(3), fwide(3), wscanf(3) NOTES
The behaviour of wprintf et al. depends on the LC_CTYPE category of the current locale. If the format string contains non-ASCII wide characters, the program will only work correctly if the LC_CTYPE category of the current locale at run time is the same as the LC_CTYPE category of the current locale at compile time. This is because the wchar_t representation is platform and locale dependent. (The GNU libc represents wide characters using their Unicode (ISO-10646) code point, but other platforms don't do this. Also, the use of ISO C99 universal character names of the form unnnn does not solve this problem.) Therefore, in interna- tionalized programs, the format string should consist of ASCII wide characters only, or should be constructed at run time in an interna- tionalized way (e.g. using gettext or iconv, followed by mbstowcs). GNU
1999-11-20 WPRINTF(3)
All times are GMT -4. The time now is 03:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy