mirror of
https://github.com/gnustep/libs-base.git
synced 2025-06-02 01:21:08 +00:00
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:
parent
927e64252e
commit
14c68f4793
3 changed files with 71 additions and 18 deletions
|
@ -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>
|
2001-07-02 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
* Source/NSString.m: Never init nocopy with null pointer - increase
|
* Source/NSString.m: Never init nocopy with null pointer - increase
|
||||||
|
|
|
@ -200,7 +200,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
|
||||||
|
|
||||||
if (theFile == NULL) /* We failed to open the file. */
|
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;
|
goto failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,6 +226,56 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
|
||||||
goto failure;
|
goto failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Rewind the file pointer to the beginning, preparing to read in
|
||||||
|
* the file.
|
||||||
|
*/
|
||||||
|
c = fseek(theFile, 0L, SEEK_SET);
|
||||||
|
if (c != 0)
|
||||||
|
{
|
||||||
|
NSLog(@"Fseek to start of file failed - %s", GSLastErrorStr(errno));
|
||||||
|
goto failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileLength == 0)
|
||||||
|
{
|
||||||
|
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
|
#if GS_WITH_GC == 1
|
||||||
tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength);
|
tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength);
|
||||||
#else
|
#else
|
||||||
|
@ -237,23 +288,13 @@ readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
|
||||||
goto failure;
|
goto failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Rewind the file pointer to the beginning, preparing to read in
|
|
||||||
* the file.
|
|
||||||
*/
|
|
||||||
c = fseek(theFile, 0L, SEEK_SET);
|
|
||||||
if (c != 0)
|
|
||||||
{
|
|
||||||
NSLog(@"Fseek to start of file failed - %s", GSLastErrorStr(errno));
|
|
||||||
goto failure;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = fread(tmp, 1, fileLength, theFile);
|
c = fread(tmp, 1, fileLength, theFile);
|
||||||
if (c != fileLength)
|
if (c != fileLength)
|
||||||
{
|
{
|
||||||
NSLog(@"read of file contents failed - %s", GSLastErrorStr(errno));
|
NSLog(@"read of file contents failed - %s", GSLastErrorStr(errno));
|
||||||
goto failure;
|
goto failure;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
*buf = tmp;
|
*buf = tmp;
|
||||||
*len = fileLength;
|
*len = fileLength;
|
||||||
|
|
|
@ -8,6 +8,12 @@ int main ()
|
||||||
id pool = [NSAutoreleasePool new];
|
id pool = [NSAutoreleasePool new];
|
||||||
id o = [NSObject new];
|
id o = [NSObject new];
|
||||||
printf ("Hello from object at 0x%x\n", (unsigned)[o self]);
|
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);
|
exit (0);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue