From 14c68f47938c74c249643f908dcbb8ba529942e5 Mon Sep 17 00:00:00 2001 From: CaS Date: Sat, 7 Jul 2001 04:36:01 +0000 Subject: [PATCH] Hack to read /proc filesystem git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@10311 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++++ Source/NSData.m | 75 ++++++++++++++++++++++++++++++++++++++----------- Testing/basic.m | 8 +++++- 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index bd5074ff2..b9992f95e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2001-07-07 Richard Frith-Macdonald + + * Source/NSData.m: readContentsOfFile() hack added to permit reading + of files like those in the /proc filesystem which don't support + lengths. + 2001-07-02 Richard Frith-Macdonald * Source/NSString.m: Never init nocopy with null pointer - increase diff --git a/Source/NSData.m b/Source/NSData.m index e2085dd3b..7dd5b52ab 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -200,7 +200,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone) if (theFile == NULL) /* We failed to open the file. */ { - NSDebugLog(@"Open (%s) attempt failed - %s", thePath, GSLastErrorStr(errno)); + NSDebugLog(@"Open (%s) attempt failed - %s", thePath, + GSLastErrorStr(errno)); goto failure; } @@ -225,18 +226,6 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone) goto failure; } -#if GS_WITH_GC == 1 - tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength); -#else - tmp = NSZoneMalloc(zone, fileLength); -#endif - if (tmp == 0) - { - NSLog(@"Malloc failed for file of length %d- %s", - fileLength, GSLastErrorStr(errno)); - goto failure; - } - /* * Rewind the file pointer to the beginning, preparing to read in * the file. @@ -248,11 +237,63 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone) goto failure; } - c = fread(tmp, 1, fileLength, theFile); - if (c != fileLength) + if (fileLength == 0) { - NSLog(@"read of file contents failed - %s", GSLastErrorStr(errno)); - goto failure; + unsigned char buf[BUFSIZ]; + + /* + * Special case ... a file of length zero may be a named pipe or some + * file in the /proc filesystem, which will return us data if we read + * from it ... so we try reading as much as we can. + */ + while ((c = fread(buf, 1, BUFSIZ, theFile)) != 0) + { + if (tmp == 0) + { +#if GS_WITH_GC == 1 + tmp = NSZoneMalloc(GSAtomicMallocZone(), c); +#else + tmp = NSZoneMalloc(zone, c); +#endif + } + else + { +#if GS_WITH_GC == 1 + tmp = NSZoneRealloc(GSAtomicMallocZone(), tmp, fileLength + c); +#else + tmp = NSZoneRealloc(zone, tmp, fileLength + c); +#endif + } + if (tmp == 0) + { + NSLog(@"Malloc failed for file of length %d - %s", + fileLength + c, GSLastErrorStr(errno)); + goto failure; + } + memcpy(tmp + fileLength, buf, c); + fileLength += c; + } + } + else + { +#if GS_WITH_GC == 1 + tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength); +#else + tmp = NSZoneMalloc(zone, fileLength); +#endif + if (tmp == 0) + { + NSLog(@"Malloc failed for file of length %d - %s", + fileLength, GSLastErrorStr(errno)); + goto failure; + } + + c = fread(tmp, 1, fileLength, theFile); + if (c != fileLength) + { + NSLog(@"read of file contents failed - %s", GSLastErrorStr(errno)); + goto failure; + } } *buf = tmp; diff --git a/Testing/basic.m b/Testing/basic.m index d0674252a..08d170f5b 100644 --- a/Testing/basic.m +++ b/Testing/basic.m @@ -6,8 +6,14 @@ int main () { id pool = [NSAutoreleasePool new]; - id o = [NSObject new]; + id o = [NSObject new]; printf ("Hello from object at 0x%x\n", (unsigned)[o self]); + + o = [NSString stringWithFormat: @"/proc/%d/status", getpid()]; + NSLog(@"'%@'", o); + o = [NSString stringWithContentsOfFile: o]; + NSLog(@"'%@'", o); + exit (0); } #else