alp-ch02-writing-good-gnu-linux-software.pdf
(
273 KB
)
Pobierz
../alp/advanced-linux-programming.pdf (3)
2
Writing Good GNU/Linux
Software
T
HIS CHAPTER COVERS SOME BASIC TECHNIQUES THAT MOST
GNU/Linux program-
mers use. By following the guidelines presented, you’ll be able to write programs that
work well within the GNU/Linux environment and meet GNU/Linux users’ expec-
tations of how programs should operate.
2.1 Interaction With the Execution Environment
When you first studied C or C++, you learned that the special
main
function is the
primary entry point for a program.When the operating system executes your pro-
gram, it automatically provides certain facilities that help the program communicate
with the operating system and the user.You probably learned about the two parame-
ters to
main
, usually called
argc
and
argv
, which receive inputs to your program.
You learned about the
stdout
and
stdin
(or the
cout
and
cin
streams in C++) that
provide console input and output.These features are provided by the C and C++
languages, and they interact with the GNU/Linux system in certain ways. GNU/
Linux provides other ways for interacting with the operating environment, too.
18
Chapter 2 Writing Good GNU/Linux Software
2.1.1 The Argument List
You run a program from a shell prompt by typing the name of the program.
Optionally, you can supply additional information to the program by typing one or
more words after the program name, separated by spaces.These are called
command-line
arguments
. (You can also include an argument that contains a space, by enclosing the
argument in quotes.) More generally, this is referred to as the program’s
argument list
because it need not originate from a shell command line. In Chapter 3, “Processes,”
you’ll see another way of invoking a program, in which a program can specify the
argument list of another program directly.
When a program is invoked from the shell, the argument list contains the entire
command line, including the name of the program and any command-line arguments
that may have been provided. Suppose, for example, that you invoke the
ls
command
in your shell to display the contents of the root directory and corresponding file sizes
with this command line:
% ls -s /
The argument list that the
ls
program receives has three elements.The first one is the
name of the program itself, as specified on the command line, namely
ls
.The second
and third elements of the argument list are the two command-line arguments,
-s
and
/
.
The
main
function of your program can access the argument list via the
argc
and
argv
parameters to
main
(if you don’t use them, you may simply omit them).The first
parameter,
argc
, is an integer that is set to the number of items in the argument list.
The second parameter,
argv
, is an array of character pointers.The size of the array is
argc
, and the array elements point to the elements of the argument list, as NUL-
terminated character strings.
Using command-line arguments is as easy as examining the contents of
argc
and
argv
. If you’re not interested in the name of the program itself, don’t forget to skip the
first element.
Listing 2.1 demonstrates how to use
argc
and
argv
.
Listing 2.1
(
arglist.c
) Using
argc
and
argv
#include <stdio.h>
int main (int argc, char* argv[])
{
printf (“The name of this program is ‘%s’.\n”, argv[0]);
printf (“This program was invoked with %d arguments.\n”, argc - 1);
/* Were any command-line arguments specified? */
if (argc > 1) {
/* Yes, print them. */
int i;
printf (“The arguments are:\n”);
for (i = 1; i < argc; ++i)
2.1 Interaction With the Execution Environment
19
printf (“ %s\n”, argv[i]);
}
return 0;
}
2.1.2 GNU/Linux Command-Line Conventions
Almost all GNU/Linux programs obey some conventions about how command-line
arguments are interpreted.The arguments that programs expect fall into two cate-
gories:
options
(or
flags
) and other arguments. Options modify how the program
behaves, while other arguments provide inputs (for instance, the names of input files).
Options come in two forms:
n
Short options
consist of a single hyphen and a single character (usually a lowercase
or uppercase letter). Short options are quicker to type.
n
Long options
consist of two hyphens, followed by a name made of lowercase and
uppercase letters and hyphens. Long options are easier to remember and easier
to read (in shell scripts, for instance).
Usually, a program provides both a short form and a long form for most options it
supports, the former for brevity and the latter for clarity. For example, most programs
understand the options
-h
and
--help
, and treat them identically. Normally, when a
program is invoked from the shell, any desired options follow the program name
immediately. Some options expect an argument immediately following. Many pro-
grams, for example, interpret the option
--output foo
to specify that output of the
program should be placed in a file named
foo
. After the options, there may follow
other command-line arguments, typically input files or input data.
For example, the command
ls -s /
displays the contents of the root directory.The
-s
option modifies the default behavior of
ls
by instructing it to display the size (in
kilobytes) of each entry.The
/
argument tells
ls
which directory to list.The
--size
option is synonymous with
-s
, so the same command could have been invoked as
ls --size /
.
The
GNU Coding Standards
list the names of some commonly used command-line
options. If you plan to provide any options similar to these, it’s a good idea to use the
names specified in the coding standards.Your program will behave more like other
programs and will be easier for users to learn.You can view the GNU Coding
Standards’ guidelines for command-line options by invoking the following from a shell
prompt on most GNU/Linux systems:
% info “(standards)User Interfaces”
20
Chapter 2 Writing Good GNU/Linux Software
2.1.3 Using
getopt_long
Parsing command-line options is a tedious chore. Luckily, the GNU C library provides
a function that you can use in C and C++ programs to make this job somewhat easier
(although still a bit annoying).This function,
getopt_long
, understands both short and
long options. If you use this function, include the header file
<getopt.h>
.
Suppose, for example, that you are writing a program that is to accept the three
options shown in Table 2.1.
Table 2.1
Example Program Options
Short Form
Long Form
Purpose
-h
--help
Display usage summary and exit
-o filename
--output filename
Specify output filename
-v
--verbose
Print verbose messages
In addition, the program is to accept zero or more additional command-line
arguments, which are the names of input files.
To use
getopt_long
, you must provide two data structures.The first is a character
string containing the valid short options, each a single letter. An option that requires
an argument is followed by a colon. For your program, the string
ho:v
indicates that
the valid options are
-h
,
-o
, and
-v
, with the second of these options followed by an
argument.
To specify the available long options, you construct an array of
struct option
ele-
ments. Each element corresponds to one long option and has four fields. In normal
circumstances, the first field is the name of the long option (as a character string, with-
out the two hyphens); the second is 1 if the option takes an argument, or 0 otherwise;
the third is
NULL
; and the fourth is a character constant specifying the short option
synonym for that long option.The last element of the array should be all zeros.You
could construct the array like this:
const struct option long_options[] = {
{ “help”, 0, NULL, ‘h’ },
{ “output”, 1, NULL, ‘o’ },
{ “verbose”, 0, NULL, ‘v’ },
{ NULL, 0, NULL, 0 }
};
You invoke the
getopt_long
function, passing it the
argc
and
argv
arguments to
main
,
the character string describing short options, and the array of
struct option
elements
describing the long options.
n
Each time you call
getopt_long
, it parses a single option, returning the short-
option letter for that option, or –1 if no more options are found.
n
Typically, you’ll call
getopt_long
in a loop, to process all the options the user has
specified, and you’ll handle the specific options in a switch statement.
2.1 Interaction With the Execution Environment
21
n
If
getopt_long
encounters an invalid option (an option that you didn’t specify as
a valid short or long option), it prints an error message and returns the character
?
(a question mark). Most programs will exit in response to this, possibly after
displaying usage information.
n
When handling an option that takes an argument, the global variable
optarg
points to the text of that argument.
n
After
getopt_long
has finished parsing all the options, the global variable
optind
contains the index (into
argv
) of the first nonoption argument.
Listing 2.2 shows an example of how you might use
getopt_long
to process your
arguments.
Listing 2.2
(
getopt_long.c
) Using
getopt_long
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
/* The name of this program. */
const char* program_name;
/* Prints usage information for this program to STREAM (typically
stdout or stderr), and exit the program with EXIT_CODE. Does not
return. */
void print_usage (FILE* stream, int exit_code)
{
fprintf (stream, “Usage: %s options [ inputfile ... ]\n”, program_name);
fprintf (stream,
“ -h --help Display this usage information.\n”
“ -o --output filename Write output to file.\n”
“ -v --verbose Print verbose messages.\n”);
exit (exit_code);
}
/* Main program entry point. ARGC contains number of argument list
elements; ARGV is an array of pointers to them. */
int main (int argc, char* argv[])
{
int next_option;
/* A string listing valid short options letters. */
const char* const short_options = “ho:v”;
/* An array describing valid long options. */
const struct option long_options[] = {
{ “help”, 0, NULL, ‘h’ },
{ “output”, 1, NULL, ‘o’ },
{ “verbose”, 0, NULL, ‘v’ },
continues
Plik z chomika:
musli_com
Inne pliki z tego folderu:
advanced-linux-programming(1).pdf
(3775 KB)
alp-apA-other-development-tools(2).pdf
(265 KB)
alp-apB-low-level-io(1).pdf
(246 KB)
alp-apC-signal-table(2).pdf
(173 KB)
alp-apD-online-resources(1).pdf
(144 KB)
Inne foldery tego chomika:
1_Security
2_Hack
3_Cryptography
4_Telecommunications
5_VoIP
Zgłoś jeśli
naruszono regulamin