/* iocapt.c version librock_CHISEL _summary Redirect the output written to a FILE * Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_iocapt #include #include #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_iocapt /* License description features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_iocapt[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /* Function wrapping and tracing features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #define librock_body_IOCaptureBegin librock_IOCaptureBegin /* For librock_WRAP section */ #define librock_body_IOCaptureEndstr librock_IOCaptureEndstr /* For librock_WRAP section */ /**************************************************************/ #endif #ifndef librock_NOIMPL_IOCaptureBegin /**************************************************************/ struct librock_IOCapture_s { FILE *f; FILE *ftemp; int savefd; }; struct librock_IOCapture_s *librock_body_IOCaptureBegin(librock_FILE *f) { /* Copyright 1998-2002, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_IOCaptureBegin /* librock_IOCaptureBegin - Redirect writes to a FILE * into a temporary file. librock_IOCaptureEndstr - End redirection, and place all redirected data into an allocated string. */ /**/ #include #include struct librock_IOCapture_s * librock_IOCaptureBegin( librock_FILE *f ); char * librock_IOCaptureEndstr( struct librock_IOCapture_s *p, char **ppasz ); /* This function issues the fflush(), dup(), dup2() and tmpfile() calls to save the output written to a FILE *. calling librock_IOCaptureEndstr places the captured output into a string, and restores all file descriptors. This function works under MSVC (WIN32) and Unix/Linux/BSD platforms. Typical use is */ char *asz = 0; struct librock_IOCapture_s *pIOC = librock_IOCaptureBegin((librock_FILE *)stdout); printf("This is a test"); librock_IOCaptureEndstr(pIOC,&asz); /* . . . */ librock_astrfree(&asz); /* malloc, free, dup, dup2, close, tmpfile, fclose, fileno rewind, librock_astrfree, librock_astrcat, librock_afgets */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ struct librock_IOCapture_s *p; p = malloc(sizeof(*p)); if (!p) { return 0; } p->f = (FILE *)f; fflush(p->f); p->ftemp = tmpfile(); if (!p->ftemp) { free(p); return 0; } p->savefd = dup(fileno(p->f)); if (p->savefd < 0) { fclose(p->ftemp); free(p); return 0; } if (dup2(fileno(p->ftemp),fileno(p->f)) < 0) { close(p->savefd); fclose(p->ftemp); free(p); return 0; } return p; } /**************************************************************/ #endif /* NOIMP section */ #ifndef librock_NOIMPL_IOCaptureEndstr /**************************************************************/ char *librock_body_IOCaptureEndstr(struct librock_IOCapture_s *p,char **ppasz) {/* Copyright 1998-2002, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ char *asz = 0; int need; fflush(p->f); dup2(p->savefd,fileno(p->f)); close(p->savefd); need = ftell(p->ftemp); rewind(p->ftemp); librock_astrensure(ppasz,need); (*ppasz)[need] = '\0'; fread(*ppasz,1,need,p->ftemp); fclose(p->ftemp); free(p); return *ppasz; } /**************************************************************/ #endif /* NOIMP section */ /* $Log: iocapt.c,v $ Revision 1.5 2002/08/01 20:17:43 forrest@mibsoftware.com rights=#1 Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.4 2002/04/09 03:41:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.3 2002/03/18 19:22:48 forrest@mibsoftware.com rights=#1 Use fread instead of afgets in endstr. Revision 1.2 2002/02/10 02:23:39 forrest@mibsoftware.com rights=#1 Coding standards: lidesc, wrap, standardized chg log Added man page. Renamed the End function. Revision 1.1 2002/01/29 14:31:50 forrest@mibsoftware.com rights=#1 Moved rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */