github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/src/lib9/fmt/sprint.c (about)

     1  /*
     2   * The authors of this software are Rob Pike and Ken Thompson,
     3   * with contributions from Mike Burrows and Sean Dorward.
     4   *
     5   *     Copyright (c) 2002-2006 by Lucent Technologies.
     6   *     Portions Copyright (c) 2004 Google Inc.
     7   * 
     8   * Permission to use, copy, modify, and distribute this software for any
     9   * purpose without fee is hereby granted, provided that this entire notice
    10   * is included in all copies of any software which is or includes a copy
    11   * or modification of this software and in all copies of the supporting
    12   * documentation for such software.
    13   * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
    14   * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES 
    15   * NOR GOOGLE INC MAKE ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING 
    16   * THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
    17   */
    18  
    19  #include <u.h>
    20  #include <libc.h>
    21  #include "fmtdef.h"
    22  
    23  int
    24  sprint(char *buf, char *fmt, ...)
    25  {
    26  	int n;
    27  	uint len;
    28  	va_list args;
    29  
    30  	len = 1<<30;  /* big number, but sprint is deprecated anyway */
    31  	/*
    32  	 * on PowerPC, the stack is near the top of memory, so
    33  	 * we must be sure not to overflow a 32-bit pointer.
    34  	 *
    35  	 * careful!  gcc-4.2 assumes buf+len < buf can never be true and
    36  	 * optimizes the test away.  casting to uintptr works around this bug.
    37  	 */
    38  	if((uintptr)buf+len < (uintptr)buf)
    39  		len = (uint)-(uintptr)buf-1;
    40  
    41  	va_start(args, fmt);
    42  	n = (int)vsnprint(buf, (int)len, fmt, args);
    43  	va_end(args);
    44  	return n;
    45  }