The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
openorpipe.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file openorpipe.c
5 
6  \version V1.10
7  \date 17.07.14
8  \brief Open a file for writing unless the filename starts with
9  a | in which case open as a pipe
10 
11  \copyright (c) UCL / Dr. Andrew C. R. Martin 1997-2014
12  \author Dr. Andrew C. R. Martin
13  \par
14  Institute of Structural & Molecular Biology,
15  University College London,
16  Gower Street,
17  London.
18  WC1E 6BT.
19  \par
20  andrew@bioinf.org.uk
21  andrew.martin@ucl.ac.uk
22 
23 **************************************************************************
24 
25  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
26  according to the conditions laid out in the accompanying file
27  COPYING.DOC.
28 
29  The code may be modified as required, but any modifications must be
30  documented so that the person responsible can be identified.
31 
32  The code may not be sold commercially or included as part of a
33  commercial product except as described in the file COPYING.DOC.
34 
35 **************************************************************************
36 
37  Description:
38  ============
39 
40 
41 **************************************************************************
42 
43  Usage:
44  ======
45 
46 **************************************************************************
47 
48  Revision History:
49  =================
50 - V1.0 26.05.97 Original By: ACRM
51 - V1.1 26.06.97 Added calls to signal()
52 - V1.2 27.02.98 Uses port.h
53 - V1.3 18.08.98 Added cast to popen() for SunOS
54 - V1.4 28.01.04 Added NOPIPE define. Allows compilation on systems
55  which don't support unix pipes
56 - V1.5 03.02.06 Added prototypes for popen() and pclose()
57 - V1.6 29.06.07 popen() and pclose() prototypes now skipped for MAC OSX
58  which defines them differently
59 - V1.7 17.03.09 popen() prototype now skipped for Windows.
60 - V1.8 02.04.09 Clean compile with NOPIPE defined
61 - V1.9 07.07.14 Use bl prefix for functions By: CTP
62 - V1.10 17.07.14 Added 'stdout' as a special file which maps to
63  standard output
64 
65 *************************************************************************/
66 /* Doxygen
67  -------
68  #GROUP General Programming
69  #SUBGROUP File IO
70  #FUNCTION blOpenOrPipe()
71  Opens a file for writing unless the filename begins with a | in which
72  case it is opened as a pipe.
73 
74  #FUNCTION blCloseOrPipe()
75  Attempts to close a file pointer as a pipe.
76 */
77 /************************************************************************/
78 /* Includes
79 */
80 #ifndef NOPIPE
81 #include "port.h" /* Required before stdio.h */
82 #endif
83 
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <signal.h>
87 #include <string.h>
88 #include "macros.h"
89 
90 /************************************************************************/
91 /* Defines and macros
92 */
93 
94 /************************************************************************/
95 /* Globals
96 */
97 
98 /************************************************************************/
99 /* Prototypes
100 */
101 #if !defined(__APPLE__) && !defined(MS_WINDOWS)
102 FILE *popen(char *, char *);
103 #endif
104 #ifndef __APPLE__
105 int pclose(FILE *);
106 #endif
107 
108 /************************************************************************/
109 /*>FILE *blOpenOrPipe(char *filename)
110  ----------------------------------
111 *//**
112 
113  \param[in] *filename A file or pipe to be opened
114  \return A file pointer
115 
116  Opens a file for writing unless the filename begins with a | in which
117  case it is opened as a pipe.
118 
119  Broken pipe signals are ignored.
120 
121 - 26.05.97 Original By: ACRM
122 - 26.06.97 Added call to signal()
123 - 18.08.98 Added case to popen() for SunOS
124 - 28.01.05 Added NOPIPE define
125 - 07.07.14 Use bl prefix for functions By: CTP
126 - 17.07.14 Added special 'stdout' file By: ACRM
127 */
128 FILE *blOpenOrPipe(char *filename)
129 {
130  char *fnam;
131 
132  KILLLEADSPACES(fnam, filename);
133  if(!strcmp(fnam, "stdout"))
134  return(stdout);
135 
136 #ifdef NOPIPE
137  return(fopen(fnam, "w"));
138 #else
139  if(fnam[0] == '|')
140  {
141  signal(SIGPIPE, SIG_IGN);
142  fnam++;
143  KILLLEADSPACES(fnam, fnam);
144  return((FILE *)popen(fnam, "w"));
145  }
146  else
147  {
148  return(fopen(fnam, "w"));
149  }
150 #endif
151 }
152 
153 /************************************************************************/
154 /*>int blCloseOrPipe(FILE *fp)
155  ---------------------------
156 *//**
157 
158  \param[in] *fp File pointer to be closed
159  \return Error code (as for fclose())
160 
161  Attempts to close a file pointer as a pipe. If it isn't associated
162  with a pipe (i.e. pclose returns (-1)), tries again to close it as
163  a normal file.
164 
165 - 26.05.97 Original By: ACRM
166 - 26.06.97 Added call to signal()
167 - 28.01.05 Added NOPIPE define
168 - 02.04.09 Moved 'int ret' to be in the #else
169 - 07.07.14 Use bl prefix for functions By: CTP
170 - 17.07.14 Added check that the file pointer isn't stdout By: ACRM
171 */
172 int blCloseOrPipe(FILE *fp)
173 {
174  if(fp==stdout)
175  return(0);
176 
177 #ifdef NOPIPE
178  return(fclose(fp));
179 #else
180  {
181  int ret;
182 
183  if((ret=pclose(fp)) == (-1))
184  return(fclose(fp));
185 
186  signal(SIGPIPE, SIG_DFL);
187  return(ret);
188  }
189 #endif
190 }
191 
int blCloseOrPipe(FILE *fp)
Definition: openorpipe.c:172
FILE * popen(char *, char *)
int pclose(FILE *)
Useful macros.
FILE * blOpenOrPipe(char *filename)
Definition: openorpipe.c:128
Port-specific defines to allow us to use things like popen() in a clean compile.
#define KILLLEADSPACES(y, x)
Definition: macros.h:408