To find out the names of open files in a process

John Chambers jc at minya.UUCP
Thu Feb 4 13:51:47 AEST 1988


In article <1427 at mips.mips.COM>, hansen at mips.COM (Craig Hansen) writes:
> In article <904 at thorin.cs.unc.edu>, gallmeis at wasp.cs.unc.edu (Bill Gallmeister) writes:
> > this kind of reminds me of someone who asked how to get the size of a malloc'ed block.
> > The answer?  "Remember how much you asked for, dimwit!"
> 
> Actually, if free() is going to be able to deallocate the space, the
> dimwit malloc() ought to be salting the size away somewhere, since
> free doesn't depend on the dimwit programmer telling it how big the
> block is. With that in mind, there's no conceivable reason why there
> isn't an msize() function for us comp.unix.dimwits.

There's another problem you've missed.  All too often, I find that I can't
remember how much I asked for, because I didn't ask for it.  True, if I
am writing the whole program, I can keep the size somewhere.  But suppose
I'm a library routine passed a pointer, and I want to make sure I don't
overflow the caller's data block.  The turkey didn't pass me its size, so
how the @#$*$&%^ am I supposed to discover it?

Of course, a good design would always pass a size along with a pointer.
Sure, and when you are writing your library sprintf() routine, do you
want to go out and modify all the calls in all the software in the world
so that there's a size parameter?  I mean, sprintf() is specified and 
cast in a huge chunk of concrete by now.  It doesn't include a size field.
How is a competent software engineer to guarantee that it doesn't overflow
the caller's array?

Open files are a similar problem.  You can say "Just remember the file
name, dimwit."  But if you're the poor library routine that has just read
garbage from a fd, and you want to give a sensible diagnostic rather than
the usual "garbage in input" sort of message, how DO you discover which
of the various character strings lying about in memory is the name of
that file?  Note that it's too late by now to request the file name from
the caller; the spec was already approved by management, and the name isn't
in your list of parameters.

Oh, well, maybe we should just be traditionalists, and not worry about
memory faults or useful diagnostics.


-- 
John Chambers <{adelie,ima,maynard,mit-eddie}!minya!{jc,root}> (617/484-6393)



More information about the Comp.unix.wizards mailing list