A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 128 lines 2.8 kB view raw
1/* 2FUNCTION 3 <<strncpy>>---counted copy string 4 5INDEX 6 strncpy 7 8ANSI_SYNOPSIS 9 #include <string.h> 10 char *strncpy(char *<[dst]>, const char *<[src]>, size_t <[length]>); 11 12TRAD_SYNOPSIS 13 #include <string.h> 14 char *strncpy(<[dst]>, <[src]>, <[length]>) 15 char *<[dst]>; 16 char *<[src]>; 17 size_t <[length]>; 18 19DESCRIPTION 20 <<strncpy>> copies not more than <[length]> characters from the 21 the string pointed to by <[src]> (including the terminating 22 null character) to the array pointed to by <[dst]>. If the 23 string pointed to by <[src]> is shorter than <[length]> 24 characters, null characters are appended to the destination 25 array until a total of <[length]> characters have been 26 written. 27 28RETURNS 29 This function returns the initial value of <[dst]>. 30 31PORTABILITY 32<<strncpy>> is ANSI C. 33 34<<strncpy>> requires no supporting OS subroutines. 35 36QUICKREF 37 strncpy ansi pure 38*/ 39 40#include <string.h> 41#include <limits.h> 42#include "plugin.h" 43#include "_ansi.h" 44#include <stdint.h> 45 46/*SUPPRESS 560*/ 47/*SUPPRESS 530*/ 48 49/* Nonzero if either X or Y is not aligned on a "long" boundary. */ 50#define ROCKBOX_UNALIGNED(X, Y) \ 51 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1))) 52 53#if LONG_MAX == 2147483647L 54#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) 55#else 56#if LONG_MAX == 9223372036854775807L 57/* Nonzero if X (a long int) contains a NULL byte. */ 58#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) 59#else 60#error long int is not a 32bit or 64bit type. 61#endif 62#endif 63 64#ifndef DETECTNULL 65#error long int is not a 32bit or 64bit byte 66#endif 67 68#define TOO_SMALL(LEN) ((LEN) < sizeof (long)) 69 70char * 71_DEFUN (strncpy, (dst0, src0), 72 char *dst0 _AND 73 _CONST char *src0 _AND 74 size_t count) 75{ 76#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) 77 char *dscan; 78 _CONST char *sscan; 79 80 dscan = dst0; 81 sscan = src0; 82 while (count > 0) 83 { 84 --count; 85 if ((*dscan++ = *sscan++) == '\0') 86 break; 87 } 88 while (count-- > 0) 89 *dscan++ = '\0'; 90 91 return dst0; 92#else 93 char *dst = dst0; 94 _CONST char *src = src0; 95 long *aligned_dst; 96 _CONST long *aligned_src; 97 98 /* If SRC and DEST is aligned and count large enough, then copy words. */ 99 if (!ROCKBOX_UNALIGNED (src, dst) && !TOO_SMALL (count)) 100 { 101 aligned_dst = (long*)dst; 102 aligned_src = (long*)src; 103 104 /* SRC and DEST are both "long int" aligned, try to do "long int" 105 sized copies. */ 106 while (count >= sizeof (long int) && !DETECTNULL(*aligned_src)) 107 { 108 count -= sizeof (long int); 109 *aligned_dst++ = *aligned_src++; 110 } 111 112 dst = (char*)aligned_dst; 113 src = (char*)aligned_src; 114 } 115 116 while (count > 0) 117 { 118 --count; 119 if ((*dst++ = *src++) == '\0') 120 break; 121 } 122 123 while (count-- > 0) 124 *dst++ = '\0'; 125 126 return dst0; 127#endif /* not PREFER_SIZE_OVER_SPEED */ 128}