home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / lib / jssnprintf.c < prev    next >
C/C++ Source or Header  |  2000-05-07  |  2KB  |  87 lines

  1. /* @(#)jssnprintf.c    1.6 00/05/07 Copyright 1985 J. Schilling */
  2. /*
  3.  *    Copyright (c) 1985 J. Schilling
  4.  */
  5. /*
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <mconfig.h>
  22. #include <sys/types.h>        /* Try to get size_t    */
  23. #include <stdio.h>        /* Try again for size_t    */
  24. #include <stdxlib.h>        /* Try again for size_t    */
  25. #include <vadefs.h>
  26. #include <standard.h>
  27. #include <schily.h>
  28.  
  29. EXPORT    int js_snprintf __PR((char *, size_t maxcnt, const char *, ...));
  30.  
  31. typedef struct {
  32.     char    *ptr;
  33.     int    count;
  34. } *BUF, _BUF;
  35.  
  36. #ifdef    PROTOTYPES
  37. static void _cput(char c, long l)
  38. #else
  39. static void _cput(c, l)
  40.     char    c;
  41.     long    l;
  42. #endif
  43. {
  44.     register BUF    bp = (BUF)l;
  45.  
  46.     if (--bp->count > 0) {
  47.         *bp->ptr++ = c;
  48.     } else {
  49.         /*
  50.          * Make sure that there will never be a negative overflow.
  51.          */
  52.         bp->count++;
  53.     }
  54. }
  55.  
  56. /* VARARGS2 */
  57. #ifdef    PROTOTYPES
  58. int js_snprintf(char *buf, size_t maxcnt, const char *form, ...)
  59. #else
  60. int js_snprintf(buf, maxcnt, form, va_alist)
  61.     char    *buf;
  62.     unsigned maxcnt;
  63.     char    *form;
  64.     va_dcl
  65. #endif
  66. {
  67.     va_list    args;
  68.     int    cnt;
  69.     _BUF    bb;
  70.  
  71.     bb.ptr = buf;
  72.     bb.count = maxcnt;
  73.  
  74. #ifdef    PROTOTYPES
  75.     va_start(args, form);
  76. #else
  77.     va_start(args);
  78. #endif
  79.     cnt = format(_cput, (long)&bb, form,  args);
  80.     va_end(args);
  81.     *(bb.ptr) = '\0';
  82.     if (bb.count <= 0)
  83.         return (-1);
  84.  
  85.     return (cnt);
  86. }
  87.