diff options
Diffstat (limited to 'lib/libc')
67 files changed, 1037 insertions, 401 deletions
diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 57b9694..0c762ec 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -1,5 +1,5 @@ # ***************************************************************************** -# * Copyright (c) 2004, 2007 IBM Corporation +# * Copyright (c) 2004, 2008 IBM Corporation # * All rights reserved. # * This program and the accompanying materials # * are made available under the terms of the BSD License @@ -17,11 +17,11 @@ STRINGCMNDIR = $(LIBCCMNDIR)/string CTYPECMNDIR = $(LIBCCMNDIR)/ctype STDLIBCMNDIR = $(LIBCCMNDIR)/stdlib STDIOCMNDIR = $(LIBCCMNDIR)/stdio +GETOPTCMNDIR = $(LIBCCMNDIR)/getopt include $(TOPCMNDIR)/make.rules -CFLAGS = -g -O2 -fno-builtin -ffreestanding -nostdinc -msoft-float -mno-altivec -mabi=no-altivec -Wall CPPFLAGS = -I$(LIBCCMNDIR)/include LDFLAGS= -nostdlib @@ -40,8 +40,9 @@ include $(STRINGCMNDIR)/Makefile.inc include $(CTYPECMNDIR)/Makefile.inc include $(STDLIBCMNDIR)/Makefile.inc include $(STDIOCMNDIR)/Makefile.inc +include $(GETOPTCMNDIR)/Makefile.inc -OBJS = $(STRING_OBJS) $(CTYPE_OBJS) $(STDLIB_OBJS) $(STDIO_OBJS) +OBJS = $(STRING_OBJS) $(CTYPE_OBJS) $(STDLIB_OBJS) $(STDIO_OBJS) $(GETOPT_OBJS) ifneq ($(NATIVEBUILD),1) # These parts of the libc use assembler, so they can only be compiled when diff --git a/lib/libc/ctype/Makefile.inc b/lib/libc/ctype/Makefile.inc index 8486a8a..25513a9 100644 --- a/lib/libc/ctype/Makefile.inc +++ b/lib/libc/ctype/Makefile.inc @@ -1,5 +1,5 @@ # ***************************************************************************** -# * Copyright (c) 2004, 2007 IBM Corporation +# * Copyright (c) 2004, 2008 IBM Corporation # * All rights reserved. # * This program and the accompanying materials # * are made available under the terms of the BSD License @@ -11,7 +11,7 @@ # ****************************************************************************/ -CTYPE_SRC_C = isdigit.c isspace.c isxdigit.c tolower.c toupper.c +CTYPE_SRC_C = isdigit.c isprint.c isspace.c isxdigit.c tolower.c toupper.c CTYPE_SRC_ASM = CTYPE_SRCS = $(CTYPE_SRC_C:%=$(CTYPECMNDIR)/%) $(CTYPE_SRC_ASM:%=$(CTYPECMNDIR)/%) CTYPE_OBJS = $(CTYPE_SRC_C:%.c=%.o) $(CTYPE_SRC_ASM:%.S=%.o) diff --git a/lib/libc/ctype/isdigit.c b/lib/libc/ctype/isdigit.c index b3d1835..62d08a1 100644 --- a/lib/libc/ctype/isdigit.c +++ b/lib/libc/ctype/isdigit.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/ctype/isprint.c b/lib/libc/ctype/isprint.c new file mode 100644 index 0000000..c74880f --- /dev/null +++ b/lib/libc/ctype/isprint.c @@ -0,0 +1,18 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <ctype.h> + +int isprint(int ch) +{ + return (ch >= 32 && ch < 127); +} diff --git a/lib/libc/ctype/isspace.c b/lib/libc/ctype/isspace.c index 0e749d7..5123019 100644 --- a/lib/libc/ctype/isspace.c +++ b/lib/libc/ctype/isspace.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/ctype/isxdigit.c b/lib/libc/ctype/isxdigit.c index 2b55d1a..9d323f3 100644 --- a/lib/libc/ctype/isxdigit.c +++ b/lib/libc/ctype/isxdigit.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/ctype/tolower.c b/lib/libc/ctype/tolower.c index f7eb03b..f775e90 100644 --- a/lib/libc/ctype/tolower.c +++ b/lib/libc/ctype/tolower.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/ctype/toupper.c b/lib/libc/ctype/toupper.c index 0eba9d0..9bcee52 100644 --- a/lib/libc/ctype/toupper.c +++ b/lib/libc/ctype/toupper.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/getopt/Makefile.inc b/lib/libc/getopt/Makefile.inc new file mode 100644 index 0000000..8a2e32f --- /dev/null +++ b/lib/libc/getopt/Makefile.inc @@ -0,0 +1,17 @@ +# ***************************************************************************** +# * Copyright (c) 2004, 2008 IBM Corporation +# * All rights reserved. +# * This program and the accompanying materials +# * are made available under the terms of the BSD License +# * which accompanies this distribution, and is available at +# * http://www.opensource.org/licenses/bsd-license.php +# * +# * Contributors: +# * IBM Corporation - initial implementation +# ****************************************************************************/ + +GETOPT_SRC_C = getopt.c +GETOPT_OBJS = $(GETOPT_SRC_C:%.c=%.o) + +%.o : $(GETOPTCMNDIR)/%.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ diff --git a/lib/libc/getopt/getopt.c b/lib/libc/getopt/getopt.c new file mode 100644 index 0000000..be626dd --- /dev/null +++ b/lib/libc/getopt/getopt.c @@ -0,0 +1,470 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +/* + * includes + ******************************************************************************* + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <getopt.h> + +/* + * global variables, types & constants + * may be removed if already defined + ******************************************************************************* + */ +int opterr = 1; +int optopt = 0; +int optind = 1; +char *optarg = NULL; + +/* + * internal values needed by getopt + * DO NOT CHANGE or REMOVE + */ +enum { + OPTIONAL_ARG = 0, + MANDATORY_ARG = 1, + NO_ARG = 2 +}; + +/* + * variables needed by getopt & getopt_long! + * DO NOT REMOVE + */ +static char *optstart = NULL; + +int +getopt(int argc, char **argv, const char *options) +{ + char *optptr; + char *argptr; + int optman; + int idx; + int ret = 0; + int argpresent; + + /* + * reset used global values + */ + optopt = 0; + optarg = NULL; + + /* + * reset getopt if a new argv pointer is passed + */ + if (optstart != argv[0]) { + optopt = 0; + optind = 1; + optarg = NULL; + optstart = argv[0]; + } + + /* + * return if no more arguments are available + */ + if (optind >= argc) { + return -1; + } + + /* + * start parsing argv[optind] + */ + idx = 0; + + /* + * return if the option does not begin with a '-' or has more than 2 characters + */ + if (argv[optind][idx] != '-') { + + if (opterr != 0) { + printf("unknown option \'%s\', expecting \'-\'\n", + argv[optind]); + } + + optopt = (int) argv[optind][idx]; + optind++; + + return '?'; + } + + /* + * continue to the next character in argv[optind] + */ + idx++; + + /* + * identify the option + * make sure if an option contains a ':' to invalidate the option + */ + optptr = strchr(argv[optind], ':'); + + if (optptr == NULL) { + optptr = strchr(options, (int) argv[optind][idx]); + } else { + optptr = NULL; + } + + /* + * check whether the option is present + */ + if (optptr == NULL) { + /* + * unknown option detected + */ + if (opterr != 0) { + printf("unknown option \'%s\'\n", argv[optind]); + } + + optopt = (int) argv[optind][idx]; + optind++; + + return '?'; + } + + /* + * the option is present in the option string + * setup return value + */ + ret = (int) *optptr; + + /* + * get option argument if needed + */ + optptr++; + + /* + * determine between mandatory and optional argument + */ + optman = NO_ARG; + + if (*optptr == ':') { + optman--; // now set to MANDATORY_ARG + } + + if (optman == MANDATORY_ARG) { + optptr++; + + if (*optptr == ':') { + optman--; // now set to OPTIONAL_ARG + } + + } + + /* + * if strlen( argv[optind ) is greater than 2, + * the argument is in the same argv + */ + if (strlen(argv[optind]) > 2) { + argptr = &argv[optind][2]; + + /* + * do not allow '-' in an argument + */ + if (strchr(argptr, '-') != NULL) { + + if (opterr != 0) { + printf + ("illegal argument value \'%s\' for option \'-%c\'\n", + argptr, ret); + } + + optopt = ret; + + return '?'; + } + + } else { + /* + * move on to the next argv + * it now either contains an argument or the next option + */ + optind++; + + /* + * make sure not to overflow + */ + if (optind < argc) { + argptr = argv[optind]; + } else { + argptr = NULL; + } + + } + + /* + * do the needed actions for the argument state + */ + switch (optman) { + case OPTIONAL_ARG: + + if (argptr == NULL) { + break; + } + + if (*argptr != '-') { + /* + * argument present + */ + optarg = argptr; + optind++; + + } + + + break; + + case MANDATORY_ARG: + argpresent = (argptr != NULL); + + if (argpresent) { + argpresent = (*argptr != '-'); + } + + if (argpresent) { + /* + * argument present + */ + optarg = argptr; + optind++; + } else { + /* + * mandatory argument missing + */ + if (opterr != 0) { + printf + ("missing argument for option \'-%c\'\n", + ret); + } + + optopt = ret; + + /* + * if the first character of options is a ':' + * return a ':' instead of a '?' in case of + * a missing argument + */ + if (*options == ':') { + ret = ':'; + } else { + ret = '?'; + } + + } + + + break; + + case NO_ARG: + + if (strlen(argv[optind - 1]) > 2) { + + if (opterr != 0) { + printf + ("too many arguments for option \'-%c\'\n", + ret); + } + + optopt = ret; + ret = '?'; + } + + + break; + + } + + return ret; +} + +int +getopt_long(int argc, char **argv, const char *shortopts, + const struct option *longopts, int *indexptr) +{ + struct option *optptr = (struct option *) longopts; + int optidx = 0; + int idx; + int ret = 0; + int argpresent; + + /* + * reset used global values + */ + optopt = 0; + optarg = NULL; + + /* + * reset indexptr + */ + *indexptr = -1; + + /* + * reset getopt if a new argv pointer is passed + */ + if (optstart != argv[0]) { + optopt = 0; + optind = 1; + optarg = NULL; + optstart = argv[0]; + } + + /* + * return if no more arguments are available + */ + if (optind >= argc) { + return -1; + } + + /* + * start parsing argv[optind] + */ + idx = 0; + + /* + * return if the option does not begin with a '-' + */ + if (argv[optind][idx] != '-') { + printf("unknown option \'%s\', expecting \'-\'\n", + argv[optind]); + + optind++; + + return '?'; + } + + /* + * move on to the next character in argv[optind] + */ + idx++; + + /* + * return getopt() in case of a short option + */ + if (argv[optind][idx] != '-') { + return getopt(argc, argv, shortopts); + } + + /* + * handle a long option + */ + idx++; + + while (optptr->name != NULL) { + + if (strcmp(&argv[optind][idx], optptr->name) == 0) { + break; + } + + optptr++; + optidx++; + } + + /* + * no matching option found + */ + if (optptr->name == NULL) { + printf("unknown option \'%s\'\n", argv[optind]); + + optind++; + + return '?'; + } + + /* + * option was found, set up index pointer + */ + *indexptr = optidx; + + /* + * get argument + */ + optind++; + + switch (optptr->has_arg) { + case no_argument: + /* + * nothing to do + */ + + break; + + case required_argument: + argpresent = (optind != argc); + + if (argpresent) { + argpresent = (argv[optind][0] != '-'); + } + + if (argpresent) { + /* + * argument present + */ + optarg = argv[optind]; + optind++; + } else { + /* + * mandatory argument missing + */ + printf("missing argument for option \'%s\'\n", + argv[optind - 1]); + + ret = '?'; + } + + + break; + + case optional_argument: + + if (optind == argc) { + break; + } + + if (argv[optind][0] != '-') { + /* + * argument present + */ + optarg = argv[optind]; + optind++; + } + + + break; + + default: + printf("unknown argument option for option \'%s\'\n", + argv[optind - 1]); + + ret = '?'; + + break; + + } + + /* + * setup return values + */ + if (ret != '?') { + + if (optptr->flag == NULL) { + ret = optptr->val; + } else { + *optptr->flag = optptr->val; + ret = 0; + } + + } + + return ret; +} diff --git a/lib/libc/include/ctype.h b/lib/libc/include/ctype.h index 3e5e47b..9051a75 100644 --- a/lib/libc/include/ctype.h +++ b/lib/libc/include/ctype.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License @@ -15,6 +15,7 @@ int isdigit(int c); int isxdigit(int c); +int isprint(int c); int isspace(int c); int tolower(int c); diff --git a/lib/libc/include/errno.h b/lib/libc/include/errno.h index eea7a36..d585934 100644 --- a/lib/libc/include/errno.h +++ b/lib/libc/include/errno.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/getopt.h b/lib/libc/include/getopt.h new file mode 100644 index 0000000..5956986 --- /dev/null +++ b/lib/libc/include/getopt.h @@ -0,0 +1,37 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#ifndef GETOPT_H +#define GETOPT_H + +extern char *optarg; +extern int optind; +extern int opterr; +extern int optopt; + +struct option { + const char *name; + int has_arg; + int *flag; + int val; +}; + +enum { + no_argument = 0, + required_argument, + optional_argument +}; + +int getopt(int argc, char **, const char *); +int getopt_long(int argc, char **, const char *, const struct option *, int *); + +#endif /* GETOPT_H */ diff --git a/lib/libc/include/limits.h b/lib/libc/include/limits.h index ce694ea..4726835 100644 --- a/lib/libc/include/limits.h +++ b/lib/libc/include/limits.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/stdarg.h b/lib/libc/include/stdarg.h index 422c598..d3d12f7 100644 --- a/lib/libc/include/stdarg.h +++ b/lib/libc/include/stdarg.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/stddef.h b/lib/libc/include/stddef.h index 318e2bb..ba2d960 100644 --- a/lib/libc/include/stddef.h +++ b/lib/libc/include/stddef.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/stdint.h b/lib/libc/include/stdint.h index e455f19..518a723 100644 --- a/lib/libc/include/stdint.h +++ b/lib/libc/include/stdint.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h index 096e035..84cddea 100644 --- a/lib/libc/include/stdio.h +++ b/lib/libc/include/stdio.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/stdlib.h b/lib/libc/include/stdlib.h index ad0ef4f..dff57f5 100644 --- a/lib/libc/include/stdlib.h +++ b/lib/libc/include/stdlib.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License @@ -23,6 +23,8 @@ void *realloc(void *ptr, size_t size); void free(void *ptr); void *memalign(size_t boundary, size_t size); +int atoi(const char *str); +long atol(const char *str); unsigned long int strtoul(const char *nptr, char **endptr, int base); long int strtol(const char *nptr, char **endptr, int base); diff --git a/lib/libc/include/string.h b/lib/libc/include/string.h index d792b1b..ebc7568 100644 --- a/lib/libc/include/string.h +++ b/lib/libc/include/string.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/include/unistd.h b/lib/libc/include/unistd.h index c09c45d..f1b1c62 100644 --- a/lib/libc/include/unistd.h +++ b/lib/libc/include/unistd.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index 8783570..ac5302d 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -1,5 +1,5 @@ # ***************************************************************************** -# * Copyright (c) 2004, 2007 IBM Corporation +# * Copyright (c) 2004, 2008 IBM Corporation # * All rights reserved. # * This program and the accompanying materials # * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c index de77906..6e23951 100644 --- a/lib/libc/stdio/fileno.c +++ b/lib/libc/stdio/fileno.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/fprintf.c b/lib/libc/stdio/fprintf.c index 2337464..866df39 100644 --- a/lib/libc/stdio/fprintf.c +++ b/lib/libc/stdio/fprintf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/fscanf.c b/lib/libc/stdio/fscanf.c index e2e28de..321b163 100644 --- a/lib/libc/stdio/fscanf.c +++ b/lib/libc/stdio/fscanf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/printf.c b/lib/libc/stdio/printf.c index e73798b..01f4592 100644 --- a/lib/libc/stdio/printf.c +++ b/lib/libc/stdio/printf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/putc.c b/lib/libc/stdio/putc.c index b21e37b..230e9d1 100644 --- a/lib/libc/stdio/putc.c +++ b/lib/libc/stdio/putc.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/putchar.c b/lib/libc/stdio/putchar.c index 524423e..5c750d9 100644 --- a/lib/libc/stdio/putchar.c +++ b/lib/libc/stdio/putchar.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/puts.c b/lib/libc/stdio/puts.c index bf2b8af..3f48dbf 100644 --- a/lib/libc/stdio/puts.c +++ b/lib/libc/stdio/puts.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/scanf.c b/lib/libc/stdio/scanf.c index 9ff8f00..96b6399 100644 --- a/lib/libc/stdio/scanf.c +++ b/lib/libc/stdio/scanf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/setvbuf.c b/lib/libc/stdio/setvbuf.c index fcc10f5..9b62dd8 100644 --- a/lib/libc/stdio/setvbuf.c +++ b/lib/libc/stdio/setvbuf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/sprintf.c b/lib/libc/stdio/sprintf.c index c84255b..9c4540e 100644 --- a/lib/libc/stdio/sprintf.c +++ b/lib/libc/stdio/sprintf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/stdchnls.c b/lib/libc/stdio/stdchnls.c index 9b45be0..41ed958 100644 --- a/lib/libc/stdio/stdchnls.c +++ b/lib/libc/stdio/stdchnls.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index 9a657ec..765feea 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c index 7a9f237..4ddd210 100644 --- a/lib/libc/stdio/vfscanf.c +++ b/lib/libc/stdio/vfscanf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License @@ -18,20 +18,20 @@ static int -_getc(FILE *stream) +_getc(FILE * stream) { int count; char c; if (stream->mode == _IONBF || stream->buf == NULL) { - if (read(stream->fd,&c,1) == 1) - return (int)c; + if (read(stream->fd, &c, 1) == 1) + return (int) c; else return EOF; } if (stream->pos == 0 || stream->pos >= BUFSIZ || - stream->buf[stream->pos] == '\0') { + stream->buf[stream->pos] == '\0') { count = read(stream->fd, stream->buf, BUFSIZ); if (count < 0) count = 0; @@ -44,16 +44,16 @@ _getc(FILE *stream) } static void -_ungetc(int ch, FILE *stream) +_ungetc(int ch, FILE * stream) { - if(stream->mode != _IONBF && stream->pos > 0) + if (stream->mode != _IONBF && stream->pos > 0) stream->pos--; } static int _is_voidage(int ch) { - if(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0') + if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\0') return 1; else return 0; @@ -61,143 +61,150 @@ _is_voidage(int ch) static int -_scanf(FILE *stream, const char *fmt, va_list *ap) +_scanf(FILE * stream, const char *fmt, va_list * ap) { - int i=0; + int i = 0; int length = 0; - fmt++; + fmt++; + + while (*fmt != '\0') { - while(*fmt != '\0') { - char tbuf[256]; char ch; - switch(*fmt) { - case 'd': - case 'i': - ch = _getc(stream); - if(length == 0) { - while(!_is_voidage(ch) && isdigit(ch)) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } - } else { - while(!_is_voidage(ch) && i < length && isdigit(ch)) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } + switch (*fmt) { + case 'd': + case 'i': + ch = _getc(stream); + if (length == 0) { + while (!_is_voidage(ch) && isdigit(ch)) { + tbuf[i] = ch; + ch = _getc(stream); + i++; } - _ungetc(ch, stream); - tbuf[i] = '\0'; - - ch = _getc(stream); - if(!_is_voidage(ch)) - _ungetc(ch, stream); - - if(strlen(tbuf) == 0) - return 0; - - *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 10); - break; - case 'X': - case 'x': - ch = _getc(stream); - if(length == 0) { - while(!_is_voidage(ch) && isxdigit(ch)) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } - } else { - while(!_is_voidage(ch) && i < length && isxdigit(ch)) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } + } else { + while (!_is_voidage(ch) && i < length + && isdigit(ch)) { + tbuf[i] = ch; + ch = _getc(stream); + i++; } + } + /* We tried to understand what this is good for... + * but we did not. We know for sure that it does not + * work on SLOF if this is active. */ + /* _ungetc(ch, stream); */ + tbuf[i] = '\0'; + + /* ch = _getc(stream); */ + if (!_is_voidage(ch)) _ungetc(ch, stream); - tbuf[i] = '\0'; - - ch = _getc(stream); - if(!_is_voidage(ch)) - _ungetc(ch, stream); - - if(strlen(tbuf) == 0) - return 0; - - *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 16); - break; - case 'O': - case 'o': - ch = _getc(stream); - if(length == 0) { - while(!_is_voidage(ch) && !(ch < '0' || ch > '7')) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } - } else { - while(!_is_voidage(ch) && i < length && !(ch < '0' || ch > '7')) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } + + if (strlen(tbuf) == 0) + return 0; + + *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 10); + break; + case 'X': + case 'x': + ch = _getc(stream); + if (length == 0) { + while (!_is_voidage(ch) && isxdigit(ch)) { + tbuf[i] = ch; + ch = _getc(stream); + i++; + } + } else { + while (!_is_voidage(ch) && i < length + && isxdigit(ch)) { + tbuf[i] = ch; + ch = _getc(stream); + i++; } + } + /* _ungetc(ch, stream); */ + tbuf[i] = '\0'; + + /* ch = _getc(stream); */ + if (!_is_voidage(ch)) _ungetc(ch, stream); - tbuf[i] = '\0'; - - ch = _getc(stream); - if(!_is_voidage(ch)) - _ungetc(ch, stream); - if(strlen(tbuf) == 0) - return 0; + if (strlen(tbuf) == 0) + return 0; - *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 8); - break; - case 'c': - ch = _getc(stream); - while(_is_voidage(ch)) + *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 16); + break; + case 'O': + case 'o': + ch = _getc(stream); + if (length == 0) { + while (!_is_voidage(ch) + && !(ch < '0' || ch > '7')) { + tbuf[i] = ch; ch = _getc(stream); - - *(va_arg(*ap, char *)) = ch; - - ch = _getc(stream); - if(!_is_voidage(ch)) - _ungetc(ch, stream); + i++; + } + } else { + while (!_is_voidage(ch) && i < length + && !(ch < '0' || ch > '7')) { + tbuf[i] = ch; + ch = _getc(stream); + i++; + } + } + /* _ungetc(ch, stream); */ + tbuf[i] = '\0'; + + /* ch = _getc(stream); */ + if (!_is_voidage(ch)) + _ungetc(ch, stream); + + if (strlen(tbuf) == 0) + return 0; - break; - case 's': + *(va_arg(*ap, int *)) = strtol(tbuf, NULL, 8); + break; + case 'c': + ch = _getc(stream); + while (_is_voidage(ch)) ch = _getc(stream); - if(length == 0) { - while(!_is_voidage(ch)) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } - } else { - while(!_is_voidage(ch) && i < length) { - tbuf[i] = ch; - ch = _getc(stream); - i++; - } + + *(va_arg(*ap, char *)) = ch; + + ch = _getc(stream); + if (!_is_voidage(ch)) + _ungetc(ch, stream); + + break; + case 's': + ch = _getc(stream); + if (length == 0) { + while (!_is_voidage(ch)) { + tbuf[i] = ch; + ch = _getc(stream); + i++; + } + } else { + while (!_is_voidage(ch) && i < length) { + tbuf[i] = ch; + ch = _getc(stream); + i++; } + } + /* _ungetc(ch, stream); */ + tbuf[i] = '\0'; + + /* ch = _getc(stream); */ + if (!_is_voidage(ch)) _ungetc(ch, stream); - tbuf[i] = '\0'; - - ch = _getc(stream); - if(!_is_voidage(ch)) - _ungetc(ch, stream); - - strcpy(va_arg(*ap, char *), tbuf); - break; - default: - if(*fmt >= '0' && *fmt <= '9') - length += *fmt - '0'; - break; + + strcpy(va_arg(*ap, char *), tbuf); + break; + default: + if (*fmt >= '0' && *fmt <= '9') + length += *fmt - '0'; + break; } fmt++; } @@ -208,34 +215,36 @@ _scanf(FILE *stream, const char *fmt, va_list *ap) int -vfscanf(FILE *stream, const char *fmt, va_list ap) +vfscanf(FILE * stream, const char *fmt, va_list ap) { int args = 0; - while(*fmt != '\0') { - - if(*fmt == '%') { - + while (*fmt != '\0') { + + if (*fmt == '%') { + char formstr[20]; - int i=0; - + int i = 0; + do { formstr[i] = *fmt; fmt++; i++; - } while(!(*fmt == 'd' || *fmt == 'i' || *fmt == 'x' || *fmt == 'X' - || *fmt == 'p' || *fmt == 'c' || *fmt == 's' || *fmt == '%' - || *fmt == 'O' || *fmt == 'o' )); + } while (! + (*fmt == 'd' || *fmt == 'i' || *fmt == 'x' + || *fmt == 'X' || *fmt == 'p' || *fmt == 'c' + || *fmt == 's' || *fmt == '%' || *fmt == 'O' + || *fmt == 'o')); formstr[i++] = *fmt; formstr[i] = '\0'; - if(*fmt != '%') { - if(_scanf(stream, formstr, &ap) <= 0) + if (*fmt != '%') { + if (_scanf(stream, formstr, &ap) <= 0) return args; else args++; } - } + } fmt++; @@ -244,13 +253,14 @@ vfscanf(FILE *stream, const char *fmt, va_list ap) return args; } -int getc(FILE *stream) +int +getc(FILE * stream) { return _getc(stream); } -int getchar(void) +int +getchar(void) { return _getc(stdin); } - diff --git a/lib/libc/stdio/vsnprintf.c b/lib/libc/stdio/vsnprintf.c index 200512f..e78fb3d 100644 --- a/lib/libc/stdio/vsnprintf.c +++ b/lib/libc/stdio/vsnprintf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/vsprintf.c b/lib/libc/stdio/vsprintf.c index c565a89..0dfd737 100644 --- a/lib/libc/stdio/vsprintf.c +++ b/lib/libc/stdio/vsprintf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdio/vsscanf.c b/lib/libc/stdio/vsscanf.c index 68760b5..b9603e9 100644 --- a/lib/libc/stdio/vsscanf.c +++ b/lib/libc/stdio/vsscanf.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 5b854fc..702f6d7 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -1,5 +1,5 @@ # ***************************************************************************** -# * Copyright (c) 2004, 2007 IBM Corporation +# * Copyright (c) 2004, 2008 IBM Corporation # * All rights reserved. # * This program and the accompanying materials # * are made available under the terms of the BSD License @@ -11,7 +11,7 @@ # ****************************************************************************/ -STDLIB_SRC_C = error.c strtoul.c strtol.c rand.c \ +STDLIB_SRC_C = error.c atoi.c atol.c strtoul.c strtol.c rand.c \ malloc.c memalign.c realloc.c free.c STDLIB_SRC_ASM = diff --git a/lib/libc/stdlib/atoi.c b/lib/libc/stdlib/atoi.c new file mode 100644 index 0000000..d2fb33b --- /dev/null +++ b/lib/libc/stdlib/atoi.c @@ -0,0 +1,18 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stdlib.h> + +int atoi(const char *str) +{ + return strtol(str, NULL, 0); +} diff --git a/lib/libc/stdlib/atol.c b/lib/libc/stdlib/atol.c new file mode 100644 index 0000000..a6aa47b --- /dev/null +++ b/lib/libc/stdlib/atol.c @@ -0,0 +1,18 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stdlib.h> + +long atol(const char *str) +{ + return strtol(str, NULL, 0); +} diff --git a/lib/libc/stdlib/error.c b/lib/libc/stdlib/error.c index d440f7c..81020ca 100644 --- a/lib/libc/stdlib/error.c +++ b/lib/libc/stdlib/error.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/free.c b/lib/libc/stdlib/free.c index 7aff279..9005450 100644 --- a/lib/libc/stdlib/free.c +++ b/lib/libc/stdlib/free.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 4bc4b79..b2a3138 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/malloc_defs.h b/lib/libc/stdlib/malloc_defs.h index 59fcd91..1933026 100644 --- a/lib/libc/stdlib/malloc_defs.h +++ b/lib/libc/stdlib/malloc_defs.h @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/memalign.c b/lib/libc/stdlib/memalign.c index 0bb430e..3b678aa 100644 --- a/lib/libc/stdlib/memalign.c +++ b/lib/libc/stdlib/memalign.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index d3a0254..87e3efd 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/realloc.c b/lib/libc/stdlib/realloc.c index d1dfdfb..652e900 100644 --- a/lib/libc/stdlib/realloc.c +++ b/lib/libc/stdlib/realloc.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/stdlib/strtol.c b/lib/libc/stdlib/strtol.c index ee14a39..aae5e54 100644 --- a/lib/libc/stdlib/strtol.c +++ b/lib/libc/stdlib/strtol.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License @@ -9,106 +9,105 @@ * Contributors: * IBM Corporation - initial implementation *****************************************************************************/ -
-long int strtol(const char *S, char **PTR,int BASE)
-{
- long rval = 0;
- short int negative = 0;
- short int digit;
- // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
- char* ptr;
- if (PTR == 0)
- {
- //override
- PTR = &ptr;
- }
- // i use PTR to advance through the string
- *PTR = (char *) S;
- //check if BASE is ok
- if ((BASE < 0) || BASE > 36)
- {
- return 0;
- }
- // ignore white space at beginning of S
- while ((**PTR == ' ')
- || (**PTR == '\t')
- || (**PTR == '\n')
- || (**PTR == '\r')
- )
- {
- (*PTR)++;
- }
- // check if S starts with "-" in which case the return value is negative
- if (**PTR == '-')
- {
- negative = 1;
- (*PTR)++;
- }
- // if BASE is 0... determine the base from the first chars...
- if (BASE == 0)
- {
- // if S starts with "0x", BASE = 16, else 10
- if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
- {
- BASE = 16;
- (*PTR)++;
- (*PTR)++;
- }
- else
- {
- BASE = 10;
- }
- }
- if (BASE == 16)
- {
- // S may start with "0x"
- if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
- {
- (*PTR)++;
- (*PTR)++;
- }
- }
- //until end of string
- while (**PTR)
- {
- if (((**PTR) >= '0') && ((**PTR) <= '9'))
- {
- //digit (0..9)
- digit = **PTR - '0';
- }
- else if (((**PTR) >= 'a') && ((**PTR) <='z'))
- {
- //alphanumeric digit lowercase(a (10) .. z (35) )
- digit = (**PTR - 'a') + 10;
- }
- else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
- {
- //alphanumeric digit uppercase(a (10) .. z (35) )
- digit = (**PTR - 'A') + 10;
- }
- else
- {
- //end of parseable number reached...
- break;
- }
- if (digit < BASE)
- {
- rval = (rval * BASE) + digit;
- }
- else
- {
- //digit found, but its too big for current base
- //end of parseable number reached...
- break;
- }
- //next...
- (*PTR)++;
- }
- if (negative)
- {
- return rval * -1;
- }
- //else
- return rval;
-}
-
+ +long int strtol(const char *S, char **PTR,int BASE) +{ + long rval = 0; + short int negative = 0; + short int digit; + // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr + char* ptr; + if (PTR == 0) + { + //override + PTR = &ptr; + } + // i use PTR to advance through the string + *PTR = (char *) S; + //check if BASE is ok + if ((BASE < 0) || BASE > 36) + { + return 0; + } + // ignore white space at beginning of S + while ((**PTR == ' ') + || (**PTR == '\t') + || (**PTR == '\n') + || (**PTR == '\r') + ) + { + (*PTR)++; + } + // check if S starts with "-" in which case the return value is negative + if (**PTR == '-') + { + negative = 1; + (*PTR)++; + } + // if BASE is 0... determine the base from the first chars... + if (BASE == 0) + { + // if S starts with "0x", BASE = 16, else 10 + if ((**PTR == '0') && (*((*PTR)+1) == 'x')) + { + BASE = 16; + (*PTR)++; + (*PTR)++; + } + else + { + BASE = 10; + } + } + if (BASE == 16) + { + // S may start with "0x" + if ((**PTR == '0') && (*((*PTR)+1) == 'x')) + { + (*PTR)++; + (*PTR)++; + } + } + //until end of string + while (**PTR) + { + if (((**PTR) >= '0') && ((**PTR) <= '9')) + { + //digit (0..9) + digit = **PTR - '0'; + } + else if (((**PTR) >= 'a') && ((**PTR) <='z')) + { + //alphanumeric digit lowercase(a (10) .. z (35) ) + digit = (**PTR - 'a') + 10; + } + else if (((**PTR) >= 'A') && ((**PTR) <='Z')) + { + //alphanumeric digit uppercase(a (10) .. z (35) ) + digit = (**PTR - 'A') + 10; + } + else + { + //end of parseable number reached... + break; + } + if (digit < BASE) + { + rval = (rval * BASE) + digit; + } + else + { + //digit found, but its too big for current base + //end of parseable number reached... + break; + } + //next... + (*PTR)++; + } + if (negative) + { + return rval * -1; + } + //else + return rval; +} diff --git a/lib/libc/stdlib/strtoul.c b/lib/libc/stdlib/strtoul.c index 74cdccd..3a86c50 100644 --- a/lib/libc/stdlib/strtoul.c +++ b/lib/libc/stdlib/strtoul.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License @@ -9,95 +9,95 @@ * Contributors: * IBM Corporation - initial implementation *****************************************************************************/ -
-unsigned long int strtoul(const char *S, char **PTR,int BASE)
-{
- unsigned long rval = 0;
- short int digit;
- // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
- char* ptr;
- if (PTR == 0)
- {
- //override
- PTR = &ptr;
- }
- // i use PTR to advance through the string
- *PTR = (char *) S;
- //check if BASE is ok
- if ((BASE < 0) || BASE > 36)
- {
- return 0;
- }
- // ignore white space at beginning of S
- while ((**PTR == ' ')
- || (**PTR == '\t')
- || (**PTR == '\n')
- || (**PTR == '\r')
- )
- {
- (*PTR)++;
- }
- // if BASE is 0... determine the base from the first chars...
- if (BASE == 0)
- {
- // if S starts with "0x", BASE = 16, else 10
- if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
- {
- BASE = 16;
- (*PTR)++;
- (*PTR)++;
- }
- else
- {
- BASE = 10;
- }
- }
- if (BASE == 16)
- {
- // S may start with "0x"
- if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
- {
- (*PTR)++;
- (*PTR)++;
- }
- }
- //until end of string
- while (**PTR)
- {
- if (((**PTR) >= '0') && ((**PTR) <='9'))
- {
- //digit (0..9)
- digit = **PTR - '0';
- }
- else if (((**PTR) >= 'a') && ((**PTR) <='z'))
- {
- //alphanumeric digit lowercase(a (10) .. z (35) )
- digit = (**PTR - 'a') + 10;
- }
- else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
- {
- //alphanumeric digit uppercase(a (10) .. z (35) )
- digit = (**PTR - 'A') + 10;
- }
- else
- {
- //end of parseable number reached...
- break;
- }
- if (digit < BASE)
- {
- rval = (rval * BASE) + digit;
- }
- else
- {
- //digit found, but its too big for current base
- //end of parseable number reached...
- break;
- }
- //next...
- (*PTR)++;
- }
- //done
- return rval;
-}
-
+ +unsigned long int strtoul(const char *S, char **PTR,int BASE) +{ + unsigned long rval = 0; + short int digit; + // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr + char* ptr; + if (PTR == 0) + { + //override + PTR = &ptr; + } + // i use PTR to advance through the string + *PTR = (char *) S; + //check if BASE is ok + if ((BASE < 0) || BASE > 36) + { + return 0; + } + // ignore white space at beginning of S + while ((**PTR == ' ') + || (**PTR == '\t') + || (**PTR == '\n') + || (**PTR == '\r') + ) + { + (*PTR)++; + } + // if BASE is 0... determine the base from the first chars... + if (BASE == 0) + { + // if S starts with "0x", BASE = 16, else 10 + if ((**PTR == '0') && (*((*PTR)+1) == 'x')) + { + BASE = 16; + (*PTR)++; + (*PTR)++; + } + else + { + BASE = 10; + } + } + if (BASE == 16) + { + // S may start with "0x" + if ((**PTR == '0') && (*((*PTR)+1) == 'x')) + { + (*PTR)++; + (*PTR)++; + } + } + //until end of string + while (**PTR) + { + if (((**PTR) >= '0') && ((**PTR) <='9')) + { + //digit (0..9) + digit = **PTR - '0'; + } + else if (((**PTR) >= 'a') && ((**PTR) <='z')) + { + //alphanumeric digit lowercase(a (10) .. z (35) ) + digit = (**PTR - 'a') + 10; + } + else if (((**PTR) >= 'A') && ((**PTR) <='Z')) + { + //alphanumeric digit uppercase(a (10) .. z (35) ) + digit = (**PTR - 'A') + 10; + } + else + { + //end of parseable number reached... + break; + } + if (digit < BASE) + { + rval = (rval * BASE) + digit; + } + else + { + //digit found, but its too big for current base + //end of parseable number reached... + break; + } + //next... + (*PTR)++; + } + //done + return rval; +} + diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index e857bbc..7ccf3c4 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -1,5 +1,5 @@ # ***************************************************************************** -# * Copyright (c) 2004, 2007 IBM Corporation +# * Copyright (c) 2004, 2008 IBM Corporation # * All rights reserved. # * This program and the accompanying materials # * are made available under the terms of the BSD License @@ -13,7 +13,7 @@ STRING_SRC_C = strcat.c strchr.c strcmp.c strcpy.c strlen.c strncmp.c \ strncpy.c strstr.c memset.c memcpy.c memmove.c memchr.c \ - memcmp.c strcasecmp.c strncasecmp.c + memcmp.c strcasecmp.c strncasecmp.c strtok.c STRING_SRC_ASM = STRING_SRCS = $(STRING_SRC_C:%=$(STRINGCMNDIR)/%) $(STRING_SRC_ASM:%=$(STRINGCMNDIR)/%) STRING_OBJS = $(STRING_SRC_C:%.c=%.o) $(STRING_SRC_ASM:%.S=%.o) diff --git a/lib/libc/string/memchr.c b/lib/libc/string/memchr.c index d4e9b7f..c3fe751 100644 --- a/lib/libc/string/memchr.c +++ b/lib/libc/string/memchr.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/memcmp.c b/lib/libc/string/memcmp.c index b64d560..3b69cef 100644 --- a/lib/libc/string/memcmp.c +++ b/lib/libc/string/memcmp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/memcpy.c b/lib/libc/string/memcpy.c index 36a4a3c..00f419b 100644 --- a/lib/libc/string/memcpy.c +++ b/lib/libc/string/memcpy.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/memmove.c b/lib/libc/string/memmove.c index 7ceee33..3acf1a9 100644 --- a/lib/libc/string/memmove.c +++ b/lib/libc/string/memmove.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/memset.c b/lib/libc/string/memset.c index 0f5a660..f8dfbf5 100644 --- a/lib/libc/string/memset.c +++ b/lib/libc/string/memset.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strcasecmp.c b/lib/libc/string/strcasecmp.c index 98cde44..f75294f 100644 --- a/lib/libc/string/strcasecmp.c +++ b/lib/libc/string/strcasecmp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strcat.c b/lib/libc/string/strcat.c index eadd6b9..eb597a0 100644 --- a/lib/libc/string/strcat.c +++ b/lib/libc/string/strcat.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strchr.c b/lib/libc/string/strchr.c index 214eaa9..528a319 100644 --- a/lib/libc/string/strchr.c +++ b/lib/libc/string/strchr.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strcmp.c b/lib/libc/string/strcmp.c index 4a765ea..48eaed2 100644 --- a/lib/libc/string/strcmp.c +++ b/lib/libc/string/strcmp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strcpy.c b/lib/libc/string/strcpy.c index 4c888ca..48eb62c 100644 --- a/lib/libc/string/strcpy.c +++ b/lib/libc/string/strcpy.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strlen.c b/lib/libc/string/strlen.c index c091f75..37a1b78 100644 --- a/lib/libc/string/strlen.c +++ b/lib/libc/string/strlen.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strncasecmp.c b/lib/libc/string/strncasecmp.c index 1aa6639..4140931 100644 --- a/lib/libc/string/strncasecmp.c +++ b/lib/libc/string/strncasecmp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strncmp.c b/lib/libc/string/strncmp.c index 357d113..a886736 100644 --- a/lib/libc/string/strncmp.c +++ b/lib/libc/string/strncmp.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strncpy.c b/lib/libc/string/strncpy.c index a2667d8..0f41f93 100644 --- a/lib/libc/string/strncpy.c +++ b/lib/libc/string/strncpy.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strstr.c b/lib/libc/string/strstr.c index f94af6d..3e090d2 100644 --- a/lib/libc/string/strstr.c +++ b/lib/libc/string/strstr.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2004, 2007 IBM Corporation + * Copyright (c) 2004, 2008 IBM Corporation * All rights reserved. * This program and the accompanying materials * are made available under the terms of the BSD License diff --git a/lib/libc/string/strtok.c b/lib/libc/string/strtok.c new file mode 100644 index 0000000..665c08d --- /dev/null +++ b/lib/libc/string/strtok.c @@ -0,0 +1,45 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <string.h> + +char * +strtok(char *src, const char *pattern) +{ + static char *nxtTok; + char *retVal = NULL; + + if (!src) + src = nxtTok; + + while (*src) { + const char *pp = pattern; + while (*pp) { + if (*pp == *src) { + break; + } + pp++; + } + if (!*pp) { + if (!retVal) + retVal = src; + else if (!src[-1]) + break; + } else + *src = '\0'; + src++; + } + + nxtTok = src; + + return retVal; +} |