Mercurial > hg > octave-shane > gnulib-hg
annotate lib/stpncpy.c @ 9309:bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 07 Oct 2007 19:14:58 +0200 |
parents | a1d177cd9523 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
7944
a1d177cd9523
* doc/gnulib-tool.texi (Initial import): Update to match current
Paul Eggert <eggert@cs.ucla.edu>
parents:
7304
diff
changeset
|
1 /* Copyright (C) 1993, 1995-1997, 2002-2003, 2005-2007 Free Software Foundation, Inc. |
4221 | 2 |
3 NOTE: The canonical source of this file is maintained with the GNU C Library. | |
4 Bugs can be reported to bug-glibc@gnu.org. | |
5 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify it |
4221 | 7 under the terms of the GNU General Public License as published by the |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
8 Free Software Foundation; either version 3 of the License, or any |
4221 | 9 later version. |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7944
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4221 | 18 |
19 /* This is almost copied from strncpy.c, written by Torbjorn Granlund. */ | |
20 | |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
7025
diff
changeset
|
21 #include <config.h> |
4221 | 22 |
23 /* Specification. */ | |
7944
a1d177cd9523
* doc/gnulib-tool.texi (Initial import): Update to match current
Paul Eggert <eggert@cs.ucla.edu>
parents:
7304
diff
changeset
|
24 #include <string.h> |
4221 | 25 |
26 #ifndef weak_alias | |
27 # define __stpncpy stpncpy | |
28 #endif | |
29 | |
4746
7f0802de0710
Better handling of collision with AIX stpncpy() function.
Bruno Haible <bruno@clisp.org>
parents:
4691
diff
changeset
|
30 /* Copy no more than N bytes of SRC to DST, returning a pointer past the |
7f0802de0710
Better handling of collision with AIX stpncpy() function.
Bruno Haible <bruno@clisp.org>
parents:
4691
diff
changeset
|
31 last non-NUL byte written into DST. */ |
4221 | 32 char * |
33 __stpncpy (char *dest, const char *src, size_t n) | |
34 { | |
35 char c; | |
36 char *s = dest; | |
37 | |
38 if (n >= 4) | |
39 { | |
40 size_t n4 = n >> 2; | |
41 | |
42 for (;;) | |
43 { | |
44 c = *src++; | |
45 *dest++ = c; | |
46 if (c == '\0') | |
47 break; | |
48 c = *src++; | |
49 *dest++ = c; | |
50 if (c == '\0') | |
51 break; | |
52 c = *src++; | |
53 *dest++ = c; | |
54 if (c == '\0') | |
55 break; | |
56 c = *src++; | |
57 *dest++ = c; | |
58 if (c == '\0') | |
59 break; | |
60 if (--n4 == 0) | |
61 goto last_chars; | |
62 } | |
63 n -= dest - s; | |
64 goto zero_fill; | |
65 } | |
66 | |
67 last_chars: | |
68 n &= 3; | |
69 if (n == 0) | |
70 return dest; | |
71 | |
72 for (;;) | |
73 { | |
74 c = *src++; | |
75 --n; | |
76 *dest++ = c; | |
77 if (c == '\0') | |
78 break; | |
79 if (n == 0) | |
80 return dest; | |
81 } | |
82 | |
83 zero_fill: | |
84 while (n-- > 0) | |
85 dest[n] = '\0'; | |
86 | |
87 return dest - 1; | |
88 } | |
89 #ifdef weak_alias | |
90 weak_alias (__stpncpy, stpncpy) | |
91 #endif |