tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Changes to add snscanf/vsnscanf



In article <48BA5C73.4040406%netbsd.org@localhost>,
Darren Reed  <darrenr%netbsd.org@localhost> wrote:
>-=-=-=-=-=-
>
>To follow up on my earlier email about snscanf/vsnscanf,
>the changes attached to this email are what I'm proposing
>for NetBSD.
>
>In summary:
>- document both functions on the man page with scanf(3)
>- add snscanf.c and vsnscanf.c to the "NetBSD section" of stdio.h
>- include snscanf.c and vsnscanf.c to the building of stdio in libc.
>
>I don't believe it is necessary to add any weak symbols or any
>other special things but I might be missing something...
>
>Darren
>
>
>-=-=-=-=-=-
>
>Index: include/stdio.h
>===================================================================
>RCS file: /room/netbsd/src/include/stdio.h,v
>retrieving revision 1.72
>diff -c -r1.72 stdio.h
>*** include/stdio.h    4 Aug 2008 17:08:49 -0000       1.72
>--- include/stdio.h    31 Aug 2008 08:46:10 -0000
>***************
>*** 394,402 ****
>--- 394,407 ----
>  int   fpurge(FILE *);
>  void  setbuffer(FILE *, char *, int);
>  int   setlinebuf(FILE *);
>+ int   snscanf(const char * __restrict, size_t, const char * __restrict, ...)
>+          __attribute__((__format__(__printf__, 3, 4)));
>  int   vasprintf(char ** __restrict, const char * __restrict,
>           _BSD_VA_LIST_)
>           __attribute__((__format__(__printf__, 2, 0)));
>+ int   vsnscanf(const char * __restrict, size_t, const char * __restrict,
>+          _BSD_VA_LIST_)
>+          __attribute__((__format__(__printf__, 4, 0)));
>  const char *fmtcheck(const char *, const char *)
>           __attribute__((__format_arg__(2)));
>  __END_DECLS
>Index: lib/libc/stdio/Makefile.inc
>===================================================================
>RCS file: /room/netbsd/src/lib/libc/stdio/Makefile.inc,v
>retrieving revision 1.35
>diff -c -r1.35 Makefile.inc
>*** lib/libc/stdio/Makefile.inc        3 Feb 2007 00:28:56 -0000       1.35
>--- lib/libc/stdio/Makefile.inc        31 Aug 2008 08:46:10 -0000
>***************
>*** 16,25 ****
>       mkdtemp.c mkstemp.c perror.c printf.c putc.c putchar.c puts.c \
>       putw.c putwc.c putwchar.c refill.c remove.c rewind.c rget.c \
>       scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \
>!      sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \
>!      vasprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c vprintf.c \
>!      vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c vswscanf.c \
>!      vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
>  
>  .if !defined(AUDIT)
>  SRCS+=       gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
>--- 16,26 ----
>       mkdtemp.c mkstemp.c perror.c printf.c putc.c putchar.c puts.c \
>       putw.c putwc.c putwchar.c refill.c remove.c rewind.c rget.c \
>       scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \
>!      snscanf.c sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c \
>!      ungetwc.c vasprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \
>!      vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsnscanf.c vsscanf.c \
>!      vswprintf.c vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c \
>!      wscanf.c wsetup.c
>  
>  .if !defined(AUDIT)
>  SRCS+=       gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
>***************
>*** 52,59 ****
>       printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3 
>  MLINKS+=putc.3 fputc.3 putc.3 putc_unlocked.3 putc.3 putchar.3 \
>       putc.3 putchar_unlocked.3 putc.3 putw.3
>! MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 
>\
>!      scanf.3 vsscanf.3
>  MLINKS+=setbuf.3 setbuffer.3 setbuf.3 setlinebuf.3 setbuf.3 setvbuf.3
>  MLINKS+=tmpnam.3 tempnam.3 tmpnam.3 tmpfile.3
>  MLINKS+=putwc.3 fputwc.3 putwc.3 putwchar.3
>--- 53,60 ----
>       printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3 
>  MLINKS+=putc.3 fputc.3 putc.3 putc_unlocked.3 putc.3 putchar.3 \
>       putc.3 putchar_unlocked.3 putc.3 putw.3
>! MLINKS+=scanf.3 fscanf.3 scanf.3 snscanf.3 scanf.3 sscanf.3 scanf.3
>vfscanf.3 \
>!      scanf.3 vscanf.3 scanf.3 vsnscanf.3 scanf.3 vsscanf.3
>  MLINKS+=setbuf.3 setbuffer.3 setbuf.3 setlinebuf.3 setbuf.3 setvbuf.3
>  MLINKS+=tmpnam.3 tempnam.3 tmpnam.3 tmpfile.3
>  MLINKS+=putwc.3 fputwc.3 putwc.3 putwchar.3
>Index: lib/libc/stdio/scanf.3
>===================================================================
>RCS file: /room/netbsd/src/lib/libc/stdio/scanf.3,v
>retrieving revision 1.20
>diff -c -r1.20 scanf.3
>*** lib/libc/stdio/scanf.3     30 May 2008 17:29:54 -0000      1.20
>--- lib/libc/stdio/scanf.3     31 Aug 2008 08:46:10 -0000
>***************
>*** 39,46 ****
>--- 39,48 ----
>  .Sh NAME
>  .Nm scanf ,
>  .Nm fscanf ,
>+ .Nm snscanf ,
>  .Nm sscanf ,
>  .Nm vscanf ,
>+ .Nm vsnscanf ,
>  .Nm vsscanf ,
>  .Nm vfscanf
>  .Nd input format conversion
>***************
>*** 53,63 ****
>--- 55,69 ----
>  .Ft int
>  .Fn fscanf "FILE * restrict stream" "const char * restrict format" ...
>  .Ft int
>+ .Fn snscanf "const char * restrict str" "size_t size" "const char *
>restrict format" ...
>+ .Ft int
>  .Fn sscanf "const char * restrict str" "const char * restrict format" ...
>  .In stdarg.h
>  .Ft int
>  .Fn vscanf "const char * restrict format" "va_list ap"
>  .Ft int
>+ .Fn vsnscanf "const char * restrict str" "size_t size" "const char *
>restrict format" "va_list ap"
>+ .Ft int
>  .Fn vsscanf "const char * restrict str" "const char * restrict format"
>"va_list ap"
>  .Ft int
>  .Fn vfscanf "FILE * restrict stream" "const char * restrict format"
>"va_list ap"
>***************
>*** 85,90 ****
>--- 91,105 ----
>  .Fn sscanf
>  reads its input from the character string pointed to by
>  .Fa str .
>+ The function
>+ .Fn sscanf
>+ uses
>+ .Fn strlen
>+ to determine the length of input. To explicitly set the size of the
>+ input buffer to be scanned,
>+ .Fn snscanf
>+ should be used and the length of the string passed by
>+ .Fa size .
>  The
>  .Fn vfscanf
>  function
>***************
>*** 106,111 ****
>--- 121,136 ----
>  and
>  .Fn vsprintf
>  functions respectively.
>+ As with
>+ .Fn sscanf
>+ above,
>+ .Fn vsscanf
>+ uses
>+ .Fn strlen
>+ to determine the length of the input buffer to be processed.
>+ To explicitly set the size of the buffer to be scanned, passing
>+ .Fa size
>+ is supported to set the length of the string.
>  Each successive
>  .Em pointer
>  argument must correspond properly with
>***************
>*** 446,451 ****
>--- 471,482 ----
>  .Bx 4.4
>  or even
>  .Bx 4.3 .
>+ The functions
>+ .Fn snscanf
>+ and
>+ .Fn vsnscanf
>+ first appeared in
>+ .Nx 4.99 .
>  .Sh NOTES
>  All of the backwards compatibility formats will be removed in the future.
>  .Sh BUGS
>
>-=-=-=-=-=-
>
>/*     $NetBSD$        */
>
>/*-
> * Copyright (c) 1990, 1993
> *     The Regents of the University of California.  All rights reserved.
> *
> * This code is derived from software contributed to Berkeley by
> * Chris Torek.
> *
> * 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 of the University 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 REGENTS 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 REGENTS 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 <sys/cdefs.h>
>#if defined(LIBC_SCCS) && !defined(lint)
>#if 0
>static char sccsid[] = "@(#)sscanf.c   8.1 (Berkeley) 6/4/93";
>#else
>__RCSID("$NetBSD$");
>#endif
>#endif /* LIBC_SCCS and not lint */
>
>#include "namespace.h"
>
>#include <assert.h>
>#include <errno.h>
>#include <stdarg.h>
>#include <stdio.h>
>#include <string.h>
>
>#include "reentrant.h"
>#include "local.h"
>
>static int eofread __P((void *, char *, int));
>
>/* ARGSUSED */
>static int
>eofread(cookie, buf, len)
>       void *cookie;
>       char *buf;
>       int len;
>{
>
>       return (0);
>}
>
>int
>snscanf(const char *str, const size_t n, char const *fmt, ...)
>{
>       int ret;
>       va_list ap;
>       FILE f;
>       struct __sfileext fext;
>
>       _DIAGASSERT(str != NULL);
>       _DIAGASSERT(fmt != NULL);
>
>       _FILEEXT_SETUP(&f, &fext);
>       f._flags = __SRD;
>       f._bf._base = f._p = __UNCONST(str);
>       f._bf._size = f._r = n;
>       f._read = eofread;
>       _UB(&f)._base = NULL;
>       f._lb._base = NULL;
>       va_start(ap, fmt);
>       ret = __svfscanf_unlocked(&f, fmt, ap);
>       va_end(ap);
>       return (ret);
>}
>
>-=-=-=-=-=-
>
>/*     $NetBSD$        */
>
>/*-
> * Copyright (c) 1990, 1993
> *     The Regents of the University of California.  All rights reserved.
> *
> * This code is derived from software contributed to Berkeley by
> * Donn Seeley at UUNET Technologies, Inc.
> *
> * 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 of the University 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 REGENTS 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 REGENTS 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 <sys/cdefs.h>
>#if defined(LIBC_SCCS) && !defined(lint)
>#if 0
>static char sccsid[] = "@(#)vsscanf.c  8.1 (Berkeley) 6/4/93";
>#else
>__RCSID("$NetBSD$");
>#endif
>#endif /* LIBC_SCCS and not lint */
>
>#include "namespace.h"
>
>#include <assert.h>
>#include <errno.h>
>#include <stdio.h>
>#include <string.h>
>#include "reentrant.h"
>#include "local.h"
>
>static int eofread __P((void *, char *, int));
>
>/* ARGSUSED */
>static int
>eofread(cookie, buf, len)
>       void *cookie;
>       char *buf;
>       int len;
>{
>       return (0);
>}
>
>int
>vsnscanf(const char *str, size_t n, const char *fmt, _BSD_VA_LIST_ ap)
>{
>       FILE f;
>       struct __sfileext fext;
>
>       _DIAGASSERT(str != NULL);
>       _DIAGASSERT(fmt != NULL);
>
>       _FILEEXT_SETUP(&f, &fext);
>       f._flags = __SRD;
>       f._bf._base = f._p = __UNCONST(str);
>       f._bf._size = f._r = n;
>       f._read = eofread;
>       _UB(&f)._base = NULL;
>       f._lb._base = NULL;
>       return (__svfscanf_unlocked(&f, fmt, ap));
>}

Looks fine to me. At this point since we are going to have 5 copies
of eofread() I'd merge them into one.

christos



Home | Main Index | Thread Index | Old Index