Sponsored Content
Top Forums Programming does snprintf guarantee null termination? Post 302195678 by fpmurphy on Thursday 15th of May 2008 06:25:58 PM
Old 05-15-2008
According to POSIX.1-2001:

"The snprintf() function shall be equivalent to sprintf(), with the addition of the n argument which states the size of the buffer referred to by s. If n is zero, nothing shall be written and s may be a null pointer. Otherwise, output bytes beyond the n-1st shall be discarded instead of being written to the array, and a null byte is written at the end of the bytes actually written into the array."
 

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
explain_snprintf(3)					     Library Functions Manual					       explain_snprintf(3)

NAME
explain_snprintf - explain snprintf(3) errors SYNOPSIS
#include <libexplain/snprintf.h> const char *explain_snprintf(char *data, size_t data_size, const char *format); const char *explain_errno_snprintf(int errnum, char *data, size_t data_size, const char *format); void explain_message_snprintf(char *message, int message_size, char *data, size_t data_size, const char *format); void explain_message_errno_snprintf(char *message, int message_size, int errnum, char *data, size_t data_size, const char *format); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the snprintf(3) system call. explain_snprintf const char *explain_snprintf(char *data, size_t data_size, const char *format); The explain_snprintf function is used to obtain an explanation of an error returned by the snprintf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. data The original data, exactly as passed to the snprintf(3) system call. data_size The original data_size, exactly as passed to the snprintf(3) system call. format The original format, exactly as passed to the snprintf(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = snprintf(data, data_size, format); if (result < 0 && errno != 0) { fprintf(stderr, "%s ", explain_snprintf(data, data_size, format)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_snprintf_or_die(3) function. explain_errno_snprintf const char *explain_errno_snprintf(int errnum, char *data, size_t data_size, const char *format); The explain_errno_snprintf function is used to obtain an explanation of an error returned by the snprintf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. data The original data, exactly as passed to the snprintf(3) system call. data_size The original data_size, exactly as passed to the snprintf(3) system call. format The original format, exactly as passed to the snprintf(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = snprintf(data, data_size, format); if (result < 0 && errno != 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_snprintf(err, data, data_size, format)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_snprintf_or_die(3) function. explain_message_snprintf void explain_message_snprintf(char *message, int message_size, char *data, size_t data_size, const char *format); The explain_message_snprintf function is used to obtain an explanation of an error returned by the snprintf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. data The original data, exactly as passed to the snprintf(3) system call. data_size The original data_size, exactly as passed to the snprintf(3) system call. format The original format, exactly as passed to the snprintf(3) system call. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = snprintf(data, data_size, format); if (result < 0 && errno != 0) { char message[3000]; explain_message_snprintf(message, sizeof(message), data, data_size, format); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_snprintf_or_die(3) function. explain_message_errno_snprintf void explain_message_errno_snprintf(char *message, int message_size, int errnum, char *data, size_t data_size, const char *format); The explain_message_errno_snprintf function is used to obtain an explanation of an error returned by the snprintf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. data The original data, exactly as passed to the snprintf(3) system call. data_size The original data_size, exactly as passed to the snprintf(3) system call. format The original format, exactly as passed to the snprintf(3) system call. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = snprintf(data, data_size, format); if (result < 0 && errno != 0) { int err = errno; char message[3000]; explain_message_errno_snprintf(message, sizeof(message), err, data, data_size, format); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_snprintf_or_die(3) function. SEE ALSO
snprintf(3) formatted output conversion explain_snprintf_or_die(3) formatted output conversion and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2010 Peter Miller explain_snprintf(3)
All times are GMT -4. The time now is 05:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy