📄 kernel interfaces with dd.htm
字号:
<tt>TC_IOSLOTS</tt>.
<a href="#co_id_129_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_129_2"></a>
Declares a variable to store the module name from the ROM of
a device on the TURBOchannel bus.
<a href="#co_id_129_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_129_3"></a>
Shows that the
<tt>strcpy</tt>
interface takes two arguments:
<ul>
<p></p><li>
The first argument specifies a pointer to a buffer large enough to hold the
string to be copied.
In the example, this buffer is the
<tt>modulename</tt>
member of the
<tt>tc_slot</tt>
structure associated with the specified bus.
<p></p></li><li>
The second argument specifies
a pointer to a string (an array of characters terminated by a
null character).
This is the string to be copied to the buffer specified by the first
argument.
In the example, this is the module name from the ROM, which is stored in
the
<tt><var>curr_module_name</var></tt>
variable.
</li></ul><p>
</p><p>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut25.html#figResultsstrcpy">Figure 18-3</a>
shows how
<tt>strcpy</tt>
copies a sample value in the code fragment.
The interface copies the string
<tt>CB</tt>
(the value contained in
<tt><var>curr_module_name</var></tt>)
to the
<tt>modulename</tt>
member of the
<tt>tc_slot</tt>
structure associated with the specified bus.
This member is presumed large enough to store the character string.
The
<tt>strcpy</tt>
interface returns the pointer to the location following the end of the
destination buffer.
<a href="#co_id_129_rtn_3">[Return to example]</a>
</p></li></ol><p>
</p><p>
<a name="figResultsstrcpy"></a>
</p><h3>Figure 18-3: Results of the strcpy Interface</h3>
<p><img src="kernel%20interfaces%20with%20DD_files/zk-0625U.gif"></p><p>
<a name="nx_id_807"></a>
<a name="CopyNullTermStrSpecLimit"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="kernel%20interfaces%20with%20DD_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#CopyNullTermStr"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#RetNumbofCharsNullTermStr"><img src="kernel%20interfaces%20with%20DD_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut26.html"><img src="kernel%20interfaces%20with%20DD_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="kernel%20interfaces%20with%20DD_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
18.1.4 Copying a Null-Terminated Character String with a Specified Limit
</h3>
<p>
<a name="nx_id_808"></a>
<a name="nx_id_809"></a>
To copy a null-terminated character string with a specified limit, call the
<tt>strncpy</tt>
interface.
The following code fragment shows a call to
<tt>strncpy</tt>:
</p><p>
</p><pre><br>.<br>.<br>.<br>
register struct device *device;
char * buffer;
<br>.<br>.<br>.<br>
strncpy(buffer, device->dev_name, 2); <a name="co_id_130_rtn_1"></a><a href="#co_id_130_1"><strong>[1]</strong></a>
if (buffer == somevalue)
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_130_1"></a>
<a name="nx_id_810"></a>
Shows that
<tt>strncpy</tt>
takes three arguments:
<ul>
<p></p><li>
The first argument specifies a pointer to a buffer of at least the same number
of bytes as specified in the third argument.
In the example, this is the pointer to the
<tt><var>buffer</var></tt>
variable.
<p></p></li><li>
The second argument specifies
a pointer to a string (an array of characters terminated by a
null character).
This is the character string to be copied and in the example is the
value pointed to by the
<tt>dev_name</tt>
member of the pointer to the
<tt>device</tt>
structure.
<p></p></li><li>
The third argument specifies the number of characters to copy, which in
the example is two characters.
</li></ul><p>
</p><p>
The code fragment sets up a condition statement that performs some tasks
based on the characters stored in the pointer to the
<tt><var>buffer</var></tt>
variable.
</p><p>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut25.html#figResultsstrncpy">Figure 18-4</a>
shows how
<tt>strncpy</tt>
copies a sample value in the code fragment.
The interface copies the first two characters of the string
<tt>none</tt>
(the value pointed to by the
<tt>dev_name</tt>
member of the pointer to the
<tt>device</tt>
structure).
The
<tt>strncpy</tt>
interface stops copying after it copies a null character or the number
of characters specified in the third argument, whichever comes first.
</p><p>
The figure also shows that
<tt>strncpy</tt>
returns a pointer to the /NULL character
at the end of the first string (or to the location following the last
copied character if there is no
NULL).
The copied string will not be null terminated if its length is greater
than or equal to the number of characters specified in the third
argument.
<a href="#co_id_130_rtn_1">[Return to example]</a>
</p></li></ol><p>
<a name="figResultsstrncpy"></a>
</p><h3>Figure 18-4: Results of the strncpy Interface</h3>
<p><img src="kernel%20interfaces%20with%20DD_files/zk-0793U.gif"></p><p>
<a name="nx_id_811"></a>
<a name="RetNumbofCharsNullTermStr"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="kernel%20interfaces%20with%20DD_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#CopyNullTermStrSpecLimit"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#DataCopyingInter"><img src="kernel%20interfaces%20with%20DD_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut26.html"><img src="kernel%20interfaces%20with%20DD_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="kernel%20interfaces%20with%20DD_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
18.1.5 Returning the Number of Characters in a Null-Terminated String
</h3>
<p>
<a name="nx_id_812"></a>
<a name="nx_id_813"></a>
To return the number of characters in a null-terminated character string,
call the
<tt>strlen</tt>
interface.
The following code fragment shows a call to
<tt>strlen</tt>:
</p><p>
</p><pre><br>.<br>.<br>.<br>
char *strptr;
<br>.<br>.<br>.<br>
if ((strlen(strptr)) > 1) <a name="co_id_131_rtn_1"></a><a href="#co_id_131_1"><strong>[1]</strong></a>
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_131_1"></a>
<a name="nx_id_814"></a>
Shows that the
<tt>strlen</tt>
interface takes one argument:
a pointer to a string (an array of characters terminated by a
null character).
In the example, this pointer is the variable
<tt><var>strptr</var></tt>.
<p>
The code fragment sets up a condition statement that performs some tasks
based on the length of the string.
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut25.html#figResultsstrlen">Figure 18-5</a>
shows how
<tt>strlen</tt>
checks the number of characters in a sample string in the code
fragment.
As the figure shows,
<tt>strlen</tt>
returns the number of characters pointed to by the
<tt><var>strptr</var></tt>
variable, which in the code fragment is four.
Note that
<tt>strlen</tt>
does not count the terminating null character.
<a href="#co_id_131_rtn_1">[Return to example]</a>
</p></li></ol><p>
</p><p>
<a name="figResultsstrlen"></a>
</p><h3>Figure 18-5: Results of the strlen Interface</h3>
<p><img src="kernel%20interfaces%20with%20DD_files/zk-0626U.gif"></p><p>
<a name="nx_id_815"></a>
<a name="DataCopyingInter"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="kernel%20interfaces%20with%20DD_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#RetNumbofCharsNullTermStr"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#CopyByteSpecLimit"><img src="kernel%20interfaces%20with%20DD_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut26.html"><img src="kernel%20interfaces%20with%20DD_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="kernel%20interfaces%20with%20DD_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h2>
18.2 Data Copying Interfaces
</h2>
<p>
<a name="nx_id_816"></a>
The data copying interfaces allow device drivers to:
</p><ul>
<p></p><li>
Copy a series of bytes with a specified limit
<p></p></li><li>
Zero a block of memory
<p></p></li><li>
Copy data from user address space to kernel address space
<p></p></li><li>
Copy data from kernel address space to user address space
<p></p></li><li>
Move data between user virtual space and system virtual space
</li></ul><p>
The following sections describe the kernel interfaces that perform these tasks.
<a name="CopyByteSpecLimit"></a>
</p><p></p><hr><p align="center">
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/Digital_UNIX_Bookshelf.html"><img src="kernel%20interfaces%20with%20DD_files/BOOKSHELF.GIF" alt="[Return to Library]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/TOC.html"><img src="kernel%20interfaces%20with%20DD_files/TOC.GIF" alt="[Contents]" border="0"></a>
<a href="#UsingKernRtnswithDrvs"><img src="kernel%20interfaces%20with%20DD_files/REW.GIF" alt="[Previous Chapter]" border="0"></a>
<a href="#DataCopyingInter"><img src="kernel%20interfaces%20with%20DD_files/PREV.GIF" alt="[Previous Section]" border="0"></a>
<a href="#ZeroingBlkofMem"><img src="kernel%20interfaces%20with%20DD_files/NEXT.GIF" alt="[Next Section]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/drivertut26.html"><img src="kernel%20interfaces%20with%20DD_files/FF.GIF" alt="[Next Chapter]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-PUBVD-TE_html/INDEX.html"><img src="kernel%20interfaces%20with%20DD_files/INDEX.GIF" alt="[Index]" border="0"></a>
<a href="http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/HELP.html"><img src="kernel%20interfaces%20with%20DD_files/HELP.GIF" alt="[Help]" border="0"></a>
</p><p></p><hr><p>
</p><h3>
18.2.1 Copying a Series of Bytes with a Specified Limit
</h3>
<p>
<a name="nx_id_817"></a>
To copy a series of bytes with a specified limit, call the
<tt>bcopy</tt>
interface.
The following code fragment shows a call to
<tt>bcopy</tt>:
</p><p>
</p><pre><br>.<br>.<br>.<br>
struct tc_slot tc_slot[TC_IOSLOTS]; <a name="co_id_132_rtn_1"></a><a href="#co_id_132_1"><strong>[1]</strong></a>
<br>.<br>.<br>.<br>
char *cp; <a name="co_id_132_rtn_2"></a><a href="#co_id_132_2"><strong>[2]</strong></a>
<br>.<br>.<br>.<br>
<br>
bcopy(tc_slot[index].modulename, cp, TC_ROMNAMLEN + 1); <a name="co_id_132_rtn_3"></a><a href="#co_id_132_3"><strong>[3]</strong></a>
<br>.<br>.<br>.<br>
</pre>
<p>
</p><ol>
<p></p><li>
<a name="co_id_132_1"></a>
<a name="nx_id_818"></a>
Declares an array of
<tt>tc_slot</tt>
structures of size
<tt>TC_IOSLOTS</tt>.
<a href="#co_id_132_rtn_1">[Return to example]</a>
<p></p></li><li>
<a name="co_id_132_2"></a>
Declares a pointer to a buffer that stores the bytes of data
copied from the first argument.
<a href="#co_id_132_rtn_2">[Return to example]</a>
<p></p></li><li>
<a name="co_id_132_3"></a>
Shows that the
<tt>bcopy</tt>
interface takes three arguments:
<ul>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -