diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-02-17 23:21:28 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2007-02-17 23:21:28 +0000 |
commit | e3940eb2080711edac189cca3f642ee89dc215f2 (patch) | |
tree | 1c390958fae49e34dce698b175487e6d4681e540 /nuttx/lib | |
parent | 2223612deb2cc6322992f8595b6d6f86fcb53ae1 (diff) | |
download | px4-nuttx-e3940eb2080711edac189cca3f642ee89dc215f2.tar.gz px4-nuttx-e3940eb2080711edac189cca3f642ee89dc215f2.tar.bz2 px4-nuttx-e3940eb2080711edac189cca3f642ee89dc215f2.zip |
NuttX RTOS
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib')
61 files changed, 7240 insertions, 0 deletions
diff --git a/nuttx/lib/Makefile b/nuttx/lib/Makefile new file mode 100644 index 000000000..2dfda7e81 --- /dev/null +++ b/nuttx/lib/Makefile @@ -0,0 +1,96 @@ +############################################################ +# Makefile +# +# Copyright (C) 2007 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt <spudmonkey@racsa.co.cr> +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name Gregory Nutt nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################ + +-include $(TOPDIR)/Make.defs + +MKDEP = $(TOPDIR)/tools/mkdeps.sh + +ASRCS = +AOBJS = $(ASRCS:.S=.o) + +MISC_SRCS = lib_init.c lib_streamsem.c lib_filesem.c +STRING_SRCS = lib_memset.c lib_memcpy.c lib_memcmp.c lib_memmove.c \ + lib_strcpy.c lib_strncpy.c lib_strcmp.c \ + lib_strlen.c lib_strdup.c lib_strtol.c lib_strchr.c +CTYPE_SRCS = +STDIO_SRCS = lib_fopen.c lib_fclose.c \ + lib_fread.c lib_libfread.c lib_fgetc.c\ + lib_fwrite.c lib_libfwrite.c lib_fflush.c\ + lib_fputc.c lib_puts.c lib_fputs.c \ + lib_ungetc.c \ + lib_printf.c lib_vprintf.c lib_fprintf.c lib_rawprintf.c lib_lowprintf.c \ + lib_vfprintf.c lib_sprintf.c lib_libsprintf.c lib_vsprintf.c \ + lib_libvsprintf.c lib_stdstream.c lib_memstream.c lib_rawstream.c lib_lowstream.c \ + lib_sscanf.c +STDLIB_SRCS = lib_getenv.c lib_rand.c +MATH_SRCS = lib_rint.c +SQ_SRCS = sq_addlast.c sq_addfirst.c sq_addafter.c \ + sq_rem.c sq_remlast.c sq_remfirst.c sq_remafter.c +DQ_SRCS = dq_addlast.c dq_addfirst.c dq_addafter.c dq_addbefore.c \ + dq_rem.c dq_remlast.c dq_remfirst.c + +CSRCS = $(MISC_SRCS) $(STRING_SRCS) $(CTYPE_SRCS) $(STDIO_SRCS) \ + $(STDLIB_SRCS) $(MATH_SRCS) $(SQ_SRCS) $(DQ_SRCS) +COBJS = $(CSRCS:.c=.o) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +BIN = liblib.a + +all: $(BIN) + +$(AOBJS): %.o: %.S + $(CC) -c $(CFLAGS) -D__ASSEMBLY__ $< -o $@ + +$(COBJS): %.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +$(BIN): $(OBJS) + $(AR) rcs $@ $(OBJS) + +.depend: Makefile $(SRCS) + $(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + touch $@ + +depend: .depend + +clean: + rm -f $(BIN) *.o *~ + +distclean: clean + rm -f Make.dep .depend + +-include Make.dep diff --git a/nuttx/lib/dq_addafter.c b/nuttx/lib/dq_addafter.c new file mode 100644 index 000000000..5533d57a1 --- /dev/null +++ b/nuttx/lib/dq_addafter.c @@ -0,0 +1,74 @@ +/************************************************************ + * dq_addafter.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: dq_addafter + * + * Description: + * dq_addafter function adds 'node' after 'qqqq' in the + * 'queue.' + * + ************************************************************/ + +void dq_addafter(dq_entry_t *prev, dq_entry_t *node, + dq_queue_t *queue) +{ + if (!queue->head || prev == queue->tail) + { + dq_addlast(node, queue); + } + else + { + dq_entry_t *next = prev->flink; + node->blink = prev; + node->flink = next; + next->blink = node; + prev->flink = node; + } +} diff --git a/nuttx/lib/dq_addbefore.c b/nuttx/lib/dq_addbefore.c new file mode 100644 index 000000000..916a7db61 --- /dev/null +++ b/nuttx/lib/dq_addbefore.c @@ -0,0 +1,73 @@ +/************************************************************ + * dq_addbefore.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: dq_addbefore + * + * Description: + * dq_addbefore adds 'node' before 'next' in 'queue' + * + ************************************************************/ + +void dq_addbefore(dq_entry_t *next, dq_entry_t *node, + dq_queue_t *queue) +{ + if (!queue->head || next == queue->head) + { + dq_addfirst(node, queue); + } + else + { + dq_entry_t *prev = next->blink; + node->flink = next; + node->blink = prev; + prev->flink = node; + next->blink = node; + } +} diff --git a/nuttx/lib/dq_addfirst.c b/nuttx/lib/dq_addfirst.c new file mode 100644 index 000000000..a11b1f8c3 --- /dev/null +++ b/nuttx/lib/dq_addfirst.c @@ -0,0 +1,73 @@ +/************************************************************ + * dq_addfirst.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: dq_addfirst + * + * Description: + * dq_addfirst affs 'node' at the beginning of 'queue' + * + ************************************************************/ + +void dq_addfirst(dq_entry_t *node, dq_queue_t *queue) +{ + node->blink = NULL; + node->flink = queue->head; + + if (!queue->head) + { + queue->head = node; + queue->tail = node; + } + else + { + queue->head->blink = node; + queue->head = node; + } +} diff --git a/nuttx/lib/dq_addlast.c b/nuttx/lib/dq_addlast.c new file mode 100644 index 000000000..85c6c72a9 --- /dev/null +++ b/nuttx/lib/dq_addlast.c @@ -0,0 +1,73 @@ +/************************************************************ + * dq_addlast.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: dq_addlast + * + * Description + * dq_addlast adds 'node' to the end of 'queue' + * + ************************************************************/ + +void dq_addlast(dq_entry_t *node, dq_queue_t *queue) +{ + node->flink = NULL; + node->blink = queue->tail; + + if (!queue->head) + { + queue->head = node; + queue->tail = node; + } + else + { + queue->tail->flink = node; + queue->tail = node; + } +} diff --git a/nuttx/lib/dq_rem.c b/nuttx/lib/dq_rem.c new file mode 100644 index 000000000..d78ac8b6d --- /dev/null +++ b/nuttx/lib/dq_rem.c @@ -0,0 +1,83 @@ +/************************************************************ + * dq_rem.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: dq_rem + * + * Descripton: + * dq_rem removes 'node' from 'queue' + * + ************************************************************/ + +void dq_rem(dq_entry_t *node, dq_queue_t *queue) +{ + dq_entry_t *prev = node->blink; + dq_entry_t *next = node->flink; + + if (!prev) + { + queue->head = next; + } + else + { + prev->flink = next; + } + + if (!next) + { + queue->tail = prev; + } + else + { + next->blink = prev; + } + + node->flink = NULL; + node->blink = NULL; +} diff --git a/nuttx/lib/dq_remfirst.c b/nuttx/lib/dq_remfirst.c new file mode 100644 index 000000000..0c7381c77 --- /dev/null +++ b/nuttx/lib/dq_remfirst.c @@ -0,0 +1,81 @@ +/************************************************************ + * dq_remfirst.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: dq_remfirst + * + * Description: + * dq_remfirst removes 'node' from the head of 'queue' + * + ************************************************************/ + +dq_entry_t *dq_remfirst(dq_queue_t *queue) +{ + dq_entry_t *ret = queue->head; + + if (ret) + { + dq_entry_t *next = ret->flink; + if (!next) + { + queue->head = NULL; + queue->tail = NULL; + } + else + { + queue->head = next; + next->blink = NULL; + } + + ret->flink = NULL; + ret->blink = NULL; + } + + return ret; +} diff --git a/nuttx/lib/dq_remlast.c b/nuttx/lib/dq_remlast.c new file mode 100644 index 000000000..476426021 --- /dev/null +++ b/nuttx/lib/dq_remlast.c @@ -0,0 +1,81 @@ +/************************************************************ + * dq_remlast.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/***********************************(************************ + * Name: dq_remlast + * + * Description: + * dq_remlast removes the last entry from 'queue' + * + ************************************************************/ + +dq_entry_t *dq_remlast(dq_queue_t *queue) +{ + dq_entry_t *ret = queue->tail; + + if (ret) + { + dq_entry_t *prev = ret->blink; + if (!prev) + { + queue->head = NULL; + queue->tail = NULL; + } + else + { + queue->tail = prev; + prev->flink = NULL; + } + + ret->flink = NULL; + ret->blink = NULL; + } + + return ret; +} diff --git a/nuttx/lib/lib_fclose.c b/nuttx/lib/lib_fclose.c new file mode 100644 index 000000000..c1cddeb69 --- /dev/null +++ b/nuttx/lib/lib_fclose.c @@ -0,0 +1,98 @@ +/************************************************************ + * lib_fclose.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#if CONFIG_NFILE_STREAMS > 0 + +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +int fclose(FILE *stream) +{ + int ret = OK; + if (stream) + { + if (stream->fs_filedes > 0) + { + ret = close(stream->fs_filedes); + } +#warning REVIEW for race conditions +#if CONFIG_NFILE_STREAMS > 0 + /* Destroy the semaphore */ + sem_destroy(&stream->fs_sem); + + /* release the buffer */ + if (stream->fs_bufstart) + { + free(stream->fs_bufstart); + } + + /* Clear the whole structure */ + + memset(stream, 0, sizeof(FILE)); +#else +#if CONFIG_NUNGET_CHARS > 0 + /* Reset the number of ungetc characters */ + + stream->fs_nungotten = 0; +#endif + /* Reset the flags */ + + stream->fs_oflags = 0; +#endif + /* Setting the fs_filedescriptor to -1 makes the stream available for reuse */ + + stream->fs_filedes = -1; + } + return ret; +} + +#endif /* CONFIG_NFILE_STREAMS */ diff --git a/nuttx/lib/lib_fflush.c b/nuttx/lib/lib_fflush.c new file mode 100644 index 000000000..b3dbb1109 --- /dev/null +++ b/nuttx/lib/lib_fflush.c @@ -0,0 +1,171 @@ +/************************************************************ + * lib_fflush.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */ + +#if CONFIG_NFILE_STREAMS > 0 + +#include <stdio.h> +#include <errno.h> +#include <nuttx/fs.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * fflush + ************************************************************/ + +/* Called by the OS when a task exits */ + +void lib_flushall(struct streamlist *list) +{ + /* Make sure that there are streams associated with this thread */ + if (list) + { + int i; + + /* Process each stream in the thread's stream list */ + + stream_semtake(list); + for (i = 0; i < CONFIG_NFILE_STREAMS; i++) + { + /* If the stream is open (i.e., assigned a non- + * negative file descriptor), then flush the + * stream. + */ + + if (list->sl_streams[i].fs_filedes >= 0) + { + (void)fflush(&list->sl_streams[i]); + } + } + stream_semgive(list); + } +} + +int fflush(FILE *stream) +{ +#if CONFIG_STDIO_BUFFER_SIZE > 0 + const unsigned char *src; + size_t bytes_written; + size_t nbuffer; + + if (stream->fs_filedes < 0 || (stream->fs_oflags & O_WROK) == 0) + { + *get_errno_ptr() = EBADF; + return ERROR; + } + + /* How many bytes are used in the buffer now */ + + nbuffer = stream->fs_bufpos - stream->fs_bufstart; + + /* Try to write that amount */ + + src = stream->fs_bufstart; + bytes_written = write(stream->fs_filedes, src, nbuffer); + if (bytes_written < 0) + { + return bytes_written; + } + + /* Update pointers and counts */ + + src += bytes_written; + nbuffer -= bytes_written; + + /* Reset the buffer position to the beginning of the buffer */ + + stream->fs_bufpos = stream->fs_bufstart; + + /* For the case of an incomplete write, nbuffer will be non-zero + * It will hold the number of bytes that were not written. + * Move the data down in the buffer to handle this (rare) case + */ + + while (nbuffer) + { + *stream->fs_bufpos++ = *src++; + --nbuffer; + } + + /* Return the number of bytes remaining in the buffer */ + + return stream->fs_bufpos - stream->fs_bufstart; +#else + return 0; +#endif +} + +#endif /* CONFIG_NFILE_STREAMS */ diff --git a/nuttx/lib/lib_fgetc.c b/nuttx/lib/lib_fgetc.c new file mode 100644 index 000000000..3fd2a5a2b --- /dev/null +++ b/nuttx/lib/lib_fgetc.c @@ -0,0 +1,98 @@ +/************************************************************ + * lib_fgetc.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * fgetc + **********************************************************/ + +int fgetc(FILE *stream) +{ + unsigned char c; + if (lib_fread(&c, 1, stream) > 0) + { + return c; + } + else + { + return EOF; + } +} diff --git a/nuttx/lib/lib_filesem.c b/nuttx/lib/lib_filesem.c new file mode 100644 index 000000000..eb2279b9d --- /dev/null +++ b/nuttx/lib/lib_filesem.c @@ -0,0 +1,143 @@ +/************************************************************ + * lib_filesem.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#if CONFIG_STDIO_BUFFER_SIZE > 0 + +#include <unistd.h> +#include <semaphore.h> +#include <errno.h> +#include <assert.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Data + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * lib_sem_initialize + ************************************************************/ + +void lib_sem_initialize(struct file_struct *stream) +{ + /* Initialize the LIB semaphore to one (to support one-at- + * a-time access to private data sets. + */ + + (void)sem_init(&stream->fs_sem, 0, 1); + + stream->fs_holder = -1; + stream->fs_counts = 0; +} + +/************************************************************ + * lib_take_semaphore + ************************************************************/ + +void lib_take_semaphore(struct file_struct *stream) +{ + pid_t my_pid = getpid(); + + /* Do I already have the semaphore? */ + + if (stream->fs_holder == my_pid) + { + /* Yes, just increment the number of references that I have */ + + stream->fs_counts++; + } + else + { + /* Take the semaphore (perhaps waiting) */ + + while (sem_wait(&stream->fs_sem) != 0) + { + /* The only case that an error should occr here is if + * the wait was awakened by a signal. + */ + + ASSERT(*get_errno_ptr() == EINTR); + } + + /* We have it. Claim the stak and return */ + + stream->fs_holder = my_pid; + stream->fs_counts = 1; + } +} + +/************************************************************ + * lib_give_semaphore + ************************************************************/ + +void lib_give_semaphore(struct file_struct *stream) +{ + pid_t my_pid = getpid(); + + /* I better be holding at least one reference to the semaphore */ + + ASSERT(stream->fs_holder == my_pid); + + /* Do I hold multiple references to the semphore */ + + if (stream->fs_counts > 1) + { + /* Yes, just release one count and return */ + + stream->fs_counts--; + } + else + { + /* Nope, this is the last reference I have */ + + stream->fs_holder = -1; + stream->fs_counts = 0; + ASSERT(sem_post(&stream->fs_sem) == 0); + } +} +#endif /* CONFIG_STDIO_BUFFER_SIZE */ diff --git a/nuttx/lib/lib_fopen.c b/nuttx/lib/lib_fopen.c new file mode 100644 index 000000000..0a935ddd2 --- /dev/null +++ b/nuttx/lib/lib_fopen.c @@ -0,0 +1,232 @@ +/************************************************************ + * lib_fopen.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#if CONFIG_NFILE_STREAMS > 0 + +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include "lib_internal.h" + +/************************************************************ + * Private Functions + ************************************************************/ + +static int lib_mode2oflags(const char *mode) +{ + int oflags = 0; + if (mode) + { + while(*mode) + { + switch (*mode) + { + /* Open for read access */ + + case 'r' : + if (*(mode + 1) == '+') + { + /* Open for read/write access */ + oflags |= O_RDWR; + mode++; + } + else + { + /* Open for read access */ + oflags |= O_RDOK; + } + break; + + /* Open for write access? */ + + case 'w' : + if (*(mode + 1) == '+') + { + /* Open for write read/access, truncating any existing file */ + oflags |= O_RDWR|O_CREAT|O_TRUNC; + mode++; + } + else + { + /* Open for write access, truncating any existing file */ + oflags |= O_WROK|O_CREAT|O_TRUNC; + } + break; + + /* Open for write/append access? */ + + case 'a' : + if (*(mode + 1) == '+') + { + /* Read from the beginning of the file; write to the end */ + oflags |= O_RDWR|O_CREAT; + mode++; + } + else + { + /* Write to the end of the file */ + oflags |= O_WROK|O_CREAT; + } + break; + + /* Open for binary access? */ + + case 'b' : + default: + break; + } + mode++; + } + } + return oflags; +} + +/************************************************************ + * Public Functions + ************************************************************/ + +struct file_struct *lib_fdopen(int fd, const char *mode, + struct filelist *flist, + struct streamlist *slist) +{ + struct inode *inode = flist->fl_files[fd].f_inode; + FILE *stream; + int oflags = lib_mode2oflags(mode); + int i; + + if (fd < 0 || !flist || !slist) + { + *get_errno_ptr() = EBADF; + return NULL; + } + + /* Make sure that the inode supports the requested access. In + * the case of fdopen, we are not actually creating the file -- in + * particular w and w+ do not truncate the file and any files have + * already been created. + */ + + if (inode_checkflags(inode, oflags) != OK) + { + return NULL; + } + + /* Find an unallocated FILE structure */ + + stream_semtake(slist); + for (i = 0 ; i < CONFIG_NFILE_STREAMS; i++) + { + stream = &slist->sl_streams[i]; + if (stream->fs_filedes < 0) + { + /* Zero the structure */ +#if CONFIG_STDIO_BUFFER_SIZE > 0 + memset(stream, 0, sizeof(FILE)); +#elif CONFIG_NUNGET_CHARS > 0 + stream->fs_nungotten = 0; +#endif + +#if CONFIG_STDIO_BUFFER_SIZE > 0 + /* Initialize the semaphore the manages access to the buffer */ + + (void)sem_init(&stream->fs_sem, 0, 1); + + /* Allocate the IO buffer */ + + stream->fs_bufstart = malloc(CONFIG_STDIO_BUFFER_SIZE); + if (!stream) + { + *get_errno_ptr() = ENOMEM; + return NULL; + } + + /* Set up pointers */ + + stream->fs_bufend = &stream->fs_bufstart[CONFIG_STDIO_BUFFER_SIZE]; + stream->fs_bufpos = stream->fs_bufstart; + stream->fs_bufpos = stream->fs_bufstart; + stream->fs_bufread = stream->fs_bufstart; + stream_semgive(slist); +#endif + /* Save the file description and open flags. Setting the + * file descriptor locks this stream. + */ + + stream->fs_filedes = fd; + stream->fs_oflags = oflags; + + return stream; + } + } + stream_semgive(slist); + return NULL; +} + +FILE *fdopen(int fd, const char *mode) +{ + struct filelist *flist = sched_getfiles(); + struct streamlist *slist = sched_getstreams(); + return lib_fdopen(fd, mode, flist, slist); +} + +FILE *fopen(const char *path, const char *mode) +{ + struct filelist *flist = sched_getfiles(); + struct streamlist *slist = sched_getstreams(); + int oflags = lib_mode2oflags(mode); + int fd = open(path, oflags, 0666); + + FILE *ret = lib_fdopen(fd, mode, flist, slist); + if (!ret) + { + (void)close(fd); + } + return ret; +} + +#endif /* CONFIG_NFILE_STREAMS */ diff --git a/nuttx/lib/lib_fprintf.c b/nuttx/lib/lib_fprintf.c new file mode 100644 index 000000000..a0e3d5330 --- /dev/null +++ b/nuttx/lib/lib_fprintf.c @@ -0,0 +1,92 @@ +/************************************************************ + * lib_fprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * fprintf + ************************************************************/ + +int fprintf(FILE *stream, const char *fmt, ...) +{ + va_list ap; + int n; + + /* vfprintf into the stream */ + + va_start(ap, fmt); + n = vfprintf(stream, fmt, ap); + va_end(ap); + return n; +} diff --git a/nuttx/lib/lib_fputc.c b/nuttx/lib/lib_fputc.c new file mode 100644 index 000000000..e8033a54e --- /dev/null +++ b/nuttx/lib/lib_fputc.c @@ -0,0 +1,98 @@ +/************************************************************ + * lib_fputc.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * fputc + **********************************************************/ + +int fputc(int c, FILE *stream) +{ + unsigned char buf = (unsigned char)c; + if (lib_fwrite(&buf, 1, stream) > 0) + { + return c; + } + else + { + return EOF; + } +} diff --git a/nuttx/lib/lib_fputs.c b/nuttx/lib/lib_fputs.c new file mode 100644 index 000000000..61e5a03c4 --- /dev/null +++ b/nuttx/lib/lib_fputs.c @@ -0,0 +1,126 @@ +/************************************************************ + * lib_fputs.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * fputs + **********************************************************/ + +/* fputs() writes the string s to stream, without its + * trailing '\0'. + */ + +int fputs(const char *s, FILE *stream) +{ + int ntowrite; + int nwritten; + int nput = EOF; + + /* Make sure that a string was provided. */ + + if (!s) + { + *get_errno_ptr() = EINVAL; + } + else + { + /* Get the length of the string. */ + + ntowrite = strlen(s); + if (ntowrite == 0) + { + nput = 0; + } + else + { + /* Write the string */ + + nwritten = lib_fwrite(s, ntowrite, stream); + if (nwritten > 0) + { + nput = nwritten; + } + } + } + return nput; +} diff --git a/nuttx/lib/lib_fread.c b/nuttx/lib/lib_fread.c new file mode 100644 index 000000000..b40febe3d --- /dev/null +++ b/nuttx/lib/lib_fread.c @@ -0,0 +1,98 @@ +/************************************************************ + * lib_fread.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * fwrite + ************************************************************/ + +size_t fread(void *ptr, size_t size, size_t n_items, FILE *stream) +{ + size_t full_size = n_items * (size_t)size; + ssize_t bytes_read; + size_t items_read = 0; + + /* Write the data into the stream buffer */ + + bytes_read = lib_fread(ptr, full_size, stream); + if (bytes_read > 0) + { + /* Return the number of full items read */ + + items_read = bytes_read / size; + } + return items_read; +} diff --git a/nuttx/lib/lib_fwrite.c b/nuttx/lib/lib_fwrite.c new file mode 100644 index 000000000..3dd22ba8b --- /dev/null +++ b/nuttx/lib/lib_fwrite.c @@ -0,0 +1,98 @@ +/************************************************************ + * lib_fwrite.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * fwrite + ************************************************************/ + +size_t fwrite(const void *ptr, size_t size, size_t n_items, FILE *stream) +{ + size_t full_size = n_items * (size_t)size; + ssize_t bytes_written; + size_t items_written = 0; + + /* Write the data into the stream buffer */ + + bytes_written = lib_fwrite(ptr, full_size, stream); + if (bytes_written > 0) + { + /* Return the number of full items written */ + + items_written = bytes_written / size; + } + return items_written; +} diff --git a/nuttx/lib/lib_getenv.c b/nuttx/lib/lib_getenv.c new file mode 100644 index 000000000..2c75a4f16 --- /dev/null +++ b/nuttx/lib/lib_getenv.c @@ -0,0 +1,163 @@ +/************************************************************ + * lib_getenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdlib.h> +#include <debug.h> + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +#ifdef ENVIRONMENT_STRINGS +static const char environment[] = ENVIRONMENT_STRINGS; +#else +static const char environment[] = ""; +#endif + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Function: getenv + * + * Description: + * + * getenv() searches the environment list for a string of + * the form name=value, and returns a pointer to the value + * in the current environment if such a string is present, + * otherwise a NULL pointer. name can be either the desired + * name, null-terminated, or of the form name=value, in which + * case getenv() uses the portion to the left of the = as the + * search key. + * + * getenv() is (by convention) declared in stdlib.h + * + ************************************************************/ + +char *getenv(const char *name) +{ + const char *penv = environment; + int size = sizeof(environment); + const char *pend = &environment[size-1]; + const char *ptmp; + + dbg("getenv(ge): name=\"%s\"\n", name); + + if (name) { + + /* Process each string in the environment. */ + while (penv < pend) { + + vdbg("(ge):\tCompare to=\"%s\"\n", penv); + + /* The logic below basically implements a version of + * strcmp where the strings may be terminated with = signs. */ + ptmp = name; + for (;;) { + + /* Are we at the end of the name-to-matching? */ + if ((!*ptmp) || (*ptmp == '=')) { + + /* Yes.. are we also at the end of the matching-name? */ + if (*penv == '=') { + + /* Yes.. return the pointer to the value. */ + dbg("(ge):\tReturning \"%s\"\n", penv+1); + return ((char*)penv+1); + + } /* end if */ + else { + + /* No.. Skip to the next name matching name candidate. */ + while(*penv++); + break; + + } /* end else */ + } /* end if */ + + /* NO.. are we at the end of the matching name candidate? */ + /* OR.. do the corresponding characters not match. */ + else if (*penv != *ptmp) { + + /* Yes.. Skip to the next name matching name candidate. */ + while(*penv++); + break; + + } /* end else if */ + else { + + /* No.. try the next characters. */ + penv++; ptmp++; + + } /* end else */ + } /* end for */ + } /* end while */ + } /* end if */ + + /* If we got here, then no matching string was found. */ + dbg("(ge):\tReturning NULL\n"); + return NULL; + +} /* end getenv */ diff --git a/nuttx/lib/lib_init.c b/nuttx/lib/lib_init.c new file mode 100644 index 000000000..216f7b780 --- /dev/null +++ b/nuttx/lib/lib_init.c @@ -0,0 +1,203 @@ +/************************************************************ + * lib_init.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <nuttx/kmalloc.h> +#include <nuttx/fs.h> +#include <nuttx/os_external.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Private Functions + ************************************************************/ + +static void _lib_semtake(struct streamlist *list) +{ + /* Take the semaphore (perhaps waiting) */ + + while (sem_wait(&list->sl_sem) != 0) + { + /* The only case that an error should occr here is if + * the wait was awakened by a signal. + */ + + ASSERT(*get_errno_ptr() == EINTR); + } +} + +static inline void _lib_semgive(struct streamlist *list) +{ + sem_post(&list->sl_sem); +} + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * lib_initialize + ************************************************************/ + +/* General library initialization hook */ + +void weak_const_function lib_initialize(void) +{ +} + +#if CONFIG_NFILE_STREAMS > 0 +/* The following function is called when a new TCB is allocated. It + * creates the streamlist instance that is stored in the TCB. + */ + +struct streamlist *lib_alloclist(void) +{ + struct streamlist *list; + list = (struct streamlist*)kzmalloc(sizeof(struct streamlist)); + if (list) + { + int i; + + /* Start with a reference count of one */ + + list->sl_crefs = 1; + + /* Initialize the list access mutex */ + + (void)sem_init(&list->sl_sem, 0, 1); + + /* Initialize each FILE structure */ + + for (i = 0; i < CONFIG_NFILE_STREAMS; i++) + { + /* Clear the IOB */ + + memset(&list->sl_streams[i], 0, sizeof(FILE)); + + /* Indicate not opened */ + + list->sl_streams[i].fs_filedes = -1; + + /* Initialize the stream semaphore to one to support one-at- + * a-time access to private data sets. + */ + + lib_sem_initialize(&list->sl_streams[i]); + } + } + return list; + +} + +/* This function is called when a TCB is closed (such as with + * pthread_create(). It increases the reference count on the stream + * list. + */ + +void lib_addreflist(struct streamlist *list) +{ + if (list) + { + /* Increment the reference count on the list */ + + _lib_semtake(list); + list->sl_crefs++; + _lib_semgive(list); + } +} + +/* this function is called when a TCB is destroyed. Note that is + * does not close the file by release this inode. This happens + * separately when the file descriptor list is freed. + */ + +void lib_releaselist(struct streamlist *list) +{ + int crefs; + if (list) + { + /* Decrement the reference count */ + + _lib_semtake(list); + crefs = --list->sl_crefs; + _lib_semgive(list); + + /* If the count decrements to zero, then there is no reference + * to the structure and it should be deallocated. + */ + + if (crefs <= 0) + { + int i; + + /* Destroy the semaphore and release the filelist */ + + (void)sem_destroy(&list->sl_sem); + sched_free(list); + + /* Initialize each FILE structure */ + + for (i = 0; i < CONFIG_NFILE_STREAMS; i++) + { + /* Destroy the semaphore that protects the IO buffer */ + + (void)sem_destroy(&list->sl_streams[i].fs_sem); + + /* Release the IO buffer */ + if (&list->sl_streams[i].fs_bufstart) + { + free(list->sl_streams[i].fs_bufstart); + } + } + } + } +} + +#endif /* CONFIG_NFILE_STREAMS */ + + diff --git a/nuttx/lib/lib_internal.h b/nuttx/lib/lib_internal.h new file mode 100644 index 000000000..41d40d428 --- /dev/null +++ b/nuttx/lib/lib_internal.h @@ -0,0 +1,166 @@ +/************************************************************ + * lib_internal.h + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +#ifndef __LIB_INTERNAL_H +#define __LIB_INTERNAL_H + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <stdio.h> +#include <semaphore.h> + +/************************************************************ + * Definitions + ************************************************************/ + +#if CONFIG_STDIO_BUFFER_SIZE <= 0 +# define lib_sem_initialize(s) +# define lib_take_semaphore(s) +# define lib_give_semaphore(s) +#endif + +#define LIB_BUFLEN_UNKNOWN (0x7fffffff) + +/************************************************************ + * Public Types + ************************************************************/ + +/* This is the generic for of a stream used by the library + * to manage variable sized output. + */ + +struct lib_stream_s; + +typedef void (*lib_putc_t)(struct lib_stream_s *this, int ch); + +struct lib_stream_s +{ + lib_putc_t put; /* Pointer to function to put one character */ + int nput; /* Total number of characters put. Written + * by put method, readable by user */ +}; + +struct lib_memstream_s +{ + struct lib_stream_s public; + char *buffer; /* Address of first byte in the buffer */ + int buflen; /* Size of the buffer in bytes */ +}; + +struct lib_stdstream_s +{ + struct lib_stream_s public; + FILE *stream; +}; + +struct lib_rawstream_s +{ + struct lib_stream_s public; + int fd; +}; + +/************************************************************ + * Public Variables + ************************************************************/ + +/************************************************************ + * Inline Functions + ************************************************************/ + +/************************************************************ + * Pulblic Function Prototypes + ************************************************************/ + +/* Defined in lib_streamsem.c */ + +extern void stream_semtake(struct streamlist *list); +extern void stream_semgive(struct streamlist *list); + +/* Defined in lib_memstream.c */ + +extern void lib_memstream(struct lib_memstream_s *memstream, + char *bufstart, int buflen); + +/* Defined in lib_stdstream.c */ + +extern void lib_stdstream(struct lib_stdstream_s *stdstream, + FILE *stream); + +/* Defined in lib_rawstream.c */ + +extern void lib_rawstream(struct lib_rawstream_s *rawstream, + int fd); + +/* Defined in lib_lowstream.c */ + +#ifdef CONFIG_ARCH_LOWPUTC +extern void lib_lowstream(struct lib_stream_s *rawstream); +#endif + +/* Defined in lib_libsprintf.c */ + +extern int lib_sprintf (struct lib_stream_s *obj, + const char *fmt, ...); + +/* Defined lib_libvsprintf.c */ + +extern int lib_vsprintf(struct lib_stream_s *obj, + const char *src, va_list ap); + +/* Defined in lib_libwrite.c */ + +extern ssize_t lib_fwrite(const void *ptr, size_t count, FILE *stream); + +/* Defined in lib_libfread.c */ + +extern ssize_t lib_fread(void *ptr, size_t count, FILE *stream); + +/* Defined in lib_sem.c */ + +#if CONFIG_STDIO_BUFFER_SIZE > 0 +extern void lib_sem_initialize(struct file_struct *stream); +extern void lib_take_semaphore(struct file_struct *stream); +extern void lib_give_semaphore(struct file_struct *stream); +#endif + +/* Defined in lib_libgetbase.c */ + +extern int lib_getbase(const char *nptr, const char **endptr); + + +#endif /* __LIB_INTERNAL_H */ diff --git a/nuttx/lib/lib_libfread.c b/nuttx/lib/lib_libfread.c new file mode 100644 index 000000000..8c3deb655 --- /dev/null +++ b/nuttx/lib/lib_libfread.c @@ -0,0 +1,257 @@ +/************************************************************ + * lib_libfread.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * lib_fread + ************************************************************/ + +ssize_t lib_fread(void *ptr, size_t count, FILE *stream) +{ + unsigned char *dest = (unsigned char*)ptr; + ssize_t bytes_read; + + /* Make sure that reading from this stream is allowed */ + + if ((!stream) || ((stream->fs_oflags & O_RDOK) == 0)) + { + *get_errno_ptr() = EBADF; + bytes_read = -1; + } + else + { + /* The stream must be stable until we complete the read */ + + lib_take_semaphore(stream); + +#if CONFIG_NUNGET_CHARS > 0 + /* First, re-read any previously ungotten characters */ + + while ((stream->fs_nungotten > 0) && (count > 0)) + { + /* Decrement the count of ungotten bytes to get an index */ + + stream->fs_nungotten--; + + /* Return the last ungotten byte */ + + *dest++ = stream->fs_ungotten[stream->fs_nungotten]; + + /* That's one less byte that we have to read */ + + count--; + } +#endif + +#if CONFIG_STDIO_BUFFER_SIZE > 0 + /* Now get any other needed chars from the buffer or the file. */ + + while (count > 0) + { + /* Is there readable data in the buffer? */ + + while ((count > 0) && (stream->fs_bufpos < stream->fs_bufread)) + { + /* Yes, copy a byte into the user buffer */ + + *dest++ = *stream->fs_bufpos++; + count--; + } + + /* The buffer is empty OR we have already supplied the number of + * bytes requested in the read. Check if we need to read + * more from the file. + */ + + if (count > 0) + { + size_t buffer_available; + + /* We need to read more data into the buffer from the file */ + + /* Mark the buffer empty */ + + stream->fs_bufpos = stream->fs_bufread = stream->fs_bufstart; + + /* How much space is available in the buffer? */ + + buffer_available = stream->fs_bufend - stream->fs_bufread; + + /* Will the number of bytes that we need to read fit into + * the buffer space that is available? If the read size is + * larger than the buffer, then read some of the data + * directly into the user's buffer. + */ + + if (count > buffer_available) + { + bytes_read = read(stream->fs_filedes, dest, count); + if (bytes_read < 0) + { + /* An error occurred on the read */ + + goto err_out; + } + else if (bytes_read == 0) + { + /* We are at the end of the file */ + + goto short_read; + } + else + { + /* Some bytes were read. Adjust the dest pointer */ + + dest += bytes_read; + + /* Were all of the requested bytes read? */ + + if (bytes_read < count) + { + /* No. We must be at the end of file. */ + + goto short_read; + } + else + { + /* Yes. We are done. */ + + count = 0; + } + } + } + else + { + /* The number of bytes required to satisfy the read + * is less than or equal to the size of the buffer + * space that we have left. Read as much as we can + * into the buffer. + */ + + bytes_read = read(stream->fs_filedes, stream->fs_bufread, buffer_available); + if (bytes_read < 0) + { + /* An error occurred on the read */ + + goto err_out; + } + else if (bytes_read == 0) + { + /* We are at the end of the file */ + + goto short_read; + } + else + { + /* Some bytes were read */ + + stream->fs_bufread += bytes_read; + } + } + } + } +#else + /* Now get any other needed chars from the file. */ + + while (count > 0) + { + bytes_read = read(stream->fs_filedes, dest, count); + if (bytes_read < 0) + { + goto err_out; + } + else if (bytes_read == 0) + { + break; + } + else + { + dest += bytes_read; + count -= bytes_read; + } + } +#endif + short_read: + bytes_read = dest - (unsigned char*)ptr; + err_out: + lib_give_semaphore(stream); + } + return bytes_read; +} diff --git a/nuttx/lib/lib_libfwrite.c b/nuttx/lib/lib_libfwrite.c new file mode 100644 index 000000000..cee26b194 --- /dev/null +++ b/nuttx/lib/lib_libfwrite.c @@ -0,0 +1,162 @@ +/************************************************************ + * lib_libfwrite.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> /* for CONFIG_STDIO_BUFFER_SIZE */ +#include <stdio.h> +#include <errno.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * lib_fwrite + ************************************************************/ + +ssize_t lib_fwrite(const void *ptr, size_t count, FILE *stream) +#if CONFIG_STDIO_BUFFER_SIZE > 0 +{ + const unsigned char *start = ptr; + const unsigned char *src = ptr; + ssize_t bytes_written; + unsigned char *dest; + + /* Make sure that writing to this stream is allowed */ + + if ((!stream) || ((stream->fs_oflags & O_WROK) == 0)) + { + *get_errno_ptr() = EBADF; + bytes_written = -1; + } + else + { + /* Loop until all of the bytes have been buffered */ + + lib_take_semaphore(stream); + while (count > 0) + { + /* Determine the number of bytes left in the buffer */ + + size_t gulp_size = stream->fs_bufend - stream->fs_bufpos; + + /* Will the user data fit into the amount of buffer space + * that we have left? + */ + + if (gulp_size > count) + { + /* Yes, clip the gulp to the size of the user data */ + + gulp_size = count; + } + + /* Adjust the number of bytes remaining to be transferred + * on the next pass through the loop (might be zero). + */ + + count -= gulp_size; + + /* Transfer the data into the buffer */ + + for (dest = stream->fs_bufpos; gulp_size > 0; gulp_size--) + { + *dest++ = *src++; + } + stream->fs_bufpos = dest; + + /* Is the buffer full? */ + + if (dest >= stream->fs_bufend) + { + /* flush the buffered data to the IO stream */ + + int bytes_buffered = fflush(stream); + if (bytes_buffered < 0) + { + bytes_written = bytes_buffered; + goto err_out; + } + } + } + + bytes_written = src - start; + + err_out: + lib_give_semaphore(stream); + } + return bytes_written; +} +#else +{ + return write(stream->fs_filedes, ptr, count); +} +#endif diff --git a/nuttx/lib/lib_libsprintf.c b/nuttx/lib/lib_libsprintf.c new file mode 100644 index 000000000..20078b7b5 --- /dev/null +++ b/nuttx/lib/lib_libsprintf.c @@ -0,0 +1,90 @@ +/************************************************************ + * lib_libsprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * lib_sprintf + ************************************************************/ + +int lib_sprintf (struct lib_stream_s *obj, const char *fmt, ...) +{ + va_list ap; + int n; + + /* Let lib_vsprintf do the real work */ + + va_start(ap, fmt); + n = lib_vsprintf(obj, fmt, ap); + va_end(ap); + return n; +} diff --git a/nuttx/lib/lib_libvsprintf.c b/nuttx/lib/lib_libvsprintf.c new file mode 100644 index 000000000..da03f7f31 --- /dev/null +++ b/nuttx/lib/lib_libvsprintf.c @@ -0,0 +1,854 @@ +/************************************************************ + * lib_libvsprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdio.h> +#include <string.h> + +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +enum +{ + FMT_RJUST = 0, /* Default */ + FMT_LJUST, + FMT_RJUST0, + FMT_CENTER +}; + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +static void utodec(char **pp, unsigned int n); +static void _utodec(char **pp, unsigned int n); +static void utohex(char **pp, unsigned int n); +static void _utohex(char **pp, unsigned int n); +static void utooct(char **pp, unsigned int n); +static void _utooct(char **pp, unsigned int n); +static void utobin(char **pp, unsigned int n); +static void _utobin(char **pp, unsigned int n); + +#ifdef CONFIG_HAVE_LONG_LONG +static void llutodec(char **pp, unsigned long long n); +static void _llutodec(char **pp, unsigned long long n); +static void llutohex(char **pp, unsigned long long n); +static void _llutohex(char **pp, unsigned long long n); +static void llutooct(char **pp, unsigned long long n); +static void _llutooct(char **pp, unsigned long long n); +static void llutobin(char **pp, unsigned long long n); +static void _llutobin(char **pp, unsigned long long n); +#endif + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +static const char g_nullstring[] = "(null)"; + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Private Functions + ************************************************************/ + +static void utodec(char **pp, unsigned int n) +{ + _utodec(pp, n); + **pp = 0; +} + +static void _utodec(char **pp, unsigned int n) +{ + unsigned int remainder = n % 10; + unsigned int dividend = n / 10; + + if (dividend) + { + _utodec(pp, dividend); + } + + **pp = (char)(remainder + (unsigned int)'0'); + (*pp)++; +} + +static void utohex(char **pp, unsigned int n) +{ + _utohex(pp, n); + **pp = 0; +} + +static void _utohex(char **pp, unsigned int n) +{ + unsigned int remainder = n & 0xf; + unsigned int dividend = n >> 4; + + if (dividend) + { + _utohex(pp, dividend); + } + + if (remainder < 10) + { + **pp = (char)(remainder + (unsigned int)'0'); + } + else + { + **pp = (char)(remainder + ((unsigned int)'a' - 10)); + } + (*pp)++; +} + +static void utooct(char **pp, unsigned int n) +{ + _utooct(pp, n); + **pp = 0; +} + +static void _utooct(char **pp, unsigned int n) +{ + unsigned int remainder = n & 0x7; + unsigned int dividend = n >> 3; + + if (dividend) + { + _utooct(pp, dividend); + } + + **pp = (char)(remainder + (unsigned int)'0'); + (*pp)++; +} + +static void utobin(char **pp, unsigned int n) +{ + _utobin(pp, n); + **pp = 0; +} + +static void _utobin(char **pp, unsigned int n) +{ + unsigned int remainder = n & 1; + unsigned int dividend = n >> 1; + + if (dividend) + { + _utobin(pp, dividend); + } + + **pp = (char)(remainder + (unsigned int)'0'); + (*pp)++; +} + +#ifdef CONFIG_HAVE_LONG_LONG +static void llutodec(char **pp, unsigned long long n) +{ + _llutodec(pp, n); + **pp = 0; +} + +static void _llutodec(char **pp, unsigned long long n) +{ + unsigned int remainder = n % 10; + unsigned long long dividend = n / 10; + + if (dividend) + { + _llutodec(pp, dividend); + } + + **pp = (char)(remainder + (unsigned int)'0'); + (*pp)++; +} + +static void llutohex(char **pp, unsigned long long n) +{ + _llutohex(pp, n); + **pp = 0; +} + +static void _llutohex(char **pp, unsigned long long n) +{ + unsigned int remainder = n & 0xf; + unsigned long long dividend = n >> 4; + if (dividend) + { + _llutohex(pp, dividend); + } + + if (remainder < 10) + { + **pp = (char)(remainder + (unsigned int)'0'); + } + else + { + **pp = (char)(remainder + ((unsigned int)'a' - 10)); + } + (*pp)++; +} + +static void llutooct(char **pp, unsigned long long n) +{ + _llutooct(pp, n); + **pp = 0; +} + +static void _llutooct(char **pp, unsigned long long n) +{ + unsigned int remainder = n & 0x7; + unsigned long long dividend = n >> 3; + + if (dividend) + { + _llutooct(pp, dividend); + } + + **pp = (char)(remainder + (unsigned int)'0'); + (*pp)++; +} + +static void llutobin(char **pp, unsigned long long n) +{ + _llutobin(pp, n); + **pp = 0; +} + +static void _llutobin(char **pp, unsigned long long n) +{ + unsigned int remainder = n & 1; + unsigned long long dividend = n >> 1; + + if (dividend) + { + _llutobin(pp, dividend); + } + + **pp = (char)(remainder + (unsigned int)'0'); + (*pp)++; +} +#endif /* CONFIG_HAVE_LONG_LONG */ + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * lib_vsprintf + ************************************************************/ + +int lib_vsprintf(struct lib_stream_s *obj, const char *src, va_list ap) +{ + char *ptmp; + char tmp[40]; + const char *pfmt; + unsigned int n; + int fmt, width, trunc, tmpwidth; + int showplus, altform, longlong; + int hasdot, hasasteriskwidth, hasasterisktrunc; + + for (; *src; src++) + { + /* Just copy regular characters */ + + if (*src != '%') + { + obj->put(obj, *src); + } + else + { + /* We have found a format specifier. Move past it. */ + + pfmt = src; + src++; + + fmt = FMT_RJUST; /* Assume right justification. */ + width = trunc = 0; + showplus = altform = longlong = 0; + hasdot = hasasteriskwidth = hasasterisktrunc = 0; + + /* Process each format qualifier. */ + + for (; *src; src++) + { + /* Break out of the loop when the format is known. */ + + if (strchr("diuxXpobeEfgGlLsc%", *src)) + { + break; + } + + /* Check for left justification. */ + + else if (*src == '-') + { + fmt = FMT_LJUST; + } + + /* Check for leading zero fill right justification. */ + + else if (*src == '0') + { + fmt = FMT_RJUST0; + } +#if 0 + /* Center justification. */ + + else if (*src == '~') + { + fmt = FMT_CENTER; + } +#endif + + else if (*src == '*') + { + int value = va_arg(ap, int); + if (hasdot) + { + trunc = value; + hasasterisktrunc = 1; + } + else + { + width = value; + hasasteriskwidth = 1; + } + } + + /* Check for field width */ + + else if (((*src) >= '1') && ((*src) <= '9')) + { + /* Accumulate the field width integer. */ + + n = ((int)(*src)) - (int)'0'; + for (src++; (((*src) >= '0') && ((*src) <= '9')); src++) + { + n = 10*n + (((int)(*src)) - (int)'0'); + } + + if (hasdot) trunc = n; + else width = n; + + /* Back up to the last digit. */ + + src--; + } + + /* Check for a decimal point. */ + + else if (*src == '.') + { + hasdot = 1; + } + + /* Check for leading plus sign. */ + + else if (*src == '+') + { + showplus = 1; + } + + /* Check for alternate form. */ + + else if (*src == '#') + { + altform = 1; + } + } + + /* "%%" means that a literal '%' was intended (instead of a format + * specification). + */ + + if (*src == '%') + { + obj->put(obj, '%'); + } + + /* Check for the string format. */ + + else if (*src == 's') + { + /* Just concatenate the string into the output */ + + ptmp = va_arg(ap, char *); + if (!ptmp) ptmp = (char*)g_nullstring; + + while(*ptmp) + { + obj->put(obj, *ptmp); + ptmp++; + } + } + + /* Check for the character output */ + + else if (*src == 'c') + { + /* Just copy the character into the output. */ + + n = va_arg(ap, int); + obj->put(obj, n); + } + else + { + /* Check for the long long prefix. */ + + if (*src == 'L') + { + longlong = 1; + ++src; + } + else if (*src == 'l') + { + if (*++src == 'l') + { + longlong = 1; + ++src; + } + } + + /* Get the ascii format into the tmp[] buffer. */ + + ptmp = tmp; + + /* Handle integer conversions */ + + if (strchr("diuxXpob", *src)) + { +#ifdef CONFIG_HAVE_LONG_LONG + if ((longlong) && (*src != 'p')) + { + /* Extract the long long value. */ + + long long lln = va_arg(ap, long long); + + /* Perform the integer conversion according to the + * format specifier + */ + + switch (*src) + { + case 'd': + case 'i': + /* Signed base 10 */ + { + /* Check for leading +/- */ + + if (lln < 0) + { + *ptmp++ = '-'; + *ptmp = 0; + lln = -lln; + } + else if (showplus) + { + *ptmp++ = '+'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + llutodec(&ptmp, (unsigned long long)lln); + } + break; + + case 'u': + /* Unigned base 10 */ + { + /* Check for leading + */ + + if (showplus) + { + *ptmp++ = '+'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + llutodec(&ptmp, (unsigned long long)lln); + } + break; + + case 'p': + case 'x': + case 'X': + /* Hexadecimal */ + { + /* Check for alternate form */ + + if (altform) + { + /* Prefix the number with "0x" */ + + *ptmp++ = '0'; + *ptmp++ = 'x'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + llutohex(&ptmp, (unsigned long long)lln); + + /* Check for upper case conversion. */ + + if ((*src) == 'X') + { + for (ptmp = tmp; *ptmp; ptmp++) + { + if (((*ptmp) >= 'a') && ((*ptmp) <= 'z')) + { + /* Convert to upper case. */ + + *ptmp += (char)((int)'A' - (int)'a'); + } + } + } + } + break; + + case 'o': + /* Octal */ + { + /* Check for alternate form */ + + if (altform) + { + /* Prefix the number with '0' */ + + *ptmp++ = '0'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + llutooct(&ptmp, (unsigned long long)lln); + } + break; + + case 'b': + /* Binary */ + { + /* Convert the unsigned value to a string. */ + + llutobin(&ptmp, (unsigned long long)lln); + } + break; + + default: + break; + } + } + else + { +#endif /* CONFIG_HAVE_LONG_LONG */ + /* Extract the integer value */ + + n = va_arg(ap, int); + + /* Perform the integer conversion according to the + * format specifier + */ + + switch (*src) + { + case 'd': + case 'i': + /* Signed base 10 */ + { + /* Check for leading +/- */ + + if ((int)n < 0) + { + *ptmp++ = '-'; + *ptmp = 0; + n = -n; + } + else if (showplus) + { + *ptmp++ = '+'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + utodec(&ptmp, (unsigned int)n); + } + break; + + case 'u': + /* Unigned base 10 */ + { + /* Check for leading + */ + + if (showplus) + { + *ptmp++ = '+'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + utodec(&ptmp, (unsigned int)n); + } + break; + + case 'x': + case 'X': + case 'p': + /* Hexadecimal */ + { + /* Check for alternate form */ + + if (altform) + { + /* Prefix the number with "0x" */ + + *ptmp++ = '0'; + *ptmp++ = 'x'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + utohex(&ptmp, (unsigned int)n); + + /* Check for upper case conversion. */ + + if ((*src) == 'X') + { + for (ptmp = tmp; *ptmp; ptmp++) + { + if (((*ptmp) >= 'a') && ((*ptmp) <= 'z')) + { + /* Convert to upper case. */ + + *ptmp += (char)((int)'A' - (int)'a'); + } + } + } + else if ((*src) == 'p') + { + if ((!width) && (fmt == FMT_RJUST)) + { + if (altform) width = 10; + else width = 8; + + fmt = FMT_RJUST0; + } + } + } + break; + + case 'o': + /* Octal */ + { + /* Check for alternate form */ + + if (altform) + { + /* Prefix the number with '0' */ + + *ptmp++ = '0'; + *ptmp = 0; + } + + /* Convert the unsigned value to a string. */ + + utooct(&ptmp, (unsigned int)n); + } + break; + + case 'b': + /* Binary */ + { + /* Convert the unsigned value to a string. */ + + utobin(&ptmp, (unsigned int)n); + } + break; + + default: + break; + } +#ifdef CONFIG_HAVE_LONG_LONG + } +#endif /* CONFIG_HAVE_LONG_LONG */ + + /* Now, get the "real" field width of the integer value*/ + + tmpwidth = strlen(tmp); + if (width <= tmpwidth) + { + /* Just copy the string. */ + + for (ptmp = tmp; *ptmp; ) + { + obj->put(obj, *ptmp); + ptmp++; + } + } + else + { + /* Apply field justification to the integer value. */ + + switch (fmt) + { + default: + case FMT_RJUST: + for (n = width - tmpwidth; n; n--) + { + obj->put(obj, ' '); + } + + for (ptmp = tmp; *ptmp; ) + { + obj->put(obj, *ptmp); + ptmp++; + } + break; + + case FMT_RJUST0: + ptmp = tmp; + if (((*ptmp) == '-') || ((*ptmp) == '+')) + { + obj->put(obj, *ptmp); + ptmp++; + } + + for (n = width - tmpwidth; n; n--) + { + obj->put(obj, '0'); + } + + while (*ptmp) + { + obj->put(obj, *ptmp); + ptmp++; + } + break; + + case FMT_LJUST: + for (ptmp = tmp; *ptmp; ) + { + obj->put(obj, *ptmp); + ptmp++; + } + + for (n = width - tmpwidth; n; n--) + { + obj->put(obj, ' '); + } + break; + } + } + } + + /* Handle floating point conversions */ + + else if (strchr("eEfgG", *src)) + { + char tmpfmt[40]; + const char *psrc; + char *pdst; + double dbl; + + /* Reconstruct the floating point format. */ + + psrc = pfmt; + pdst = tmpfmt; + while (psrc <= src) *pdst++ = *psrc++; + *pdst = 0; + + /* Extract the floating point number. */ + + dbl = va_arg(ap, double); + + /* Then let the lib_sprintf do the work. */ + + if (hasasteriskwidth) + { + if (hasasterisktrunc) + { + lib_sprintf(obj, tmpfmt, width, trunc, dbl); + } + else + { + lib_sprintf(obj, tmpfmt, width, dbl); + } + } + else + { + if (hasasterisktrunc) + { + lib_sprintf(obj, tmpfmt, trunc, dbl); + } + else + { + lib_sprintf(obj, tmpfmt, dbl); + } + } + } + } + } + } + + return obj->nput; +} + diff --git a/nuttx/lib/lib_lowprintf.c b/nuttx/lib/lib_lowprintf.c new file mode 100644 index 000000000..ef9f9ff0f --- /dev/null +++ b/nuttx/lib/lib_lowprintf.c @@ -0,0 +1,106 @@ +/************************************************************ + * lib_lowprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#ifdef CONFIG_ARCH_LOWPUTC + +#include <stdio.h> +#include <debug.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * lib_lowprintf + **********************************************************/ + +int lib_lowprintf(const char *fmt, ...) +{ + struct lib_stream_s stream; + va_list ap; + int ret; + + /* Wrap the stdout in a stream object and let lib_vsprintf + * do the work. + */ + + lib_lowstream(&stream); + + va_start(ap, fmt); + ret= lib_vsprintf(&stream, fmt, ap); + va_end(ap); + return ret; +} + +#endif /* CONFIG_ARCH_LOWPUTC */ diff --git a/nuttx/lib/lib_lowstream.c b/nuttx/lib/lib_lowstream.c new file mode 100644 index 000000000..e0985d3f1 --- /dev/null +++ b/nuttx/lib/lib_lowstream.c @@ -0,0 +1,72 @@ +/************************************************************ + * lib_lowstream.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#ifdef CONFIG_ARCH_LOWPUTC + +#include <stdio.h> +#include <errno.h> +#include <nuttx/arch.h> + +#include "lib_internal.h" + +/************************************************************ + * Private Functions + ************************************************************/ + +static void lowstream_putc(struct lib_stream_s *this, int ch) +{ + if (this && up_putc(ch) != EOF) + { + this->nput++; + } +} + +/************************************************************ + * Public Functions + ************************************************************/ + +void lib_lowstream(struct lib_stream_s *stream) +{ + stream->put = lowstream_putc; + stream->nput = 0; +} + +#endif /* CONFIG_ARCH_LOWPUTC */ diff --git a/nuttx/lib/lib_memcmp.c b/nuttx/lib/lib_memcmp.c new file mode 100644 index 000000000..b433eda90 --- /dev/null +++ b/nuttx/lib/lib_memcmp.c @@ -0,0 +1,74 @@ +/************************************************************ + * lib_memcmp.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_MEMCMP +int memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *p1 = (unsigned char *)s1; + unsigned char *p2 = (unsigned char *)s2; + + while (n-- > 0) + { + if (*p1 < *p2) + { + return -1; + } + else if (*p1 > *p2) + { + return 0; + } + + p1++; + p2++; + } + return 0; +} +#endif diff --git a/nuttx/lib/lib_memcpy.c b/nuttx/lib/lib_memcpy.c new file mode 100644 index 000000000..0953b4d28 --- /dev/null +++ b/nuttx/lib/lib_memcpy.c @@ -0,0 +1,60 @@ +/************************************************************ + * lib_memcpy.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_MEMCPY +void *memcpy(void *dest, const void *src, size_t n) +{ + unsigned char *pout = (unsigned char*)dest; + unsigned char *pin = (unsigned char*)src; + while (n-- > 0) *pout++ = *pin++; + return dest; +} +#endif diff --git a/nuttx/lib/lib_memmove.c b/nuttx/lib/lib_memmove.c new file mode 100644 index 000000000..cad0fa2fc --- /dev/null +++ b/nuttx/lib/lib_memmove.c @@ -0,0 +1,72 @@ +/************************************************************ + * lib_memmove.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_MEMMOVE +void *memmove(void *dest, const void *src, size_t count) +{ + char *tmp, *s; + if (dest <= src) + { + tmp = (char*) dest; + s = (char*) src; + while (count--) + *tmp++ = *s++; + } + else + { + tmp = (char*) dest + count; + s = (char*) src + count; + while (count--) + *--tmp = *--s; + } + return dest; +} +#endif diff --git a/nuttx/lib/lib_memset.c b/nuttx/lib/lib_memset.c new file mode 100644 index 000000000..25b4929eb --- /dev/null +++ b/nuttx/lib/lib_memset.c @@ -0,0 +1,59 @@ +/************************************************************ + * lib_memset.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_MEMSET +void *memset(void *s, int c, size_t n) +{ + unsigned char *p = (unsigned char*)s; + while (n-- > 0) *p++ = c; + return s; +} +#endif diff --git a/nuttx/lib/lib_memstream.c b/nuttx/lib/lib_memstream.c new file mode 100644 index 000000000..713c77e16 --- /dev/null +++ b/nuttx/lib/lib_memstream.c @@ -0,0 +1,70 @@ +/************************************************************ + * lib_memstream.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include "lib_internal.h" + +/************************************************************ + * Private Functions + ************************************************************/ + +static void memstream_putc(struct lib_stream_s *this, int ch) +{ + struct lib_memstream_s *mthis = (struct lib_memstream_s *)this; + if (this && this->nput < mthis->buflen - 1) + { + mthis->buffer[this->nput] = ch; + this->nput++; + mthis->buffer[this->nput] = '\0'; + } +} + +/************************************************************ + * Public Functions + ************************************************************/ + +void lib_memstream(struct lib_memstream_s *memstream, + char *bufstart, int buflen) +{ + memstream->public.put = memstream_putc; + memstream->public.nput = 0; /* Will be buffer index */ + memstream->buffer = bufstart; /* Start of buffer */ + memstream->buflen = buflen - 1; /* Save space for null terminator */ +} + + diff --git a/nuttx/lib/lib_printf.c b/nuttx/lib/lib_printf.c new file mode 100644 index 000000000..c57000fd0 --- /dev/null +++ b/nuttx/lib/lib_printf.c @@ -0,0 +1,103 @@ +/************************************************************ + * lib_printf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * printf + **********************************************************/ + +int printf(const char *fmt, ...) +{ + struct lib_stdstream_s stdstream; + va_list ap; + int ret; + + /* Wrap the stdout in a stream object and let lib_vsprintf + * do the work. + */ + + lib_stdstream(&stdstream, stdout); + + va_start(ap, fmt); + ret= lib_vsprintf(&stdstream.public, fmt, ap); + va_end(ap); + return ret; +} diff --git a/nuttx/lib/lib_puts.c b/nuttx/lib/lib_puts.c new file mode 100644 index 000000000..711fd8195 --- /dev/null +++ b/nuttx/lib/lib_puts.c @@ -0,0 +1,111 @@ +/************************************************************ + * lib_puts.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/************************************************************ + * Public Constant Data + ************************************************************/ + +/************************************************************ + * Public Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * puts + ************************************************************/ + +/* puts() writes the string s and a trailing newline to stdout. */ +int puts(const char *s) +{ + int nwritten; + int nput = EOF; + + /* Write the string (the next two steps must be atomic) */ + + lib_take_semaphore(stdout); + + /* Write the string without its trailing '\0' */ + + nwritten = fputs(s, stdout); + if (nwritten > 0) + { + /* Followed by a newline */ + char newline = '\n'; + if (lib_fwrite(&newline, 1, stdout) > 0) + { + nput = nwritten + 1; + } + } + lib_give_semaphore(stdout); + return nput; +} diff --git a/nuttx/lib/lib_rand.c b/nuttx/lib/lib_rand.c new file mode 100644 index 000000000..5ce708bc8 --- /dev/null +++ b/nuttx/lib/lib_rand.c @@ -0,0 +1,212 @@ +/************************************************************ + * lib_rand.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdlib.h> + +/************************************************************ + * Definitions + ************************************************************/ + +#ifndef RND_ORDER +#define RND_ORDER 1 +#endif + +/* Values needed by the random number generator */ + +#define RND1_CONSTK 470001 +#define RND1_CONSTP 999563 +#define RND2_CONSTK1 366528 +#define RND2_CONSTK2 508531 +#define RND2_CONSTP 998917 +#define RND3_CONSTK1 360137 +#define RND3_CONSTK2 519815 +#define RND3_CONSTK3 616087 +#define RND3_CONSTP 997783 + +#if RND_ORDER == 1 +# define RND_CONSTP RND1_CONSTP +#elif RND_ORDER == 2 +# define RND_CONSTP RND2_CONSTP +#else +# define RND_CONSTP RND3_CONSTP +#endif + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +static unsigned int nrand(unsigned int nLimit); +static double frand1(void); +#if (RND_ORDER > 1) +static double frand2(void); +#if (RND_ORDER > 2) +static double frand3(void); +#endif +#endif + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +static unsigned long g_nRandInt1; +#if (RND_ORDER > 1) +static unsigned long g_nRandInt2; +#if (RND_ORDER > 2) +static unsigned long g_nRandInt3; +#endif +#endif + +/************************************************************ + * Function: srand, rand + ************************************************************/ + +void srand(unsigned int seed) +{ + g_nRandInt1 = seed; +#if (RND_ORDER > 1) + g_nRandInt2 = seed; + (void)frand1(); +#if (RND_ORDER > 2) + g_nRandInt3 = seed; + (void)frand2(); +#endif +#endif + +} /* end srand */ + +int rand(void) +{ + return (int)nrand(32768); + +} /* end rand */ + +static unsigned int nrand(unsigned int nLimit) +{ + unsigned long nResult; + double fRatio; + + /* Loop to be sure a legal random number is generated */ + do { + + /* Get a random integer in the requested range */ +#if (RND_ORDER == 1) + fRatio = frand1(); +#elif (RND_ORDER == 2) + fRatio = frand2(); +#else + fRatio = frand3(); +#endif + + /* Then, produce the return-able value */ + nResult = (unsigned long)(((double)nLimit) * fRatio); + + } while (nResult >= (unsigned long)nLimit); + + return (unsigned int)nResult; + +} /* end nrand */ + +static double frand1(void) +{ + unsigned long nRandInt; + + /* First order congruential generator */ + nRandInt = (RND1_CONSTK * g_nRandInt1) % RND1_CONSTP; + g_nRandInt1 = nRandInt; + + /* Construct an floating point value in the range from 0.0 up to 1.0 */ + return ((double)nRandInt) / ((double)RND_CONSTP); + +} /* end frand */ + +#if (RND_ORDER > 1) +static double frand2(void) +{ + unsigned long nRandInt; + + /* Second order congruential generator */ + nRandInt = (RND2_CONSTK1 * g_nRandInt1 + RND2_CONSTK2 * g_nRandInt2) % + RND2_CONSTP; + g_nRandInt2 = g_nRandInt1; + g_nRandInt1 = nRandInt; + + /* Construct an floating point value in the range from 0.0 up to 1.0 */ + return ((double)nRandInt) / ((double)RND_CONSTP); + +} /* end frand */ + +#if (RND_ORDER > 2) +static double frand(void) +{ + unsigned long nRandInt; + + /* Third order congruential generator */ + nRandInt = (RND3_CONSTK1 * g_nRandInt1 + RND3_CONSTK2 * g_nRandInt2 + + RND3_CONSTK2 * g_nRandInt3) % RND3_CONSTP; + g_nRandInt3 = g_nRandInt2; + g_nRandInt2 = g_nRandInt1; + g_nRandInt1 = nRandInt; + + /* Construct an floating point value in the range from 0.0 up to 1.0 */ + return ((double)nRandInt) / ((double)RND_CONSTP); + +} /* end frand */ +#endif +#endif diff --git a/nuttx/lib/lib_rawprintf.c b/nuttx/lib/lib_rawprintf.c new file mode 100644 index 000000000..537777665 --- /dev/null +++ b/nuttx/lib/lib_rawprintf.c @@ -0,0 +1,100 @@ +/************************************************************ + * lib_rawprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include <debug.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * lib_rawprintf + **********************************************************/ + +int lib_rawprintf(const char *fmt, ...) +{ + struct lib_rawstream_s rawstream; + va_list ap; + int ret; + + /* Wrap the stdout in a stream object and let lib_vsprintf + * do the work. + */ + + lib_rawstream(&rawstream, 1); + + va_start(ap, fmt); + ret= lib_vsprintf(&rawstream.public, fmt, ap); + va_end(ap); + return ret; +} diff --git a/nuttx/lib/lib_rawstream.c b/nuttx/lib/lib_rawstream.c new file mode 100644 index 000000000..e7c194f47 --- /dev/null +++ b/nuttx/lib/lib_rawstream.c @@ -0,0 +1,78 @@ +/************************************************************ + * lib_rawstream.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include <errno.h> +#include "lib_internal.h" + +/************************************************************ + * Private Functions + ************************************************************/ + +static void rawstream_putc(struct lib_stream_s *this, int ch) +{ + struct lib_rawstream_s *rthis = (struct lib_rawstream_s *)this; + char buffer = ch; + if (this && rthis->fd >= 0) + { + int nwritten; + do + { + nwritten = write(rthis->fd, &buffer, 1); + if (nwritten == 1) + { + this->nput++; + } + } + while (nwritten < 0 && *get_errno_ptr() == EINTR); + } +} + +/************************************************************ + * Public Functions + ************************************************************/ + +void lib_rawstream(struct lib_rawstream_s *rawstream, int fd) +{ + rawstream->public.put = rawstream_putc; + rawstream->public.nput = 0; + rawstream->fd = fd; +} + + diff --git a/nuttx/lib/lib_rint.c b/nuttx/lib/lib_rint.c new file mode 100644 index 000000000..f747eea86 --- /dev/null +++ b/nuttx/lib/lib_rint.c @@ -0,0 +1,123 @@ +/************************************************************ + * lib_rint.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdlib.h> + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +double rint(double x) +{ + double retValue; + +/* If the current rounding mode rounds toward negative + * infinity, rint() is identical to floor(). If the current + * rounding mode rounds toward positive infinity, rint() is + * identical to ceil(). */ +#if ((defined(FP_ROUND_POSITIVE)) && (FP_ROUNDING_POSITIVE != 0)) + retValue = ceil(x); + +#elif ((defined(FP_ROUND_NEGATIVE)) && (FP_ROUNDING_NEGATIVE != 0)) + retValue = floor(x); + +#else + + /* In the default rounding mode (round to nearest), rint(x) is the + * integer nearest x with the additional stipulation that if + * |rint(x)-x|=1/2, then rint(x) is even. */ + + long dwInteger = (long)x; + double fRemainder = x - (double)dwInteger; + + if (x < 0.0) { + + /* fRemainder should be in range 0 .. -1 */ + if (fRemainder == -0.5) + dwInteger = ((dwInteger+1)&~1); + else if (fRemainder < -0.5) { + dwInteger--; + + } /* end if */ + } /* end if */ + else { + + /* fRemainder should be in range 0 .. 1 */ + if (fRemainder == 0.5) + dwInteger = ((dwInteger+1)&~1); + else if (fRemainder > 0.5) { + dwInteger++; + + } /* end if */ + } /* end else */ + + retValue = (double)dwInteger; +#endif + + return retValue; +} diff --git a/nuttx/lib/lib_sem.c b/nuttx/lib/lib_sem.c new file mode 100644 index 000000000..9c105dfca --- /dev/null +++ b/nuttx/lib/lib_sem.c @@ -0,0 +1,143 @@ +/************************************************************ + * lib_sem.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#ifdef CONFIG_STDIO_BUFFERED_IO + +#include <unistd.h> +#include <semaphore.h> +#include <errno.h> +#include <assert.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Data + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * lib_sem_initialize + ************************************************************/ + +void lib_sem_initialize(FILE *stream) +{ + /* Initialize the LIB semaphore to one (to support one-at- + * a-time access to private data sets. + */ + + (void)sem_init(&stream->sem, 0, 1); + + stream->holder = -1; + stream->counts = 0; +} + +/************************************************************ + * lib_take_semaphore + ************************************************************/ + +void lib_take_semaphore(FILE *stream) +{ + pid_t my_pid = getpid(); + + /* Do I already have the semaphore? */ + + if (stream->holder == my_pid) + { + /* Yes, just increment the number of references that I have */ + + stream->counts++; + } + else + { + /* Take the semaphore (perhaps waiting) */ + + while (sem_wait(&stream->sem) != 0) + { + /* The only case that an error should occr here is if + * the wait was awakened by a signal. + */ + + ASSERT(*get_errno_ptr() == EINTR); + } + + /* We have it. Claim the stak and return */ + + stream->holder = my_pid; + stream->counts = 1; + } +} + +/************************************************************ + * lib_give_semaphore + ************************************************************/ + +void lib_give_semaphore(FILE *stream) +{ + pid_t my_pid = getpid(); + + /* I better be holding at least one reference to the semaphore */ + + ASSERT(stream->holder == my_pid); + + /* Do I hold multiple references to the semphore */ + + if (stream->counts > 1) + { + /* Yes, just release one count and return */ + + stream->counts--; + } + else + { + /* Nope, this is the last reference I have */ + + stream->holder = -1; + stream->counts = 0; + ASSERT(sem_post(&stream->sem) == 0); + } +} +#endif /* CONFIG_STDIO_BUFFERED_IO */ diff --git a/nuttx/lib/lib_sprintf.c b/nuttx/lib/lib_sprintf.c new file mode 100644 index 000000000..47ec01301 --- /dev/null +++ b/nuttx/lib/lib_sprintf.c @@ -0,0 +1,95 @@ +/************************************************************ + * lib_sprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * sprintf + ************************************************************/ + +int sprintf (char *buf, const char *fmt, ...) +{ + struct lib_memstream_s memstream; + va_list ap; + int n; + + /* Initialize a memory stream to write to the buffer */ + + lib_memstream(&memstream, buf, LIB_BUFLEN_UNKNOWN); + + /* Then let lib_vsprintf do the real work */ + + va_start(ap, fmt); + n = lib_vsprintf(&memstream.public, fmt, ap); + va_end(ap); + return n; +} diff --git a/nuttx/lib/lib_sscanf.c b/nuttx/lib/lib_sscanf.c new file mode 100644 index 000000000..4f100c8ca --- /dev/null +++ b/nuttx/lib/lib_sscanf.c @@ -0,0 +1,328 @@ +/************************************************************ + * lib_sscanf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include <debug.h> + +/************************************************************ + * Definitions + ************************************************************/ + +#define MAXLN 128 + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +int vsscanf(char *buf, const char *s, va_list ap); + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +static const char spaces[] = " \t\n\r\f\v"; + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Function: sscanf + * + * Description: + * ANSI standard sscanf implementation. + * + ************************************************************/ + +int sscanf(const char *buf, const char *fmt, ...) +{ + va_list ap; + int count; + + va_start(ap, fmt); + count = vsscanf((char*)buf, fmt, ap); + va_end(ap); + return count; + +} /* end sscanf */ + +/************************************************************ + * Function: vsscanf + * + * Description: + * ANSI standard vsscanf implementation. + * + ************************************************************/ +int vsscanf(char *buf, const char *s, va_list ap) +{ + int count; + int noassign; + int width; + int base = 10; + int lflag; + char *tv; + const char *tc; + char tmp[MAXLN]; + + vdbg("vsscanf: buf=\"%s\" fmt=\"%s\"\n", buf, s); + + count = noassign = width = lflag = 0; + while (*s && *buf) + { + /* Skip over white space */ + + while (isspace(*s)) + s++; + + /* Check for a conversion specifier */ + + if (*s == '%') + { + vdbg("vsscanf: Specifier found\n"); + + /* Check for qualifiers on the conversion specifier */ + s++; + for (; *s; s++) + { + vdbg("vsscanf: Processing %c\n", *s); + + if (strchr("dibouxcsefg%", *s)) + break; + if (*s == '*') + noassign = 1; + else if (*s == 'l' || *s == 'L') + lflag = 1; + else if (*s >= '1' && *s <= '9') { + for (tc = s; isdigit(*s); s++); + strncpy(tmp, tc, s - tc); + tmp[s - tc] = '\0'; + width = atoi(tmp); + /* atob(&width, tmp, 10); */ + s--; + } + } + + /* Process %s: String conversion */ + + if (*s == 's') + { + vdbg("vsscanf: Performing string conversion\n"); + + while (isspace(*buf)) + buf++; + if (!width) + { + width = strcspn(buf, spaces); + } + if (!noassign) + { + tv = va_arg(ap, char*); + strncpy(tv, buf, width); + tv[width] = '\0'; + } + buf += width; + } + + /* Process %c: Character conversion */ + + else if (*s == 'c') + { + vdbg("vsscanf: Performing character conversion\n"); + + if (!width) + width = 1; + if (!noassign) + { + tv = va_arg(ap, char*); + strncpy(tv, buf, width); + tv[width] = '\0'; + } + buf += width; + } + + /* Process %d, %o, %b, %x, %u: Various integer conversions */ + + else if (strchr("dobxu", *s)) + { + vdbg("vsscanf: Performing integer conversion\n"); + + /* Skip over any white space before the integer string */ + + while (isspace(*buf)) + buf++; + + /* The base of the integer conversion depends on the specific + * conversion specification. + */ + + if (*s == 'd' || *s == 'u') + base = 10; + else if (*s == 'x') + base = 16; + else if (*s == 'o') + base = 8; + else if (*s == 'b') + base = 2; + + /* Copy the integer string into a temporary working buffer. */ + + if (!width) + { + if (isspace(*(s + 1)) || *(s + 1) == 0) + { + width = strcspn(buf, spaces); + } + else + { + width = strchr(buf, *(s + 1)) - buf; + } + } + strncpy(tmp, buf, width); + tmp[width] = '\0'; + + vdbg("vsscanf: tmp[]=\"%s\"\n", tmp); + + /* Perform the integer conversion */ + + buf += width; + if (!noassign) + { + int *pint = va_arg(ap, int*); + int tmpint = strtol(tmp, NULL, base); + vdbg("vsscanf: Return %d to 0x%p\n", tmpint, pint); + *pint = tmpint; + } + } + + /* Process %f: Floating point conversion */ + + else if (*s == 'f') + { + vdbg("vsscanf: Performing floating point conversion\n"); + + /* Skip over any white space before the real string */ + + while (isspace(*buf)) + buf++; + + /* Copy the real string into a temporary working buffer. */ + + if (!width) + { + if (isspace(*(s + 1)) || *(s + 1) == 0) + { + width = strcspn(buf, spaces); + } + else + { + width = strchr(buf, *(s + 1)) - buf; + } + } + strncpy(tmp, buf, width); + tmp[width] = '\0'; + buf += width; + + vdbg("vsscanf: tmp[]=\"%s\"\n", tmp); + + /* Perform the floating point conversion */ + + if (!noassign) + { + /* strtod always returns a double */ + + double dvalue = strtod(tmp, NULL); + void *pv = va_arg(ap, void*); + + vdbg("vsscanf: Return %f to 0x%p\n", dvalue, pv); + + /* But we have to check whether we need to return a + * float or a double. + */ + + if (lflag) + { + *((double*)pv) = dvalue; + } + else + { + *((float*)pv) = (float)dvalue; + } + } + } + + if (!noassign) + count++; + width = noassign = lflag = 0; + s++; + } + + /* Its is not a conversion specifier */ + + else + { + while (isspace(*buf)) + buf++; + if (*s != *buf) + break; + else + s++, buf++; + } + } + return count; +} diff --git a/nuttx/lib/lib_stdstream.c b/nuttx/lib/lib_stdstream.c new file mode 100644 index 000000000..a6fe7849a --- /dev/null +++ b/nuttx/lib/lib_stdstream.c @@ -0,0 +1,70 @@ +/************************************************************ + * lib_stdstream.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include "lib_internal.h" + +/************************************************************ + * Private Functions + ************************************************************/ + +static void stdstream_putc(struct lib_stream_s *this, int ch) +{ + struct lib_stdstream_s *sthis = (struct lib_stdstream_s *)this; + if (this) + { + if (putc(ch, sthis->stream) != EOF) + { + this->nput++; + } + } +} + +/************************************************************ + * Public Functions + ************************************************************/ + +void lib_stdstream(struct lib_stdstream_s *stdstream, + FILE *stream) +{ + stdstream->public.put = stdstream_putc; + stdstream->public.nput = 0; + stdstream->stream = stream; +} + + diff --git a/nuttx/lib/lib_strchr.c b/nuttx/lib/lib_strchr.c new file mode 100644 index 000000000..5b3426dbb --- /dev/null +++ b/nuttx/lib/lib_strchr.c @@ -0,0 +1,71 @@ +/************************************************************ + * lib_strchr.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +/* The strchr() function returns a pointer to the first + * occurrence of the character c in the string s. + */ + +char *strchr(const char *s, int c) +{ + if (s) + { + for (; *s; s++) + { + if (*s == c) + { + return (char*)s; + } + } + } + + return NULL; +} + diff --git a/nuttx/lib/lib_strcmp.c b/nuttx/lib/lib_strcmp.c new file mode 100644 index 000000000..0b1ce89d5 --- /dev/null +++ b/nuttx/lib/lib_strcmp.c @@ -0,0 +1,63 @@ +/************************************************************ + * lib_strcmp.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_STRCMP +int strcmp(const char *cs, const char *ct) +{ + register signed char result; + for (;;) + { + if ((result = *cs - *ct++) != 0 || !*cs++) + break; + } + return result; +} +#endif diff --git a/nuttx/lib/lib_strcpy.c b/nuttx/lib/lib_strcpy.c new file mode 100644 index 000000000..d5cc99993 --- /dev/null +++ b/nuttx/lib/lib_strcpy.c @@ -0,0 +1,59 @@ +/************************************************************ + * lib_strcpy.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_STRCPY +char *strcpy(char *dest, const char *src) +{ + char *tmp = dest; + while ((*dest++ = *src++) != '\0'); + return tmp; +} +#endif diff --git a/nuttx/lib/lib_strdup.c b/nuttx/lib/lib_strdup.c new file mode 100644 index 000000000..285d76c2c --- /dev/null +++ b/nuttx/lib/lib_strdup.c @@ -0,0 +1,61 @@ +/************************************************************ + * lib_strdup.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <stdlib.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +char *strdup(const char *s) +{ + char *news = NULL; + if (s) + { + news = malloc(strlen(s) + 1); + if (news) + { + strcpy(news, s); + } + } + return news; +} diff --git a/nuttx/lib/lib_streamsem.c b/nuttx/lib/lib_streamsem.c new file mode 100644 index 000000000..d0dbad56d --- /dev/null +++ b/nuttx/lib/lib_streamsem.c @@ -0,0 +1,99 @@ +/************************************************************ + * lib_streamsem.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#if CONFIG_NFILE_STREAMS > 0 + +#include <sys/types.h> + +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <semaphore.h> +#include <errno.h> + +#include <nuttx/fs.h> +#include "lib_internal.h" + +/************************************************************ + * Private types + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Public Variables + ************************************************************/ + +/************************************************************ + * Private Functions + ************************************************************/ + +/************************************************************ + * Public Functions + ************************************************************/ + +void stream_semtake(struct streamlist *list) +{ + /* Take the semaphore (perhaps waiting) */ + + while (sem_wait(&list->sl_sem) != 0) + { + /* The only case that an error should occr here is if + * the wait was awakened by a signal. + */ + + ASSERT(*get_errno_ptr() == EINTR); + } +} + +void stream_semgive(struct streamlist *list) +{ + sem_post(&list->sl_sem); +} + +#endif /* CONFIG_NFILE_STREAMS */ + diff --git a/nuttx/lib/lib_strlen.c b/nuttx/lib/lib_strlen.c new file mode 100644 index 000000000..f2cf6f227 --- /dev/null +++ b/nuttx/lib/lib_strlen.c @@ -0,0 +1,59 @@ +/************************************************************ + * lib_strlen.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_STRLEN +size_t strlen(const char *s) +{ + const char *sc; + for (sc = s; *sc != '\0'; ++sc); + return sc - s; +} +#endif diff --git a/nuttx/lib/lib_strncpy.c b/nuttx/lib/lib_strncpy.c new file mode 100644 index 000000000..11d999fe8 --- /dev/null +++ b/nuttx/lib/lib_strncpy.c @@ -0,0 +1,57 @@ +/************************************************************ + * lib_strncpy.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> + +/************************************************************ + * Global Functions + ************************************************************/ + +#ifndef CONFIG_ARCH_STRNCPY +char *strncpy(char *dest, const char *src, size_t n) +{ + char *ret = dest; /* Value to be returned */ + char *end = dest + n; /* End of dest buffer + 1 byte */ + + while ((*dest++ = *src++) != '\0' && dest != end); + return ret; +} +#endif diff --git a/nuttx/lib/lib_strtol.c b/nuttx/lib/lib_strtol.c new file mode 100644 index 000000000..8e2d26f3f --- /dev/null +++ b/nuttx/lib/lib_strtol.c @@ -0,0 +1,176 @@ +/************************************************************ + * lib_strtol.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> +#include <string.h> +#include <ctype.h> + +/************************************************************ + * Private Functions + ************************************************************/ + +/* Skip leading spaces */ + +static void lib_skipspace(const char **nptr) +{ + register const char *tmp = *nptr; + while (isspace(*tmp)) tmp++; + *nptr = tmp; +} + +static int lib_isbasedigit(int c, int base, int *value) +{ + int tmp = 0; + int ret = 0; + + if (base <= 10) + { + if (c >= '0' && c <= base + '0' - 1) + { + tmp = c - '0'; + ret = 1; + } + } + else if (base <= 36) + { + if (c >= '0' && c <= '9') + { + tmp = c - '0'; + ret = 1; + } + else if (c >= 'a' && c <= 'a' + base - 11) + { + tmp = c - 'a' + 10; + ret = 1; + } + else if (c >= 'A' && c <= 'A' + base - 11) + { + tmp = c - 'A' + 10; + ret = 1; + } + } + + if (value) + { + *value = tmp; + } + return ret; +} + +/************************************************************ + * Public Functions + ************************************************************/ + +/* Limited to base 1-36 */ + +long strtol(const char *nptr, char **endptr, int base) +{ + unsigned long accum = 0; + int value; + int negate = 0; + + if (nptr) + { + /* Skip leading spaces */ + + lib_skipspace(&nptr); + + /* Check for leading + or - */ + + if (*nptr == '-') + { + negate = 1; + nptr++; + lib_skipspace(&nptr); + } + else if (*nptr == '+') + { + nptr++; + lib_skipspace(&nptr); + } + + /* Check for unspecified base */ + + if (!base) + { + base = 10; + if (*nptr == '0') + { + base = 8; + nptr++; + if ((*nptr == 'X' || *nptr == 'x') && + lib_isbasedigit(nptr[1], 16, NULL)) + { + base = 16; + nptr++; + } + } + } + else if (base == 16) + { + if (nptr[0] == '0' && (nptr[1] == 'X' || nptr[1] == 'x')) + { + nptr += 2; + } + } + + while (lib_isbasedigit(*nptr, base, &value)) + { + accum = accum*base + value; + nptr++; + } + + if (endptr) + { + *endptr = (char *)nptr; + } + + if (negate) + { + return -(long)accum; + } + } + return (long)accum; +} + +unsigned long strtoul(const char *nptr, char **endptr, int base) +{ + return (unsigned long)strtol(nptr, endptr, base); +} diff --git a/nuttx/lib/lib_ungetc.c b/nuttx/lib/lib_ungetc.c new file mode 100644 index 000000000..45cde1efc --- /dev/null +++ b/nuttx/lib/lib_ungetc.c @@ -0,0 +1,124 @@ +/************************************************************ + * lib_ungetc.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <nuttx/config.h> + +#if CONFIG_NFILE_STREAMS > 0 + +#include <stdio.h> +#include <errno.h> +#include <nuttx/fs.h> +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * fgetc + **********************************************************/ + +int ungetc(int c, FILE *stream) +{ +#if CONFIG_NUNGET_CHARS > 0 + int nungotten; +#endif + + /* Stream must be open for read access */ + + if ((stream && stream->fs_filedes < 0) || + ((stream->fs_oflags & O_RDOK) == 0)) + { + *get_errno_ptr() = EBADF; + return EOF; + } + +#if CONFIG_NUNGET_CHARS > 0 + nungotten = stream->fs_nungotten; + if (stream->fs_nungotten < CONFIG_NUNGET_CHARS) + { + stream->fs_ungotten[nungotten] = c; + stream->fs_nungotten = nungotten + 1; + return c; + } + else +#endif + { + *get_errno_ptr() = ENOMEM; + return EOF; + } +} + +#endif /* CONFIG_NFILE_STREAMS */ diff --git a/nuttx/lib/lib_vfprintf.c b/nuttx/lib/lib_vfprintf.c new file mode 100644 index 000000000..7d1b666a4 --- /dev/null +++ b/nuttx/lib/lib_vfprintf.c @@ -0,0 +1,93 @@ +/************************************************************ + * lib_vfprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdio.h> +#include <semaphore.h> + +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Public Functions + ************************************************************/ + +int vfprintf(FILE *stream, const char *fmt, va_list ap) +{ + struct lib_stdstream_s stdstream; + int n = ERROR; + + if (stream) + { + /* Wrap the stream in a stream object and let lib_vsprintf + * do the work. + */ + + lib_stdstream(&stdstream, stream); + n = lib_vsprintf(&stdstream.public, fmt, ap); + } + return n; +} diff --git a/nuttx/lib/lib_vprintf.c b/nuttx/lib/lib_vprintf.c new file mode 100644 index 000000000..6c731ca9a --- /dev/null +++ b/nuttx/lib/lib_vprintf.c @@ -0,0 +1,91 @@ +/************************************************************ + * lib_vprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <stdio.h> + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Function Prototypes + ************************************************************/ + +/********************************************************** + * Global Constant Data + **********************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/********************************************************** + * Private Constant Data + **********************************************************/ + +/************************************************************ + * Private Variables + **********************************************************/ + +/************************************************************ + * Global Functions + **********************************************************/ + +/************************************************************ + * vprintf + **********************************************************/ + +int vprintf(const char *fmt, va_list ap) +{ + /* vfprintf into stdout */ + + return vfprintf(stdout, fmt, ap); +} diff --git a/nuttx/lib/lib_vsprintf.c b/nuttx/lib/lib_vsprintf.c new file mode 100644 index 000000000..ec3d34fdb --- /dev/null +++ b/nuttx/lib/lib_vsprintf.c @@ -0,0 +1,95 @@ +/************************************************************ + * lib_vsprintf.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <sys/types.h> +#include <stdio.h> + +#include "lib_internal.h" + +/************************************************************ + * Definitions + ************************************************************/ + +/************************************************************ + * Private Type Declarations + ************************************************************/ + +/************************************************************ + * Private Function Prototypes + ************************************************************/ + +/************************************************************ + * Global Constant Data + ************************************************************/ + +/************************************************************ + * Global Variables + ************************************************************/ + +/************************************************************ + * Private Constant Data + ************************************************************/ + +/************************************************************ + * Private Variables + ************************************************************/ + +/************************************************************ + * Global Functions + ************************************************************/ + +/************************************************************ + * vsprintf + ************************************************************/ + +int vsprintf (char *dest, const char *src, va_list ap) +{ + struct lib_memstream_s memstream; + + /* Wrap the destination buffer in a stream object and let + * lib_vsprintf do the work. + */ + + lib_memstream(&memstream, dest, LIB_BUFLEN_UNKNOWN); + return lib_vsprintf(&memstream.public, src, ap); +} diff --git a/nuttx/lib/sq_addafter.c b/nuttx/lib/sq_addafter.c new file mode 100644 index 000000000..896aed5ca --- /dev/null +++ b/nuttx/lib/sq_addafter.c @@ -0,0 +1,71 @@ +/************************************************************ + * sq_addafter.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: sq_addafter.c + * + * Description: + * The sq_addafter function adds 'node' after 'prev' in the + * 'queue.' + * + ************************************************************/ + +void sq_addafter(sq_entry_t *prev, sq_entry_t *node, + sq_queue_t *queue) +{ + if (!queue->head || prev == queue->tail) + { + sq_addlast(node, queue); + } + else + { + node->flink = prev->flink; + prev->flink = node; + } +} diff --git a/nuttx/lib/sq_addfirst.c b/nuttx/lib/sq_addfirst.c new file mode 100644 index 000000000..f59fbc568 --- /dev/null +++ b/nuttx/lib/sq_addfirst.c @@ -0,0 +1,67 @@ +/************************************************************ + * sq_addfirst.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: sq_addfirst + * + * Description: + * The sq_addfirst function places the 'node' at the head + * of the 'queue' + * + ************************************************************/ + +void sq_addfirst(sq_entry_t *node, sq_queue_t *queue) +{ + node->flink = queue->head; + if (!queue->head) + { + queue->tail = node; + } + queue->head = node; +} diff --git a/nuttx/lib/sq_addlast.c b/nuttx/lib/sq_addlast.c new file mode 100644 index 000000000..172808b33 --- /dev/null +++ b/nuttx/lib/sq_addlast.c @@ -0,0 +1,72 @@ +/************************************************************ + * sq_addlast.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: sq_addlast + * + * Description: + * The sq_addlast function places the 'node' at the tail of + * the 'queue' + ************************************************************/ + +void sq_addlast(sq_entry_t *node, sq_queue_t *queue) +{ + node->flink = NULL; + if (!queue->head) + { + queue->head = node; + queue->tail = node; + } + else + { + queue->tail->flink = node; + queue->tail = node; + } +} + diff --git a/nuttx/lib/sq_rem.c b/nuttx/lib/sq_rem.c new file mode 100644 index 000000000..30a49b9b1 --- /dev/null +++ b/nuttx/lib/sq_rem.c @@ -0,0 +1,83 @@ +/************************************************************ + * sq_rem.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * public Functions + ************************************************************/ + +/************************************************************ + * Name: sq_rem + * + * Description: + * sq_rem removes a 'node' for 'queue.' + * + ************************************************************/ + +void sq_rem(sq_entry_t *node, sq_queue_t *queue) +{ + if (queue->head && node) + { + if (node == queue->head) + { + queue->head = node->flink; + if (node == queue->tail) + { + queue->tail = NULL; + } + } + else + { + sq_entry_t *prev; + for(prev = (sq_entry_t*)queue->head; + prev && prev->flink != node; + prev = prev->flink); + + if (prev) + { + sq_remafter(prev, queue); + } + } + } +} diff --git a/nuttx/lib/sq_remafter.c b/nuttx/lib/sq_remafter.c new file mode 100644 index 000000000..353e8ec0a --- /dev/null +++ b/nuttx/lib/sq_remafter.c @@ -0,0 +1,78 @@ +/************************************************************ + * sq_remafter.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: + * + * Description: + * sq_remafter removes the entry following 'node; from the + * 'queue' Returns a reference to the removed entry. + * + ************************************************************/ + +sq_entry_t *sq_remafter(sq_entry_t *node, sq_queue_t *queue) +{ + sq_entry_t *ret = node->flink; + if (queue->head && ret) + { + if (queue->tail == ret) + { + queue->tail = node; + node->flink = NULL; + } + else + { + node->flink = ret->flink; + } + + ret->flink = NULL; + } + + return ret; +} diff --git a/nuttx/lib/sq_remfirst.c b/nuttx/lib/sq_remfirst.c new file mode 100644 index 000000000..dbbd5f378 --- /dev/null +++ b/nuttx/lib/sq_remfirst.c @@ -0,0 +1,75 @@ +/************************************************************ + * sq_remfirst.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: sq_remfirst + * + * Description: + * sq_remfirst function removes the first entry from + * 'queue' + * + ************************************************************/ + +sq_entry_t *sq_remfirst(sq_queue_t *queue) +{ + sq_entry_t *ret = queue->head; + + if (ret) + { + queue->head = ret->flink; + if (!queue->head) + { + queue->tail = NULL; + } + + ret->flink = NULL; + } + + return ret; +} diff --git a/nuttx/lib/sq_remlast.c b/nuttx/lib/sq_remlast.c new file mode 100644 index 000000000..35e5cda6a --- /dev/null +++ b/nuttx/lib/sq_remlast.c @@ -0,0 +1,87 @@ +/************************************************************ + * sq_remlast.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************/ + +/************************************************************ + * Compilation Switches + ************************************************************/ + +/************************************************************ + * Included Files + ************************************************************/ + +#include <queue.h> + +/************************************************************ + * Public Functions + ************************************************************/ + +/************************************************************ + * Name: sq_remlast + * + * Description: + * Removes the last entry in a singly-linked queue. + * + ************************************************************/ + +sq_entry_t *sq_remlast(sq_queue_t *queue) +{ + sq_entry_t *ret = queue->tail; + + if (ret) + { + if (queue->head == queue->tail) + { + queue->head = NULL; + queue->tail = NULL; + } + else + { + sq_entry_t *prev; + for(prev = queue->head; + prev && prev->flink != ret; + prev = prev->flink); + + if (prev) + { + prev->flink = NULL; + queue->tail = prev; + } + } + + ret->flink = NULL; + } + + return ret; +} |