1 .\" Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions are
7 .\" Redistributions of source code and documentation must retain the above
8 .\" copyright notice, this list of conditions and the following
11 .\" Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
18 .\" This product includes software developed or owned by Caldera
19 .\" International, Inc. Neither the name of Caldera International, Inc.
20 .\" nor the names of other contributors may be used to endorse or promote
21 .\" products derived from this software without specific prior written
24 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
25 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 .\" DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
35 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 .\" @(#)p2 8.1 (Berkeley) 6/8/93
46 When a C program is run as a command,
47 the arguments on the command line are made available
62 is the command name itself,
65 is always greater than 0.
67 The following program illustrates the mechanism:
68 it simply echoes its arguments
70 (This is essentially the
74 main(argc, argv) /* echo arguments */
80 for (i = 1; i < argc; i++)
81 printf("%s%c", argv[i], (i<argc-1) ? ' ' : '\en');
85 is a pointer to an array
86 whose individual elements are pointers to arrays of characters;
89 so they can be treated as strings.
90 The program starts by printing
92 and loops until it has printed them all.
94 The argument count and the arguments
97 If you want to keep them around so other
98 routines can get at them, you must
99 copy them to external variables.
101 The ``Standard Input'' and ``Standard Output''
103 The simplest input mechanism is to read the ``standard input,''
104 which is generally the user's terminal.
107 returns the next input character each time it is called.
108 A file may be substituted for the terminal by
125 instead of the terminal.
127 itself need know nothing about where its input
129 This is also true if the input comes from another program via
136 provides the standard input for
138 from the standard output of
144 when it encounters the end of file
146 on whatever you are reading.
149 is normally defined to be
151 but it is unwise to take any advantage
153 As will become clear shortly,
154 this value is automatically defined for you when
155 you compile a program,
156 and need not be of any concern.
162 on the ``standard output,''
163 which is also by default the terminal.
164 The output can be captured on a file
174 writes the standard output on
176 instead of the terminal.
178 is created if it doesn't exist;
179 if it already exists, its previous contents are overwritten.
180 And a pipe can be used:
184 puts the standard output of
186 into the standard input of
191 which formats output in various ways,
193 the same mechanism as
200 may be intermixed in any order;
201 the output will appear in the order of the calls.
203 Similarly, the function
205 provides for formatted input conversion;
206 it will read the standard input and break it
207 up into strings, numbers, etc.,
210 uses the same mechanism as
212 so calls to them may also be intermixed.
215 read only one input and write one output;
216 for such programs I/O
223 may be entirely adequate,
224 and it is almost always enough to get started.
225 This is particularly true if
228 pipe facility is used to connect the output of
229 one program to the input of the next.
230 For example, the following program
231 strips out all ascii control characters
233 (except for newline and tab).
237 main() /* ccstrip: strip non-graphic characters */
240 while ((c = getchar()) != EOF)
241 if ((c >= ' ' && c < 0177) || c == '\et' || c == '\en')
250 should appear at the beginning of each source file.
251 It causes the C compiler to read a file
252 .IT /usr/include/stdio.h ) (
254 standard routines and symbols
255 that includes the definition of
258 If it is necessary to treat multiple files,
261 to collect the files for you:
263 cat file1 file2 ... | ccstrip >output
265 and thus avoid learning how to access files from a program.
269 at the end is not necessary to make the program work
271 but it assures that any caller
272 of the program will see a normal termination status
274 from the program when it completes.
275 Section 6 discusses status returns in more detail.