File Handling in Linux 8

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:

Filename patterns
These are what the shell uses for filename expansion (`globbing').
Regular Expressions
These are used by editors, grep, etc. for matching text, but they normally aren't applied to filenames.

5.1.1 How do I compare strings using filename patterns?

Unless you are unlucky, your system should have a function 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.

5.1.2 How do I compare strings using regular expressions?

There are a number of slightly different syntaxes for regular expressions; most systems use at least two: the one recognised by 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 regcomp() and 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 :-)

5.2 What's the best way to send mail from a program?

There are several ways to send email from a Unix program. Which is the best method to use in a given situation varies, so I'll present two of them. A third possibility, not covered here, is to connect to a local SMTP port (or a smarthost) and use SMTP directly; see RFC 821.

5.2.1 The simple method: /bin/mail

For simple applications, it may be sufficient to invoke mail (usually `/bin/mail', but could be `/usr/bin/mail' on some systems).
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 sendmail for delivery.
This example mails a test message to root on the local system:
#include <stdio.h>
 
#define MAILPROG "/bin/mail"
 
int main()
{
    FILE *mail = popen(MAILPROG " -s 'Test Message' root", "w");
    if (!mail)
    {
        perror("popen");
        exit(1);
    }
 
    fprintf(mail, "This is a test.\n");
 
    if (pclose(mail))
    {
        fprintf(stderr, "mail failed!\n");
        exit(1);
    }
}
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)

5.2.2 Invoking the MTA directly: /usr/lib/sendmail

The mail program is an example of a Mail User Agent, a program intended to be invoked by the user to send and receive mail, but which does not handle the actual transport. A program for transporting mail is called an MTA, and the most commonly found MTA on Unix systems is called sendmail. There are other MTAs in use, such as MMDF, but these generally include a program that emulates the usual behaviour of sendmail.
Historically, sendmail has 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 sendmail by its full path, which is system-dependent.
To understand how sendmail behaves, 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 sendmail to originate a message: either the envelope recipients can be explicitly supplied, or sendmail can 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.
 
 
 

0 comments:

Post a Comment