⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</BLOCKQUOTE>

<H3><A NAME="WorkingwithProtocolFilesUsingthege">Working with

Protocol Files Using the <TT><FONT SIZE=4 FACE="Courier">getprotoent()</FONT></TT><FONT SIZE=4>

Function</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">getprotoent</FONT></TT> function

is used to get entries in the <TT><FONT FACE="Courier">/etc/protocols</FONT></TT>

file for the protocols database. Here's the syntax for the <TT><FONT FACE="Courier">getprotoent</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($protoName, $aliases, $number) = getprotoent();</FONT></TT>

</BLOCKQUOTE>

<P>

<TT><FONT FACE="Courier">$protoName</FONT></TT> is the name of

a particular system protocol. <TT><FONT FACE="Courier">$aliases</FONT></TT>

is a scalar list of alternative names for this system protocol,

with each name being separated from the other by a space. The

<TT><FONT FACE="Courier">$number</FONT></TT> is the ID for the

particular system protocol.

<P>

The first call to <TT><FONT FACE="Courier">getprotoent</FONT></TT>

returns the first element in <TT><FONT FACE="Courier">/etc/protocols</FONT></TT>.

Each subsequent call then returns the next entry in the <TT><FONT FACE="Courier">/etc/protocols</FONT></TT>

file. <TT><FONT FACE="Courier">getprotoent</FONT></TT> returns

the empty list when the last entry is read.

<P>

The <TT><FONT FACE="Courier">getprotobyname()</FONT></TT> and

<TT><FONT FACE="Courier">getprotobynumber()</FONT></TT> functions

are used to search for entries the <TT><FONT FACE="Courier">/etc/protocols</FONT></TT>

file. The <TT><FONT FACE="Courier">getprotobyname</FONT></TT>

function searches for a particular protocol entry by using a name,

whereas <TT><FONT FACE="Courier">getprotobynumber()</FONT></TT>

uses the protocol ID. Here is the syntax for the two functions:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($protoName, $aliases, $number) = getprotobyname

($name);<BR>

($protoName, $aliases, $number) = getprotobynumber ($number);</FONT></TT>

</BLOCKQUOTE>

<P>

Both functions return an empty list if no matching protocol database

entries are found.

<P>

The <TT><FONT FACE="Courier">setprotoent()</FONT></TT> and <TT><FONT FACE="Courier">endprotoent()</FONT></TT>

functions are used to access the entries in the<TT><FONT FACE="Courier">

/etc/protocols</FONT></TT> file. The <TT><FONT FACE="Courier">setprotoent</FONT></TT>

function rewinds the <TT><FONT FACE="Courier">/etc/protocols</FONT></TT>

file.

<P>

Here's the syntax for the <TT><FONT FACE="Courier">setprotoent</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">setprotoent (keepopen);</FONT></TT>

</BLOCKQUOTE>

<P>

If <TT><FONT FACE="Courier">keepopen</FONT></TT> is non-zero,

the <TT><FONT FACE="Courier">/etc/protocols</FONT></TT> file is

left open for reading and any previously cached information about

the file is kept in memory. If <TT><FONT FACE="Courier">keepopen</FONT></TT>

is set to zero, any cached information in memory is flushed, and

the file is read again with the first entry available for a call

to <TT><FONT FACE="Courier">getprotoent()</FONT></TT>. The <TT><FONT FACE="Courier">endprotoent()</FONT></TT>

function accepts no parameters and simply closes the <TT><FONT FACE="Courier">/etc/protocols</FONT></TT>

file after flushing any buffers.

<H3><A NAME="ThegetserventFunction">The <TT><FONT SIZE=4 FACE="Courier">getservent</FONT></TT><FONT SIZE=4>

Function</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">getservent()</FONT></TT> function

is used to search the <TT><FONT FACE="Courier">/etc/services</FONT></TT>

file for entries in the system services database. Here's the syntax

for the <TT><FONT FACE="Courier">getservent()</FONT></TT> function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($name, $aliases, $portnum, $protoname)

= getservent();</FONT></TT>

</BLOCKQUOTE>

<P>

<TT><FONT FACE="Courier">$name</FONT></TT> is the identifier of

a particular system service. <TT><FONT FACE="Courier">$aliases</FONT></TT>

is a scalar list of alias names for the system service specified

in <TT><FONT FACE="Courier">$name</FONT></TT>. The names in <TT><FONT FACE="Courier">$aliases</FONT></TT>

are separated from each other by a white space. <TT><FONT FACE="Courier">$portnum</FONT></TT>

is the port number assigned to the system protocol and indicates

the location of the port at which the service is residing. The

value in <TT><FONT FACE="Courier">$portnum</FONT></TT> is a packed

array of integers, which must be unpacked using <TT><FONT FACE="Courier">unpack</FONT></TT>

with a <TT><FONT FACE="Courier">C*</FONT></TT> format specifier.

<TT><FONT FACE="Courier">$protoname</FONT></TT> is a protocol

name. The first call to <TT><FONT FACE="Courier">getservent</FONT></TT>

returns the first element in <TT><FONT FACE="Courier">/etc/services</FONT></TT>.

Further calls return subsequent entries; when <TT><FONT FACE="Courier">/etc/services</FONT></TT>

is exhausted, <TT><FONT FACE="Courier">getservent</FONT></TT>

returns the empty list.

<P>

The <TT><FONT FACE="Courier">setservent()</FONT></TT> and <TT><FONT FACE="Courier">endservent()</FONT></TT>

functions are used to access entries in the <TT><FONT FACE="Courier">/etc/services</FONT></TT>

file. The <TT><FONT FACE="Courier">setservent()</FONT></TT> function

rewinds the <TT><FONT FACE="Courier">/etc/services</FONT></TT>

file. Here's the syntax for the function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">setservent ($keepopen);</FONT></TT>

</BLOCKQUOTE>

<P>

If <TT><FONT FACE="Courier">keepopen</FONT></TT> is non-zero,

the <TT><FONT FACE="Courier">/etc/services</FONT></TT> file is

left open for reading and any previously cached information about

the file is kept in memory. If <TT><FONT FACE="Courier">keepopen</FONT></TT>

is set to zero, any cached information in memory is flushed and

the file is read again with the first entry available for a call

to <TT><FONT FACE="Courier">getservent()</FONT></TT>. The <TT><FONT FACE="Courier">endservent()</FONT></TT>

function accepts no parameters and simply closes the <TT><FONT FACE="Courier">/etc/services</FONT></TT>

file after flushing any buffers.

<P>

The <TT><FONT FACE="Courier">getservbyname</FONT></TT> and <TT><FONT FACE="Courier">getservbyport</FONT></TT>

functions are used to search the <TT><FONT FACE="Courier">/etc/services</FONT></TT>

file. The <TT><FONT FACE="Courier">getservbyname()</FONT></TT>

function looks for an entry given a name, whereas the <TT><FONT FACE="Courier">getservbyport()</FONT></TT>

function looks for an entry given a port number. Here's the syntax

for the <TT><FONT FACE="Courier">getservbyname</FONT></TT> function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($name, $aliases, $portnum, $protoName)

<BR>

&nbsp;&nbsp;&nbsp;= getservbyname ($searchname, $searchproto);</FONT></TT>

</BLOCKQUOTE>

<P>

Here's the syntax for the <TT><FONT FACE="Courier">getservbyport</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($name, $aliases, $portnum, $protoName)

<BR>

&nbsp;&nbsp;&nbsp;= getservbyport ($searchportnum, $searchproto);</FONT></TT>

</BLOCKQUOTE>

<P>

<TT><FONT FACE="Courier">$searchportnum</FONT></TT> and <TT><FONT FACE="Courier">$searchname</FONT></TT>

are the port number and name of the protocol, respectively. <TT><FONT FACE="Courier">$searchproto</FONT></TT>

is the port number and protocol type to search for.

<P>

Both functions return the same type of values as the four-element

list returned by <TT><FONT FACE="Courier">getservent()</FONT></TT>.

(The empty list is returned if the name and type are not matched.)

Similarly, the <TT><FONT FACE="Courier">getservbyport</FONT></TT>

function searches for a service name that matches a particular

service port number.

<H2><A NAME="SystemLevelFunctions"><FONT SIZE=5 COLOR=#FF0000>System-Level

Functions</FONT></A></H2>

<P>

This section lists those system-level functions that you're not

likely to use but should know about to perform that one special

function.

<H3><A NAME="ThechrootFunction">The <TT><FONT SIZE=4 FACE="Courier">chroot</FONT></TT><FONT SIZE=4>

Function</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">chroot</FONT></TT> function enables

you to change the root directory for your program. The root directory

is passed on to any child processes created by the application

calling the <TT><FONT FACE="Courier">chroot</FONT></TT> function.

<P>

Here's the syntax for the <TT><FONT FACE="Courier">chroot</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">chroot ($dirname);</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><FONT FACE="Courier">$dirname</FONT></TT> is the pathname

of the directory to use as the root directory. The value of <TT><FONT FACE="Courier">$dirname</FONT></TT>

name specified by <TT><FONT FACE="Courier">dirname</FONT></TT>

is appended to every pathname specified by your program and its

subprocesses. For example, use a statement like this one to force

all further access to files in the <TT><FONT FACE="Courier">/pub</FONT></TT>

directory:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">chroot (&quot;/pub&quot;);</FONT></TT>

