Back to index

glibc  2.9
vpprintf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,94,97,2000,01 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdarg.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <hurd.h>
00023 
00024 #ifdef USE_IN_LIBIO
00025 # include <libioP.h>
00026 #endif
00027 
00028 static ssize_t
00029 do_write (void *cookie,     const char *buf, size_t n)
00030 {
00031   error_t error = __io_write ((io_t) cookie, buf, n, -1,
00032                            (mach_msg_type_number_t *) &n);
00033   if (error)
00034     return __hurd_fail (error);
00035   return n;
00036 }
00037 
00038 /* Write formatted output to PORT, a Mach port supporting the i/o protocol,
00039    according to the format string FORMAT, using the argument list in ARG.  */
00040 int
00041 vpprintf (io_t port, const char *format, va_list arg)
00042 {
00043   int done;
00044 
00045 #ifdef USE_IN_LIBIO
00046 
00047   struct locked_FILE
00048   {
00049     struct _IO_cookie_file cfile;
00050 #ifdef _IO_MTSAFE_IO
00051     _IO_lock_t lock;
00052 #endif
00053   } temp_f;
00054 #ifdef _IO_MTSAFE_IO
00055   temp_f.cfile.__fp.file._lock = &temp_f.lock;
00056 #endif
00057 
00058   _IO_cookie_init (&temp_f.cfile, _IO_NO_READS,
00059                  (void *) port, (cookie_io_functions_t) { write: do_write });
00060 
00061   done = _IO_vfprintf (&temp_f.cfile.__fp.file, format, arg);
00062 
00063 #else
00064 
00065   FILE f;
00066 
00067   /* Create an unbuffered stream talking to PORT on the stack.  */
00068   memset ((void *) &f, 0, sizeof (f));
00069   f.__magic = _IOMAGIC;
00070   f.__mode.__write = 1;
00071   f.__cookie = (void *) port;
00072   f.__room_funcs = __default_room_functions;
00073   f.__io_funcs.__write = do_write;
00074   f.__seen = 1;
00075   f.__userbuf = 1;
00076 
00077   /* vfprintf will use a buffer on the stack for the life of the call.  */
00078   done = vfprintf (&f, format, arg);
00079 
00080 #endif
00081 
00082   return done;
00083 }