Hack to read /proc filesystem

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@10311 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2001-07-07 04:36:01 +00:00
parent 8c8afa6b29
commit 7d3ac56536
3 changed files with 71 additions and 18 deletions

View file

@ -1,3 +1,9 @@
2001-07-07 Richard Frith-Macdonald <rfm@gnu.org>
* 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 <rfm@gnu.org>
* Source/NSString.m: Never init nocopy with null pointer - increase

View file

@ -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;

View file

@ -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