1<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>strscpy</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="The Linux Kernel API"><link rel="up" href="ch02s02.html" title="String Manipulation"><link rel="prev" href="API-strlcpy.html" title="strlcpy"><link rel="next" href="API-strcat.html" title="strcat"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center"><span class="phrase">strscpy</span></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="API-strlcpy.html">Prev</a> </td><th width="60%" align="center">String Manipulation</th><td width="20%" align="right"> <a accesskey="n" href="API-strcat.html">Next</a></td></tr></table><hr></div><div class="refentry"><a name="API-strscpy"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>strscpy — 2 Copy a C-string into a sized buffer 3 </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="funcsynopsis"><table border="0" class="funcprototype-table" summary="Function synopsis" style="cellspacing: 0; cellpadding: 0;"><tr><td><code class="funcdef">ssize_t <b class="fsfunc">strscpy </b>(</code></td><td>char * <var class="pdparam">dest</var>, </td></tr><tr><td> </td><td>const char * <var class="pdparam">src</var>, </td></tr><tr><td> </td><td>size_t <var class="pdparam">count</var><code>)</code>;</td></tr></table><div class="funcprototype-spacer"> </div></div></div><div class="refsect1"><a name="id-1.4.4.6.5"></a><h2>Arguments</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="parameter"><code>dest</code></em></span></dt><dd><p> 4 Where to copy the string to 5 </p></dd><dt><span class="term"><em class="parameter"><code>src</code></em></span></dt><dd><p> 6 Where to copy the string from 7 </p></dd><dt><span class="term"><em class="parameter"><code>count</code></em></span></dt><dd><p> 8 Size of destination buffer 9 </p></dd></dl></div></div><div class="refsect1"><a name="id-1.4.4.6.6"></a><h2>Description</h2><p> 10 Copy the string, or as much of it as fits, into the dest buffer. 11 The routine returns the number of characters copied (not including 12 the trailing NUL) or -E2BIG if the destination buffer wasn't big enough. 13 The behavior is undefined if the string buffers overlap. 14 The destination buffer is always NUL terminated, unless it's zero-sized. 15 </p><p> 16 17 Preferred to <code class="function"><a class="link" href="API-strlcpy.html" title="strlcpy">strlcpy</a></code> since the API doesn't require reading memory 18 from the src string beyond the specified <span class="quote">“<span class="quote">count</span>”</span> bytes, and since 19 the return value is easier to error-check than <code class="function"><a class="link" href="API-strlcpy.html" title="strlcpy">strlcpy</a></code>'s. 20 In addition, the implementation is robust to the string changing out 21 from underneath it, unlike the current <code class="function"><a class="link" href="API-strlcpy.html" title="strlcpy">strlcpy</a></code> implementation. 22 </p><p> 23 24 Preferred to <code class="function"><a class="link" href="API-strncpy.html" title="strncpy">strncpy</a></code> since it always returns a valid string, and 25 doesn't unnecessarily force the tail of the destination buffer to be 26 zeroed. If the zeroing is desired, it's likely cleaner to use <code class="function"><a class="link" href="API-strscpy.html" title="strscpy">strscpy</a></code> 27 with an overflow test, then just <code class="function"><a class="link" href="API-memset.html" title="memset">memset</a></code> the tail of the dest buffer. 28</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="API-strlcpy.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch02s02.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="API-strcat.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="phrase">strlcpy</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="phrase">strcat</span></td></tr></table></div></body></html> 29