Fixes for standard (non socket) files.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13235 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2002-03-25 17:27:49 +00:00
parent b623b97a14
commit 58355c8ac0
2 changed files with 66 additions and 42 deletions

View file

@ -7,6 +7,8 @@
not automatically flushed! not automatically flushed!
* Source/GSWindowsFileHandle.m: watch for exceptional conditions * Source/GSWindowsFileHandle.m: watch for exceptional conditions
so we can handle socket connection failures in mingw32. so we can handle socket connection failures in mingw32.
Added code to try to cope with standard files properly, not just
network sockets.
* Source/GSTcpPort.m: ditto. * Source/GSTcpPort.m: ditto.
* Source/Unicode.m: GSToUnicode(), GSFromUnicode(), if terminate * Source/Unicode.m: GSToUnicode(), GSFromUnicode(), if terminate
option is specified, always produce output with a nul terminator option is specified, always produce output with a nul terminator

View file

@ -43,11 +43,14 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <string.h>
#if HAVE_UNISTD_H #if HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <io.h>
#include <stdio.h>
#include <errno.h> #include <errno.h>
// Maximum data in single I/O operation // Maximum data in single I/O operation
@ -170,7 +173,14 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
[self setNonBlocking: wasNonBlocking]; [self setNonBlocking: wasNonBlocking];
if (closeOnDealloc == YES) if (closeOnDealloc == YES)
{ {
close(descriptor); if (isStandardFile)
{
(void)_close(descriptor);
}
else
{
(void)closesocket(descriptor);
}
descriptor = -1; descriptor = -1;
} }
} }
@ -326,7 +336,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
NSLog(@"unable to bind to port %s:%d - %s", NSLog(@"unable to bind to port %s:%d - %s",
inet_ntoa(sin.sin_addr), inet_ntoa(sin.sin_addr),
GSSwapBigI16ToHost(sin.sin_port), GSLastErrorStr(errno)); GSSwapBigI16ToHost(sin.sin_port), GSLastErrorStr(errno));
(void) close(net); (void) closesocket(net);
[self release]; [self release];
return nil; return nil;
} }
@ -334,7 +344,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
if (listen(net, 5) < 0) if (listen(net, 5) < 0)
{ {
NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno)); NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno));
(void) close(net); (void) closesocket(net);
[self release]; [self release];
return nil; return nil;
} }
@ -342,7 +352,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
if (getsockname(net, (struct sockaddr*)&sin, &size) < 0) if (getsockname(net, (struct sockaddr*)&sin, &size) < 0)
{ {
NSLog(@"unable to get socket name - %s", GSLastErrorStr(errno)); NSLog(@"unable to get socket name - %s", GSLastErrorStr(errno));
(void) close(net); (void) closesocket(net);
[self release]; [self release];
return nil; return nil;
} }
@ -360,7 +370,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (id) initForReadingAtPath: (NSString*)path - (id) initForReadingAtPath: (NSString*)path
{ {
int d = open([path fileSystemRepresentation], O_RDONLY|O_BINARY); int d = _open([path fileSystemRepresentation], O_RDONLY|O_BINARY);
if (d < 0) if (d < 0)
{ {
@ -378,7 +388,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (id) initForWritingAtPath: (NSString*)path - (id) initForWritingAtPath: (NSString*)path
{ {
int d = open([path fileSystemRepresentation], O_WRONLY|O_BINARY); int d = _open([path fileSystemRepresentation], O_WRONLY|O_BINARY);
if (d < 0) if (d < 0)
{ {
@ -396,7 +406,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (id) initForUpdatingAtPath: (NSString*)path - (id) initForUpdatingAtPath: (NSString*)path
{ {
int d = open([path fileSystemRepresentation], O_RDWR|O_BINARY); int d = _open([path fileSystemRepresentation], O_RDWR|O_BINARY);
if (d < 0) if (d < 0)
{ {
@ -465,47 +475,48 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (id) initWithNullDevice - (id) initWithNullDevice
{ {
self = [self initWithFileDescriptor: open("/dev/null", O_RDWR|O_BINARY) isNullDevice = YES;
closeOnDealloc: YES]; isStandardFile = YES;
if (self) descriptor = -1;
{
isNullDevice = YES;
}
return self; return self;
} }
- (id) initWithFileDescriptor: (int)desc closeOnDealloc: (BOOL)flag - (id) initWithFileDescriptor: (int)desc closeOnDealloc: (BOOL)flag
{ {
unsigned long dummy;
self = [super init]; self = [super init];
if (self) if (self)
{ {
#if 0 struct _stat sbuf;
struct stat sbuf;
// FIXME if (_fstat(desc, &sbuf) < 0)
if (fstat(desc, &sbuf) < 0)
{ {
NSLog(@"unable to get status of descriptor - %s", isStandardFile = NO;
GSLastErrorStr(errno));
[self release];
return nil;
} }
if (S_ISREG(sbuf.st_mode))
isStandardFile = YES;
else else
isStandardFile = NO; {
#else if (S_ISREG(sbuf.st_mode))
isStandardFile = NO; isStandardFile = YES;
#endif else
isStandardFile = NO;
}
dummy = 0; if (isStandardFile == NO)
if (ioctlsocket(desc, FIONBIO, &dummy) < 0) {
{ unsigned long dummy = 0;
NSLog(@"unable to get blocking mode - %s", GSLastErrorStr(errno));
/*
* This is probably a socket ... try
* using a socket specific call and see if that fails.
*/
if (ioctlsocket(desc, FIONBIO, &dummy) < 0)
{
NSLog(@"unable to get status/type of descriptor - %s",
GSLastErrorStr(errno));
[self release];
return nil;
}
wasNonBlocking = (dummy == 0) ? NO : YES; wasNonBlocking = (dummy == 0) ? NO : YES;
ioctlsocket(desc, FIONBIO, &dummy); // Reset }
}
isNonBlocking = wasNonBlocking; isNonBlocking = wasNonBlocking;
descriptor = desc; descriptor = desc;
@ -522,12 +533,14 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (id) initWithNativeHandle: (void*)hdl - (id) initWithNativeHandle: (void*)hdl
{ {
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: NO]; return [self initWithFileDescriptor: _open_osfhandle(hdl, 0)
closeOnDealloc: NO];
} }
- (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag - (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag
{ {
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: flag]; return [self initWithFileDescriptor: _open_osfhandle(hdl, 0)
closeOnDealloc: flag];
} }
- (void) checkAccept - (void) checkAccept
@ -632,7 +645,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (void*) nativeHandle - (void*) nativeHandle
{ {
return (void*)0; return _get_osfhandle(descriptor);
} }
// Synchronous I/O operations // Synchronous I/O operations
@ -864,7 +877,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
off_t result = -1; off_t result = -1;
if (isStandardFile && descriptor >= 0) if (isStandardFile && descriptor >= 0)
result = lseek(descriptor, 0, SEEK_CUR); result = _lseek(descriptor, 0, SEEK_CUR);
if (result < 0) if (result < 0)
{ {
[NSException raise: NSFileHandleOperationException [NSException raise: NSFileHandleOperationException
@ -879,7 +892,9 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
off_t result = -1; off_t result = -1;
if (isStandardFile && descriptor >= 0) if (isStandardFile && descriptor >= 0)
result = lseek(descriptor, 0, SEEK_END); {
result = _lseek(descriptor, 0, SEEK_END);
}
if (result < 0) if (result < 0)
{ {
[NSException raise: NSFileHandleOperationException [NSException raise: NSFileHandleOperationException
@ -894,7 +909,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
off_t result = -1; off_t result = -1;
if (isStandardFile && descriptor >= 0) if (isStandardFile && descriptor >= 0)
result = lseek(descriptor, (off_t)pos, SEEK_SET); result = _lseek(descriptor, (off_t)pos, SEEK_SET);
if (result < 0) if (result < 0)
{ {
[NSException raise: NSFileHandleOperationException [NSException raise: NSFileHandleOperationException
@ -916,7 +931,14 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
[self ignoreWriteDescriptor]; [self ignoreWriteDescriptor];
[self setNonBlocking: wasNonBlocking]; [self setNonBlocking: wasNonBlocking];
(void)close(descriptor); if (isStandardFile)
{
(void)_close(descriptor);
}
else
{
(void)closesocket(descriptor);
}
descriptor = -1; descriptor = -1;
acceptOK = NO; acceptOK = NO;
connectOK = NO; connectOK = NO;
@ -951,7 +973,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
- (void) truncateFileAtOffset: (unsigned long long)pos - (void) truncateFileAtOffset: (unsigned long long)pos
{ {
[self seekToFileOffset: pos]; _chsize(descriptor, pos);
} }
- (void) writeInBackgroundAndNotify: (NSData*)item forModes: (NSArray*)modes - (void) writeInBackgroundAndNotify: (NSData*)item forModes: (NSArray*)modes