alp-ch02-writing-good-gnu-linux-software - 03 0430 CH02...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Writing Good GNU/Linux Software 2 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 28

alp-ch02-writing-good-gnu-linux-software - 03 0430 CH02...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online