2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation for NSPipe for GNUStep
|
1997-09-01 21:59:51 +00:00
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Date: 1997
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2006-10-18 05:14:04 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
$Date$ $Revision$
|
1997-09-01 21:59:51 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "config.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
#include "GNUstepBase/preface.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSObject.h"
|
|
|
|
#include "Foundation/NSFileHandle.h"
|
|
|
|
#include "Foundation/NSDebug.h"
|
2006-10-07 12:50:18 +00:00
|
|
|
#include "GNUstepBase/GSFunctions.h"
|
2002-05-02 21:22:06 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-12-11 19:09:56 +00:00
|
|
|
#include <unistd.h>
|
2002-02-20 06:42:05 +00:00
|
|
|
#endif
|
1997-12-11 19:09:56 +00:00
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
2003-06-27 14:34:13 +00:00
|
|
|
* <p>The NSPipe provides an encapsulation of the UNIX concept of pipe.<br />
|
2002-08-20 10:22:05 +00:00
|
|
|
* With NSPipe, it is possible to redirect the standard input or
|
|
|
|
* standard output.
|
2003-06-27 14:34:13 +00:00
|
|
|
* </p>
|
|
|
|
* <p>The file handles created by NSPipe are automatically closed when they
|
|
|
|
* are no longer in use (ie when the NSPipe instance is deallocated), so you
|
|
|
|
* don't need to close them explicitly.
|
|
|
|
* </p>
|
2002-08-20 10:22:05 +00:00
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
@implementation NSPipe
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* Returns a newly allocated and initialized NSPipe object that has been
|
|
|
|
* sent an autorelease message.
|
|
|
|
*/
|
2000-09-15 10:17:42 +00:00
|
|
|
+ (id) pipe
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2000-09-15 10:17:42 +00:00
|
|
|
return AUTORELEASE([[self alloc] init]);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
2000-09-15 10:17:42 +00:00
|
|
|
- (void) dealloc
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2000-09-15 10:17:42 +00:00
|
|
|
RELEASE(readHandle);
|
|
|
|
RELEASE(writeHandle);
|
|
|
|
[super dealloc];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
2000-09-15 10:17:42 +00:00
|
|
|
- (id) init
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
2000-09-15 10:17:42 +00:00
|
|
|
if (self != nil)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2005-10-11 19:09:26 +00:00
|
|
|
#ifndef __MINGW32__
|
1997-09-01 21:59:51 +00:00
|
|
|
int p[2];
|
|
|
|
|
|
|
|
if (pipe(p) == 0)
|
|
|
|
{
|
2003-06-27 14:34:13 +00:00
|
|
|
readHandle = [[NSFileHandle alloc] initWithFileDescriptor: p[0]
|
|
|
|
closeOnDealloc: YES];
|
|
|
|
writeHandle = [[NSFileHandle alloc] initWithFileDescriptor: p[1]
|
|
|
|
closeOnDealloc: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
2001-05-09 08:32:52 +00:00
|
|
|
else
|
|
|
|
{
|
2006-10-07 12:50:18 +00:00
|
|
|
NSLog(@"Failed to create pipe ... %@", GSLastError());
|
2001-05-09 08:32:52 +00:00
|
|
|
DESTROY(self);
|
|
|
|
}
|
2000-09-13 19:52:42 +00:00
|
|
|
#else
|
2005-02-23 16:05:09 +00:00
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
2000-09-13 19:52:42 +00:00
|
|
|
HANDLE readh, writeh;
|
|
|
|
|
2005-02-23 16:05:09 +00:00
|
|
|
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
if (CreatePipe(&readh, &writeh, &saAttr, 0) != 0)
|
2000-09-13 19:52:42 +00:00
|
|
|
{
|
2003-06-27 14:34:13 +00:00
|
|
|
readHandle = [[NSFileHandle alloc] initWithNativeHandle: readh
|
|
|
|
closeOnDealloc: YES];
|
|
|
|
writeHandle = [[NSFileHandle alloc] initWithNativeHandle: writeh
|
|
|
|
closeOnDealloc: YES];
|
2000-09-13 19:52:42 +00:00
|
|
|
}
|
2006-10-07 12:50:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to create pipe ... %@", GSLastError());
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
2000-09-13 19:52:42 +00:00
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-06-27 14:34:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the file handle for reading from the pipe.
|
|
|
|
*/
|
2005-07-08 11:48:37 +00:00
|
|
|
- (NSFileHandle*) fileHandleForReading
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
return readHandle;
|
|
|
|
}
|
|
|
|
|
2003-06-27 14:34:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the file handle for writing to the pipe.
|
|
|
|
*/
|
2005-07-08 11:48:37 +00:00
|
|
|
- (NSFileHandle*) fileHandleForWriting
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
return writeHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|