delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/06/09/16:04:27

From: Alain Magloire <alainm AT rcsm DOT ece DOT mcgill DOT ca>
Message-Id: <199906092004.QAA18356@mccoy2.ECE.McGill.CA>
Subject: {v,}snprintf.c ???
To: djgpp-workers AT delorie DOT com
Date: Wed, 9 Jun 1999 16:04:31 -0400 (EDT)
In-Reply-To: <375EB600.979DA70D@cyberoptics.com> from "Eric Rudd" at Jun 9, 99 01:44:16 pm
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Bonjour

  I was playing with djgpp this afternoon and notice snprintf()
was missing.  It's not ANSI  not even POSIX, I beleive. But more and
more apps use it because of the security that it provides, sprintf() and
vsprintf() provide no buffer checks.

I think it's define in Unix98 and most modern OS will have it.
Well at least the 3 sitting in front of me (Solaris, Linux, QNX/NTO).

If you decide to use it and need the *.txh equivalent let me know.

stdio.h:
int vsnprintf(char *s, size_t n, const char *format, va_list ap);
int snprintf(char *s, size_t n, const char *format, ...);

----------------------------------------
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <limits.h>
#include <libc/file.h>

int
snprintf(char *str, size_t n, const char *fmt, ...)
{
  FILE _strbuf;
  int len;

  if ((int)n < 1)
    return EOF;
  _strbuf._flag = _IOWRT|_IOSTRG;
  _strbuf._ptr = str;
  _strbuf._cnt = n - 1;
  len = _doprnt(fmt, &(fmt)+1, &_strbuf);
  *_strbuf._ptr = 0;
  return len;
}
----------------------------------------
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <libc/file.h>

int
vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{
  FILE f;
  int len;

  if ((int)n < 1)
    return (EOF);
  f._flag = _IOWRT|_IOSTRG;
  f._ptr = str;
  f._cnt = n - 1;
  len = _doprnt(fmt, ap, &f);
  *f._ptr = 0;
  return len;
}
------------------------------------------------
-- 
au revoir, alain
----
Aussi haut que l'on soit assis, on est toujours assis que sur son cul !!!

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019