Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   wordfree.3p    ( 3 )

выполнять расширения слов (perform word expansions)

Обоснование (Rationale)

This function was included as an alternative to glob(). There had been continuing controversy over exactly what features should be included in glob(). It is hoped that by providing wordexp() (which provides all of the shell word expansions, but which may be slow to execute) and glob() (which is faster, but which only performs pathname expansion, without tilde or parameter expansion) this will satisfy the majority of applications.

While wordexp() could be implemented entirely as a library routine, it is expected that most implementations run a shell in a subprocess to do the expansion.

Two different approaches have been proposed for how the required information might be presented to the shell and the results returned. They are presented here as examples.

One proposal is to extend the echo utility by adding a -q option. This option would cause echo to add a <backslash> before each <backslash> and <blank> that occurs within an argument. The wordexp() function could then invoke the shell as follows:

(void) strcpy(buffer, "echo -q"); (void) strcat(buffer, words); if ((flags & WRDE_SHOWERR) == 0) (void) strcat(buffer, "2>/dev/null"); f = popen(buffer, "r");

The wordexp() function would read the resulting output, remove unquoted <backslash> characters, and break into words at unquoted <blank> characters. If the WRDE_NOCMD flag was set, wordexp() would have to scan words before starting the subshell to make sure that there would be no command substitution. In any case, it would have to scan words for unquoted special characters.

Another proposal is to add the following options to sh:

-w wordlist This option provides a wordlist expansion service to applications. The words in wordlist shall be expanded and the following written to standard output:

1. The count of the number of words after expansion, in decimal, followed by a null byte

2. The number of bytes needed to represent the expanded words (not including null separators), in decimal, followed by a null byte

3. The expanded words, each terminated by a null byte

If an error is encountered during word expansion, sh exits with a non-zero status after writing the former to report any words successfully expanded

-P Run in ``protected'' mode. If specified with the -w option, no command substitution shall be performed.

With these options, wordexp() could be implemented fairly simply by creating a subprocess using fork() and executing sh using the line:

execl(<shell path>, "sh", "-P", "-w", words, (char *)0);

after directing standard error to /dev/null.

It seemed objectionable for a library routine to write messages to standard error, unless explicitly requested, so wordexp() is required to redirect standard error to /dev/null to ensure that no messages are generated, even for commands executed for command substitution. The WRDE_SHOWERR flag can be specified to request that error messages be written.

The WRDE_REUSE flag allows the implementation to avoid the expense of freeing and reallocating memory, if that is possible. A minimal implementation can call wordfree() when WRDE_REUSE is set.