5.1 How do I compare strings using wildcards?
The answer to that depends on what exactly you mean by `wildcards'.
There are two quite different concepts that qualify as `wildcards'. They are:
These are what the shell uses for filename expansion (`globbing').
These are used by editors,
grep, etc. for matching text, but they normally aren't applied to filenames.
fnmatch()to do filename matching. This generally allows only the Bourne shell style of pattern; i.e. it recognises `*', `[...]' and `?', but probably won't support the more arcane patterns available in the Korn and Bourne-Again shells.
If you don't have this function, then rather than reinvent the wheel, you are probably better off snarfing a copy from the BSD or GNU sources.
Also, for the common cases of matching actual filenames, look for
glob(), which will find all existing files matching a pattern.
ed, sometimes known as `Basic Regular Expressions', and the one recognised by
egrep, `Extended Regular Expressions'. Perl has it's own slightly different flavour, as does Emacs.
To support this multitude of formats, there is a corresponding multitude of implementations. Systems will generally have regexp-matching functions (usually
regexec()) supplied, but be wary; some systems have more than one implementation of these functions available, with different interfaces. In addition, there are many library implementations available. (It's common, BTW, for regexps to be compiled to an internal form before use, on the assumption that you may compare several separate strings against the same regexp.)
One library available for this is the `rx' library, available from the GNU mirrors. This seems to be under active development, which may be a good or a bad thing depending on your point of view :-)
WARNING: Some versions of UCB Mail may execute commands prefixed by `~!' or `~|' given in the message body even in non-interactive mode. This can be a security risk.
Invoked as `mail -s 'subject' recipients...' it will take a message body on standard input, and supply a default header (including the specified subject), and pass the message to
This example mails a test message to
rooton the local system:
#define MAILPROG "/bin/mail"
FILE *mail = popen(MAILPROG " -s 'Test Message' root", "w");
fprintf(mail, "This is a test.\n");
fprintf(stderr, "mail failed!\n");
}If the text to be sent is already in a file, then one can do:
system(MAILPROG " -s 'file contents' root </tmp/filename");These methods can be extended to more complex cases, but there are many pitfalls to watch out for:
- If using system() or popen(), you must be very careful about quoting arguments to protect them from filename expansion or word splitting
- Constructing command lines from user-specified data is a common source of buffer-overrun errors and other security holes
- This method does not allow for CC: or BCC: recipients to be specified (some versions of /bin/mail may allow this, some do not)
sendmail. There are other MTAs in use, such as
MMDF, but these generally include a program that emulates the usual behaviour of
sendmailhas usually been found in `/usr/lib', but the current trend is to move library programs out of `/usr/lib' into directories such as `/usr/sbin' or `/usr/libexec'. As a result, one normally invokes
sendmailby its full path, which is system-dependent.
To understand how
sendmailbehaves, it's useful to understand the concept of an envelope. This is very much like paper mail; the envelope defines who the message is to be delivered to, and who it is from (for the purpose of reporting errors). Contained in the envelope are the headers, and the body, separated by a blank line. The format of the headers is specified primarily by RFC 822; see also the MIME RFCs.
There are two main ways to use
sendmailto originate a message: either the envelope recipients can be explicitly supplied, or
sendmailcan be instructed to deduce them from the message headers. Both methods have advantages and disadvantages.
To Look for similar posts on File handling in Linux explore the following links from the same blog as well.