</BLOCKQUOTE>

<P>

This forces even absolute pathnames in a program to be prepended

with <TT><FONT FACE="Courier">/pub</FONT></TT>. The <TT><FONT FACE="Courier">chroot</FONT></TT>

function is helpful when writing applications for the World Wide

Web because you can limit user access to a known directory tree.

Thus, if a user types <TT><FONT FACE="Courier">/etc/passwd</FONT></TT>,

the request is turned into <TT><FONT FACE="Courier">/pub/etc/passwd</FONT></TT>.

<H3><A NAME="ThedumpFunction">The <TT><FONT SIZE=4 FACE="Courier">dump</FONT></TT><FONT SIZE=4>

Function</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">dump</FONT></TT> function enables

you to generate a UNIX core dump from within your Perl program.

It is meant to be used with the <TT><FONT FACE="Courier">undump</FONT></TT>

command. Here's the syntax for this command:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">dump[(label)];</FONT></TT>

</BLOCKQUOTE>

<P>

<TT><I><FONT FACE="Courier">label</FONT></I></TT> is optional

and specifies the place to start for the UNIX <TT><FONT FACE="Courier">undump</FONT></TT>

command.<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD ><B>Caution</B></TD></TR>

<TR VALIGN=TOP><TD ><BLOCKQUOTE>

Be careful when working with <TT><FONT FACE="Courier">dump</FONT></TT> and <TT><FONT FACE="Courier">undump</FONT></TT>. Only the execution state of a program is kept, not the state of the environment. For example, if the code being undumped was accessing a 

file when the core was dumped, the file will not be open and is therefore not available to the undumped code.

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<H3><A NAME="UsingtheioctlFunction">Using the <TT><FONT SIZE=4 FACE="Courier">ioctl</FONT></TT><FONT SIZE=4>

Function</FONT></A></H3>

<P>

In UNIX, the <TT><FONT FACE="Courier">ioctl</FONT></TT> function

has been the traditional catch-all for all input/output operations

that can fit in the <TT><FONT FACE="Courier">open</FONT></TT>,

<TT><FONT FACE="Courier">read</FONT></TT>, <TT><FONT FACE="Courier">write</FONT></TT>,

and <TT><FONT FACE="Courier">close</FONT></TT> functions. The

<TT><FONT FACE="Courier">ioctl</FONT></TT> function is sometimes

not portable across some UNIX systems and almost certainly not

for Windows NT or Windows 95 systems. However, <TT><FONT FACE="Courier">ioctl</FONT></TT>

is too useful to discard because it may be the only route to get

you the extra functionality you need to access terminal and system

facilities.

<P>

A very good text to read to learn more about <TT><FONT FACE="Courier">ioctl</FONT></TT>

is W. Richard Stevens's <I>Advanced Programming in the UNIX Environment</I>,

Addison Wesley, 1992. This book tells you more about <TT><FONT FACE="Courier">ioctl</FONT></TT>

than you'll want to know at one reading. It's a great source of

UNIX information.

<P>

Here's an example of how to emulate the <TT><FONT FACE="Courier">getkey()</FONT></TT>

function prevalent in DOS machines. The <TT><FONT FACE="Courier">getkey()</FONT></TT>function

returns one character read back from the keyboard. When reading

from the keyboard using <TT><FONT FACE="Courier">&lt;STDIN&gt;</FONT></TT>,

the program has to wait until the user hits the Return key. Waiting

for the Return key lets the user back up and correct mistakes.

This editing feature is available because the terminal is in a

&quot;cooked&quot; mode. That is, the terminal driver is smart

enough to recognize a Backspace key and take a character off the

input queue.

<P>

To process each character at one time, you have to place the terminal

in &quot;raw&quot; mode. The raw mode passes all typed keystrokes

into the reading application without processing. Note that the

following lines may be different on your operating system. The

following lines are meant to serve only as an example and are

adapted from an example in the Perl FAQ by Don Carson (<TT><FONT FACE="Courier">dbc@tc.fluke.com</FONT></TT>):

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$saveioctl = ioctl(STDIN,0,0);&nbsp;&nbsp;&nbsp;&nbsp;

# Gets device info<BR>

$saveioctl &amp;= 0xff;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

reset right most bits<BR>

ioctl(STDIN,1,$saveioctl | 32);&nbsp;&nbsp;&nbsp;&nbsp;# Set raw

mode</FONT></TT>

</BLOCKQUOTE>

<P>

Here's how to read a single character:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">sysread(STDIN,$c,1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Read a single character</FONT></TT>

</BLOCKQUOTE>

<P>

And here's how to put the terminal back in &quot;cooked&quot;

mode:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">ioctl(STDIN,1,$saveioctl);&nbsp;&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -