Fork me on GitHub

root/lib/strings.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. swap_asc_word
  2. strcmp
  3. strncmp
  4. strcpy
  5. strncpy
  6. strcat
  7. strncat
  8. strlen
  9. get_basename
  10. remove_trailing_slash
  11. is_dir
  12. atoi
  13. memcpy_b
  14. memcpy_w
  15. memcpy_l
  16. memset_b
  17. memset_w
  18. memset_l

   1 /*
   2  * fiwix/lib/strings.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/types.h>
   9 #include <fiwix/tty.h>
  10 #include <fiwix/mm.h>
  11 #include <fiwix/stdio.h>
  12 #include <fiwix/string.h>
  13 
  14 /* convert from big-endian to little-endian (word swap) */
  15 void swap_asc_word(char *str, int len)
  16 {
  17         int n, n2;
  18         short int *ptr;
  19         char *buf;
  20 
  21         if(!(buf = (void *)kmalloc())) {
  22                 return;
  23         }
  24 
  25         ptr = (short int *)str;
  26 
  27         for(n = 0, n2 = 0; n < len; n++) {
  28                 buf[n2++] = *ptr >> 8;
  29                 buf[n2++] = *ptr & 0xFF;
  30                 ptr++;
  31         }
  32         for(n = len - 1; n > 0; n--) {
  33                 if(buf[n] == NULL || buf[n] == ' ') {
  34                         buf[n] = NULL;
  35                 } else {
  36                         break;
  37                 }
  38         }
  39         memcpy_b(str, buf, len);
  40         kfree((unsigned int)buf);
  41 }
  42 
  43 int strcmp(const char *str1, const char *str2)
  44 {
  45         while(*str1) {
  46                 if(*str1 != *str2) {
  47                         return 1;
  48                 }
  49                 str1++;
  50                 str2++;
  51         }
  52         if(!(*str2)) {
  53                 return 0;
  54         }
  55         return 1;
  56 }
  57 
  58 int strncmp(const char *str1, const char *str2, __ssize_t n)
  59 {
  60         while(n > 0) {
  61                 if(*str1 != *str2) {
  62                         return 1;
  63                 }
  64                 str1++;
  65                 str2++;
  66                 n--;
  67         }
  68         return 0;
  69 }
  70 
  71 char *strcpy(char *dest, const char *src)
  72 {
  73         if(!dest || !src) {
  74                 return NULL;
  75         }
  76 
  77         while(*src) {
  78                 *dest = *src;
  79                 dest++;
  80                 src++;
  81         }
  82         *dest = NULL;           /* NULL-terminated */
  83         return dest;
  84 }
  85 
  86 void strncpy(char *dest, const char *src, int len)
  87 {
  88         if(!dest || !src) {
  89                 return;
  90         }
  91 
  92         while((*src) && len) {
  93                 *dest = *src;
  94                 dest++;
  95                 src++;
  96                 len--;
  97         }
  98         *dest = NULL;           /* NULL-terminated */
  99 }
 100 
 101 char *strcat(char *dest, const char *src)
 102 {
 103         char *orig;
 104 
 105         orig = dest;
 106         while(*dest) {
 107                 dest++;
 108         }
 109         while(*src) {
 110                 *dest = *src;
 111                 dest++;
 112                 src++;
 113         }
 114         *dest = NULL;
 115         return orig;
 116 }
 117 
 118 char *strncat(char *dest, const char *src, __ssize_t len)
 119 {
 120         char *orig;
 121 
 122         orig = dest;
 123         while(*dest) {
 124                 dest++;
 125         }
 126         while(*src && len) {
 127                 *dest = *src;
 128                 dest++;
 129                 src++;
 130                 len--;
 131         }
 132         *dest = NULL;
 133         return orig;
 134 }
 135 
 136 int strlen(const char *str)
 137 {
 138         int n;
 139 
 140         n = 0;
 141         while(str && *str) {
 142                 n++;
 143                 str++;
 144         }
 145         return n;
 146 }
 147 
 148 char * get_basename(const char *path)
 149 {
 150         char *basename;
 151         char c;
 152 
 153         basename = NULL;
 154 
 155         while(path) {
 156                 while(*path == '/') {
 157                         path++;
 158                 }
 159                 if(*path != NULL) {
 160                         basename = (char *)path;
 161                 }
 162                 while((c = *(path++)) && (c != '/'));
 163                 if(!c) {
 164                         break;
 165                 }
 166         }
 167         return basename;
 168 }
 169 
 170 char * remove_trailing_slash(char *path)
 171 {
 172         char *p;
 173 
 174         p = path + (strlen(path) - 1);
 175         while(p > path && *p == '/') {
 176                 *p = NULL;
 177                 p--;
 178         }
 179         return path;
 180 }
 181 
 182 int is_dir(const char *path)
 183 {
 184         while(*(path + 1)) {
 185                 path++;
 186         }
 187         if(*path == '/') {
 188                 return 1;
 189         }
 190         return 0;
 191 }
 192 
 193 int atoi(const char *str)
 194 {
 195         int n;
 196 
 197         n = 0;
 198         while(IS_SPACE(*str)) {
 199                 str++;
 200         }
 201         while(IS_NUMERIC(*str)) {
 202                 n = (n * 10) + (*str++ - '0');
 203         }
 204         return n;
 205 }
 206 
 207 void memcpy_b(void *dest, const void *src, unsigned int count)
 208 {
 209         unsigned char *d;
 210         unsigned char *s;
 211 
 212         d = (unsigned char *)dest;
 213         s = (unsigned char *)src;
 214         while(count--) {
 215                 *d = *s;
 216                 d++;
 217                 s++;
 218         }
 219 }
 220 
 221 void memcpy_w(void *dest, const void *src, unsigned int count)
 222 {
 223         unsigned short int *d;
 224         unsigned short int *s;
 225 
 226         d = (unsigned short int *)dest;
 227         s = (unsigned short int *)src;
 228         while(count--) {
 229                 *d = *s;
 230                 d++;
 231                 s++;
 232         }
 233 }
 234 
 235 void memcpy_l(void *dest, const void *src, unsigned int count)
 236 {
 237         unsigned int *d;
 238         unsigned int *s;
 239 
 240         d = (unsigned int *)dest;
 241         s = (unsigned int *)src;
 242         while(count--) {
 243                 *d = *s;
 244                 d++;
 245                 s++;
 246         }
 247 }
 248 
 249 void memset_b(void *dest, unsigned char value, unsigned int count)
 250 {
 251         unsigned char *d;
 252         
 253         d = (unsigned char *)dest;
 254         while(count--) {
 255                 *d = value;
 256                 d++;
 257         }
 258 }
 259 
 260 void memset_w(void *dest, unsigned short int value, unsigned int count)
 261 {
 262         unsigned short int *d;
 263         
 264         d = (unsigned short int *)dest;
 265         while(count--) {
 266                 *d = value;
 267                 d++;
 268         }
 269 }
 270 
 271 void memset_l(void *dest, unsigned int value, unsigned int count)
 272 {
 273         unsigned int *d;
 274         
 275         d = (unsigned int *)dest;
 276         while(count--) {
 277                 *d = value;
 278                 d++;
 279         }
 280 }

/* [previous][next][first][last][top][bottom][index][help] */