﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-选择java 进入自由开放的国度-随笔分类-Reading</title><link>http://www.blogjava.net/soochow_hhb/category/5173.html</link><description>&lt;style&gt;
#glowtext{
filter:glow(color=green,strength=2);width:100%;
}
&lt;/style&gt;
&lt;span id="glowtext"&gt;
&lt;script language="JavaScript1.2"&gt;
var message=" 具有丰富知识和经验的人，比只有一种知识和经验的人更容易产生新的联想和独到的见解。"
var neonbasecolor="#FF930D"
var neontextcolor="#00B3FD"
var flashspeed=300  //in milliseconds

var n=0
if (document.all){
document.write('&lt;font color="'+neonbasecolor+'"&gt;')
for (m=0;m&lt;message.length;m++)
document.write('&lt;span id="neonlight" class="content"&gt;'+message.charAt(m)+'&lt;/span&gt;')
document.write('&lt;/font&gt;')

//cache reference to neonlight array
var tempref=document.all.neonlight
}
else
document.write(message)

function neon(){

//Change all letters to base color
if (n==0){
for (m=0;m&lt;message.length;m++)
tempref[m].style.color=neonbasecolor
}

//cycle through and change individual letters to neon color
tempref[n].style.color=neontextcolor

if (n&lt;tempref.length-1)
n++
else{
n=0
clearInterval(flashing)
setTimeout("beginneon()",1500)
return
}
}

function beginneon(){
if (document.all)
flashing=setInterval("neon()",flashspeed)
}
beginneon()

