3 * Copyright (C) 2007, 2008, 2009, 2010, 2011 Sebastien Bourdeauducq
4 * Copyright (C) Linus Torvalds and Linux kernel developers
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 3 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * strchr - Find the first occurrence of a character in a string
28 * @s: The string to be searched
29 * @c: The character to search for
31 char *strchr(const char *s
, int c
)
33 for (; *s
!= (char)c
; ++s
)
40 * strpbrk - Find the first occurrence of a set of characters
41 * @cs: The string to be searched
42 * @ct: The characters to search for
44 char *strpbrk(const char *cs
, const char *ct
)
46 const char *sc1
, *sc2
;
48 for (sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
49 for (sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
58 * strrchr - Find the last occurrence of a character in a string
59 * @s: The string to be searched
60 * @c: The character to search for
62 char *strrchr(const char *s
, int c
)
64 const char *p
= s
+ strlen(s
);
73 * strnchr - Find a character in a length limited string
74 * @s: The string to be searched
75 * @count: The number of characters to be searched
76 * @c: The character to search for
78 char *strnchr(const char *s
, size_t count
, int c
)
80 for (; count
-- && *s
!= '\0'; ++s
)
87 * strcpy - Copy a %NUL terminated string
88 * @dest: Where to copy the string to
89 * @src: Where to copy the string from
91 char *strcpy(char *dest
, const char *src
)
95 while ((*dest
++ = *src
++) != '\0')
101 * strncpy - Copy a length-limited, %NUL-terminated string
102 * @dest: Where to copy the string to
103 * @src: Where to copy the string from
104 * @count: The maximum number of bytes to copy
106 * The result is not %NUL-terminated if the source exceeds
109 * In the case where the length of @src is less than that of
110 * count, the remainder of @dest will be padded with %NUL.
113 char *strncpy(char *dest
, const char *src
, size_t count
)
118 if ((*tmp
= *src
) != 0)
127 * strcmp - Compare two strings
129 * @ct: Another string
131 int strcmp(const char *cs
, const char *ct
)
136 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
143 * strncmp - Compare two strings using the first characters only
145 * @ct: Another string
146 * @count: Number of characters
148 int strncmp(const char *cs
, const char *ct
, size_t count
)
156 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
164 * strcat - Append one %NUL-terminated string to another
165 * @dest: The string to be appended to
166 * @src: The string to append to it
168 char *strcat(char *dest
, const char *src
)
174 while ((*dest
++ = *src
++) != '\0')
180 * strncat - Append a length-limited, %NUL-terminated string to another
181 * @dest: The string to be appended to
182 * @src: The string to append to it
183 * @count: The maximum numbers of bytes to copy
185 * Note that in contrast to strncpy(), strncat() ensures the result is
188 char *strncat(char *dest
, const char *src
, size_t count
)
195 while ((*dest
++ = *src
++) != 0) {
206 * strlen - Find the length of a string
207 * @s: The string to be sized
209 size_t strlen(const char *s
)
213 for (sc
= s
; *sc
!= '\0'; ++sc
)
219 * strnlen - Find the length of a length-limited string
220 * @s: The string to be sized
221 * @count: The maximum number of bytes to search
223 size_t strnlen(const char *s
, size_t count
)
227 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
233 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
234 * @s: The string to be searched
235 * @accept: The string to search for
237 size_t strspn(const char *s
, const char *accept
)
243 for (p
= s
; *p
!= '\0'; ++p
) {
244 for (a
= accept
; *a
!= '\0'; ++a
) {
256 * memcmp - Compare two areas of memory
257 * @cs: One area of memory
258 * @ct: Another area of memory
259 * @count: The size of the area.
261 int memcmp(const void *cs
, const void *ct
, size_t count
)
263 const unsigned char *su1
, *su2
;
266 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--)
267 if ((res
= *su1
- *su2
) != 0)
273 * memset - Fill a region of memory with the given value
274 * @s: Pointer to the start of the area.
275 * @c: The byte to fill the area with
276 * @count: The size of the area.
278 void *memset(void *s
, int c
, size_t count
)
288 * memcpy - Copies one area of memory to another
291 * @n: The size to copy.
293 void *memcpy(void *to
, const void *from
, size_t n
)
302 const char *cfrom
= from
;
310 const char *cfrom
= from
;
315 if(n
> 2 && (long)to
& 2) {
317 const short *sfrom
= from
;
325 const short *sfrom
= from
;
333 const char *cfrom
= from
;
341 const long *lfrom
= from
;
349 const short *sfrom
= from
;
356 const char *cfrom
= from
;
363 * memmove - Copies one area of memory to another, overlap possible
366 * @n: The size to copy.
368 void *memmove(void *dest
, const void *src
, size_t count
)
378 tmp
= (char *)dest
+ count
;
379 s
= (char *)src
+ count
;
388 * strstr - Find the first substring in a %NUL terminated string
389 * @s1: The string to be searched
390 * @s2: The string to search for
392 char *strstr(const char *s1
, const char *s2
)
402 if (!memcmp(s1
, s2
, l2
))
410 * memchr - Find a character in an area of memory.
411 * @s: The memory area
412 * @c: The byte to search for
413 * @n: The size of the area.
415 * returns the address of the first occurrence of @c, or %NULL
418 void *memchr(const void *s
, int c
, size_t n
)
420 const unsigned char *p
= s
;
422 if ((unsigned char)c
== *p
++) {
423 return (void *)(p
- 1);
430 * strtoul - convert a string to an unsigned long
431 * @nptr: The start of the string
432 * @endptr: A pointer to the end of the parsed string will be placed here
433 * @base: The number base to use
435 unsigned long strtoul(const char *nptr
, char **endptr
, int base
)
437 unsigned long result
= 0,value
;
444 if ((toupper(*nptr
) == 'X') && isxdigit(nptr
[1])) {
449 } else if (base
== 16) {
450 if (nptr
[0] == '0' && toupper(nptr
[1]) == 'X')
453 while (isxdigit(*nptr
) &&
454 (value
= isdigit(*nptr
) ? *nptr
-'0' : toupper(*nptr
)-'A'+10) < base
) {
455 result
= result
*base
+ value
;
459 *endptr
= (char *)nptr
;
464 * strtol - convert a string to a signed long
465 * @nptr: The start of the string
466 * @endptr: A pointer to the end of the parsed string will be placed here
467 * @base: The number base to use
469 long strtol(const char *nptr
, char **endptr
, int base
)
472 return -strtoul(nptr
+1,endptr
,base
);
473 return strtoul(nptr
,endptr
,base
);
476 int skip_atoi(const char **s
)
481 i
= i
*10 + *((*s
)++) - '0';
485 char *number(char *buf
, char *end
, unsigned long num
, int base
, int size
, int precision
, int type
)
489 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
490 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
493 digits
= (type
& PRINTF_LARGE
) ? large_digits
: small_digits
;
494 if (type
& PRINTF_LEFT
)
495 type
&= ~PRINTF_ZEROPAD
;
496 if (base
< 2 || base
> 36)
498 c
= (type
& PRINTF_ZEROPAD
) ? '0' : ' ';
500 if (type
& PRINTF_SIGN
) {
501 if ((signed long) num
< 0) {
503 num
= - (signed long) num
;
505 } else if (type
& PRINTF_PLUS
) {
508 } else if (type
& PRINTF_SPACE
) {
513 if (type
& PRINTF_SPECIAL
) {
522 else while (num
!= 0) {
523 tmp
[i
++] = digits
[num
% base
];
529 if (!(type
&(PRINTF_ZEROPAD
+PRINTF_LEFT
))) {
541 if (type
& PRINTF_SPECIAL
) {
546 } else if (base
==16) {
555 if (!(type
& PRINTF_LEFT
)) {
562 while (i
< precision
--) {
581 * vscnprintf - Format a string and place it in a buffer
582 * @buf: The buffer to place the result into
583 * @size: The size of the buffer, including the trailing null space
584 * @fmt: The format string to use
585 * @args: Arguments for the format string
587 * The return value is the number of characters which have been written into
588 * the @buf not including the trailing '\0'. If @size is <= 0 the function
591 * Call this function if you are already dealing with a va_list.
592 * You probably want scnprintf() instead.
594 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
598 i
=vsnprintf(buf
,size
,fmt
,args
);
599 return (i
>= size
) ? (size
- 1) : i
;
604 * snprintf - Format a string and place it in a buffer
605 * @buf: The buffer to place the result into
606 * @size: The size of the buffer, including the trailing null space
607 * @fmt: The format string to use
608 * @...: Arguments for the format string
610 * The return value is the number of characters which would be
611 * generated for the given input, excluding the trailing null,
612 * as per ISO C99. If the return is greater than or equal to
613 * @size, the resulting string is truncated.
615 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
621 i
=vsnprintf(buf
,size
,fmt
,args
);
627 * scnprintf - Format a string and place it in a buffer
628 * @buf: The buffer to place the result into
629 * @size: The size of the buffer, including the trailing null space
630 * @fmt: The format string to use
631 * @...: Arguments for the format string
633 * The return value is the number of characters written into @buf not including
634 * the trailing '\0'. If @size is <= 0 the function returns 0.
637 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
643 i
= vsnprintf(buf
, size
, fmt
, args
);
645 return (i
>= size
) ? (size
- 1) : i
;
649 * vsprintf - Format a string and place it in a buffer
650 * @buf: The buffer to place the result into
651 * @fmt: The format string to use
652 * @args: Arguments for the format string
654 * The function returns the number of characters written
655 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
658 * Call this function if you are already dealing with a va_list.
659 * You probably want sprintf() instead.
661 int vsprintf(char *buf
, const char *fmt
, va_list args
)
663 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
667 * sprintf - Format a string and place it in a buffer
668 * @buf: The buffer to place the result into
669 * @fmt: The format string to use
670 * @...: Arguments for the format string
672 * The function returns the number of characters written
673 * into @buf. Use snprintf() or scnprintf() in order to avoid
676 int sprintf(char * buf
, const char *fmt
, ...)
682 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
687 /* From linux/lib/ctype.c, Copyright (C) 1991, 1992 Linus Torvalds */
688 const unsigned char _ctype
[] = {
689 _C
,_C
,_C
,_C
,_C
,_C
,_C
,_C
, /* 0-7 */
690 _C
,_C
|_S
,_C
|_S
,_C
|_S
,_C
|_S
,_C
|_S
,_C
,_C
, /* 8-15 */
691 _C
,_C
,_C
,_C
,_C
,_C
,_C
,_C
, /* 16-23 */
692 _C
,_C
,_C
,_C
,_C
,_C
,_C
,_C
, /* 24-31 */
693 _S
|_SP
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 32-39 */
694 _P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 40-47 */
695 _D
,_D
,_D
,_D
,_D
,_D
,_D
,_D
, /* 48-55 */
696 _D
,_D
,_P
,_P
,_P
,_P
,_P
,_P
, /* 56-63 */
697 _P
,_U
|_X
,_U
|_X
,_U
|_X
,_U
|_X
,_U
|_X
,_U
|_X
,_U
, /* 64-71 */
698 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
, /* 72-79 */
699 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
, /* 80-87 */
700 _U
,_U
,_U
,_P
,_P
,_P
,_P
,_P
, /* 88-95 */
701 _P
,_L
|_X
,_L
|_X
,_L
|_X
,_L
|_X
,_L
|_X
,_L
|_X
,_L
, /* 96-103 */
702 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
, /* 104-111 */
703 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
, /* 112-119 */
704 _L
,_L
,_L
,_P
,_P
,_P
,_P
,_C
, /* 120-127 */
705 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
706 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
707 _S
|_SP
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 160-175 */
708 _P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 176-191 */
709 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
, /* 192-207 */
710 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_P
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_L
, /* 208-223 */
711 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
, /* 224-239 */
712 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_P
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
}; /* 240-255 */
715 * rand - Returns a pseudo random number
718 static unsigned int randseed
;
719 unsigned int rand(void)
721 randseed
= 129 * randseed
+ 907633385;
725 void srand(unsigned int seed
)