[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Library bindings or os.execute?
- From: Sean Conner <sean@...>
- Date: Thu, 15 Jun 2017 13:18:40 -0400
It was thus said that the Great Parke once stated:
> On Tue, Jun 13, 2017 at 11:35 PM, Sean Conner <email@example.com> wrote:
> > Several years ago, I starting writing a program to index all the files on
> > my computer (a Linux system). Part of this indexing involved classifying
> > the type of file and for that, Linux came with a program called 'file' that
> > does that. It supported the option "-f" to read filenames from stdin and it
> > would write the file type (I used the "-i" option to print out mime types).
> > So my thought was: I can invoke "file", feeding it filenames in stdin,
> > and reading the file types on stdout. Only issue was: popen() (and by
> > extension, io.popen() but this was before I got into Lua) only supports
> > reading or writing (but not both). I was trying to avoid having to generate
> > a list prior (1,499,996 entries---probably more now) to generating the
> > types.
> > "Not a problem," thought I. "I wrote a Unix shell as a college project,
> > I know how to execute a program with redirection." And so I did a
> > bi-directional "popen()" function---
> > ---which failed miserably.
> Why do you need bi-directional popen for this task?
> for entry in io.popen 'find / | file -f -' : lines () do
> print ( entry ) end
> A similar approach should work in C.
The main reason is I didn't think of that. A secondary reason was that I
was doing a bit more than just calling 'file' on each entry (I was also
calling stat() and recording the metainformation so that could be indexed as