home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / samba-1.9.18p7.tar.gz / samba-1.9.18p7.tar / samba-1.9.18p7 / source / slprintf.c < prev    next >
C/C++ Source or Header  |  1998-05-12  |  2KB  |  111 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    snprintf replacement
  5.    Copyright (C) Andrew Tridgell 1998
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "includes.h"
  23.  
  24. extern int DEBUGLEVEL;
  25.  
  26.  
  27. /* this is like vsnprintf but the 'n' limit does not include
  28.    the terminating null. So if you have a 1024 byte buffer then
  29.    pass 1023 for n */
  30. int vslprintf(char *str, int n, char *format, va_list ap)
  31. {
  32. #ifdef HAVE_VSNPRINTF
  33.     int ret = vsnprintf(str, n, format, ap);
  34.     if (ret > n || ret < 0) {
  35.         str[n] = 0;
  36.         return -1;
  37.     }
  38.     str[ret] = 0;
  39.     return ret;
  40. #else
  41.     static char *buf;
  42.     static int len=8000;
  43.     int ret;
  44.  
  45.     /* this code is NOT a proper vsnprintf() implementation. It
  46.        relies on the fact that all calls to slprintf() in Samba
  47.        pass strings which have already been through pstrcpy() or
  48.        fstrcpy() and never more than 2 strings are
  49.        concatenated. This means the above buffer is absolutely
  50.        ample and can never be overflowed.
  51.  
  52.        In the future we would like to replace this with a proper
  53.        vsnprintf() implementation but right now we need a solution
  54.        that is secure and portable. This is it.  */
  55.  
  56.     if (!buf) {
  57.         buf = malloc(len);
  58.         if (!buf) {
  59.             /* can't call debug or we would recurse */
  60.             exit(1);
  61.         }
  62.     }
  63.  
  64.     ret = vsprintf(buf, format, ap);
  65.  
  66.     if (ret < 0) {
  67.         str[0] = 0;
  68.         return -1;
  69.     }
  70.  
  71.     if (ret < n) {
  72.         n = ret;
  73.     } else if (ret > n) {
  74.         ret = -1;
  75.     }
  76.  
  77.     buf[n] = 0;
  78.     
  79.     memcpy(str, buf, n+1);
  80.  
  81.     return ret;
  82. #endif
  83. }
  84.  
  85. #ifdef __STDC__
  86.  int slprintf(char *str, int n, char *format, ...)
  87. {
  88. #else
  89.  int slprintf(va_alist)
  90. va_dcl
  91. {
  92.     char *str, *format;
  93.     int n;
  94. #endif
  95.     va_list ap;  
  96.     int ret;
  97.  
  98. #ifdef __STDC__
  99.     va_start(ap, format);
  100. #else
  101.     va_start(ap);
  102.     str = va_arg(ap,char *);
  103.     n = va_arg(ap,int);
  104.     format = va_arg(ap,char *);
  105. #endif
  106.  
  107.     ret = vslprintf(str,n,format,ap);
  108.     va_end(ap);
  109.     return ret;
  110. }
  111.