&lt;/script&gt;
&lt;/span&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 11:05:43 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 11:05:43 GMT</pubDate><ttl>60</ttl><item><title>Getting Started with CGI Programming in C (转，译)</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/06/26/55055.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 26 Jun 2006 00:41:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/06/26/55055.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/55055.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/06/26/55055.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/55055.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/55055.html</trackback:ping><description><![CDATA[
		<div>
				<cite>
						<a href="http://www.cs.tut.fi/%7Ejkorpela/indexen.html">IT and communication</a>
				</cite>:
<cite><a href="http://www.cs.tut.fi/%7Ejkorpela/www.html">Web authoring and surfing</a></cite>:
<cite><a href="http://www.cs.tut.fi/%7Ejkorpela/forms/index.html">Forms</a></cite>:</div>
		<h1>
Getting Started with CGI Programming in C
</h1>
		<p class="summary">
This is an introduction to writing CGI programs in the
<a href="http://www.eskimo.com/%7escs/C-faq/top.html" title="comp.lang.c Frequently Asked Questions">C language</a>.
The reader is assumed to know the basics of C as well how
to write simple
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/index.html" title="Annotated links to tutorials, references, and specialized documents about HTML forms">forms</a>
in
<a title="Getting Started with HTML; a primer by J. Korpela" href="http://www.cs.tut.fi/%7Ejkorpela/html-primer.html">HTML</a>
and to be able to install CGI scripts on
a Web server.
The principles are illustrated with very simple examples.
</p>
		<div class="warning">
Two important warnings:
<ul><li>
To avoid wasting your time, please check--from applicable local
documents or by contacting local webmaster--whether you can
install and run CGI scripts written in C on the server.
At the same time, please check how to do that in detail--specifically,
where you need to put your CGI scripts.
</li><li>This document was written to illustrate the idea of CGI
scripting to C programmers. In practice, CGI programs are usually
written in other languages, such as
<a href="http://www.cs.tut.fi/%7Ejkorpela/perl/index.html">Perl</a>,  and for good reasons:
except for very simple cases, CGI programming in C is clumsy
and error-prone.
</li></ul></div>
		<h2>Content</h2>
		<ul>
				<li>
						<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/cgic.html#why">Why CGI programming?</a>
				</li>
				<li>
						<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/cgic.html#what">So what is CGI programming?</a>
				</li>
				<li>
						<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/cgic.html#get">How to process a simple form</a>
				</li>
				<li>
						<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/cgic.html#post">How to process a form with <code>METHOD="POST"</code></a>
				</li>
		</ul>
		<h2>
				<a name="why">Why CGI programming?</a>
		</h2>
		<p>
As my document
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/index.html" title="Annotated links to tutorials, references, and specialized documents about HTML forms"><cite>How to write HTML forms</cite></a> briefly explains,
you need a <em>server side-script</em> in order to use HTML
forms reliably. Typically there are simple server-side scripts
available
for
simple, common ways of processing form submissions, such as sending
the data in text format by E-mail to a specified address.
</p>
		<p>
But for more advanced processing, such as collecting data into
a file or database, or retrieving information and sending it back,
or doing some calculations with the submitted data, you will
probably need to write a server-side script of your own.
</p>
		<p>
CGI is simply an <em>interface</em> between HTML forms and server-side
scripts. It is not the only possibility--see the excellent tutorial
<a href="http://www.garshol.priv.no/download/text/http-tut.html"><cite>How the web works: HTTP and CGI explained</cite></a>
by
<a href="http://www.garshol.priv.no/">Lars Marius Garshol</a>
for both an introduction to the concepts of CGI and notes on other
possibilities. But CGI is widely used and useable.
</p>
		<p>
If someone suggests using JavaScript as an <em>alternative</em> to CGI,
ask him to read my
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/javascript.html"><cite>JavaScript and HTML: possibilities and caveats</cite></a>.
Briefly, JavaScript is inherently unreliable at least if not "backed up"
with server-side scripting.

</p>
		<h2>
				<a name="what">So what is CGI programming?</a>
		</h2>
		<p>
The above-mentioned 
<a href="http://www.garshol.priv.no/download/text/http-tut.html"><cite>How the web works: HTTP and CGI explained</cite></a>
is a great tutorial. There are some shorter introductions like
<a href="http://www.virtualville.com/library/cgi.html"><cite>Introduction to the Common Gateway Interface (CGI)</cite></a>
in the
<a href="http://www.virtualville.com/library.html">Virtualville Library</a>.
The following introduction of mine is just another attempt to present
the basics; please consult other sources if you get confused or need
more information.
</p>
		<p>
				<a name="simpleform">Let us consider the following simple HTML form:</a>
		</p>
		<pre>
				<code class="html">&lt;FORM ACTION="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/mult.cgi"&gt;<br />&lt;P&gt;Please specify the multiplicands:<br />&lt;INPUT NAME="m" SIZE="5"&gt;<br />&lt;INPUT NAME="n" SIZE="5"&gt;&lt;BR&gt;<br />&lt;INPUT TYPE="SUBMIT" VALUE="Multiply!"&gt;<br />&lt;/FORM&gt;<br /></code>
		</pre>
		<p>It will look like the following on your current browser:
</p>
		<form action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/mult.cgi">
				<div>Please specify the multiplicands:
<input name="m" size="5" /><input name="n" size="5" /><br /><input value="Multiply!" type="submit" /></div>
		</form>
		<p>You can try it if you like. Just in case the server used isn't
running and accessible when you try it, here's what you would get
as the result:
</p>
		<blockquote>
				<h3>Multiplication results</h3>
				<p>The product of 4 and 9 is 36.
</p>
		</blockquote>
		<p>
What we discuss here is <em>how</em> it works.
</p>
		<p>
Assume that you type <kbd>4</kbd> into one input field and
<kbd>9</kbd> into another and then invoke submission (typically,
by clicking on a submit button), your browser will
send, by HTTP, a request to the server at
<code>www.cs.tut.fi</code>. The browser pick up this server name
from the value of <code>ACTION</code> attribute where it
occurs as the host name part of a URL.
(Quite often the <code>ACTION</code> attribute refers, often using
a relative URL, to a script on the same server as the document
resides on, but this is not necessary, as this example shows.)
</p>
		<p>
When sending the request, the browser provides additional information,
specifying a relative URL, in this case<br /><code>/cgi-bin/run/~jkorpela/mult.cgi?m=4&amp;n=9</code><br />
This was constructed from that part of the <code>ACTION</code> value
which follows the host name, by appending a question mark
<code>?</code> and the form data in
<a href="http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type" title="Specification of the application/x-www-form-urlencoded encoding">a specifically encoded format</a>.
</p>
		<p>
The server to which the request was sent
(in this case, <code>www.cs.tut.fi</code>)
will then process
it according to its own rules. Typically, the server's configuration
defines how the relative URLs are mapped to file names and which
directories/folders are interpreted as containing CGI scripts.
As you may guess, the part <code>cgi-bin/</code> in the URL causes
such interpretation in this case. This means that instead of just
picking up and
sending back (to the browser which sent the request) an HTML document
or some other file, the server <em>invokes</em> a script or a program
specified in the URL (<code>mult.cgi</code> in this case) and passes
some data to it (the data <code>m=4&amp;n=9</code> in this case).</p>
		<p>
				<small>It depends on the server how this really happens.
In this particular case, the server actually runs the (executable)
program in the file <code>mult.cgi</code> in the subdirectory
<code>cgi-bin</code> of user
<code>jkorpela</code>'s home directory. It could be something
quite different, depending on server configuration.</small>
		</p>
		<p>
The often-mysticized abbreviation <dfn>CGI</dfn>, for
<dfn>Common Gateway Interface</dfn>, refers just to a convention on how
the invocation and parameter passing takes place in detail.
<dfn>Invocation</dfn> means different things in different cases.
For a <a href="http://www.cs.tut.fi/%7Ejkorpela/perl/index.html">Perl</a> script, the server would invoke a Perl interpreter and
make it execute the script in an interpretive manner. For an
executable program, which has typically been produced by a compiler
and a loader from a source program in a language like C, it would
just be started as a separate process.
Although the word <em>script</em> typically suggests that the code
is interpreted, the term <dfn>CGI script</dfn> refers both to such
scripts and to executable programs.
See
<a href="http://www.webthing.com/tutorials/cgifaq.1.html#2">answer to question <i>Is it a script or a program?</i></a>
in
<a href="http://www.webthing.com/tutorials/cgifaq.html"><cite>CGI Programming FAQ</cite></a> by
Nick Kew.</p>
		<p class="warning">
				<a name="compile">You need to <strong>compile and load</strong></a>
your C program on the
<strong>server</strong> (or, in principle, on a system with the
same architecture, so that binaries produced for it are executable
on the server too).</p>
		<p> And you need to put the executable into
a suitable directory and name it according to server-specific conventions
For example, if the server runs some flavor of Unix and has the
Gnu C compiler available, you would typically use a compilation
command like <code>gcc -o mult.cgi mult.c</code> and
then move (<code>mv</code>) <code>mult.c</code> to a directory with
a name like <code>cgi-bin</code>. But you really need to check local
instructions for such issues.</p>
		<p>
				<a name="ext">The <strong>filename extension <code>.cgi</code></strong></a>
has no fixed meaning in general. But there can be <em>server-dependent</em>
(and operating system dependent) rules for naming executable files.
<em>Typical</em> extensions for executables are <code>.cgi</code>
and <code>.exe</code>.</p>
		<h2>
				<a name="get">How to process a simple form</a>
		</h2>
		<p class="important">For forms which use <code>METHOD="GET"</code> (as our
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/cgic.html#simpleform">simple example</a> above uses, since
this is the default),
CGI specifications say that the data is passed to
the script or program in an environment variable called
<code>QUERY_STRING</code>.
</p>
		<p>
It depends on the scripting or programming
language used how a program can access the value of an environment
variable. In the C language, you would use the
library function <code>getenv</code> (defined in the
standard library <code>stdlib</code>) to access the value as
a string. You might then use various techniques to pick up data
from the string, convert parts of it to numeric values, etc.
</p>
		<p>
The <em>output</em> from the script or program to "primary output
stream" (such as <code>stdin</code> in the C language) is handled
in a special way. Effectively, it is directed so that it gets sent
back to the browser. Thus, by writing a C program that it writes
an HTML document onto its standard output, you will make that document
appear on user's screen as a response to the form submission.
</p>
		<p>
In this case, <a href="http://www.cs.tut.fi/%7Ejkorpela/forms/mult.c" title="The source program as a separate document">the source program</a>
in C is the following:
</p>
		<pre>
				<code class="C">#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />int main(void)<br />{<br />char *data;<br />long m,n;<br />printf("%s%c%c\n",<br />"Content-Type:text/html;charset=iso-8859-1",13,10);<br />printf("&lt;TITLE&gt;Multiplication results&lt;/TITLE&gt;\n");<br />printf("&lt;H3&gt;Multiplication results&lt;/H3&gt;\n");<br />data = getenv("QUERY_STRING");<br />if(data == NULL)<br />  printf("&lt;P&gt;Error! Error in passing data from form to script.");<br />else if(sscanf(data,"m=%ld&amp;n=%ld",&amp;m,&amp;n)!=2)<br />  printf("&lt;P&gt;Error! Invalid data. Data must be numeric.");<br />else<br />  printf("&lt;P&gt;The product of %ld and %ld is %ld.",m,n,m*n);<br />return 0;<br />}<br /></code>
		</pre>
		<p>
				<small>As a disciplined programmer, you have probably noticed
that the program makes no check against integer overflow, so it
will return bogus results for very large operands. In real life,
such checks would be needed, but such considerations would take us
too far from our topic.</small>
		</p>
		<p>Note: The first <code>printf</code> function call prints out
data which will be sent by the server as an HTTP header.
This is required for several reasons, including the fact that
a CGI script can send any data (such as an image or a plain text file)
to the browser, not just HTML documents.
For HTML documents, you can just use the <code>printf</code> function
call above as such; however, if your
<a href="http://www.cs.tut.fi/%7Ejkorpela/chars.html" title="A tutorial on character code issues">character encoding</a> is different from ISO 8859-1 (ISO Latin 1),
which is the most common on the Web, you need to replace
<code>iso-8859-1</code> by the
<a href="ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets" title="Official (IANA) registry of &quot;charset&quot; names">registered name of the encoding ("charset") you use</a>.

</p>
		<p>I have compiled this program and saved the executable program
under the name <code>mult.cgi</code> in my directory for CGI scripts
at <code>www.cs.tut.fi</code>.
This implies that <em>any</em>
form with 
<code>action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/mult.cgi"</code>
will, when submitted, be processed by that program.</p>
		<p class="warning">As a consequence, anyone could write a form
of his own with the same <code>ACTION</code> attribute and pass
whatever data he likes to my program. Therefore, the program
needs to be able to <strong>handle any data</strong>.
Generally, you need to check the data before starting to process it.
</p>
		<h2>
				<a name="post">How to process a form with <code>METHOD="POST"</code></a>
		</h2>
		<p>Let us consider next a different processing for form data.
Assume that we wish to write a form which takes a line of text
as input so that the form data is sent to
a CGI script which
<em>appends the data to a text file</em> on the server.
(That text file could be readable by the author of the form and the
script only, or it could be made readable to the world through another
script.)
</p>
		<p>It might seem that the problem is similar to the
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/cgic.html#get">example considered above</a>; one would just need
a different form and a different script (program).
But in fact, there is a difference. The example above can be regarded
as a "pure query" which does not change the "state of the world",
and in particular it is "idempotent", i.e. the same form data could
be submitted as many times as you like without causing any problems
(except minor waste of resources). But our current task needs
to cause such changes--a change in the content of a file which is
intended to be more or less permanent. Therefore, one should use
<code>METHOD="POST"</code>. This is explained in more detail in
the document
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/methods.html"><cite>Methods <code>GET</code> and <code>POST</code> in HTML forms - what's the difference?</cite></a>
Here we will take it for granted that
<code>METHOD="POST"</code> needs to be used and consider the
technical consequences.
</p>
		<p class="important">For forms which use <code>METHOD="POST"</code>,
CGI specifications say that the data is passed to
the script or program
in the standard input stream (<code>stdin</code>), and the
length (in bytes, i.e. characters) of the data is passed
in an environment variable called
<code>CONTENT_LENGTH</code>.
</p>
		<p>Reading from standard input sounds probably simpler than
reading from an environment variable, but there are complications.
The server is <em>not</em> required to pass the data so that
when the CGI script tries to read more data than there is, it would
get an end of file indication! That is, if you read e.g. using
the <code>getchar</code> function in a C program, it is <em>undefined</em>
what happens after reading all the data characters; it is not guaranteed
that the function will return <code>EOF</code>.
</p>
		<p class="warning">
When reading the input, the program must not try to read more
than <code>CONTENT_LENGTH</code> characters.
</p>
		<p>
A relatively simple
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/collect.c" title="The source program as a separate document">C program</a>
for accepting input via CGI and
<code>METHOD="POST"</code> is the following:
</p>
		<pre>
				<code class="C">#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />#define MAXLEN 80<br />#define EXTRA 5<br />/* 4 for field name "data", 1 for "=" */<br />#define MAXINPUT MAXLEN+EXTRA+2<br />/* 1 for added line break, 1 for trailing NUL */<br />#define DATAFILE "../data/data.txt"<br /><br />void unencode(char *src, char *last, char *dest)<br />{<br /> for(; src != last; src++, dest++)<br />   if(*src == '+')<br />     *dest = ' ';<br />   else if(*src == '%') {<br />     int code;<br />     if(sscanf(src+1, "%2x", &amp;code) != 1) code = '?';<br />     *dest = code;<br />     src +=2; }     <br />   else<br />     *dest = *src;<br /> *dest = '\n';<br /> *++dest = '\0';<br />}<br /><br />int main(void)<br />{<br />char *lenstr;<br />char input[MAXINPUT], data[MAXINPUT];<br />long len;<br />printf("%s%c%c\n",<br />"Content-Type:text/html;charset=iso-8859-1",13,10);<br />printf("&lt;TITLE&gt;Response&lt;/TITLE&gt;\n");<br />lenstr = getenv("CONTENT_LENGTH");<br />if(lenstr == NULL || sscanf(lenstr,"%ld",&amp;len)!=1 || len &gt; MAXLEN)<br />  printf("&lt;P&gt;Error in invocation - wrong FORM probably.");<br />else {<br />  FILE *f;<br />  fgets(input, len+1, stdin);<br />  unencode(input+EXTRA, input+len, data);<br />  f = fopen(DATAFILE, "a");<br />  if(f == NULL)<br />    printf("&lt;P&gt;Sorry, cannot store your data.");<br />  else<br />    fputs(data, f);<br />  fclose(f);<br />  printf("&lt;P&gt;Thank you! The following contribution of yours has \<br />been stored:&lt;BR&gt;%s",data);<br />  }<br />return 0;<br />}<br /></code>
		</pre>
		<p>Essentially, the program retrieves the
information about the number of characters in the input
from value of the
<code>CONTENT_LENGTH</code> environment variable.
Then it unencodes (decodes) the data, since the data arrives in
<a href="http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type" title="Specification of the application/x-www-form-urlencoded encoding">a specifically encoded format</a>.
The program has been written for a form where the text input field
has the name <code>data</code> (actually, just the length of the name
matters here). For example, if the user types<br /><kbd>Hello there!</kbd><br />
then the data will be passed to the program encoded as
<kbd>data=Hello+there%21</kbd><br />
(with space encoded as <code>+</code> and exclamation mark encoded
as <code>%21</code>). The <code>unencode</code> routine in the program
converts this back to the original format. After that,
the data is appended to a file (with a fixed file name),
as well as echoed back to the user.
</p>
		<p>
Having compiled the program I have saved it as <code>collect.cgi</code>
into the directory for CGI scripts. Now a form like the following
can be used for data submissions:
</p>
		<pre>
				<code class="html">&lt;FORM ACTION="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/collect.cgi"<br /> METHOD="POST"&gt;<br />&lt;P&gt;Please type your input (80 chars max.):&lt;BR&gt;<br />&lt;INPUT NAME="data" SIZE="60" MAXLENGTH="80"&gt;&lt;BR&gt;<br />&lt;INPUT TYPE="SUBMIT" VALUE="Send"&gt;<br />&lt;/FORM&gt;<br /></code>
		</pre>
		<p>Finally, we can write
<a href="http://www.cs.tut.fi/%7Ejkorpela/forms/viewdata.c">a simple program for viewing the data</a>;
it only needs to copy the content of a given text file onto
standard output:
</p>
		<pre>
				<code class="C">#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />#define DATAFILE "../data/data.txt"<br />int main(void)<br />{<br />FILE *f = fopen(DATAFILE,"r");<br />int ch;<br />if(f == NULL) {<br />  printf("%s%c%c\n",<br />  "Content-Type:text/html;charset=iso-8859-1",13,10);<br />  printf("&lt;TITLE&gt;Failure&lt;/TITLE&gt;\n");<br />  printf("&lt;P&gt;&lt;EM&gt;Unable to open data file, sorry!&lt;/EM&gt;"); }<br />else {<br />  printf("%s%c%c\n",<br />  "Content-Type:text/plain;charset=iso-8859-1",13,10);<br />  while((ch=getc(f)) != EOF)<br />    putchar(ch);<br />  fclose(f); }<br />return 0;<br />}<br /></code>
		</pre>
		<p>Notice that this program prints (when succesful) the data
as plain text, preceded by a header which says this, i.e.
has <code>text/plain</code> instead of <code>text/html</code>.
</p>
		<p>
A form which invokes that program can be very simple, since
no input data is needed:
</p>
		<pre>
				<code class="html">&lt;FORM ACTION="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/viewdata.cgi"&gt;<br />&lt;P&gt;&lt;INPUT TYPE="SUBMIT" VALUE="View"&gt;<br />&lt;/FORM&gt;<br /></code>
		</pre>
		<p>Finally, here's what the two forms look like.
You can now test them:

</p>
		<h3>Form for submitting data</h3>
		<p>Please notice that anything you submit here will become
visible to the world:
</p>
		<form action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/collect.cgi" method="post">
				<div>Please type your input (80 chars max.):<br /><input name="data" size="60" maxlength="80" /><br /><input value="Send" type="submit" /></div>
		</form>
		<h3>Form for checking submitted data</h3>
		<p>The content of the text file to which the submissions are
stored will be displayed as plain text.
</p>
		<form action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/viewdata.cgi">
				<div>
						<input value="View" type="submit" />
				</div>
		</form>
		<hr title="Further reading" />
		<p>You may now wish to read
<a href="http://hoohoo.ncsa.uiuc.edu/cgi/interface.html"><cite>The CGI specification</cite></a>
which tells you all the basic details about CGI. The next step is
probably to see what the
<cite><a href="http://www.webthing.com/tutorials/cgifaq.html">CGI Programming FAQ</a></cite> contains.</p>
		<p>There is a lot of material, including
<a href="http://www.cgi-resources.com/Documentation/Introduction_to_CGI/">introductions</a> and
<a href="http://www.cgi-resources.com/Documentation/CGI_Tutorials/">tutorials</a>,
in the
<a href="http://www.cgi-resources.com/">CGI Resource Index</a>.
Notice in particular the section <a href="http://www.cgi-resources.com/Programs_and_Scripts/C_and_C++/Libraries_and_Classes/"><cite>Programs and Scripts: C and C++: Libraries and Classes</cite></a>
which contains libraries which can make it easier to process form data.
It can be instructive to parse simple data format by using code of
your own,
as was done in the simple examples above, 
but in practical application a library routine might be better.
</p>
		<hr title="Information about this document" />
		<div>
				<a title="ISO 8601, the date and time representation standard" href="http://www.cs.tut.fi/%7Ejkorpela/iso8601.html">
Date</a> of last update: 2001-04-15. Technical fix 2002-11-26.</div>
		<div class="footer">
This page belongs to division
<cite><a href="http://www.cs.tut.fi/%7Ejkorpela/www.html">Web authoring and surfing</a></cite>,
subdivision
<cite><a href="http://www.cs.tut.fi/%7Ejkorpela/forms/index.html">Forms</a></cite> in
the free information site
<cite><a href="http://www.cs.tut.fi/%7Ejkorpela/indexen.html">IT and communication</a></cite>
by
<a href="http://www.cs.tut.fi/%7Ejkorpela/personal.html" title="Jukka K. Korpela, an IT generalist and specialist (personal home page)">Jukka "Yucca" Korpela</a>.
</div>
<img src ="http://www.blogjava.net/soochow_hhb/aggbug/55055.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-06-26 08:41 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/06/26/55055.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VNC的使用</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/06/23/54744.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Fri, 23 Jun 2006 09:34:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/06/23/54744.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/54744.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/06/23/54744.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/54744.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/54744.html</trackback:ping><description><![CDATA[
		<strong>『VNC简介』</strong>
		<br />　　网络遥控技术是指由一部计算机（主控端）去控制另一部计算机（被控端），而且当主控端在控制端时，就如同用户亲自坐在被控端前操作一样，可以执行被控端的应用程序，及使用被控端的系统资源。 <br />　　VNC（Virtual Network Computing）是一套由AT&amp;T实验室所开发的可操控远程的计算机的软件，其采用了GPL授权条款，任何人都可免费取得该软件。VNC软件主要由两个部分组成：VNC server及VNC viewer。用户需先将VNC server安装在被控端的计算机上后，才能在主控端执行VNC viewer控制被控端。 <br />　　（在windows中也由一套著名的网络遥控软件――Symantec公司推出的pcAnywhere。 <br />　　VNC server与VNC viewer支持多种操作系统，如Unix系列（Unix，Linux，Solaris等），windows及MacOS，因此可将VNC server 及VNC viewer分别安装在不同的操作系统中进行控制。如果目前操作的主控端计算机没有安装VNC viewer，也可以通过一般的网页浏览器来控制被控端。 <br />　　整个VNC运行的工作流程如下： <br />　　（1） VNC客户端通过浏览器或VNC Viewer连接至VNC Server。 <br />　　（2） VNC Server传送一对话窗口至客户端，要求输入连接密码，以及存取的VNC Server显示装置。 <br />　　（3） 在客户端输入联机密码后，VNC Server验证客户端是否具有存取权限。 <br />　　（4） 若是客户端通过VNC Server的验证，客户端即要求VNC Server显示桌面环境。 <br />　　（5） VNC Server通过X Protocol 要求X Server将画面显示控制权交由VNC Server负责。 <br />　　（6） VNC Server将来由 X Server的桌面环境利用VNC通信协议送至客户端，并且允许客户端控制VNC Server的桌面环境及输入装置。 <br />　　<br />　　<b>『VNC的安装与使用』</b><br />　　本人的操作环境：被控端 Redhat8.0，主控端Windows XP。 <br />　　1． 载VNC Server与VNC viewer. <br />　　VNC Server下载地址：http://www.linuxeden.com/download/softdetail.php?softid=744 <br />　　VNC viewer下载地址：http://download.pchome.net/php/dl.php?sid=2603 <br />　　2．安装。 <br />　　rpm –Uvh vnc-3.3.6-2.i386.rpm (如果是源代码请看包里的说明) <br />　　安装winvnc.exe <br />　　3.在Linux上启动VNC Server <br />　　执行vncserver命令： <br />　　[root@linux root]# vncserver <br />　　You will require a password to access your desktops. <br />　　Password: ----为了不想任何人都可以任意遥控此计算机。因此当第 <br />　　Verify: ---1次启动VNC server时，会要求设置网络遥控的密码。 <br />　　New ‘X’ desktop is linux:1 ----一定要记住这一行稍后会用到。 <br />　　Creating default startup script /root/.vnc/xstartup <br />　　Starting applications specified in /root/.vnc/xstartup <br />　　Log file is /root/.vnc/linux:1.log <br />　　（经上述步骤后，便已启动了VNC Server。如果你想要更改VNC Server的密码，只要执行vncpasswd命令即可。） <br />　　4．在Microsoft Windows上运行VNC Viewer <br />　　直接运行“vncviewer.exe”,系统会出现”Connection details”对话框。 <br />　　在“Connection details”对话框中的“VNC server”文本框中输入VNC Server的IP地址（或主机名及显示装置编号，（请看3。在Linux上启动VNC server的这一行，New ‘X’ desktop is linux:1 得到此信息），例如：192.168.0.1：1（冒号后面的1是执行VNC Server生成的显示装置编号），单击“OK”按钮后，VNC Server即会开始检查所输入的信息，若是信息错误，系统会出现“Failed to connect to server”的错误信息：若是信息正确，则会接着出现“VNC Authentication”对话框。 <br />　　若是在“VNC Authentication”对话框中输入的密码正确，就可以成功地打开Linux的桌面窗口。 <br />　　5. 从浏览器远程遥控。 <br />　　启动VNC Server 后直接打开浏览器，在地址栏中输入被控端的网址或IP地址，并在网址后加上“：5800＋显示编号”的端口号即可操控该计算机。 <br />　　例如：http://192.168.01.:5801 (如果显示编号为1，一般第一次设置的显示编号都是1，就用5800＋1＝5801。) <br />　　<br />　　6．FAQ <br />　　A．为什么连接后，不能显示桌面，而只有一个Terminal窗口？ <br />　　试着修改/root/.vnc/xstartup,把最后一行 twm&amp; 改成 gnome-session&amp; or kde&amp;(据说KDE在目前的VNC Viewer上的表现不太稳定) <br />　　<br />　　B． 为什么重新启动VNC Server后，连接不上了？ <br />　　因为重新启动VNC Server时，系统会指定一个新的显示编号，需使用此新的编号，否则就无法连接。 <img src ="http://www.blogjava.net/soochow_hhb/aggbug/54744.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-06-23 17:34 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/06/23/54744.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>++运算符重载问题</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/04/28/43731.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Fri, 28 Apr 2006 03:01:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/04/28/43731.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/43731.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/04/28/43731.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/43731.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/43731.html</trackback:ping><description><![CDATA[
		<p>简单探讨了++运算符的问题：<br /></p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008080"> 1</span>
				<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">#include </span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">iostream</span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #008080"> 2</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />using namespace std;<br /></span>
				<span style="COLOR: #008080"> 3</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
				</span>
				<span style="COLOR: #008080"> 4</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> temp<br /></span>
				<span style="COLOR: #008080"> 5</span>
				<span style="COLOR: #000000">
						<img id="Codehighlighter1_53_429_Open_Image" onclick="this.style.display='none'; Codehighlighter1_53_429_Open_Text.style.display='none'; Codehighlighter1_53_429_Closed_Image.style.display='inline'; Codehighlighter1_53_429_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
						<img id="Codehighlighter1_53_429_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_53_429_Closed_Text.style.display='none'; Codehighlighter1_53_429_Open_Image.style.display='inline'; Codehighlighter1_53_429_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				</span>
				<span id="Codehighlighter1_53_429_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_53_429_Open_Text">
						<span style="COLOR: #000000">{<br /></span>
						<span style="COLOR: #008080"> 6</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">private</span>
						<span style="COLOR: #000000"> :<br /></span>
						<span style="COLOR: #008080"> 7</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> x ;<br /></span>
						<span style="COLOR: #008080"> 8</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">public</span>
						<span style="COLOR: #000000"> :<br /></span>
						<span style="COLOR: #008080"> 9</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /> temp()<br /></span>
						<span style="COLOR: #008080">10</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_95_102_Open_Image" onclick="this.style.display='none'; Codehighlighter1_95_102_Open_Text.style.display='none'; Codehighlighter1_95_102_Closed_Image.style.display='inline'; Codehighlighter1_95_102_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_95_102_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_95_102_Closed_Text.style.display='none'; Codehighlighter1_95_102_Open_Image.style.display='inline'; Codehighlighter1_95_102_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span>
						<span id="Codehighlighter1_95_102_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_95_102_Open_Text">
								<span style="COLOR: #000000">{x </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">5</span>
								<span style="COLOR: #000000">;}</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">11</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_106_204_Open_Image" onclick="this.style.display='none'; Codehighlighter1_106_204_Open_Text.style.display='none'; Codehighlighter1_106_204_Closed_Image.style.display='inline'; Codehighlighter1_106_204_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_106_204_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_106_204_Closed_Text.style.display='none'; Codehighlighter1_106_204_Open_Image.style.display='inline'; Codehighlighter1_106_204_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />  </span>
						<span id="Codehighlighter1_106_204_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span>
						<span id="Codehighlighter1_106_204_Open_Text">
								<span style="COLOR: #008000">/*</span>
								<span style="COLOR: #008000">  void operator ++(void)<br /></span>
								<span style="COLOR: #008080">12</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   {<br /></span>
								<span style="COLOR: #008080">13</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />       x++;<br /></span>
								<span style="COLOR: #008080">14</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   }<br /></span>
								<span style="COLOR: #008080">15</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />  <br /></span>
								<span style="COLOR: #008080">16</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" /> void operator ++( int)<br /></span>
								<span style="COLOR: #008080">17</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   {<br /></span>
								<span style="COLOR: #008080">18</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />       x++;<br /></span>
								<span style="COLOR: #008080">19</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />   }</span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">20</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   temp</span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> operator </span>
						<span style="COLOR: #000000">++</span>
						<span style="COLOR: #000000">()<br /></span>
						<span style="COLOR: #008080">21</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_232_291_Open_Image" onclick="this.style.display='none'; Codehighlighter1_232_291_Open_Text.style.display='none'; Codehighlighter1_232_291_Closed_Image.style.display='inline'; Codehighlighter1_232_291_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_232_291_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_232_291_Closed_Text.style.display='none'; Codehighlighter1_232_291_Open_Image.style.display='inline'; Codehighlighter1_232_291_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
						<span id="Codehighlighter1_232_291_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_232_291_Open_Text">
								<span style="COLOR: #000000">{<br /></span>
								<span style="COLOR: #008080">22</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />      x</span>
								<span style="COLOR: #000000">++</span>
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">23</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   cout </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">++a</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> endl;<br /></span>
								<span style="COLOR: #008080">24</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">25</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />   }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">26</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
								<br />
						</span>
						<span style="COLOR: #008080">27</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   temp</span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> operator </span>
						<span style="COLOR: #000000">++</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000">)<br /></span>
						<span style="COLOR: #008080">28</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_323_382_Open_Image" onclick="this.style.display='none'; Codehighlighter1_323_382_Open_Text.style.display='none'; Codehighlighter1_323_382_Closed_Image.style.display='inline'; Codehighlighter1_323_382_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_323_382_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_323_382_Closed_Text.style.display='none'; Codehighlighter1_323_382_Open_Image.style.display='inline'; Codehighlighter1_323_382_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
						<span id="Codehighlighter1_323_382_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_323_382_Open_Text">
								<span style="COLOR: #000000">{<br /></span>
								<span style="COLOR: #008080">29</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />      x</span>
								<span style="COLOR: #000000">++</span>
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">30</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   cout </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">a++</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> endl;<br /></span>
								<span style="COLOR: #008080">31</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #0000ff">this</span>
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #008080">32</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />   }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">33</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   <br /></span>
						<span style="COLOR: #008080">34</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
						<span style="COLOR: #0000ff">void</span>
						<span style="COLOR: #000000"> show()<br /></span>
						<span style="COLOR: #008080">35</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_406_427_Open_Image" onclick="this.style.display='none'; Codehighlighter1_406_427_Open_Text.style.display='none'; Codehighlighter1_406_427_Closed_Image.style.display='inline'; Codehighlighter1_406_427_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_406_427_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_406_427_Closed_Text.style.display='none'; Codehighlighter1_406_427_Open_Image.style.display='inline'; Codehighlighter1_406_427_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
						<span id="Codehighlighter1_406_427_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.blogjava.net/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_406_427_Open_Text">
								<span style="COLOR: #000000">{<br /></span>
								<span style="COLOR: #008080">36</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />     cout </span>
								<span style="COLOR: #000000">&lt;&lt;</span>
								<span style="COLOR: #000000"> x;<br /></span>
								<span style="COLOR: #008080">37</span>
								<span style="COLOR: #000000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />   }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">38</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">;<br /></span>
				<span style="COLOR: #008080">39</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<br />
				</span>
				<span style="COLOR: #008080">40</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> main()<br /></span>
				<span style="COLOR: #008080">41</span>
				<span style="COLOR: #000000">
						<img id="Codehighlighter1_444_587_Open_Image" onclick="this.style.display='none'; Codehighlighter1_444_587_Open_Text.style.display='none'; Codehighlighter1_444_587_Closed_Image.style.display='inline'; Codehighlighter1_444_587_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
						<img id="Codehighlighter1_444_587_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_444_587_Closed_Text.style.display='none'; Codehighlighter1_444_587_Open_Image.style.display='inline'; Codehighlighter1_444_587_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" />
				</span>
				<span id="Codehighlighter1_444_587_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
						<img src="http://www.blogjava.net/images/dot.gif" />
				</span>
				<span id="Codehighlighter1_444_587_Open_Text">
						<span style="COLOR: #000000">{<br /></span>
						<span style="COLOR: #008080">42</span>
						<span style="COLOR: #000000">
								<img id="Codehighlighter1_446_508_Open_Image" onclick="this.style.display='none'; Codehighlighter1_446_508_Open_Text.style.display='none'; Codehighlighter1_446_508_Closed_Image.style.display='inline'; Codehighlighter1_446_508_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" />
								<img id="Codehighlighter1_446_508_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_446_508_Closed_Text.style.display='none'; Codehighlighter1_446_508_Open_Image.style.display='inline'; Codehighlighter1_446_508_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />
						</span>
						<span id="Codehighlighter1_446_508_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span>
						<span id="Codehighlighter1_446_508_Open_Text">
								<span style="COLOR: #008000">/*</span>
								<span style="COLOR: #008000">
										<br />
								</span>
								<span style="COLOR: #008080">43</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />  operator ++(void)  =&gt; ++a;<br /></span>
								<span style="COLOR: #008080">44</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />  operator ++(int)   =&gt; a++;<br /></span>
								<span style="COLOR: #008080">45</span>
								<span style="COLOR: #008000">
										<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />
								</span>
								<span style="COLOR: #008000">*/</span>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #008080">46</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />temp a;<br /></span>
						<span style="COLOR: #008080">47</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">++</span>
						<span style="COLOR: #000000">a;  </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000">只能++a,而a++就会有警告。</span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #008080">48</span>
						<span style="COLOR: #008000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">a</span>
						<span style="COLOR: #000000">++</span>
						<span style="COLOR: #000000">;<br /></span>
						<span style="COLOR: #008080">49</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />a.show();<br /></span>
						<span style="COLOR: #008080">50</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />  system(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">pause</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">);<br /></span>
						<span style="COLOR: #008080">51</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">return</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000">;<br /></span>
						<span style="COLOR: #008080">52</span>
						<span style="COLOR: #000000">
								<img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #008080">53</span>
				<span style="COLOR: #000000">
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
				</span>
		</div>
		<p>
				<font color="#008000">operator ++(void)  =&gt; ++a;<br /></font>
				<span style="COLOR: #008000">operator ++(int)     =&gt; a++;<br /></span>
				<br />++a;  //call operator++(void)。<br />a++;  //call operator++(int)</p>
<img src ="http://www.blogjava.net/soochow_hhb/aggbug/43731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-04-28 11:01 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/04/28/43731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux终端控制和信号初步</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/04/02/38713.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Sun, 02 Apr 2006 03:52:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/04/02/38713.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/38713.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/04/02/38713.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/38713.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/38713.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 讲解了终端的基本控制方法,通过五个例子一步一步完善,最终形成了完善的终端控制例程.1.play_again0.c功能:读入用户输入,输入y 返回0,输入n 返回1Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 #include &lt;s...&nbsp;&nbsp;<a href='http://www.blogjava.net/soochow_hhb/archive/2006/04/02/38713.html'>阅读全文</a><img src ="http://www.blogjava.net/soochow_hhb/aggbug/38713.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-04-02 11:52 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/04/02/38713.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux下RTP编程(使用JRTPLIB)(转)</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/30/38364.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Thu, 30 Mar 2006 13:37:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/30/38364.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/38364.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/30/38364.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/38364.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/38364.html</trackback:ping><description><![CDATA[
流媒体指的是在网络中使用流技术传输的连续时基媒体，其特点是在播放前不需要下载整个文件，而是采用边下载边播放的方式，它是视频会议、IP电话等应用场
合的技术基础。RTP是进行实时流媒体传输的标准协议和关键技术，本文介绍如何在Linux下利用JRTPLIB进行实时流媒体编程。 

<div align="left">  <div align="left"><a name="0">一、流媒体简介</a><br />       
随着Internet的日益普及，在网络上传输的数据已经不再局限于文字和图形，而是逐渐向声音和视频等多媒体格式过渡。目前在网络上传输音频/视频
（Audio/Video，简称A/V）等多媒体文件时，基本上只有下载和流式传输两种选择。通常说来，A/V文件占据的存储空间都比较大，在带宽受限的
网络环境中下载可能要耗费数分钟甚至数小时，所以这种处理方法的延迟很大。如果换用流式传输的话，声音、影像、动画等多媒体文件将由专门的流媒体服务器负
责向用户连续、实时地发送，这样用户可以不必等到整个文件全部下载完毕，而只需要经过几秒钟的启动延时就可以了，当这些多媒体数据在客户机上播放时，文件
的剩余部分将继续从流媒体服务器下载。</div><br /><div align="left"><div align="left">       
流（Streaming）是近年在Internet上出现的新概念，其定义非常广泛，主要是指通过网络传输多媒体数据的技术总称。流媒体包含广义和狭义两
种内涵：广义上的流媒体指的是使音频和视频形成稳定和连续的传输流和回放流的一系列技术、方法和协议的总称，即流媒体技术；狭义上的流媒体是相对于传统的
下载-回放方式而言的，指的是一种从Internet上获取音频和视频等多媒体数据的新方法，它能够支持多媒体数据流的实时传输和实时播放。通过运用流媒
体技术，服务器能够向客户机发送稳定和连续的多媒体数据流，客户机在接收数据的同时以一个稳定的速率回放，而不用等数据全部下载完之后再进行回放。</div><br /><div align="left"><div align="left">由
于受网络带宽、计算机处理能力和协议规范等方面的限制，要想从Internet上下载大量的音频和视频数据，无论从下载时间和存储空间上来讲都是不太现实
的，而流媒体技术的出现则很好地解决了这一难题。目前实现流媒体传输主要有两种方法：顺序流（progressive
streaming）传输和实时流（realtime streaming）传输，它们分别适合于不同的应用场合。</div><br /><div align="left"><div align="left"><strong>顺序流传输</strong></div><br /><div align="left"><div align="left">       
顺序流传输采用顺序下载的方式进行传输，在下载的同时用户可以在线回放多媒体数据，但给定时刻只能观看已经下载的部分，不能跳到尚未下载的部分，也不能在
传输期间根据网络状况对下载速度进行调整。由于标准的HTTP服务器就可以发送这种形式的流媒体，而不需要其他特殊协议的支持，因此也常常被称作HTTP
流式传输。顺序流式传输比较适合于高质量的多媒体片段，如片头、片尾或者广告等。</div><br /><div align="left"><div align="left"><strong>实时流传输</strong></div><br /><div align="left"><div align="left">       
实时流式传输保证媒体信号带宽能够与当前网络状况相匹配，从而使得流媒体数据总是被实时地传送，因此特别适合于现场事件。实时流传输支持随机访问，即用户
可以通过快进或者后退操作来观看前面或者后面的内容。从理论上讲，实时流媒体一经播放就不会停顿，但事实上仍有可能发生周期性的暂停现象，尤其是在网络状
况恶化时更是如此。与顺序流传输不同的是，实时流传输需要用到特定的流媒体服务器，而且还需要特定网络协议的支持。</div><br /><div align="left"><div align="left"><a name="1">二、流媒体协议</a><br />实
时传输协议（Real-time Transport
Protocol，PRT）是在Internet上处理多媒体数据流的一种网络协议，利用它能够在一对一（unicast，单播）或者一对多
（multicast，多播）的网络环境中实现传流媒体数据的实时传输。RTP通常使用UDP来进行多媒体数据的传输，但如果需要的话可以使用TCP或者
ATM等其它协议，整个RTP协议由两个密切相关的部分组成：RTP数据协议和RTP控制协议。实时流协议（Real Time Streaming
Protocol，RTSP）最早由Real
Networks和Netscape公司共同提出，它位于RTP和RTCP之上，其目的是希望通过IP网络有效地传输多媒体数据。</div><br /><div align="left"><div align="left"><strong>2.1 RTP数据协议</strong></div><br /><div align="left"><div align="left">RTP数据协议负责对流媒体数据进行封包并实现媒体流的实时传输，每一个RTP数据报都由头部（Header）和负载（Payload）两个部分组成，其中头部前12个字节的含义是固定的，而负载则可以是音频或者视频数据。RTP数据报的头部格式如图1所示：<br /><br /></div><br /><div align="left"><div align="left"><a name="N10082"><strong><img src="http://www-128.ibm.com/developerworks/cn/linux/l-mdst/image001.jpg" border="0" hspace="0" /><br />图1 RTP头部格式<br /><br /></strong></a><img alt="" src="http://www.chinavideo.org/administrator/image001.jpg" border="0" /></div><br /><div align="left"><div align="left">其中比较重要的几个域及其意义如下： <br /></div><br /><div align="left"><ul><li><strong>CSRC记数（CC）</strong>　
　表示CSRC标识的数目。CSRC标识紧跟在RTP固定头部之后，用来表示RTP数据报的来源，RTP协议允许在同一个会话中存在多个数据源，它们可以
通过RTP混合器合并为一个数据源。例如，可以产生一个CSRC列表来表示一个电话会议，该会议通过一个
RTP混合器将所有讲话者的语音数据组合为一个RTP数据源。 </li><li><strong>负载类型（PT）</strong>　　标明RTP负载的格式，包括所采用的编码算法、采样频率、承载通道等。例如，类型2表明该RTP数据包中承载的是用ITU G.721算法编码的语音数据，采样频率为8000Hz，并且采用单声道。 </li><li><strong>序列号</strong>　　用来为接收方提供探测数据丢失的方法，但如何处理丢失的数据则是应用程序自己的事情，RTP协议本身并不负责数据的重传。 </li><li><strong>时间戳</strong>　　记录了负载中第一个字节的采样时间，接收方能够时间戳能够确定数据的到达是否受到了延迟抖动的影响，但具体如何来补偿延迟抖动则是应用程序自己的事情。 </li></ul></div><div align="left">从RTP
数据报的格式不难看出，它包含了传输媒体的类型、格式、序列号、时间戳以及是否有附加数据等信息，这些都为实时的流媒体传输提供了相应的基础。RTP协议
的目的是提供实时数据（如交互式的音频和视频）的端到端传输服务，因此在RTP中没有连接的概念，它可以建立在底层的面向连接或面向非连接的传输协议之
上；RTP也不依赖于特别的网络地址格式，而仅仅只需要底层传输协议支持组帧（Framing）和分段（Segmentation）就足够了；另外RTP
本身还不提供任何可靠性机制，这些都要由传输协议或者应用程序自己来保证。在典型的应用场合下，RTP
一般是在传输协议之上作为应用程序的一部分加以实现的，如图2所示：</div><br /><div align="left"><div align="left"><a name="N100B2"><strong><img src="http://www-128.ibm.com/developerworks/cn/linux/l-mdst/image002.jpg" border="0" hspace="0" /><br />图2 RTP与各种网络协议的关系<br /><br /></strong></a><img alt="" src="http://www.chinavideo.org/administrator/image002.jpg" border="0" /></div><br /><div align="left"><div align="left"><strong>2.2 RTCP控制协议</strong></div><br /><div align="left"><div align="left">RTCP
控制协议需要与RTP数据协议一起配合使用，当应用程序启动一个RTP会话时将同时占用两个端口，分别供RTP
和RTCP使用。RTP本身并不能为按序传输数据包提供可靠的保证，也不提供流量控制和拥塞控制，这些都由RTCP来负责完成。通常RTCP会采用与
RTP相同的分发机制，向会话中的所有成员周期性地发送控制信息，应用程序通过接收这些数据，从中获取会话参与者的相关资料，以及网络状况、分组丢失概率
等反馈信息，从而能够对服务质量进行控制或者对网络状况进行诊断。</div><br /><div align="left"><div align="left">RTCP协议的功能是通过不同的RTCP数据报来实现的，主要有如下几种类型： <br /></div><br /><div align="left"><ul><li><strong>SR</strong>　　发送端报告，所谓发送端是指发出RTP数据报的应用程序或者终端，发送端同时也可以是接收端。 </li><li><strong>RR</strong>　　接收端报告，所谓接收端是指仅接收但不发送RTP数据报的应用程序或者终端。 </li><li><strong>SDES</strong>　　源描述，主要功能是作为会话成员有关标识信息的载体，如用户名、邮件地址、电话号码等，此外还具有向会话成员传达会话控制信息的功能。 </li><li><strong>BYE</strong>　　通知离开，主要功能是指示某一个或者几个源不再有效，即通知会话中的其他成员自己将退出会话。 </li><li><strong>APP</strong>　　由应用程序自己定义，解决了RTCP的扩展性问题，并且为协议的实现者提供了很大的灵活性。 </li></ul></div><div align="left">RTCP数据报携带有服务质量监控的必要信息，能够对服务质量进行动态的调整，并能够对网络拥塞进行有效的控制。由于RTCP数据报采用的是多播方式，因此会话中的所有成员都可以通过RTCP数据报返回的控制信息，来了解其他参与者的当前情况。</div><br /><div align="left"><div align="left">在
一个典型的应用场合下，发送媒体流的应用程序将周期性地产生发送端报告SR，该RTCP数据报含有不同媒体流间的同步信息，以及已经发送的数据报和字节的
计数，接收端根据这些信息可以估计出实际的数据传输速率。另一方面，接收端会向所有已知的发送端发送接收端报告RR，该RTCP数据报含有已接收数据报的
最大序列号、丢失的数据报数目、延时抖动和时间戳等重要信息，发送端应用根据这些信息可以估计出往返时延，并且可以根据数据报丢失概率和时延抖动情况动态
调整发送速率，以改善网络拥塞状况，或者根据网络状况平滑地调整应用程序的服务质量。</div><br /><div align="left"><div align="left"><strong>2.3 RTSP实时流协议</strong></div><br /><div align="left"><div align="left">作
为一个应用层协议，RTSP提供了一个可供扩展的框架，它的意义在于使得实时流媒体数据的受控和点播变得可能。总的说来，RTSP是一个流媒体表示协议，
主要用来控制具有实时特性的数据发送，但它本身并不传输数据，而是必须依赖于下层传输协议所提供的某些服务。RTSP
可以对流媒体提供诸如播放、暂停、快进等操作，它负责定义具体的控制消息、操作方法、状态码等，此外还描述了与RTP间的交互操作。</div><br /><div align="left"><div align="left">RTSP
在制定时较多地参考了HTTP/1.1协议，甚至许多描述与HTTP/1.1完全相同。RTSP之所以特意使用与HTTP/1.1类似的语法和操作，在很
大程度上是为了兼容现有的Web基础结构，正因如此，HTTP/1.1的扩展机制大都可以直接引入到RTSP 中。</div><br /><div align="left"><div align="left">由RTSP
控制的媒体流集合可以用表示描述（Presentation
Description）来定义，所谓表示是指流媒体服务器提供给客户机的一个或者多个媒体流的集合，而表示描述则包含了一个表示中各个媒体流的相关信
息，如数据编码/解码算法、网络地址、媒体流的内容等。</div><br /><div align="left"><div align="left">虽
然RTSP服务器同样也使用标识符来区别每一流连接会话（Session），但RTSP连接并没有被绑定到传输层连接（如TCP等），也就是说在整个
RTSP连接期间，RTSP用户可打开或者关闭多个对RTSP服务器的可靠传输连接以发出RTSP
请求。此外，RTSP连接也可以基于面向无连接的传输协议（如UDP等）。</div><br /><div align="left"><div align="left">RTSP协议目前支持以下操作： <br /></div><br /><div align="left"><ul><li><strong>检索媒体</strong>　　允许用户通过HTTP或者其它方法向媒体服务器提交一个表示描述。如表示是组播的，则表示描述就包含用于该媒体流的组播地址和端口号；如果表示是单播的，为了安全在表示描述中应该只提供目的地址。 </li><li><strong>邀请加入</strong>　　媒体服务器可以被邀请参加正在进行的会议，或者在表示中回放媒体，或者在表示中录制全部媒体或其子集，非常适合于分布式教学。 </li><li><strong>添加媒体</strong>　　通知用户新加入的可利用媒体流，这对现场讲座来讲显得尤其有用。与HTTP/1.1类似，RTSP请求也可以交由代理、通道或者缓存来进行处理。 </li></ul></div><div align="left"><a name="2">三、流媒体编程 </a><br />RTP
是目前解决流媒体实时传输问题的最好办法，如果需要在Linux平台上进行实时流媒体编程，可以考虑使用一些开放源代码的RTP库，如LIBRTP、
JRTPLIB等。JRTPLIB是一个面向对象的RTP库，它完全遵循RFC
1889设计，在很多场合下是一个非常不错的选择，下面就以JRTPLIB为例，讲述如何在Linux平台上运用RTP协议进行实时流媒体编程。</div><br /><div align="left"><div align="left"><strong>3.1 环境搭建</strong></div><br /><div align="left"><div align="left">JRTPLIB
是一个用C++语言实现的RTP库，目前已经可以运行在Windows、Linux、FreeBSD、
Solaris、Unix和VxWorks等多种操作系统上。要为Linux 系统安装JRTPLIB，首先从JRTPLIB的网站（http:
//lumumba.luc.ac.be/jori/jrtplib/jrtplib.html）下载最新的源码包，此处使用的是jrtplib-
2.7b.tar.bz2。假设下载后的源码包保存在/usr/local/src目录下，执行下面的命令可以对其进行解压缩：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />[root@linuxgam src]# bzip2 -dc jrtplib-2.7b.tar.bz2 | tar xvf -<br /></pre></td></tr></tbody></table><div align="left"><div align="left">接下去需要对JRTPLIB进行配置和编译：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />[root@linuxgam src]# cd jrtplib-2.7<br />[root@linuxgam jrtplib-2.7b]# ./configure <br />[root@linuxgam jrtplib-2.7b]# make<br /></pre></td></tr></tbody></table><div align="left"><div align="left">最后再执行如下命令就可以完成JRTPLIB的安装：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />[root@linuxgam jrtplib-2.7b]# make install<br /></pre></td></tr></tbody></table><div align="left"><div align="left"><strong>3.2 初始化</strong></div><br /><div align="left"><div align="left">在
使用JRTPLIB进行实时流媒体数据传输之前，首先应该生成RTPSession类的一个实例来表示此次RTP会话，然后调用Create()方法来对
其进行初始化操作。RTPSession类的Create()方法只有一个参数，用来指明此次RTP会话所采用的端口号。清单1给出了一个最简单的初始化
框架，它只是完成了RTP会话的初始化工作，还不具备任何实际的功能。</div><br /><div align="left"><a name="N1014B"><strong>代码清单1：initial.cpp</strong></a><br /></div><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />#include "rtpsession.h"<br /><br />int main(void)<br />{<br />	RTPSession sess;<br />	sess.Create(5000);<br />	return 0;<br />}<br /></pre></td></tr></tbody></table><div align="left"><div align="left">如
果RTP会话创建过程失败，Create()方法将会返回一个负数，通过它虽然可以很容易地判断出函数调用究竟是成功的还是失败的，但却很难明白出错的原
因到底什么。JRTPLIB采用了统一的错误处理机制，它提供的所有函数如果返回负数就表明出现了某种形式的错误，而具体的出错信息则可以通过调用
RTPGetErrorString()函数得到。RTPGetErrorString()函数将错误代码作为参数传入，然后返回该错误代码所对应的错误
信息。清单2给出了一个更加完整的初始化框架，它可以对RTP会话初始化过程中所产生的错误进行更好的处理：</div><br /><div align="left"><a name="N10158"><strong>代码清单2：framework.cpp</strong></a><br /></div><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />#include &lt;stdio.h&gt;<br />#include "rtpsession.h"<br /><br />int main(void)<br />{<br />  RTPSession sess;<br />  int status;<br />  char* msg;<br /><br />  sess.Create(6000);<br />  msg = RTPGetErrorString(status);<br />  printf("Error String: %s\\n", msg);<br />  return 0;<br />}<br /></pre></td></tr></tbody></table><div align="left"><div align="left">设
置恰当的时戳单元，是RTP会话初始化过程所要进行的另外一项重要工作，这是通过调用RTPSession类的
SetTimestampUnit()方法来实现的，该方法同样也只有一个参数，表示的是以秒为单元的时戳单元。例如，当使用RTP会话传输8000Hz
采样的音频数据时，由于时戳每秒钟将递增8000，所以时戳单元相应地应该被设置成1/8000：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />sess.SetTimestampUnit(1.0/8000.0);<br /></pre></td></tr></tbody></table><div align="left"><div align="left"><strong>3.3 数据发送</strong></div><br /><div align="left"><div align="left">当RTP
会话成功建立起来之后，接下去就可以开始进行流媒体数据的实时传输了。首先需要设置好数据发送的目标地址，
RTP协议允许同一会话存在多个目标地址，这可以通过调用RTPSession类的AddDestination()、
DeleteDestination()和ClearDestinations()方法来完成。例如，下面的语句表示的是让RTP会话将数据发送到本地主
机的6000端口：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />unsigned long addr = ntohl(inet_addr("127.0.0.1"));<br />sess.AddDestination(addr, 6000);<br /></pre></td></tr></tbody></table><div align="left"><div align="left">目标地址全部指定之后，接着就可以调用RTPSession类的SendPacket()方法，向所有的目标地址发送流媒体数据。SendPacket()是RTPSession类提供的一个重载函数，它具有下列多种形式：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />int SendPacket(void *data,int len)<br />int SendPacket(void *data,int len,unsigned char pt,bool mark,unsigned long timestampinc)<br />int SendPacket(void *data,int len,unsigned short hdrextID,void *hdrextdata,int numhdrextwords)<br />int SendPacket(void *data,int len,unsigned char pt,bool mark,unsigned long timestampinc,<br />				unsigned short hdrextID,void *hdrextdata,int numhdrextwords)<br /></pre></td></tr></tbody></table><div align="left"><div align="left">SendPacket()最典型的用法是类似于下面的语句，其中第一个参数是要被发送的数据，而第二个参数则指明将要发送数据的长度，再往后依次是RTP负载类型、标识和时戳增量。</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />sess.SendPacket(buffer, 5, 0, false, 10);<br /></pre></td></tr></tbody></table><div align="left"><div align="left">对
于同一个RTP会话来讲，负载类型、标识和时戳增量通常来讲都是相同的，JRTPLIB允许将它们设置为会话的默认参数，这是通过调用
RTPSession类的SetDefaultPayloadType()、SetDefaultMark()和
SetDefaultTimeStampIncrement()方法来完成的。为RTP会话设置这些默认参数的好处是可以简化数据的发送，例如，如果为
RTP会话设置了默认参数：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />sess.SetDefaultPayloadType(0);<br />sess.SetDefaultMark(false);<br />sess.SetDefaultTimeStampIncrement(10);<br /></pre></td></tr></tbody></table><div align="left"><div align="left">之后在进行数据发送时只需指明要发送的数据及其长度就可以了：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />sess.SendPacket(buffer, 5);<br /></pre></td></tr></tbody></table><div align="left"><div align="left"><strong>3.4 数据接收</strong></div><br /><div align="left"><div align="left">对
于流媒体数据的接收端，首先需要调用RTPSession类的PollData()方法来接收发送过来的RTP或者
RTCP数据报。由于同一个RTP会话中允许有多个参与者（源），你既可以通过调用RTPSession类的GotoFirstSource()和
GotoNextSource()方法来遍历所有的源，也可以通过调用RTPSession类的GotoFirstSourceWithData()和
GotoNextSourceWithData()方法来遍历那些携带有数据的源。在从RTP会话中检测出有效的数据源之后，接下去就可以调用
RTPSession类的GetNextPacket()方法从中抽取RTP数据报，当接收到的RTP数据报处理完之后，一定要记得及时释放。下面的代码
示范了该如何对接收到的RTP数据报进行处理：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />if (sess.GotoFirstSourceWithData()) {<br />  do {<br />    RTPPacket *pack;					<br />    pack = sess.GetNextPacket();					<br />    // 处理接收到的数据<br />    delete pack;<br />  } while (sess.GotoNextSourceWithData());<br />}<br /></pre></td></tr></tbody></table><div align="left"><div align="left">JRTPLIB为RTP数据报定义了三种接收模式，其中每种接收模式都具体规定了哪些到达的RTP数据报将会被接受，而哪些到达的RTP数据报将会被拒绝。通过调用RTPSession类的SetReceiveMode()方法可以设置下列这些接收模式：</div><br /><div align="left"><ul><li><strong>RECEIVEMODE_ALL</strong>　　缺省的接收模式，所有到达的RTP数据报都将被接受； </li><li><strong>RECEIVEMODE_IGNORESOME</strong>　　除了某些特定的发送者之外，所有到达的RTP数据报都将被接受，而被拒绝的发送者列表可以通过调用AddToIgnoreList()、DeleteFromIgnoreList()和ClearIgnoreList()方法来进行设置； </li><li><strong>RECEIVEMODE_ACCEPTSOME</strong>　　除了某些特定的发送者之外，所有到达的RTP数据报都将被拒绝，而被接受的发送者列表可以通过调用AddToAcceptList ()、DeleteFromAcceptList和ClearAcceptList ()方法来进行设置。 </li></ul></div><div align="left"><strong>3.5 控制信息</strong></div><br /><div align="left"><div align="left">JRTPLIB
是一个高度封装后的RTP库，程序员在使用它时很多时候并不用关心RTCP数据报是如何被发送和接收的，因为这些都可以由JRTPLIB自己来完成。只要
PollData()或者SendPacket()方法被成功调用，JRTPLIB就能够自动对到达的
RTCP数据报进行处理，并且还会在需要的时候发送RTCP数据报，从而能够确保整个RTP会话过程的正确性。</div><br /><div align="left"><div align="left">而
另一方面，通过调用RTPSession类提供的SetLocalName()、SetLocalEMail()、
SetLocalLocation()、SetLocalPhone()、SetLocalTool()和SetLocalNote()方法，
JRTPLIB又允许程序员对RTP会话的控制信息进行设置。所有这些方法在调用时都带有两个参数，其中第一个参数是一个char型的指针，指向将要被设
置的数据；而第二个参数则是一个int型的数值，表明该数据中的前面多少个字符将会被使用。例如下面的语句可以被用来设置控制信息中的电子邮件地址：</div><br /><div align="left"><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />sess.SetLocalEMail("<br /><script language="JavaScript" type="text/javascript"><!-- 
var prefix = '&#109;a' + 'i&#108;' + '&#116;o'; 
var path = 'hr' + 'ef' + '='; 
var addy82776 = 'x&#105;&#97;&#111;wp' + '&#64;' + 'l&#105;n&#117;xg&#97;m' + '&#46;' + 'c&#111;m'; 
document.write( '<a ' + path + '\'' + prefix + ':' + addy82776 + '\'>' ); 
document.write( addy82776 ); 
document.write( '<\/a>' ); 
//--></script><a href="mailto:xiaowp@linuxgam.com">xiaowp@linuxgam.com</a><br /><noscript> 
此邮件地址受spam bots保护，需要使用 Javascript 功能来查阅。
</noscript><br />",19);<br /></pre></td></tr></tbody></table><div align="left"><div align="left">在RTP
会话过程中，不是所有的控制信息都需要被发送，通过调用RTPSession类提供的
EnableSendName()、EnableSendEMail()、EnableSendLocation()、EnableSendPhone
()、EnableSendTool()和EnableSendNote()方法，可以为当前RTP会话选择将被发送的控制信息。</div><br /><div align="left"><div align="left"><strong>3.6 实际应用</strong></div><br /><div align="left"><div align="left">最后通过一个简单的流媒体发送-接收实例，介绍如何利用JRTPLIB来进行实时流媒体的编程。清单3给出了数据发送端的完整代码，它负责向用户指定的IP地址和端口，不断地发送RTP数据包：</div><br /><div align="left"><a name="N101D3"><strong>代码清单3：sender.cpp</strong></a><br /></div><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />#include &lt;stdio.h&gt;<br />#include &lt;string.h&gt;<br />#include "rtpsession.h"<br /><br />// 错误处理函数<br />void checkerror(int err)<br />{<br />  if (err &lt; 0) {<br />    char* errstr = RTPGetErrorString(err);<br />    printf("Error:%s\\n", errstr);<br />    exit(-1);<br />  }<br />}<br /><br />int main(int argc, char** argv)<br />{<br />  RTPSession sess;<br />  unsigned long destip;<br />  int destport;<br />  int portbase = 6000;<br />  int status, index;<br />  char buffer[128];<br /><br />  if (argc != 3) {<br />    printf("Usage: ./sender destip destport\\n");<br />    return -1;<br />  }<br /><br />  // 获得接收端的IP地址和端口号<br />  destip = inet_addr(argv[1]);<br />  if (destip == INADDR_NONE) {<br />    printf("Bad IP address specified.\\n");<br />    return -1;<br />  }<br />  destip = ntohl(destip);<br />  destport = atoi(argv[2]);<br /><br />  // 创建RTP会话<br />  status = sess.Create(portbase);<br />  checkerror(status);<br /><br />  // 指定RTP数据接收端<br />  status = sess.AddDestination(destip, destport);<br />  checkerror(status);<br /><br />  // 设置RTP会话默认参数<br />  sess.SetDefaultPayloadType(0);<br />  sess.SetDefaultMark(false);<br />  sess.SetDefaultTimeStampIncrement(10);<br /><br />  // 发送流媒体数据<br />  index = 1;<br />  do {<br />    sprintf(buffer, "%d: RTP packet", index ++);<br />    sess.SendPacket(buffer, strlen(buffer));<br />    printf("Send packet !\\n");<br />  } while(1);<br /><br />  return 0;<br />}<br /></pre></td></tr></tbody></table><div align="left"><div align="left">清单4则给出了数据接收端的完整代码，它负责从指定的端口不断地读取RTP数据包：</div><br /><div align="left"><a name="N101E0"><strong>代码清单4：receiver.cpp</strong></a><br /></div><table align="left" border="1" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><td><pre><br /><br />#include &lt;stdio.h&gt;<br />#include "rtpsession.h"<br />#include "rtppacket.h"<br /><br />// 错误处理函数<br />void checkerror(int err)<br />{<br />  if (err &lt; 0) {<br />    char* errstr = RTPGetErrorString(err);<br />    printf("Error:%s\\n", errstr);<br />    exit(-1);<br />  }<br />}<br /><br />int main(int argc, char** argv)<br />{<br />  RTPSession sess;<br />  int localport;<br />  int status;<br /><br />  if (argc != 2) {<br />    printf("Usage: ./sender localport\\n");<br />    return -1;<br />  }<br /><br />   // 获得用户指定的端口号<br />  localport = atoi(argv[1]);<br /><br />  // 创建RTP会话<br />  status = sess.Create(localport);<br />  checkerror(status);<br /><br />  do {<br />    // 接受RTP数据<br />    status = sess.PollData();<br />	// 检索RTP数据源<br />    if (sess.GotoFirstSourceWithData()) {<br />      do {<br />        RTPPacket* packet;<br />        // 获取RTP数据报<br />        while ((packet = sess.GetNextPacket()) != NULL) {<br />          printf("Got packet !\\n");<br />          // 删除RTP数据报<br />          delete packet;<br />        }<br />      } while (sess.GotoNextSourceWithData());<br />    }<br />  } while(1);<br /><br />  return 0;<br />}<br /></pre></td></tr></tbody></table><div align="left"><div align="left">本文源码 <a href="http://www-128.ibm.com/developerworks/cn/linux/l-mdst/code.zip" target="_self"><u>下载</u></a></div><br /><div align="left"><div align="left"><a name="4">四、小结</a><br />随
着多媒体数据在Internet上所承担的作用变得越来越重要，需要实时传输音频和视频等多媒体数据的场合也将变得越来越多，如IP电话、视频点播、在线
会议等。RTP是用来在Internet上进行实时流媒体传输的一种协议，目前已经被广泛地应用在各种场合，JRTPLIB是一个面向对象的RTP封装
库，利用它可以很方便地完成Linux平台上的实时流媒体编程。</div><br /><div align="left"><div align="left"><a name="resources">参考资料 </a></div><br /><div align="left"><ul><li>1. 在JRTPLIB的网站http://lumumba.luc.ac.be/jori/jrtplib/jrtplib.html上，可以下载到JRTPLIB最新的源码包，并且还能找到一些与RTP相关的资源。<br /><br /></li><li>2. 顾淑珍等编著，宽带增值服务开发实例，北京：机械工业出版社，2002<br /><br /></li><li>3. 黄永峰等编著，IP网络多媒体通信技术，北京：人民邮电出版社，2003</li></ul><br />注：本文作者：肖文鹏      摘自：<a href="http://www.chinavideo.org/developerworks/cn/"><u>developerWorks 中国</u></a>  </div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><img src ="http://www.blogjava.net/soochow_hhb/aggbug/38364.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-30 21:37 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/30/38364.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux连接控制,对tty终端驱动的控制</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/28/37842.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Tue, 28 Mar 2006 09:20:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/28/37842.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/37842.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/28/37842.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/37842.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/37842.html</trackback:ping><description><![CDATA[说明了磁盘文件和设备文件的不同之处,通过对tty的控制,了解了控制终端的一些参数方法,但还有很多问题没有搞明白.<br /><br />
下面是显示终端echo状态的代码,可以按照这个方式去显示和设置其他的参数.<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">termios.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">main()<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">  struct termios info;<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> rv;<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">  rv </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tcgetattr(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">info);<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (rv </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    perror(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">tcgetattr</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">    exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (info.c_lflag </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> ECHO)<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">      printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">echo is on, since its bit is 1\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">    printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">echo is off, since its bit is 0\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">}</span></div><br />
设置施用tcsetattr进行.关于其他参数,情参阅man termios.h了解更多信息.<br /><br /><img src ="http://www.blogjava.net/soochow_hhb/aggbug/37842.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-28 17:20 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/28/37842.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux中命令pwd的实现</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37565.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 27 Mar 2006 02:50:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37565.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/37565.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37565.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/37565.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/37565.html</trackback:ping><description><![CDATA[如果要实现pwd命令，必需对linux的文件系统非常了解。下面我们先从文件系统说起，然后过度到编写pwd的知识。<br />
1、linux的文件系统<br />
　从用户的角度来看，linux的文件系统就是一个目录树，从系统的角度看，是对磁盘划分的抽象。磁盘的第一级抽象：从磁盘到分区；第二级：从磁盘到块序
列，盘片被划分为磁道，磁道被划分为扇区；第三级：从块序列到三个区域的划分。在linux下，磁盘被划分为三个区域：超级块，i-结点和书局区。超级块
是文件系统的第一个块，存放文件系统本身的信息，i-结点存放每个文件的属性『大小，文件所有者，权限等』。数据区存放文件的内容。<br />
2、创建一个文件的过程<br />
    四个步骤：<br />
　　—　存储属性，分配i-结点；<br />
    —　存储数据，维护一个存放文件的块列表；<br />
    —　记录分配情况，在i-结点的磁盘分布区记录了存放文件的块序列；<br />
    —　添加文件名到目录，在目录文件中增加一条记录。<br />
3、目录的工作过程<br />
    目录是包含了文件名子列表的特殊文件，可以使用命令ls -1ia
director来查看对应的i-node号。理解目录和目录的工作过程是理解文件系统的关键。在每个目录文件的列表中有两个特殊的项：.代表本级目
录，..代表上级目录，并且都有对应的i-node号，所以我们可以通过这两个i-node号逐级向上就可以找到当前工作目录的绝对路径。但是什么时间算
到了根目录呢？先执行ls -i /看看结果，可以发现，根目录下的.和..对应的i-node号相同，这可以作为搜索结束的条件。<br /><br />
4、pwd命令的实现<br />
代码如下：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"> * starts in current directory and recursively climbs up to root<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 128, 0);"> * of filesystem, prints top part then prints current part<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"> * use readdir() to get info about each thing<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sys</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">types.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sys</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">stat.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">dirent.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdlib.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">string.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"> #include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">unistd.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"> ino_t get_inode(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> printpathto(ino_t);<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> inum_to_name(ino_t, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">   printpathto(get_inode(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);"> }<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> printpathto(ino_t this_inode)<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 128, 0);">  * prints path leading down to an object with this inode<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 128, 0);">  * kind of recursive<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 128, 0);">  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">   ino_t my_inode;<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> its_name[BUFSIZ];<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(get_inode(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">..</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> this_inode)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">not lead to root</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">   {<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">     chdir(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">..</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);                                </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">up one dir</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">     inum_to_name(this_inode, its_name, BUFSIZ); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">get its name</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">     my_inode </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> get_inode(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);">     printpathto(my_inode);<br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">     printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/%s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, its_name);<br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">   }<br /></span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);"> }<br /></span><span style="color: rgb(0, 128, 128);">43</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">44</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> inum_to_name(ino_t inode_to_find, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">namebuf, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> buflen)<br /></span><span style="color: rgb(0, 128, 128);">45</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">46</span> <span style="color: rgb(0, 128, 0);">  *looks through current directory for a file with this node<br /></span><span style="color: rgb(0, 128, 128);">47</span> <span style="color: rgb(0, 128, 0);">  *number and copies its name into namebuf<br /></span><span style="color: rgb(0, 128, 128);">48</span> <span style="color: rgb(0, 128, 0);">  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">49</span> <span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">50</span> <span style="color: rgb(0, 0, 0);">   DIR </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">dir_ptr;<br /></span><span style="color: rgb(0, 128, 128);">51</span> <span style="color: rgb(0, 0, 0);">   struct dirent </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">direntp;<br /></span><span style="color: rgb(0, 128, 128);">52</span> <span style="color: rgb(0, 0, 0);">   dir_ptr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> opendir(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">53</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(dir_ptr </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);">54</span> <span style="color: rgb(0, 0, 0);">   {<br /></span><span style="color: rgb(0, 128, 128);">55</span> <span style="color: rgb(0, 0, 0);">     perror(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">56</span> <span style="color: rgb(0, 0, 0);">     exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">57</span> <span style="color: rgb(0, 0, 0);">   }<br /></span><span style="color: rgb(0, 128, 128);">58</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">((direntp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> readdir(dir_ptr)) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);">59</span> <span style="color: rgb(0, 0, 0);">   {<br /></span><span style="color: rgb(0, 128, 128);">60</span> <span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (direntp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">d_ino </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> inode_to_find)<br /></span><span style="color: rgb(0, 128, 128);">61</span> <span style="color: rgb(0, 0, 0);">     {<br /></span><span style="color: rgb(0, 128, 128);">62</span> <span style="color: rgb(0, 0, 0);">       strncpy(namebuf, direntp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">d_name, buflen);<br /></span><span style="color: rgb(0, 128, 128);">63</span> <span style="color: rgb(0, 0, 0);">       namebuf[buflen </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">\0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">64</span> <span style="color: rgb(0, 0, 0);">       closedir(dir_ptr);<br /></span><span style="color: rgb(0, 128, 128);">65</span> <span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> ;<br /></span><span style="color: rgb(0, 128, 128);">66</span> <span style="color: rgb(0, 0, 0);">     }<br /></span><span style="color: rgb(0, 128, 128);">67</span> <span style="color: rgb(0, 0, 0);">   }<br /></span><span style="color: rgb(0, 128, 128);">68</span> <span style="color: rgb(0, 0, 0);">   fprintf(stderr, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">error looking for inum %d\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, inode_to_find);<br /></span><span style="color: rgb(0, 128, 128);">69</span> <span style="color: rgb(0, 0, 0);">   exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">70</span> <span style="color: rgb(0, 0, 0);"> }<br /></span><span style="color: rgb(0, 128, 128);">71</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">72</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">73</span> <span style="color: rgb(0, 0, 0);"> ino_t get_inode(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">fname)<br /></span><span style="color: rgb(0, 128, 128);">74</span> <span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">75</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">76</span> <span style="color: rgb(0, 128, 0);">    * return inode number of the file<br /></span><span style="color: rgb(0, 128, 128);">77</span> <span style="color: rgb(0, 128, 0);">    </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">78</span> <span style="color: rgb(0, 0, 0);">    struct stat info;<br /></span><span style="color: rgb(0, 128, 128);">79</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">((stat(fname, </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">info)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">80</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);">81</span> <span style="color: rgb(0, 0, 0);">      fprintf(stderr, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Cannot stat</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">82</span> <span style="color: rgb(0, 0, 0);">      perror(fname);<br /></span><span style="color: rgb(0, 128, 128);">83</span> <span style="color: rgb(0, 0, 0);">      exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">84</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">85</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">86</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> info.st_ino;<br /></span><span style="color: rgb(0, 128, 128);">87</span> <span style="color: rgb(0, 0, 0);"> }<br /></span><span style="color: rgb(0, 128, 128);">88</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">89</span> <span style="color: rgb(0, 0, 0);"></span></div><br />
结论：<br />
　　对于理解系统编程，理解操作系统，学习相关的命令，并探究命令的实现过程，最后自给编写一个同样功能的命令，不失为一种很好的学习方法。<br /><img src ="http://www.blogjava.net/soochow_hhb/aggbug/37565.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-27 10:50 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37565.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux中命令ls的实现</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37532.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 27 Mar 2006 00:47:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37532.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/37532.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37532.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/37532.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/37532.html</trackback:ping><description><![CDATA[基本实现了明令ls -l的功能，能显示列表，但还有如下缺陷：结果没有排序，不能显示总大小，不能显示其他目录下的文件列表。<br /><br />
程序中使用了掩码的技术来实现数值到文件类型、文件权限列表字符串的转换，使用命令getpwuid(uid_t uid)将用户id转换为名子，使用getgrgid(gid_t gid)将组id转换为组名。<br /><br />
下面是代码：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">  1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sys</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">types.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  3</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sys</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">stat.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  4</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">dirent.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  5</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">string.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  6</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  7</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do_ls(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">[]);<br /></span><span style="color: rgb(0, 128, 128);">  8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> dostat(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">  9</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_file_info(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">filename, struct stat </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">buf);<br /></span><span style="color: rgb(0, 128, 128);"> 10</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> mode_to_letters(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">[]);<br /></span><span style="color: rgb(0, 128, 128);"> 11</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">uid_to_name(uid_t);<br /></span><span style="color: rgb(0, 128, 128);"> 12</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">gid_to_name(gid_t);<br /></span><span style="color: rgb(0, 128, 128);"> 13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 14</span> <span style="color: rgb(0, 0, 0);">main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ac, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">av[])<br /></span><span style="color: rgb(0, 128, 128);"> 15</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 16</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (ac </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 17</span> <span style="color: rgb(0, 0, 0);">      do_ls(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 18</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 19</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">ac)<br /></span><span style="color: rgb(0, 128, 128);"> 20</span> <span style="color: rgb(0, 0, 0);">      {<br /></span><span style="color: rgb(0, 128, 128);"> 21</span> <span style="color: rgb(0, 0, 0);">        printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%s: \n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">*++</span><span style="color: rgb(0, 0, 0);">av);<br /></span><span style="color: rgb(0, 128, 128);"> 22</span> <span style="color: rgb(0, 0, 0);">        do_ls(</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">av);<br /></span><span style="color: rgb(0, 128, 128);"> 23</span> <span style="color: rgb(0, 0, 0);">      }<br /></span><span style="color: rgb(0, 128, 128);"> 24</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 25</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 26</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do_ls(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> dirname[])<br /></span><span style="color: rgb(0, 128, 128);"> 27</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 28</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 29</span> <span style="color: rgb(0, 128, 0);">   * list files in directory called dirname<br /></span><span style="color: rgb(0, 128, 128);"> 30</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 31</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 32</span> <span style="color: rgb(0, 0, 0);">  DIR </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">dir_ptr;             </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">the directory</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 33</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  struct dirent </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">direntp;   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">each entry</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 34</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 35</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( (dir_ptr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> opendir(dirname)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);"> 36</span> <span style="color: rgb(0, 0, 0);">     fprintf(stderr, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ls1:cannot open %s\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, dirname);<br /></span><span style="color: rgb(0, 128, 128);"> 37</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 38</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);"> 39</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> ( (direntp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> readdir(dir_ptr)) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);"> 40</span> <span style="color: rgb(0, 0, 0);">        dostat(direntp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">d_name);<br /></span><span style="color: rgb(0, 128, 128);"> 41</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 42</span> <span style="color: rgb(0, 0, 0);">    closedir(dir_ptr);<br /></span><span style="color: rgb(0, 128, 128);"> 43</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);"> 44</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 45</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 46</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 47</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 48</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> dostat(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">filename)<br /></span><span style="color: rgb(0, 128, 128);"> 49</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 50</span> <span style="color: rgb(0, 0, 0);">  struct stat info;<br /></span><span style="color: rgb(0, 128, 128);"> 51</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (stat(filename, </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">info) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 52</span> <span style="color: rgb(0, 0, 0);">     perror(filename);<br /></span><span style="color: rgb(0, 128, 128);"> 53</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 54</span> <span style="color: rgb(0, 0, 0);">     show_file_info(filename, </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">info);<br /></span><span style="color: rgb(0, 128, 128);"> 55</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 56</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 57</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_file_info(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">filename, struct stat </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">buf)<br /></span><span style="color: rgb(0, 128, 128);"> 58</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 59</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 60</span> <span style="color: rgb(0, 128, 0);">   * display some info stat in a name = value formant<br /></span><span style="color: rgb(0, 128, 128);"> 61</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 62</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">uid_to_name(uid_t);<br /></span><span style="color: rgb(0, 128, 128);"> 63</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> ctime(time_t </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 64</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> gid_to_name(gid_t), filemode();<br /></span><span style="color: rgb(0, 128, 128);"> 65</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">void mode_to_letters();</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 66</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> modestr[</span><span style="color: rgb(0, 0, 0);">11</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 128, 128);"> 67</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 68</span> <span style="color: rgb(0, 0, 0);">   mode_to_letters(buf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_mode, modestr);<br /></span><span style="color: rgb(0, 128, 128);"> 69</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 70</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, modestr);<br /></span><span style="color: rgb(0, 128, 128);"> 71</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%4d</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,  buf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_nlink);<br /></span><span style="color: rgb(0, 128, 128);"> 72</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%-8s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,   uid_to_name(buf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_uid));<br /></span><span style="color: rgb(0, 128, 128);"> 73</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%-8s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,  gid_to_name(buf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_gid));<br /></span><span style="color: rgb(0, 128, 128);"> 74</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%8ld</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,   (</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);">)buf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_size);<br /></span><span style="color: rgb(0, 128, 128);"> 75</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">printf("modtime:%d\n",buf-&gt;st_mtime);</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 76</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%.12s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">ctime(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">buf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_mtime));<br /></span><span style="color: rgb(0, 128, 128);"> 77</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%s\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, filename);  <br /></span><span style="color: rgb(0, 128, 128);"> 78</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 79</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 80</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 81</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 82</span> <span style="color: rgb(0, 128, 0);"> * utility functions<br /></span><span style="color: rgb(0, 128, 128);"> 83</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 84</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 85</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> mode_to_letters(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> mode, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> str[])<br /></span><span style="color: rgb(0, 128, 128);"> 86</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 87</span> <span style="color: rgb(0, 0, 0);">  strcpy(str, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">----------</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 88</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 89</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(S_ISDIR(mode)) str[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">d</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 90</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(S_ISCHR(mode)) str[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">c</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 91</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(S_ISBLK(mode)) str[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">b</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 92</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 93</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IRUSR) str[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 94</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IWUSR) str[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 95</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IXUSR) str[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">x</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 96</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 97</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IRGRP) str[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 98</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IWGRP) str[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 99</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IXGRP) str[</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">x</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">100</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">101</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IROTH) str[</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">102</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IWOTH) str[</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">w</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">103</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(mode </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> S_IXOTH) str[</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">x</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;  <br /></span><span style="color: rgb(0, 128, 128);">104</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">105</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">106</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">pwd.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">107</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">108</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">uid_to_name(uid_t uid)<br /></span><span style="color: rgb(0, 128, 128);">109</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">110</span> <span style="color: rgb(0, 128, 0);"> * returns pointer to username associated with uid, uses getpw()<br /></span><span style="color: rgb(0, 128, 128);">111</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">112</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">113</span> <span style="color: rgb(0, 0, 0);">  struct passwd  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pw_ptr;<br /></span><span style="color: rgb(0, 128, 128);">114</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> numstr[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 128, 128);">115</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">116</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( (pw_ptr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getpwuid(uid)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);">117</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);">118</span> <span style="color: rgb(0, 0, 0);">    printf(numstr, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%d</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, uid);<br /></span><span style="color: rgb(0, 128, 128);">119</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> numstr;<br /></span><span style="color: rgb(0, 128, 128);">120</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">121</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">122</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> pw_ptr</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">pw_name;<br /></span><span style="color: rgb(0, 128, 128);">123</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">124</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">125</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">grp.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">126</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">127</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">gid_to_name(gid_t gid)<br /></span><span style="color: rgb(0, 128, 128);">128</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">129</span> <span style="color: rgb(0, 0, 0);">  struct group </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">grp_ptr;<br /></span><span style="color: rgb(0, 128, 128);">130</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> numstr[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 128, 128);">131</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">132</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((grp_ptr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getgrgid(gid)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);">133</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);">134</span> <span style="color: rgb(0, 0, 0);">    sprintf(numstr, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%d</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, gid);<br /></span><span style="color: rgb(0, 128, 128);">135</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> numstr;<br /></span><span style="color: rgb(0, 128, 128);">136</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">137</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">138</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> grp_ptr</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">gr_name;<br /></span><span style="color: rgb(0, 128, 128);">139</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">140</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">141</span> <span style="color: rgb(0, 0, 0);"></span></div><br />
争取早日将其他功能实现。<br /><img src ="http://www.blogjava.net/soochow_hhb/aggbug/37532.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-27 08:47 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37532.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux中命令who的实现</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37531.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 27 Mar 2006 00:41:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37531.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/37531.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37531.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/37531.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/37531.html</trackback:ping><description><![CDATA[实现了linux中w ho 命令，采用了缓冲机制，一次从utmp文件中读取16条数据，这样可以大大提高性能。<br /><br />
下面是代码，采用了缓冲机制：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">  1</span> <span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  2</span> <span style="color: rgb(0, 128, 0);"> * what is diffient to who1 is that we add a buffer area in who2.c.<br /></span><span style="color: rgb(0, 128, 128);">  3</span> <span style="color: rgb(0, 128, 0);"> *  In this way, the efferency is improved .<br /></span><span style="color: rgb(0, 128, 128);">  4</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  5</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  6</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">utmp.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  7</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fcntl.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  8</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">unistd.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  9</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdlib.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 10</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">time.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 12</span> <span style="color: rgb(0, 0, 0);">#define SHOWHOST          </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">include remote machine on output<br /></span><span style="color: rgb(0, 128, 128);"> 13</span> <span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 14</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">-------------------------------------------------------------------------</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 15</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">#define NRECS   </span><span style="color: rgb(0, 0, 0);">16</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 16</span> <span style="color: rgb(0, 0, 0);">#define NULLUT ((struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">)NULL)<br /></span><span style="color: rgb(0, 128, 128);"> 17</span> <span style="color: rgb(0, 0, 0);">#define UTSIZE (sizeof(struct utmp))<br /></span><span style="color: rgb(0, 128, 128);"> 18</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 19</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> utmpbuf[NRECS </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> UTSIZE];            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">storage</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 20</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">  num_recs;                           </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">num stored</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 21</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">  cur_rec;                            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">next to go</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 22</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">  fd_utmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;                       </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">read from</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 23</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 24</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> utmp_open(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">filename)<br /></span><span style="color: rgb(0, 128, 128);"> 25</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 26</span> <span style="color: rgb(0, 0, 0);">  fd_utmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open(filename, O_RDONLY);<br /></span><span style="color: rgb(0, 128, 128);"> 27</span> <span style="color: rgb(0, 0, 0);">  cur_rec </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> num_recs </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 28</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> fd_utmp;<br /></span><span style="color: rgb(0, 128, 128);"> 29</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 30</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 31</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> utmp_reload()<br /></span><span style="color: rgb(0, 128, 128);"> 32</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 33</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> amt_read;<br /></span><span style="color: rgb(0, 128, 128);"> 34</span> <span style="color: rgb(0, 0, 0);">  amt_read </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> read(fd_utmp, utmpbuf, NRECS </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> UTSIZE);<br /></span><span style="color: rgb(0, 128, 128);"> 35</span> <span style="color: rgb(0, 0, 0);">  num_recs </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> amt_read</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">UTSIZE;                        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">how many did we get?</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 36</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  cur_rec </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;                                       </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">reset pointer</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 37</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 38</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> num_recs;<br /></span><span style="color: rgb(0, 128, 128);"> 39</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 40</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 41</span> <span style="color: rgb(0, 0, 0);">struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> utmp_next()<br /></span><span style="color: rgb(0, 128, 128);"> 42</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 43</span> <span style="color: rgb(0, 0, 0);">  struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> recp;<br /></span><span style="color: rgb(0, 128, 128);"> 44</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( fd_utmp </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> NULLUT;<br /></span><span style="color: rgb(0, 128, 128);"> 45</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 46</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cur_rec </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> num_recs </span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);"> utmp_reload() </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> NULLUT;<br /></span><span style="color: rgb(0, 128, 128);"> 47</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 48</span> <span style="color: rgb(0, 0, 0);">  recp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">utmpbuf[cur_rec </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> UTSIZE];<br /></span><span style="color: rgb(0, 128, 128);"> 49</span> <span style="color: rgb(0, 0, 0);">  cur_rec</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 50</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> recp;<br /></span><span style="color: rgb(0, 128, 128);"> 51</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 52</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 53</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> utmp_close()<br /></span><span style="color: rgb(0, 128, 128);"> 54</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 55</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(fd_utmp </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) close(fd_utmp);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">don't close if not open</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 56</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 57</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 58</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">-------------------------------------------------------------------------</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 59</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 60</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_info(struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 61</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showtime(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> timeval);<br /></span><span style="color: rgb(0, 128, 128);"> 62</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 63</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br /></span><span style="color: rgb(0, 128, 128);"> 64</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 65</span> <span style="color: rgb(0, 0, 0);">  struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> utbufp;       </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">holds pointer to next rec</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 66</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> utmp_next();  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">return pointer to next</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 67</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 68</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(utmp_open(UTMP_FILE) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 69</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);"> 70</span> <span style="color: rgb(0, 0, 0);">    perror(UTMP_FILE);<br /></span><span style="color: rgb(0, 128, 128);"> 71</span> <span style="color: rgb(0, 0, 0);">    exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 72</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);"> 73</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> ( (utbufp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> utmp_next() ) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> ((struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">) NULL) )<br /></span><span style="color: rgb(0, 128, 128);"> 74</span> <span style="color: rgb(0, 0, 0);">    show_info(utbufp);<br /></span><span style="color: rgb(0, 128, 128);"> 75</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 76</span> <span style="color: rgb(0, 0, 0);">  utmp_close();<br /></span><span style="color: rgb(0, 128, 128);"> 77</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 78</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 79</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 80</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 81</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 82</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 83</span> <span style="color: rgb(0, 128, 0);"> * display contents of the utmp struct <br /></span><span style="color: rgb(0, 128, 128);"> 84</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 85</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_info( struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> utbufp)<br /></span><span style="color: rgb(0, 128, 128);"> 86</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 87</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 88</span> <span style="color: rgb(0, 128, 0);">   * remove the blank record<br /></span><span style="color: rgb(0, 128, 128);"> 89</span> <span style="color: rgb(0, 128, 0);">   *  the entry ut_type in struct utmp indicate the types<br /></span><span style="color: rgb(0, 128, 128);"> 90</span> <span style="color: rgb(0, 128, 0);">   *   USER_PROCESS is the auser process<br /></span><span style="color: rgb(0, 128, 128);"> 91</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 92</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);"> ut_type </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> USER_PROCESS) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 93</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 94</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">% -8.8s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_name);<br /></span><span style="color: rgb(0, 128, 128);"> 95</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 96</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">% -12.12s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_line);<br /></span><span style="color: rgb(0, 128, 128);"> 97</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 98</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">printf("%101d", utbufp-&gt;ut_time);</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 99</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  showtime(utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_time);<br /></span><span style="color: rgb(0, 128, 128);">100</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">101</span> <span style="color: rgb(0, 0, 0);">  #ifdef SHOWHOST<br /></span><span style="color: rgb(0, 128, 128);">102</span> <span style="color: rgb(0, 0, 0);">    printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">(%s)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_host);<br /></span><span style="color: rgb(0, 128, 128);">103</span> <span style="color: rgb(0, 0, 0);">  #endif<br /></span><span style="color: rgb(0, 128, 128);">104</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);  <br /></span><span style="color: rgb(0, 128, 128);">105</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">106</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">107</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showtime(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> timeval)<br /></span><span style="color: rgb(0, 128, 128);">108</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">109</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">110</span> <span style="color: rgb(0, 128, 0);">   * display time<br /></span><span style="color: rgb(0, 128, 128);">111</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">112</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">cp;<br /></span><span style="color: rgb(0, 128, 128);">113</span> <span style="color: rgb(0, 0, 0);">   cp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ctime(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">timeval);<br /></span><span style="color: rgb(0, 128, 128);">114</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%12.12s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, cp</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">115</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">116</span> <span style="color: rgb(0, 0, 0);"></span></div>下面的代码没有采用缓冲机制，每次读取一条记录：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">utmp.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fcntl.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">unistd.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdlib.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">time.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">#define SHOWHOST          </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">include remote machine on output</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_info(struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showtime(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> timeval);<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">  struct utmp current_record;    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">read info into here</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">              utmpfd;           </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">read from this descriptor</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">            reclen </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sizeof(current_record);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">how many bytes to read</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( (utmpfd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open(UTMP_FILE, O_RDONLY)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">    perror(UTMP_FILE);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">UTMP_FILE is in utmp.h</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">    exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 128, 0);">   * read data structure from UTMP_FILE<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">( read(utmpfd, </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">current_record, reclen) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> reclen)<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">        show_info(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">current_record);<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">  close(utmpfd);<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> return 0;</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 128, 0);"> * display contents of the utmp struct <br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_info( struct utmp </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> utbufp)<br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 128, 0);">   * remove the blank record<br /></span><span style="color: rgb(0, 128, 128);">43</span> <span style="color: rgb(0, 128, 0);">   *  the entry ut_type in struct utmp indicate the types<br /></span><span style="color: rgb(0, 128, 128);">44</span> <span style="color: rgb(0, 128, 0);">   *   USER_PROCESS is the auser process<br /></span><span style="color: rgb(0, 128, 128);">45</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">46</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);"> ut_type </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> USER_PROCESS) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">47</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);">48</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">% -8.8s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_name);<br /></span><span style="color: rgb(0, 128, 128);">49</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">50</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">% -12.12s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_line);<br /></span><span style="color: rgb(0, 128, 128);">51</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">52</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">printf("%101d", utbufp-&gt;ut_time);</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">53</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  showtime(utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_time);<br /></span><span style="color: rgb(0, 128, 128);">54</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">55</span> <span style="color: rgb(0, 0, 0);">  #ifdef SHOWHOST<br /></span><span style="color: rgb(0, 128, 128);">56</span> <span style="color: rgb(0, 0, 0);">    printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">(%s)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, utbufp</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">ut_host);<br /></span><span style="color: rgb(0, 128, 128);">57</span> <span style="color: rgb(0, 0, 0);">  #endif<br /></span><span style="color: rgb(0, 128, 128);">58</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);  <br /></span><span style="color: rgb(0, 128, 128);">59</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">60</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">61</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showtime(</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> timeval)<br /></span><span style="color: rgb(0, 128, 128);">62</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">63</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">64</span> <span style="color: rgb(0, 128, 0);">   * display time<br /></span><span style="color: rgb(0, 128, 128);">65</span> <span style="color: rgb(0, 128, 0);">   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">66</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">cp;<br /></span><span style="color: rgb(0, 128, 128);">67</span> <span style="color: rgb(0, 0, 0);">   cp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ctime(</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">timeval);<br /></span><span style="color: rgb(0, 128, 128);">68</span> <span style="color: rgb(0, 0, 0);">   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%12.12s</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, cp</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">69</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">70</span> <span style="color: rgb(0, 0, 0);"></span></div><br />
可以对比一下性能，记录少的情况下不明显，但是在记录超过1000条时就可以明显看出来了。这个大家可以做一下实验。<br /><br />
这个who命令实现的还算比较完美。<br /><img src ="http://www.blogjava.net/soochow_hhb/aggbug/37531.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-27 08:41 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37531.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux中命令more的实现</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37530.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 27 Mar 2006 00:36:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37530.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/37530.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37530.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/37530.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/37530.html</trackback:ping><description><![CDATA[实现了命令more的基本功能，但还有如下缺陷：more的反白先是不能固定，不能显示百分比，在输入命令后必需按回车。<br /><br />
下面是代码，在FC4上使用gcc 编译通过。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">  1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdlib.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  4</span> <span style="color: rgb(0, 0, 0);">#define PAGELEN </span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  5</span> <span style="color: rgb(0, 0, 0);">#define LINELEN </span><span style="color: rgb(0, 0, 0);">512</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  6</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  7</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do_more(FILE </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">  8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> see_more();<br /></span><span style="color: rgb(0, 128, 128);">  9</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> see_more(FILE </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 10</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 12</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ac, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">av[])<br /></span><span style="color: rgb(0, 128, 128);"> 13</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 14</span> <span style="color: rgb(0, 0, 0);">  FILE </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">fp;<br /></span><span style="color: rgb(0, 128, 128);"> 15</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 16</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (ac </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 17</span> <span style="color: rgb(0, 0, 0);">       do_more(stdin);<br /></span><span style="color: rgb(0, 128, 128);"> 18</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 19</span> <span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">ac)<br /></span><span style="color: rgb(0, 128, 128);"> 20</span> <span style="color: rgb(0, 0, 0);">           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen(</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">av, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> NULL)<br /></span><span style="color: rgb(0, 128, 128);"> 21</span> <span style="color: rgb(0, 0, 0);">           {<br /></span><span style="color: rgb(0, 128, 128);"> 22</span> <span style="color: rgb(0, 0, 0);">             do_more(fp);<br /></span><span style="color: rgb(0, 128, 128);"> 23</span> <span style="color: rgb(0, 0, 0);">             fclose(fp);<br /></span><span style="color: rgb(0, 128, 128);"> 24</span> <span style="color: rgb(0, 0, 0);">           }<br /></span><span style="color: rgb(0, 128, 128);"> 25</span> <span style="color: rgb(0, 0, 0);">           </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 26</span> <span style="color: rgb(0, 0, 0);">             exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 27</span> <span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);"> 28</span> <span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 29</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 30</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 31</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> do_more(FILE </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">fp)<br /></span><span style="color: rgb(0, 128, 128);"> 32</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 33</span> <span style="color: rgb(0, 128, 0);"> *  read PAGELEN lines, then call see_more() for further instructions<br /></span><span style="color: rgb(0, 128, 128);"> 34</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 35</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 36</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> line[LINELEN];<br /></span><span style="color: rgb(0, 128, 128);"> 37</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> num_of_lines </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 38</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> see_more(FILE </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">), reply;<br /></span><span style="color: rgb(0, 128, 128);"> 39</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 40</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 41</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> get input from stardard input device</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 42</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 43</span> <span style="color: rgb(0, 128, 0);">   FILE *fp_tty;<br /></span><span style="color: rgb(0, 128, 128);"> 44</span> <span style="color: rgb(0, 128, 0);">   fp_tty = fopen("/dev/tty", "r");<br /></span><span style="color: rgb(0, 128, 128);"> 45</span> <span style="color: rgb(0, 128, 0);">   if (fp_tty = NULL) <br /></span><span style="color: rgb(0, 128, 128);"> 46</span> <span style="color: rgb(0, 128, 0);">       exit(1);</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 47</span> <span style="color: rgb(0, 0, 0);">   <br /></span><span style="color: rgb(0, 128, 128);"> 48</span> <span style="color: rgb(0, 0, 0);">   FILE    </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">fp_tty;<br /></span><span style="color: rgb(0, 128, 128);"> 49</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 50</span> <span style="color: rgb(0, 0, 0);">    fp_tty </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/dev/tty</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">r</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );        <br /></span><span style="color: rgb(0, 128, 128);"> 51</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( fp_tty </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL )               <br /></span><span style="color: rgb(0, 128, 128);"> 52</span> <span style="color: rgb(0, 0, 0);">        exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);                           <br /></span><span style="color: rgb(0, 128, 128);"> 53</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 54</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (fgets(line, LINELEN, fp))<br /></span><span style="color: rgb(0, 128, 128);"> 55</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);"> 56</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( num_of_lines </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> PAGELEN)<br /></span><span style="color: rgb(0, 128, 128);"> 57</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);"> 58</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">printf("\n====== \n");<br /></span><span style="color: rgb(0, 128, 128);"> 59</span> <span style="color: rgb(0, 128, 0);">      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">reply = see_more();</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 60</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">      reply </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> see_more(fp_tty);<br /></span><span style="color: rgb(0, 128, 128);"> 61</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (reply </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 62</span> <span style="color: rgb(0, 0, 0);">      num_of_lines </span><span style="color: rgb(0, 0, 0);">-=</span><span style="color: rgb(0, 0, 0);"> reply;<br /></span><span style="color: rgb(0, 128, 128);"> 63</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);"> 64</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 65</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (fputs(line, stdout) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> EOF) <br /></span><span style="color: rgb(0, 128, 128);"> 66</span> <span style="color: rgb(0, 0, 0);">        exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 67</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 68</span> <span style="color: rgb(0, 0, 0);">    num_of_lines</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 69</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 70</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">printf("\n====== num_of_lines = %d \n", num_of_lines);</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 71</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);"> 72</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 73</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 74</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> see_more()<br /></span><span style="color: rgb(0, 128, 128);"> 75</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 76</span> <span style="color: rgb(0, 128, 0);"> *  print message , wait for response, return # of lines to advance<br /></span><span style="color: rgb(0, 128, 128);"> 77</span> <span style="color: rgb(0, 128, 0);"> *  q means no, space means yes, CR means one line.<br /></span><span style="color: rgb(0, 128, 128);"> 78</span> <span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 79</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 80</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c;<br /></span><span style="color: rgb(0, 128, 128);"> 81</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\033[7m more? \033[m</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 82</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> ( (c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getchar()) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> EOF)<br /></span><span style="color: rgb(0, 128, 128);"> 83</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);"> 84</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( c </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">q</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 85</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( c </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> PAGELEN;<br /></span><span style="color: rgb(0, 128, 128);"> 86</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( c </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 87</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);"> 88</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 89</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 90</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 91</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 92</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> see_more(FILE </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">cmd)<br /></span><span style="color: rgb(0, 128, 128);"> 93</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 94</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c;<br /></span><span style="color: rgb(0, 128, 128);"> 95</span> <span style="color: rgb(0, 0, 0);">  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\033[7m more? \033[m</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 96</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> ( (c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getc(cmd)) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> EOF)<br /></span><span style="color: rgb(0, 128, 128);"> 97</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);"> 98</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( c </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">q</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 99</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( c </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> PAGELEN;<br /></span><span style="color: rgb(0, 128, 128);">100</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( c </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">101</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">102</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">103</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">104</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">105</span> <span style="color: rgb(0, 0, 0);"></span></div>有两个子函数，do_more()和see_more()，前者处理显示，后者处理命令。<br /><br />
以后在进行改进。<br /><img src ="http://www.blogjava.net/soochow_hhb/aggbug/37530.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-27 08:36 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37530.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C中传地址和传指针比较</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37529.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 27 Mar 2006 00:28:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37529.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/37529.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/37529.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/37529.html</trackback:ping><description><![CDATA[下面是在linux下察看文件大小的代码,比较了C中传地址和传指针的用法,并指出了不同点和使用指针时的注意事项.<br /><br />
传地址:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sys</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">stat.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> filename </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/etc/passwd</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">  struct stat infobuf1;<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (stat(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/etc/passwd</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">infobuf) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)  <br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">       perror(filename);       <br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">  {<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">      printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">The size of %s is %d\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, filename,  infobuf.st_size);    <br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"></span></div>必需使用地址符&amp;,在引用结构体内的元素时使用.运算符.<br /><br />
传指针:<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">sys</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">stat.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> filename </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/etc/passwd</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">  struct stat infobuf1;<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">  struct stat </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> infobuf </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">infobuf1;<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(stat(filename, infobuf) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">       perror(filename);       <br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">  {      <br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">The size of %s is %d\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, filename,  infobuf</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">st_size);<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">  <br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);"></span></div>在第9行声明指针后,必需要进行初始化,否则在linux下,被初始化为一个随机的地址,在第11行对其操作时酒会发声错误(在linux下提示"段错误").在第15行,引用结构体内的元素时,使用地址运算符-&gt;,或者使用(*infobuf).st_size.<br /><br />
总结:<br />
    1、指针引用前必需初始化，初始化最简单的办法就是声明一个同类型的变量，因为变量会被初始化，会被分配内存空间，所以指针就有指的地方了。如：传指针程序段中的第8，9行。<br />
    2、理解传地址和传指针的区别。不管怎么说都是要一个地址，譬如在函数stat(char *
filename, struct stat * buf),
对于buf传入时必须是一个地址，不管是指针或是一个变量的地址，归根都是一样的，都是内存中的同一块区域。<br />
    3、对于函数声明为stat(char &amp;filename, struct stat &amp;buf)的形式，传入时就不能是地址或者指针，而必需是变量了。<br /><br /><img src ="http://www.blogjava.net/soochow_hhb/aggbug/37529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-27 08:28 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/27/37529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Makefile文件编写</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/03/09/34538.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Thu, 09 Mar 2006 11:23:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/03/09/34538.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/34538.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/03/09/34538.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/34538.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/34538.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US></SPAN><SPAN style="FONT-FAMILY: 宋体"></SPAN><SPAN lang=EN-US></SPAN><FONT size=2><SPAN style="FONT-FAMILY: 宋体">开始使用</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体">编程时，一个很讨厌的问题就是如何写</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件，由于在</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体">下不像在</SPAN><SPAN lang=EN-US>Windows</SPAN><SPAN style="FONT-FAMILY: 宋体">下那么熟悉，有那么多好的软件（也许是对</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体">孤陋寡闻了）。虽然象</SPAN><SPAN lang=EN-US>Kylix</SPAN><SPAN style="FONT-FAMILY: 宋体">和</SPAN><SPAN lang=EN-US>Anjuta</SPAN><SPAN style="FONT-FAMILY: 宋体">这样的集成编译环境，但是</SPAN><SPAN lang=EN-US>Kylix</SPAN><SPAN style="FONT-FAMILY: 宋体">太大太慢，用它编写</SPAN><SPAN lang=EN-US>console</SPAN><SPAN style="FONT-FAMILY: 宋体">程序不亚于高射炮打蚊子——大材小用，而</SPAN><SPAN lang=EN-US>Anjuta</SPAN><SPAN style="FONT-FAMILY: 宋体">又太不稳定，况且字体有那么难看。不说了，还是言归正传，看看</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">该如何编写。</SPAN></FONT></P><BR>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>1. </SPAN><SPAN style="FONT-FAMILY: 宋体">简单的</SPAN><SPAN lang=EN-US>GCC</SPAN><SPAN style="FONT-FAMILY: 宋体">语法：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">如果你只有一个文件（或者只有几个文件），那么就可以不写</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件（当然有</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">更加方便），用</SPAN><SPAN lang=EN-US>gcc</SPAN><SPAN style="FONT-FAMILY: 宋体">直接编译就行了。在这里我们只介绍几个我经常用的几个参数，第一是</SPAN> <SPAN lang=EN-US>“-o”</SPAN><SPAN style="FONT-FAMILY: 宋体">，它后面的参数表示要输出的目标文件，再一个是</SPAN> <SPAN lang=EN-US>“-c”</SPAN><SPAN style="FONT-FAMILY: 宋体">，表示仅编译（</SPAN><SPAN lang=EN-US>Compile</SPAN><SPAN style="FONT-FAMILY: 宋体">），不连接（</SPAN><SPAN lang=EN-US>Make</SPAN><SPAN style="FONT-FAMILY: 宋体">），如果没有</SPAN><SPAN lang=EN-US>”-c”</SPAN><SPAN style="FONT-FAMILY: 宋体">参数，那么就表示连接，如下面的几个命令：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –c test.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，表示只编译</SPAN><SPAN lang=EN-US>test.c</SPAN><SPAN style="FONT-FAMILY: 宋体">文件，成功时输出目标文件</SPAN><SPAN lang=EN-US>test.o</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –c test.c –o test.o </SPAN><SPAN style="FONT-FAMILY: 宋体">，与上一条命令完全相同</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –o test test.o</SPAN><SPAN style="FONT-FAMILY: 宋体">，将</SPAN><SPAN lang=EN-US>test.o</SPAN><SPAN style="FONT-FAMILY: 宋体">连接成可执行的二进制文件</SPAN><SPAN lang=EN-US>test</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –o test test.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，将</SPAN><SPAN lang=EN-US>test.c</SPAN><SPAN style="FONT-FAMILY: 宋体">编译并连接成可执行的二进制文件</SPAN><SPAN lang=EN-US>test</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc test.c –o test</SPAN><SPAN style="FONT-FAMILY: 宋体">，与上一条命令相同</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –c test1.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，只编译</SPAN><SPAN lang=EN-US>test1.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，成功时输出目标文件</SPAN><SPAN lang=EN-US>test1.o</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –c test2.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，只编译</SPAN><SPAN lang=EN-US>test2.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，成功时输出目标文件</SPAN><SPAN lang=EN-US>test2.o</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –o test test1.o test2.o</SPAN><SPAN style="FONT-FAMILY: 宋体">，将</SPAN><SPAN lang=EN-US>test1.o</SPAN><SPAN style="FONT-FAMILY: 宋体">和</SPAN><SPAN lang=EN-US>test2.o</SPAN><SPAN style="FONT-FAMILY: 宋体">连接为可执行的二进制文件</SPAN><SPAN lang=EN-US>test</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>gcc –c test test1.c test2.c</SPAN><SPAN style="FONT-FAMILY: 宋体">，将</SPAN><SPAN lang=EN-US>test1.o</SPAN><SPAN style="FONT-FAMILY: 宋体">和</SPAN><SPAN lang=EN-US>test2.o</SPAN><SPAN style="FONT-FAMILY: 宋体">编译并连接为可执行的二进制文件</SPAN><SPAN lang=EN-US>test</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">注：如果你想编译</SPAN><SPAN lang=EN-US>cpp</SPAN><SPAN style="FONT-FAMILY: 宋体">文件，那么请用</SPAN><SPAN lang=EN-US>g++</SPAN><SPAN style="FONT-FAMILY: 宋体">，否则会有类似如下莫名其妙的错误：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2>cc3r3i2U.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0’......</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">还有一个参数是</SPAN><SPAN lang=EN-US>”-l”</SPAN><SPAN style="FONT-FAMILY: 宋体">参数，与之紧紧相连的是表示连接时所要的链接库，比如多线程，如果你使用了</SPAN><SPAN lang=EN-US>pthread_create</SPAN><SPAN style="FONT-FAMILY: 宋体">函数，那么你就应该在编译语句的最后加上</SPAN><SPAN lang=EN-US>”-lpthread”</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>”-l”</SPAN><SPAN style="FONT-FAMILY: 宋体">表示连接，</SPAN><SPAN lang=EN-US>”pthread”</SPAN><SPAN style="FONT-FAMILY: 宋体">表示要连接的库，注意他们在这里要连在一起写，还有比如你使用了光标库</SPAN><SPAN lang=EN-US>curses</SPAN><SPAN style="FONT-FAMILY: 宋体">，那么呢就应该在后面加上</SPAN><SPAN lang=EN-US>”-lcurses”</SPAN><SPAN style="FONT-FAMILY: 宋体">，比如下面的写法：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2>gcc –o test test1.o test2.o –lpthread –lcurses</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">当然</SPAN><SPAN lang=EN-US>gcc</SPAN><SPAN style="FONT-FAMILY: 宋体">的参数我感觉有几百个，不过我们平时在</SPAN><SPAN lang=EN-US>x86</SPAN><SPAN style="FONT-FAMILY: 宋体">机器上用的就这么些，况且这里也不是</SPAN><SPAN lang=EN-US>GCC</SPAN><SPAN style="FONT-FAMILY: 宋体">教程，所以，就此打住。</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>2. Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">基本语法</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">我这里没有</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">的详细设计书，只是凭着看别人的</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件和一些网上的参考资料，作一些简单的介绍（我自己理解的，不对的地方还请各位老大们指出，鄙人将不甚感激）</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>2.1 </SPAN><SPAN style="FONT-FAMILY: 宋体">目标：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">大家在看别人使用</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件时肯定经常见到有的人常用</SPAN><SPAN lang=EN-US>make all, make install, make clean</SPAN><SPAN style="FONT-FAMILY: 宋体">等命令，同样只有一个</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件，那么</SPAN><SPAN lang=EN-US>all</SPAN><SPAN style="FONT-FAMILY: 宋体">、</SPAN><SPAN lang=EN-US>install</SPAN><SPAN style="FONT-FAMILY: 宋体">、</SPAN><SPAN lang=EN-US>clean</SPAN><SPAN style="FONT-FAMILY: 宋体">参数是如何控制</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件的运行呢（这句话有问题，但我不知道该怎么说，大家能看懂我的意思，就放我一马吧）？在这里，如果向上面的命令如果能够正确运行的话，那么在</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件里一定有这样的几行，他们的开始一定是</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>all: </SPAN><SPAN style="FONT-FAMILY: 宋体">×××××××</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><SPAN></SPAN><SPAN><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体"><FONT size=2>×××××××××××</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US>install: </SPAN><SPAN style="FONT-FAMILY: 宋体">××××××</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><SPAN></SPAN><SPAN><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体"><FONT size=2>×××××××××××</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US>clean: </SPAN><SPAN style="FONT-FAMILY: 宋体">×××××××××</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体">×××××××××××</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">当然也不尽然，因为</SPAN><SPAN lang=EN-US>all</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>install</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>clean</SPAN><SPAN style="FONT-FAMILY: 宋体">我们可以用其他的变量来代替，但是着了我们就简单起见，就下定论了，各位别怪。</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">在上面提到的</SPAN><SPAN lang=EN-US>all</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>install</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>clean</SPAN><SPAN style="FONT-FAMILY: 宋体">等就是我们所说的目标。</SPAN><SPAN lang=EN-US>make all</SPAN><SPAN style="FONT-FAMILY: 宋体">命令，就告诉</SPAN><SPAN lang=EN-US>make</SPAN><SPAN style="FONT-FAMILY: 宋体">我们将执行</SPAN><SPAN lang=EN-US>all</SPAN><SPAN style="FONT-FAMILY: 宋体">所指定的目标。为了便于理解</SPAN><SPAN lang=EN-US>Make</SPAN><SPAN style="FONT-FAMILY: 宋体">程序的流程，我们给大家看一个与</SPAN><SPAN lang=EN-US>gcc</SPAN><SPAN style="FONT-FAMILY: 宋体">毫无关系的</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US># </SPAN><SPAN style="FONT-FAMILY: 宋体">＃表示</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件中的注释，下面是</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件的具体内容</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><FONT size=2>all:</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make all”</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><FONT size=2>clean:</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make clean”</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><FONT size=2>install:</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@ehco you have typed command “make <B>$@</B>”</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US>#Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件结束</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">注意在这里，</SPAN><SPAN lang=EN-US>all:</SPAN><SPAN style="FONT-FAMILY: 宋体">、</SPAN><SPAN lang=EN-US>clean:</SPAN><SPAN style="FONT-FAMILY: 宋体">、</SPAN><SPAN lang=EN-US>install:</SPAN><SPAN style="FONT-FAMILY: 宋体">行要顶格些，而所有的</SPAN><SPAN lang=EN-US>@echo</SPAN><SPAN style="FONT-FAMILY: 宋体">前要加</SPAN><SPAN lang=EN-US>tab</SPAN><SPAN style="FONT-FAMILY: 宋体">键来跳格缩进。下面是运行结果：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#cat Makefile<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"># </SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">＃表示</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">文件中的注释，下面是</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">文件的具体内容</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><O /></SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>all:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>clean:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>install:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@ehco you have typed command “make <B>$@</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#make all<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#make clean<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#make <B>install</B><O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make <B>install</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">不知大家注意到没有，我们在</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件里有一个符号</SPAN><B><SPAN lang=EN-US>$@</SPAN></B><SPAN style="FONT-FAMILY: 宋体">，其中</SPAN><SPAN lang=EN-US>$</SPAN><SPAN style="FONT-FAMILY: 宋体">表示变量名，其后的要当作变量来解释，</SPAN><SPAN lang=EN-US>$@</SPAN><SPAN style="FONT-FAMILY: 宋体">是</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">预先定义的一个变量，表示目标命令，比如在上面的文件里属于</SPAN><SPAN lang=EN-US>install</SPAN><SPAN style="FONT-FAMILY: 宋体">目标，那么</SPAN><SPAN lang=EN-US>$@</SPAN><SPAN style="FONT-FAMILY: 宋体">就表示</SPAN><SPAN lang=EN-US>install</SPAN><SPAN style="FONT-FAMILY: 宋体">，同样，如果你将</SPAN><SPAN lang=EN-US>clean</SPAN><SPAN style="FONT-FAMILY: 宋体">目标下面的加引号的</SPAN><SPAN lang=EN-US>”make clean”</SPAN><SPAN style="FONT-FAMILY: 宋体">换为：</SPAN><SPAN lang=EN-US>”make $@”</SPAN><SPAN style="FONT-FAMILY: 宋体">，那么命令</SPAN><SPAN lang=EN-US>make clean</SPAN><SPAN style="FONT-FAMILY: 宋体">的输出与原来是一摸一样的。大家可以下来试试。</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>2.2 </SPAN><SPAN style="FONT-FAMILY: 宋体">依赖</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">我们现在提出这样一个问题：我如何用一个</SPAN><SPAN lang=EN-US>make</SPAN><SPAN style="FONT-FAMILY: 宋体">命令将替代所有的</SPAN><SPAN lang=EN-US>make all</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US> make install</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>make clean</SPAN><SPAN style="FONT-FAMILY: 宋体">命令呢？当然我们可以象刚才那样写一个</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#cat Makefile<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"># </SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">＃表示</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">文件中的注释，下面是</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">文件的具体内容</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><O /></SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>all:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>clean:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>install:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@ehco you have typed command “make <B>$@</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>doall:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make <B>$@</B>l”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><SPAN></SPAN><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@ehco you have typed command “make <B>install</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#make doall<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make <B>doall</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make <B>install</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">在这里，</SPAN><SPAN lang=EN-US>doall:</SPAN><SPAN style="FONT-FAMILY: 宋体">目标有</SPAN><SPAN lang=EN-US>4</SPAN><SPAN style="FONT-FAMILY: 宋体">调语句，他们都是连在一起并都是由</SPAN><SPAN lang=EN-US>tab</SPAN><SPAN style="FONT-FAMILY: 宋体">键开始的。当然，这样能够完成任务，但是太笨了，我们这样来写：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#cat Makefile<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><FONT size=2><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"># </SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">＃表示</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">文件中的注释，下面是</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial">文件的具体内容</SPAN><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><O /></SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>all:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>clean:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>install:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@ehco you have typed command “make <B>$@</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>doall: all clean install<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make <B>$@</B>l”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#make doall<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make clean”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make <B>install</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make <B>doall</B>”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">相信大家已经看清了</SPAN><SPAN lang=EN-US>doall:</SPAN><SPAN style="FONT-FAMILY: 宋体">的运行方式，它先运行</SPAN><SPAN lang=EN-US>all</SPAN><SPAN style="FONT-FAMILY: 宋体">目标，然后运行</SPAN><SPAN lang=EN-US>clean</SPAN><SPAN style="FONT-FAMILY: 宋体">目标，然后是</SPAN><SPAN lang=EN-US>install</SPAN><SPAN style="FONT-FAMILY: 宋体">，最后是自己本身的目标，并且每个</SPAN><SPAN lang=EN-US>$@</SPAN><SPAN style="FONT-FAMILY: 宋体">还是保持着各自的目标名称。</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">在这里，我们称</SPAN><SPAN lang=EN-US>all, clean, install</SPAN><SPAN style="FONT-FAMILY: 宋体">为目标</SPAN><SPAN lang=EN-US>doall</SPAN><SPAN style="FONT-FAMILY: 宋体">所依赖的目标，简称为</SPAN><SPAN lang=EN-US>doall</SPAN><SPAN style="FONT-FAMILY: 宋体">的依赖。也就是你要执行</SPAN><SPAN lang=EN-US>doall</SPAN><SPAN style="FONT-FAMILY: 宋体">，请先执行他们（</SPAN><SPAN lang=EN-US>all, clean, install</SPAN><SPAN style="FONT-FAMILY: 宋体">），最后在执行我的代码。</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">注意依赖一定是</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">里面的目标，否则你非要运行，结局是注定的：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#cat Makefile<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>all:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.85pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>xxx: all WAHAHA:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]make xxx<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>you have typed command “make all”<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>make: *** No rule to make target ‘WAHAHA’, needed by `xxx’,<SPAN>&nbsp; </SPAN><B>Stop</B>.<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">【轻松一下】我们能否利于“相互依赖”来作弄一下</SPAN><SPAN lang=EN-US>make</SPAN><SPAN style="FONT-FAMILY: 宋体">？</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#cat Makefile<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>tar1: tar2<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>tar2: tar1<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@echo this line cann’t be shown on you screen!<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]make tar1<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>make: Circular tar2 &lt;- tar1 dependency dropped.<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN style="FONT-FAMILY: 宋体"><FONT size=2>呵呵，骗不了的</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN lang=EN-US>3</SPAN><SPAN style="FONT-FAMILY: 宋体">．实战：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">有了上面的说明，我们就可以开始写一些弱智一些地</SPAN><SPAN lang=EN-US>Makefile</SPAN><SPAN style="FONT-FAMILY: 宋体">文件了。比如我们有如下的文件：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2>tmp/</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp; </SPAN>+---- include/</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp; </SPAN>|<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>+---- f1.h</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp; </SPAN>|<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>+----f2.h</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp; </SPAN>+----f1.c</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp; </SPAN>+----f2.c </FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US><FONT size=2><SPAN>&nbsp;&nbsp; </SPAN>+---main.c</FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">其中</SPAN><SPAN lang=EN-US>f1.c</SPAN><SPAN style="FONT-FAMILY: 宋体">中</SPAN><SPAN lang=EN-US>#include “include/f1.h”</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>f2.c</SPAN><SPAN style="FONT-FAMILY: 宋体">中</SPAN><SPAN lang=EN-US>#include”include/f2.h”</SPAN><SPAN style="FONT-FAMILY: 宋体">，</SPAN><SPAN lang=EN-US>main.c</SPAN><SPAN style="FONT-FAMILY: 宋体">中又</SPAN><SPAN lang=EN-US>#include”include/f1.h”, #include”include/f2.h”</SPAN><SPAN style="FONT-FAMILY: 宋体">，主函数在</SPAN><SPAN lang=EN-US>main.c</SPAN><SPAN style="FONT-FAMILY: 宋体">中，要将他们联合起来编译为目标为</SPAN><SPAN lang=EN-US>testmf</SPAN><SPAN style="FONT-FAMILY: 宋体">的文件，我们就可以按下面的方式写（当然是弱智的）：</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]#cat Makefile<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>main: main.o f1.o f2.o<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>gcc –o testmf main.o f1.o f2.o<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>f1.o: f1.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>gcc –c –o file1.o file1.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>f2.o: f2.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>gcc –c –o file2.o file2.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>main.o<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>gcc –c –o main.o main.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>clean:<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>rm –rf f1.o f2.o main.o testmf<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]make<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>gcc –c –o main.o main.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>gcc –c –o file1.o file1.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>gcc –c –o file2.o file2.c<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>gcc –o testmf main.o f1.o f2.o<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>[root@xxx test]ls<O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><SPAN lang=EN-US style="moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"><FONT size=2>f1.c <B>f1.o</B> f2.c <B>f2.o</B> main.c <B>main.o</B> <SPAN>include/</SPAN> <SPAN>testmf</SPAN><O /></FONT></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">如果你的程序没有问题的话，就应该可以执行了</SPAN><SPAN lang=EN-US>./testmf</SPAN></FONT></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体">大家可能发现问题了：对目标文件</SPAN><SPAN lang=EN-US>f1.o f2.o</SPAN><SPAN style="FONT-FAMILY: 宋体">和</SPAN><SPAN lang=EN-US>main.o</SPAN><SPAN style="FONT-FAMILY: 宋体">，他们的写法是如此的类似，我们能够将他们一块写？有的，不过今天没时间了，以后再写吧。</SPAN></FONT></P><img src ="http://www.blogjava.net/soochow_hhb/aggbug/34538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-03-09 19:23 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/03/09/34538.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>我的创业日记（序）——人生在于一种体验</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/02/27/32735.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Mon, 27 Feb 2006 13:51:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/02/27/32735.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/32735.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/02/27/32735.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/32735.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/32735.html</trackback:ping><description><![CDATA[<P align=left>【自序】<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创业并不是始于今天，而是今天我突然觉得非常有必要将这个过程记录下来，不管结果是成功还是失败，等若干年后，再回首这段经历，是不是我人生的一个财富呢？近来看了很多电影，在很多部里都听到这么一句话“人生在于一种体验”，我觉得对我非常贴切。同时我的经历是不是对于朋友们有点启发呢？我走得弯路是不是可以给朋友们以明鉴呢？<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创业到底需要具备什么条件？对我而言，完全是一种冲动，去年九月份，和同学接到一个项目，非常的兴奋，有天需求方问我怎么出票，我怎么收钱的问题？积在心中的创业的火焰一下子燃烧了，和同学一拍即合，注册公司。当初的想法是多么的简单啊！另外在我心中还有一个信念就是不管做什么事情，都必须要有“德”，另外还要有“道”（当然这个道字包含了很多含义），直到一次偶遇，我才真正明白了这两个字德确切含义。偶遇是这样的，那天我女朋友带了一个老太太回来，我很是纳闷，我们吃过饭后，就聊了起来，那老太太原来是我们学校的退休教授，曾退休后在北京创业，并且公司规模扩大的很快，她是因为身体原因才放手回来修养，她谈到了她的创业经历，其中最令我“震撼”的是，她也提到了“道”“德”，这也是她的宗旨，并且解释了“道”和“德”的确切含义。“道”就是指要自己才能走遍天下，“德”指两个人要直心相对（看到老祖宗德造字艺术了吧）。我觉得这两个字是创业需要具备的条件，当然是个人观点。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创业当然要有“业”，当初的冲动并不能保证将来的成功，有了一点的积累之后，就要好好确定一下自己的创业方向了（当然，大部分是最初都有的），这是最最关键的了，并且要定位好的，在后面的章节中我会将其中的过程细细道来。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创业要有钱，有地方。但我当初没有钱，也没有地方。在公司刚注册的三个月内，我们主要开发我们的起家项目，搞定之后，我们顺利的拿到了“启动资金”，在公司成立4个多月后，我们有了“家”，但当我们两个“主人”进去的时候，面对空荡荡的房子，这种滋味啊！真是说不清道不明！<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 现在我们才算真是一个公司了，有了公司就要管理，财务，开发，市场，融资等等，这个我们能适应吗？今天还听广播说一个为了创业，但是没有钱的人，想了一个“融资”（可能他都不知道是融资）的方法，自己做了一个大调幅，挂在了闹市区，这是融资吗？都是做技术出身的，我们有这个闯劲吗？我们却是需要很多东西，需要再学习很多东西。前面提到的那个老教授说，她刚开始创业的时候，花了13天拿到了会计证，我早就有拿会计证的打算，但我有她的那个劲头吗？<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创业不是玩家家，创业是实实在在的东西，创业是要你能按照规则生存下去，可能因为冲动而创业，但创业后不能老冲动，冲动一下，可能你要考虑一些东西。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我的创业之路有多长，我的创业日记能不能一直写下去，需要时间去检验！<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 人生在于一种体验。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;写于2006－02－27</P><img src ="http://www.blogjava.net/soochow_hhb/aggbug/32735.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-02-27 21:51 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/02/27/32735.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++中关于继承的多态</title><link>http://www.blogjava.net/soochow_hhb/archive/2006/01/08/27179.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Sun, 08 Jan 2006 12:37:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2006/01/08/27179.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/27179.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2006/01/08/27179.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/27179.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/27179.html</trackback:ping><description><![CDATA[LibMat<BR>Book<BR>AudioBook<BR><BR>Class：LibMat<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;LibMat&nbsp;&nbsp;<BR><IMG id=Codehighlighter1_15_85_Open_Image onclick="this.style.display='none'; Codehighlighter1_15_85_Open_Text.style.display='none'; Codehighlighter1_15_85_Closed_Image.style.display='inline'; Codehighlighter1_15_85_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_15_85_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_15_85_Closed_Text.style.display='none'; Codehighlighter1_15_85_Open_Image.style.display='inline'; Codehighlighter1_15_85_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_15_85_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_15_85_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;LibMat();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;virtual&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">LibMat();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;virtual&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">;</SPAN></DIV>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;LibMat.cpp:&nbsp;implementation&nbsp;of&nbsp;the&nbsp;LibMat&nbsp;class.<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>//////////////////////////////////////////////////////////////////////<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">LibMat.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">iostream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>using&nbsp;namespace&nbsp;std;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//////////////////////////////////////////////////////////////////////</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Construction/Destruction</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>//////////////////////////////////////////////////////////////////////<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>LibMat::LibMat()<BR><IMG id=Codehighlighter1_377_423_Open_Image onclick="this.style.display='none'; Codehighlighter1_377_423_Open_Text.style.display='none'; Codehighlighter1_377_423_Closed_Image.style.display='inline'; Codehighlighter1_377_423_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_377_423_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_377_423_Closed_Text.style.display='none'; Codehighlighter1_377_423_Open_Image.style.display='inline'; Codehighlighter1_377_423_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_377_423_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_377_423_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">LibMat::LibMat()&nbsp;constructor\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>LibMat::</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">LibMat()<BR><IMG id=Codehighlighter1_444_494_Open_Image onclick="this.style.display='none'; Codehighlighter1_444_494_Open_Text.style.display='none'; Codehighlighter1_444_494_Closed_Image.style.display='inline'; Codehighlighter1_444_494_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_444_494_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_444_494_Closed_Text.style.display='none'; Codehighlighter1_444_494_Open_Image.style.display='inline'; Codehighlighter1_444_494_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_444_494_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_444_494_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">LibMat&nbsp;::&nbsp;~&nbsp;LibMat()&nbsp;destructor\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;LibMat&nbsp;::print()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG id=Codehighlighter1_526_585_Open_Image onclick="this.style.display='none'; Codehighlighter1_526_585_Open_Text.style.display='none'; Codehighlighter1_526_585_Closed_Image.style.display='inline'; Codehighlighter1_526_585_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_526_585_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_526_585_Closed_Text.style.display='none'; Codehighlighter1_526_585_Open_Image.style.display='inline'; Codehighlighter1_526_585_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_526_585_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_526_585_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">LibMat::print()&nbsp;--&nbsp;I&nbsp;am&nbsp;a&nbsp;LibMat&nbsp;object!\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV><BR>Class：Book<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">LibMat.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>using&nbsp;namespace&nbsp;std;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Book&nbsp;:&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;LibMat&nbsp;&nbsp;<BR><IMG id=Codehighlighter1_89_327_Open_Image onclick="this.style.display='none'; Codehighlighter1_89_327_Open_Text.style.display='none'; Codehighlighter1_89_327_Closed_Image.style.display='inline'; Codehighlighter1_89_327_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_89_327_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_89_327_Closed_Text.style.display='none'; Codehighlighter1_89_327_Open_Image.style.display='inline'; Codehighlighter1_89_327_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_89_327_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_89_327_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">:<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Book(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;string&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;string&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;virtual&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">Book();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;virtual&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_217_232_Open_Image onclick="this.style.display='none'; Codehighlighter1_217_232_Open_Text.style.display='none'; Codehighlighter1_217_232_Closed_Image.style.display='inline'; Codehighlighter1_217_232_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_217_232_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_217_232_Closed_Text.style.display='none'; Codehighlighter1_217_232_Open_Image.style.display='inline'; Codehighlighter1_217_232_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;string</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;title()&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN id=Codehighlighter1_217_232_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_217_232_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;_title;}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_264_280_Open_Image onclick="this.style.display='none'; Codehighlighter1_264_280_Open_Text.style.display='none'; Codehighlighter1_264_280_Closed_Image.style.display='inline'; Codehighlighter1_264_280_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_264_280_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_264_280_Closed_Text.style.display='none'; Codehighlighter1_264_280_Open_Image.style.display='inline'; Codehighlighter1_264_280_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;string</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;author()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN id=Codehighlighter1_264_280_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_264_280_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;_author;}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">protected</SPAN><SPAN style="COLOR: #000000">:<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;_title;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;_author;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Book.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">iostream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>using&nbsp;namespace&nbsp;std;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//////////////////////////////////////////////////////////////////////</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Construction/Destruction</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>//////////////////////////////////////////////////////////////////////<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Book::Book(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;string&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">title,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;string&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">author)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>:&nbsp;_title(title),&nbsp;_author(author)<BR><IMG id=Codehighlighter1_317_395_Open_Image onclick="this.style.display='none'; Codehighlighter1_317_395_Open_Text.style.display='none'; Codehighlighter1_317_395_Closed_Image.style.display='inline'; Codehighlighter1_317_395_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_317_395_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_317_395_Closed_Text.style.display='none'; Codehighlighter1_317_395_Open_Image.style.display='inline'; Codehighlighter1_317_395_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_317_395_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_317_395_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Book::Book(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;_title&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;_author&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)&nbsp;constructor\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Book::</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">Book()<BR><IMG id=Codehighlighter1_412_452_Open_Image onclick="this.style.display='none'; Codehighlighter1_412_452_Open_Text.style.display='none'; Codehighlighter1_412_452_Closed_Image.style.display='inline'; Codehighlighter1_412_452_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_412_452_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_412_452_Closed_Text.style.display='none'; Codehighlighter1_412_452_Open_Image.style.display='inline'; Codehighlighter1_412_452_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_412_452_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_412_452_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Book:~Book&nbsp;destructor!\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;Book::print()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG id=Codehighlighter1_481_613_Open_Image onclick="this.style.display='none'; Codehighlighter1_481_613_Open_Text.style.display='none'; Codehighlighter1_481_613_Closed_Image.style.display='inline'; Codehighlighter1_481_613_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_481_613_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_481_613_Closed_Text.style.display='none'; Codehighlighter1_481_613_Open_Image.style.display='inline'; Codehighlighter1_481_613_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_481_613_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_481_613_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Book&nbsp;::print()&nbsp;--&nbsp;I&nbsp;am&nbsp;a&nbsp;Book&nbsp;object!\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">My&nbsp;Title&nbsp;is&nbsp;:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;_title&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;My&nbsp;author&nbsp;is&nbsp;:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;_author&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;endl;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>测试代码：main.cpp<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">AudioBook.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">iostream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>using&nbsp;namespace&nbsp;std;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;LibMat</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main&nbsp;()<BR><IMG id=Codehighlighter1_107_330_Open_Image onclick="this.style.display='none'; Codehighlighter1_107_330_Open_Text.style.display='none'; Codehighlighter1_107_330_Closed_Image.style.display='inline'; Codehighlighter1_107_330_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_107_330_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_107_330_Closed_Text.style.display='none'; Codehighlighter1_107_330_Open_Image.style.display='inline'; Codehighlighter1_107_330_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_107_330_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_107_330_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">LibMat&nbsp;libmat;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">print(libmat);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">polymorphism<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Book&nbsp;b("The&nbsp;Castle",&nbsp;"Franz&nbsp;Kafka");<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">print(b);</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">----------------------\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;AudioBook&nbsp;a(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">The&nbsp;Castle</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Franz&nbsp;kafka</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Hanhongbo</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;print(a);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;LibMat</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;mat)<BR><IMG id=Codehighlighter1_363_416_Open_Image onclick="this.style.display='none'; Codehighlighter1_363_416_Open_Text.style.display='none'; Codehighlighter1_363_416_Closed_Image.style.display='inline'; Codehighlighter1_363_416_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_363_416_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_363_416_Closed_Text.style.display='none'; Codehighlighter1_363_416_Open_Image.style.display='inline'; Codehighlighter1_363_416_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_363_416_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_363_416_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">in&nbsp;global&nbsp;print():\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;mat.print();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV><img src ="http://www.blogjava.net/soochow_hhb/aggbug/27179.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2006-01-08 20:37 <a href="http://www.blogjava.net/soochow_hhb/archive/2006/01/08/27179.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>开始阅读《对象模型＝策略 模式 应用》</title><link>http://www.blogjava.net/soochow_hhb/archive/2005/11/23/21150.html</link><dc:creator>soochow_hhb 以java论成败 以架构论英雄</dc:creator><author>soochow_hhb 以java论成败 以架构论英雄</author><pubDate>Wed, 23 Nov 2005 09:04:00 GMT</pubDate><guid>http://www.blogjava.net/soochow_hhb/archive/2005/11/23/21150.html</guid><wfw:comment>http://www.blogjava.net/soochow_hhb/comments/21150.html</wfw:comment><comments>http://www.blogjava.net/soochow_hhb/archive/2005/11/23/21150.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/soochow_hhb/comments/commentRss/21150.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/soochow_hhb/services/trackbacks/21150.html</trackback:ping><description><![CDATA[希望这本书能给我带来对象设计的更多理念，为我的项目设计做指导！<img src ="http://www.blogjava.net/soochow_hhb/aggbug/21150.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/soochow_hhb/" target="_blank">soochow_hhb 以java论成败 以架构论英雄</a> 2005-11-23 17:04 <a href="http://www.blogjava.net/soochow_hhb/archive/2005/11/23/21150.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>