[Libsysio-commit] HEAD: libsysio/src cprintf.c module.mk init.c
Brought to you by:
lward
From: Lee W. <lw...@us...> - 2007-08-20 16:03:02
|
Update of /cvsroot/libsysio/libsysio/src In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv30003/src Modified Files: module.mk init.c Added Files: cprintf.c Log Message: Break cprintf, and friends, out of init.c and put them in their own file. Also, removed the dependency on _SYSIO_TRACING. --- NEW FILE --- /* * This Cplant(TM) source code is the property of Sandia National * Laboratories. * * This Cplant(TM) source code is copyrighted by Sandia National * Laboratories. * * The redistribution of this Cplant(TM) source code is subject to the * terms of the GNU Lesser General Public License * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html) * * Cplant(TM) Copyright 1998-2007 Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the US Government. * Export of this program may require a license from the United States * Government. */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Questions or comments about this library should be sent to: * * Lee Ward * Sandia National Laboratories, New Mexico * P.O. Box 5800 * Albuquerque, NM 87185-1319 * * le...@sa... */ #if defined(__linux__) && !defined(_BSD_SOURCE) #define _BSD_SOURCE #endif #include <stdio.h> #include <stdlib.h> #include <sys/syscall.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <stdarg.h> #include "sysio.h" #include "native.h" #if !(defined(_HAVE_ASPRINTF) && _HAVE_ASPRINTF) /* * Print a string to allocated memory. */ static int vasprintf(char **strp, const char *fmt, va_list ap) { size_t siz; int oerrno; char *s; va_list aq; int n; siz = 50; oerrno = errno; if (!(s = malloc(siz))) { errno = oerrno; return -1; } for (;;) { va_copy(aq, ap); n = vsnprintf (s, siz, fmt, aq); va_end(aq); if (n > -1 && (size_t )n < siz) break; if (n > -1) /* glibc 2.1 */ siz = n+1; /* precise */ else /* glibc 2.0 */ siz *= 2; /* twice the old */ if (!(s = realloc (s, siz))) break; } *strp = s; errno = oerrno; return n; } #if 0 static int asprintf(char **strp, const char *fmt, ...) { va_list ap; int n; va_start(ap, fmt); n = vasprintf(strp, fmt, ap); va_end(ap); return n; } #endif #endif /* !(defined(_HAVE_ASPRINTF) && _HAVE_ASPRINTF) */ static void _sysio_cwrite(const char *buf, size_t len) { int oerrno; oerrno = errno; (void )syscall(SYSIO_SYS_write, STDERR_FILENO, buf, len); errno = oerrno; } /* * Console printf. */ void _sysio_cprintf(const char *fmt, ...) { va_list ap; int len; char *buf; va_start(ap, fmt); buf = NULL; len = vasprintf(&buf, fmt, ap); va_end(ap); if (len < 0) return; _sysio_cwrite(buf, len); free(buf); } Index: module.mk =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/module.mk,v retrieving revision 1.13 retrieving revision 1.14 diff -u -w -b -B -p -r1.13 -r1.14 --- module.mk 2 Jul 2007 18:58:16 -0000 1.13 +++ module.mk 20 Aug 2007 16:02:56 -0000 1.14 @@ -29,6 +29,6 @@ SRCDIR_SRCS = src/access.c src/chdir.c s src/symlink.c src/readlink.c \ src/truncate.c src/unlink.c src/utime.c \ $(FILE_SUPPORT) $(LUSTRE_SRCDIR_SRCS) \ - src/tracing.c + src/tracing.c src/cprintf.c SRCDIR_EXTRA = src/module.mk Index: init.c =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/init.c,v retrieving revision 1.36 retrieving revision 1.37 diff -u -w -b -B -p -r1.36 -r1.37 --- init.c 2 Jul 2007 18:58:16 -0000 1.36 +++ init.c 20 Aug 2007 16:02:56 -0000 1.37 @@ -234,89 +234,6 @@ _sysio_shutdown() } #ifdef SYSIO_TRACING - -#if !(defined(_HAVE_ASPRINTF) && _HAVE_ASPRINTF) -/* - * Print a string to allocated memory. - */ -static int -vasprintf(char **strp, const char *fmt, va_list ap) -{ - size_t siz; - int oerrno; - char *s; - va_list aq; - int n; - - siz = 50; - oerrno = errno; - if (!(s = malloc(siz))) { - errno = oerrno; - return -1; - } - for (;;) { - va_copy(aq, ap); - n = vsnprintf (s, siz, fmt, aq); - va_end(aq); - if (n > -1 && (size_t )n < siz) - break; - if (n > -1) /* glibc 2.1 */ - siz = n+1; /* precise */ - else /* glibc 2.0 */ - siz *= 2; /* twice the old */ - if (!(s = realloc (s, siz))) - break; - } - *strp = s; - errno = oerrno; - return n; -} - -#if 0 -static int -asprintf(char **strp, const char *fmt, ...) -{ - va_list ap; - int n; - - va_start(ap, fmt); - n = vasprintf(strp, fmt, ap); - va_end(ap); - return n; -} -#endif -#endif /* !(defined(_HAVE_ASPRINTF) && _HAVE_ASPRINTF) */ - -static void -_sysio_cwrite(const char *buf, size_t len) -{ - int oerrno; - - oerrno = errno; - (void )syscall(SYSIO_SYS_write, STDERR_FILENO, buf, len); - errno = oerrno; -} - -/* - * Console printf. - */ -void -_sysio_cprintf(const char *fmt, ...) -{ - va_list ap; - int len; - char *buf; - - va_start(ap, fmt); - buf = NULL; - len = vasprintf(&buf, fmt, ap); - va_end(ap); - if (len < 0) - return; - _sysio_cwrite(buf, len); - free(buf); -} - /* * Register a trace callback. * |