1 /* This is part of libio/iostream, providing -*- C++ -*- input/output. 2 Copyright (C) 1993 Free Software Foundation 3 4 This file is part of the GNU IO Library. This library is free 5 software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the 7 Free Software Foundation; either version 2, or (at your option) 8 any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this library; see the file COPYING. If not, write to the Free 17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 As a special exception, if you link this library with files 20 compiled with a GNU compiler to produce an executable, this does not cause 21 the resulting executable to be covered by the GNU General Public License. 22 This exception does not however invalidate any other reasons why 23 the executable file might be covered by the GNU General Public License. */ 24 25 /* Written by Per Bothner (bothner@cygnus.com). */ 26 27 #ifdef __GNUG__ 28 #pragma implementation 29 #endif 30 #include "libioP.h" 31 #include <pfstream.h> 32 #include <procbuf.h> 33 34 ipfstream::ipfstream(const char *name, int mode, int prot) 35 { 36 const char* p; 37 38 // Look for '| command' (as used by ftp). 39 for (p = name; *p == ' ' || *p == '\t'; p++) ; 40 if (*p == '|') { 41 procbuf *pbuf = new procbuf(); 42 init(pbuf); 43 if (!pbuf->open(p+1, mode)) 44 set(ios::badbit); 45 return; 46 } 47 48 // Look for 'command |' 49 while (*p) p++; // Point to last 50 while (p[-1] == ' ' || p[-1] == '\t' || p[-1] == '\n') p--; 51 if (p[-1] == '|') { 52 // Must remove the final '|'. 53 p--; 54 #if !defined (__GNUC__) || defined (__STRICT_ANSI__) 55 char *command = new char[p-name+1]; 56 #else 57 char command[p-name+1]; 58 #endif 59 memcpy(command, name, p-name); 60 command[p-name] = '\0'; 61 62 procbuf *pbuf = new procbuf(); 63 if (pbuf->open(command, mode)) 64 set(ios::badbit); 65 #if !defined (__GNUC__) || defined (__STRICT_ANSI__) 66 delete command; 67 #endif 68 return; 69 } 70 71 init(new filebuf()); 72 if (!rdbuf()->open(name, mode, prot)) 73 set(ios::badbit); 74 } 75 76 opfstream::opfstream(const char *name, int mode, int prot) 77 { 78 const char *p; 79 // Look for '| command'. 80 for (p = name; *p == ' ' || *p == '\t'; p++) ; 81 if (*p == '|') { 82 procbuf *pbuf = new procbuf(); 83 init(pbuf); 84 if (!pbuf->open(p+1, mode)) 85 set(ios::badbit); 86 } 87 else { 88 init(new filebuf()); 89 if (!rdbuf()->open(name, mode, prot)) 90 set(ios::badbit); 91 } 92 } 93