From: Erik M. <er...@us...> - 2001-10-07 22:59:00
|
Update of /cvsroot/blob/blob/src/lib In directory usw-pr-cvs1:/tmp/cvs-serv4549 Modified Files: Makefile.am util.c Added Files: strcpy.c strlen.c strncmp.c strtou32.c Log Message: Move string functions to separate files so the linker will pick up only the necessary functions. --- NEW FILE: strcpy.c --- /* * strcpy.c: copy string * * Copyright (C) 2001 Erik Mouw (J.A...@it...) * * $Id: strcpy.c,v 1.1 2001/10/07 22:58:56 erikm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ident "$Id: strcpy.c,v 1.1 2001/10/07 22:58:56 erikm Exp $" #ifdef HAVE_CONFIG_H # include <blob/config.h> #endif #include <blob/util.h> char *strcpy(char *dest, const char *src) { while(*src != '\0') *dest++ = *src++; *dest = '\0'; return dest; } --- NEW FILE: strlen.c --- /* * strlen.c: return string length * * Copyright (C) 2001 Erik Mouw (J.A...@it...) * * $Id: strlen.c,v 1.1 2001/10/07 22:58:56 erikm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ident "$Id: strlen.c,v 1.1 2001/10/07 22:58:56 erikm Exp $" #ifdef HAVE_CONFIG_H # include <blob/config.h> #endif #include <blob/util.h> int strlen(const char *s) { int i = 0; for(;*s != '\0'; s++) i++; return i; } --- NEW FILE: strncmp.c --- /* * strncmp.c: compare two strings * * Copyright (C) 2001 Erik Mouw (J.A...@it...) * * $Id: strncmp.c,v 1.1 2001/10/07 22:58:56 erikm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ident "$Id: strncmp.c,v 1.1 2001/10/07 22:58:56 erikm Exp $" #ifdef HAVE_CONFIG_H # include <blob/config.h> #endif #include <blob/util.h> int strncmp(const char *s1, const char *s2, int maxlen) { int i; for(i = 0; i < maxlen; i++) { if(s1[i] != s2[i]) return ((int) s1[i]) - ((int) s2[i]); if(s1[i] == 0) return 0; } return 0; } --- NEW FILE: strtou32.c --- /* * strtou32.c: convert string to u32 * * Copyright (C) 2001 Erik Mouw (J.A...@it...) * * $Id: strtou32.c,v 1.1 2001/10/07 22:58:56 erikm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ident "$Id: strtou32.c,v 1.1 2001/10/07 22:58:56 erikm Exp $" #ifdef HAVE_CONFIG_H # include <blob/config.h> #endif #include <blob/types.h> #include <blob/util.h> /* test for a digit. return value if digit or -1 otherwise */ static int digitvalue(char isdigit) { if (isdigit >= '0' && isdigit <= '9' ) return isdigit - '0'; else return -1; } /* test for a hexidecimal digit. return value if digit or -1 otherwise */ static int xdigitvalue(char isdigit) { if (isdigit >= '0' && isdigit <= '9' ) return isdigit - '0'; if (isdigit >= 'a' && isdigit <= 'f') return 10 + isdigit - 'a'; return -1; } /* convert a string to an u32 value. if the string starts with 0x, it * is a hexidecimal string, otherwise we treat is as decimal. returns * the converted value on success, or -1 on failure. no, we don't care * about overflows if the string is too long. */ int strtou32(const char *str, u32 *value) { int i; *value = 0; if(strncmp(str, "0x", 2) == 0) { /* hexadecimal mode */ str += 2; while(*str != '\0') { if((i = xdigitvalue(*str)) < 0) return -1; *value = (*value << 4) | (u32)i; str++; } } else { /* decimal mode */ while(*str != '\0') { if((i = digitvalue(*str)) < 0) return -1; *value = (*value * 10) + (u32)i; str++; } } return 0; } Index: Makefile.am =================================================================== RCS file: /cvsroot/blob/blob/src/lib/Makefile.am,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- Makefile.am 2001/10/07 22:36:11 1.4 +++ Makefile.am 2001/10/07 22:58:56 1.5 @@ -33,6 +33,10 @@ led.c \ reboot.c \ serial.c \ + strcpy.c \ + strlen.c \ + strncmp.c \ + strtou32.c \ terminal.c \ time.c \ util.c Index: util.c =================================================================== RCS file: /cvsroot/blob/blob/src/lib/util.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- util.c 2001/10/07 19:34:17 1.2 +++ util.c 2001/10/07 22:58:56 1.3 @@ -87,112 +87,3 @@ while(dest < limit) *dest++ = wordToWrite; } /* MyMemSet */ - - - - -int strncmp(const char *s1, const char *s2, int maxlen) -{ - int i; - - for(i = 0; i < maxlen; i++) { - if(s1[i] != s2[i]) - return ((int) s1[i]) - ((int) s2[i]); - if(s1[i] == 0) - return 0; - } - - return 0; -} - - - - -int strlen(const char *s) -{ - int i = 0; - - for(;*s != '\0'; s++) - i++; - - return i; -} - - - - -char *strcpy(char *dest, const char *src) -{ - while(*src != '\0') - *dest++ = *src++; - - *dest = '\0'; - - return dest; -} - - - - -/* test for a digit. return value if digit or -1 otherwise */ -static int digitvalue(char isdigit) -{ - if (isdigit >= '0' && isdigit <= '9' ) - return isdigit - '0'; - else - return -1; -} - - - - -/* test for a hexidecimal digit. return value if digit or -1 otherwise */ -static int xdigitvalue(char isdigit) -{ - if (isdigit >= '0' && isdigit <= '9' ) - return isdigit - '0'; - if (isdigit >= 'a' && isdigit <= 'f') - return 10 + isdigit - 'a'; - return -1; -} - - - - -/* convert a string to an u32 value. if the string starts with 0x, it - * is a hexidecimal string, otherwise we treat is as decimal. returns - * the converted value on success, or -1 on failure. no, we don't care - * about overflows if the string is too long. - */ -int strtoval(const char *str, u32 *value) -{ - int i; - - *value = 0; - - if(strncmp(str, "0x", 2) == 0) { - /* hexadecimal mode */ - str += 2; - - while(*str != '\0') { - if((i = xdigitvalue(*str)) < 0) - return -1; - - *value = (*value << 4) | (u32)i; - - str++; - } - } else { - /* decimal mode */ - while(*str != '\0') { - if((i = digitvalue(*str)) < 0) - return -1; - - *value = (*value * 10) + (u32)i; - - str++; - } - } - - return 0; -} |