Implementation
We conclude this tutorial with a note regarding implementation.
The function expand_wildcards
might have been implemented as follows:
void Path::expand_wildcards(List<Path> & ret) {
const char *tmp = tmpnam();
ostrstream o;
o << "ls " << *this << " > " << tmp <<ends;
system(o.str());
ifstream f(tmp);
String s;
ret.make_empty();
while (f >> s)
ret.put(s);
}
First the shell command ls pattern>tmp\fR
is sent to the shell to perform the actual pattern expansion,
then the file containing
the results is read and the resulting list is constructed.
This implementation has
several problems:
-
It is slow. Invoking system requires starting
up an entire subshell.
-
It is unreliable. It requires that ls
be somewhere along the current search path, and further that the ls
found have the appropriate behavior. If the user has installed a local
ls, there is no guarantee the above function will
work correctly.
-
It uses /bin/sh, which is the C shell on BSD systems, and the Bourne
shell on most other systems.
Using different shells on different machines means that
the programmer cannot be guaranteed that patterns
will be expanded in the same way
across all machines.
Also, the pattern matching capabilities of both the C shell
and the Bourne shell are inferior to those of the Korn shell.
To overcome these problems, the actual implementation of
expand_wildcards steals code from the Korn shell. The resulting
implementation is fast, reliable, and (of course) Korn shell-based.
Acknowledgement Special thanks to Dave Korn for his patient
explanations of the inner workings of the Korn shell.
Previous topic:
Wildcards
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004