Subversion Repositories Kolibri OS

Rev

Rev 4921 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. /*
  19. FUNCTION
  20. <<sscanf>>, <<fscanf>>, <<scanf>>---scan and format input
  21.  
  22. INDEX
  23.         scanf
  24. INDEX
  25.         _scanf_r
  26. INDEX
  27.         fscanf
  28. INDEX
  29.         _fscanf_r
  30. INDEX
  31.         sscanf
  32. INDEX
  33.         _sscanf_r
  34.  
  35. ANSI_SYNOPSIS
  36.         #include <stdio.h>
  37.  
  38.         int scanf(const char *restrict <[format]>, ...);
  39.         int fscanf(FILE *restrict <[fd]>, const char *restrict <[format]>, ...);
  40.         int sscanf(const char *restrict <[str]>, const char *restrict <[format]>, ...);
  41.  
  42.         int _scanf_r(struct _reent *<[ptr]>, const char *restrict <[format]>, ...);
  43.         int _fscanf_r(struct _reent *<[ptr]>, FILE *restrict <[fd]>,
  44.                       const char *restrict <[format]>, ...);
  45.         int _sscanf_r(struct _reent *<[ptr]>, const char *restrict <[str]>,
  46.                       const char *restrict <[format]>, ...);
  47.  
  48.  
  49. TRAD_SYNOPSIS
  50.         #include <stdio.h>
  51.  
  52.         int scanf(<[format]> [, <[arg]>, ...])
  53.         char *<[format]>;
  54.  
  55.         int fscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
  56.         FILE *<[fd]>;
  57.         char *<[format]>;
  58.  
  59.         int sscanf(<[str]>, <[format]> [, <[arg]>, ...]);
  60.         char *<[str]>;
  61.         char *<[format]>;
  62.  
  63.         int _scanf_r(<[ptr]>, <[format]> [, <[arg]>, ...])
  64.         struct _reent *<[ptr]>;
  65.         char *<[format]>;
  66.  
  67.         int _fscanf_r(<[ptr]>, <[fd]>, <[format]> [, <[arg]>, ...]);
  68.         struct _reent *<[ptr]>;
  69.         FILE *<[fd]>;
  70.         char *<[format]>;
  71.  
  72.         int _sscanf_r(<[ptr]>, <[str]>, <[format]> [, <[arg]>, ...]);
  73.         struct _reent *<[ptr]>;
  74.         char *<[str]>;
  75.         char *<[format]>;
  76.  
  77.  
  78. DESCRIPTION
  79.         <<scanf>> scans a series of input fields from standard input,
  80.                 one character at a time.  Each field is interpreted according to
  81.                 a format specifier passed to <<scanf>> in the format string at
  82.         <<*<[format]>>>.  <<scanf>> stores the interpreted input from
  83.                 each field at the address passed to it as the corresponding argument
  84.                 following <[format]>.  You must supply the same number of
  85.                 format specifiers and address arguments as there are input fields.
  86.  
  87.         There must be sufficient address arguments for the given format
  88.         specifiers; if not the results are unpredictable and likely
  89.         disasterous.  Excess address arguments are merely ignored.
  90.  
  91.         <<scanf>> often produces unexpected results if the input diverges from
  92.         an expected pattern. Since the combination of <<gets>> or <<fgets>>
  93.         followed by <<sscanf>> is safe and easy, that is the preferred way
  94.         to be certain that a program is synchronized with input at the end
  95.                 of a line.
  96.  
  97.         <<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
  98.         source of input: <<fscanf>> reads from a file, and <<sscanf>>
  99.                 from a string.
  100.  
  101.         The routines <<_scanf_r>>, <<_fscanf_r>>, and <<_sscanf_r>> are reentrant
  102.         versions of <<scanf>>, <<fscanf>>, and <<sscanf>> that take an additional
  103.         first argument pointing to a reentrancy structure.
  104.  
  105.         The string at <<*<[format]>>> is a character sequence composed
  106.         of zero or more directives. Directives are composed of
  107.         one or more whitespace characters, non-whitespace characters,
  108.         and format specifications.
  109.  
  110.         Whitespace characters are blank (<< >>), tab (<<\t>>), or
  111.                 newline (<<\n>>).
  112.         When <<scanf>> encounters a whitespace character in the format string
  113.         it will read (but not store) all consecutive whitespace characters
  114.         up to the next non-whitespace character in the input.
  115.  
  116.         Non-whitespace characters are all other ASCII characters except the
  117.         percent sign (<<%>>).  When <<scanf>> encounters a non-whitespace
  118.         character in the format string it will read, but not store
  119.         a matching non-whitespace character.
  120.  
  121.         Format specifications tell <<scanf>> to read and convert characters
  122.         from the input field into specific types of values, and store then
  123.         in the locations specified by the address arguments.
  124.  
  125.         Trailing whitespace is left unread unless explicitly
  126.         matched in the format string.
  127.  
  128.         The format specifiers must begin with a percent sign (<<%>>)
  129.         and have the following form:
  130.  
  131. .       %[*][<[width]>][<[size]>]<[type]>
  132.  
  133.         Each format specification begins with the percent character (<<%>>).
  134.         The other fields are:
  135.         O+
  136.                 o *
  137.  
  138.                 an optional marker; if present, it suppresses interpretation and
  139.         assignment of this input field.
  140.  
  141.         o <[width]>
  142.  
  143.                 an optional maximum field width: a decimal integer,
  144.                 which controls the maximum number of characters that
  145.                 will be read before converting the current input field.  If the
  146.                 input field has fewer than <[width]> characters, <<scanf>>
  147.                 reads all the characters in the field, and then
  148.                 proceeds with the next field and its format specification.
  149.  
  150.                 If a whitespace or a non-convertable character occurs
  151.                 before <[width]> character are read, the characters up
  152.                 to that character are read, converted, and stored.
  153.                 Then <<scanf>> proceeds to the next format specification.
  154.  
  155.         o <[size]>
  156.  
  157.                 <<h>>, <<j>>, <<l>>, <<L>>, <<t>>, and <<z>> are optional size
  158.                 characters which override the default way that <<scanf>>
  159.                 interprets the data type of the corresponding argument.
  160.  
  161.                 @multitable @columnfractions 0.18 0.30 0.52
  162.                 @headitem
  163.                 Modifier
  164.                 @tab
  165.                 Type(s)
  166.                 @tab
  167.                 @item
  168.                 hh
  169.                 @tab
  170.                 d, i, o, u, x, n
  171.                 @tab
  172.                 convert input to char, store in char object
  173.                 @item
  174.                 h
  175.                 @tab
  176.                 d, i, o, u, x, n
  177.                 @tab
  178.                 convert input to short, store in short object
  179.                 @item
  180.                 h
  181.                 @tab
  182.                 D, I, O, U, X, e, f, c, s, p
  183.                 @tab
  184.                 no effect
  185.                 @item
  186.                 j
  187.                 @tab
  188.                 d, i, o, u, x, n
  189.                 @tab
  190.                 convert input to intmax_t, store in intmax_t object
  191.                 @item
  192.                 j
  193.                 @tab
  194.                 all others
  195.                 @tab
  196.                 no effect
  197.                 @item
  198.                 l
  199.                 @tab
  200.                 d, i, o, u, x, n
  201.                 @tab
  202.                 convert input to long, store in long object
  203.                 @item
  204.                 l
  205.                 @tab
  206.                 e, f, g
  207.                 @tab
  208.                 convert input to double, store in a double object
  209.                 @item
  210.                 l
  211.                 @tab
  212.                 D, I, O, U, X, c, s, p
  213.                 @tab
  214.                 no effect
  215.                 @item
  216.                 ll
  217.                 @tab
  218.                 d, i, o, u, x, n
  219.                 @tab
  220.                 convert to long long, store in long long object
  221.                 @item
  222.                 L
  223.                 @tab
  224.                 d, i, o, u, x, n
  225.                 @tab
  226.                 convert to long long, store in long long object
  227.                 @item
  228.                 L
  229.                 @tab
  230.                 e, f, g, E, G
  231.                 @tab
  232.                 convert to long double, store in long double object
  233.                 @item
  234.                 L
  235.                 @tab
  236.                 all others
  237.                 @tab
  238.                 no effect
  239.                 @item
  240.                 t
  241.                 @tab
  242.                 d, i, o, u, x, n
  243.                 @tab
  244.                 convert input to ptrdiff_t, store in ptrdiff_t object
  245.                 @item
  246.                 t
  247.                 @tab
  248.                 all others
  249.                 @tab
  250.                 no effect
  251.                 @item
  252.                 z
  253.                 @tab
  254.                 d, i, o, u, x, n
  255.                 @tab
  256.                 convert input to size_t, store in size_t object
  257.                 @item
  258.                 z
  259.                 @tab
  260.                 all others
  261.                 @tab
  262.                 no effect
  263.                 @end multitable
  264.  
  265.         o <[type]>
  266.  
  267.                 A character to specify what kind of conversion
  268.                 <<scanf>> performs.  Here is a table of the conversion
  269.                 characters:
  270.  
  271.                 o+
  272.                 o %
  273.                 No conversion is done; the percent character (<<%>>) is stored.
  274.  
  275.                 o c
  276.                 Scans one character.  Corresponding <[arg]>: <<(char *arg)>>.
  277.  
  278.                 o s
  279.                 Reads a character string into the array supplied.
  280.                 Corresponding <[arg]>: <<(char arg[])>>.
  281.  
  282.                 o [<[pattern]>]
  283.                 Reads a non-empty character string into memory
  284.                 starting at <[arg]>.  This area must be large
  285.                 enough to accept the sequence and a
  286.                 terminating null character which will be added
  287.                 automatically.  (<[pattern]> is discussed in the paragraph following
  288.                 this table). Corresponding <[arg]>: <<(char *arg)>>.
  289.  
  290.                 o d
  291.                 Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
  292.  
  293.                 o D
  294.                 Reads a decimal integer into the corresponding
  295.                 <[arg]>: <<(long *arg)>>.
  296.  
  297.                 o o
  298.                 Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
  299.  
  300.                 o O
  301.                 Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
  302.  
  303.                 o u
  304.                 Reads an unsigned decimal integer into the corresponding
  305.                 <[arg]>: <<(unsigned int *arg)>>.
  306.  
  307.                 o U
  308.                 Reads an unsigned decimal integer into the corresponding <[arg]>:
  309.                 <<(unsigned long *arg)>>.
  310.  
  311.                 o x,X
  312.                 Read a hexadecimal integer into the corresponding <[arg]>:
  313.                 <<(int *arg)>>.
  314.  
  315.                 o e, f, g
  316.                 Read a floating-point number into the corresponding <[arg]>:
  317.                 <<(float *arg)>>.
  318.  
  319.                 o E, F, G
  320.                 Read a floating-point number into the corresponding <[arg]>:
  321.                 <<(double *arg)>>.
  322.  
  323.                 o i
  324.                 Reads a decimal, octal or hexadecimal integer into the
  325.                 corresponding <[arg]>: <<(int *arg)>>.
  326.  
  327.                 o I
  328.                 Reads a decimal, octal or hexadecimal integer into the
  329.                 corresponding <[arg]>: <<(long *arg)>>.
  330.  
  331.                 o n
  332.                 Stores the number of characters read in the corresponding
  333.                 <[arg]>: <<(int *arg)>>.
  334.  
  335.                 o p
  336.                 Stores a scanned pointer.  ANSI C leaves the details
  337.                 to each implementation; this implementation treats
  338.                 <<%p>> exactly the same as <<%U>>.  Corresponding
  339.                 <[arg]>: <<(void **arg)>>.  
  340.                 o-
  341.  
  342.         A <[pattern]> of characters surrounded by square brackets can be used
  343.         instead of the <<s>> type character.  <[pattern]> is a set of
  344.         characters which define a search set of possible characters making up
  345.         the <<scanf>> input field.  If the first character in the brackets is a
  346.         caret (<<^>>), the search set is inverted to include all ASCII characters
  347.         except those between the brackets.  There is also a range facility
  348.         which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
  349.         The hyphen must not be the first or last character in the set.
  350.         The character prior to the hyphen must be lexically less than the
  351.         character after it.
  352.  
  353.         Here are some <[pattern]> examples:
  354.                 o+
  355.                 o %[abcd]
  356.                 matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
  357.  
  358.                 o %[^abcd]
  359.                 matches strings containing any characters except <<a>>, <<b>>,
  360.                 <<c>>, or <<d>>
  361.  
  362.                 o %[A-DW-Z]
  363.                 matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
  364.                 <<X>>, <<Y>>, <<Z>>
  365.  
  366.                 o %[z-a]
  367.                 matches the characters  <<z>>, <<->>, and <<a>>
  368.                 o-
  369.  
  370.         Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
  371.         <<F>>, <<G>>) must correspond to the following general form:
  372.  
  373. .               [+/-] ddddd[.]ddd [E|e[+|-]ddd]
  374.  
  375.         where objects inclosed in square brackets are optional, and <<ddd>>
  376.         represents decimal, octal, or hexadecimal digits.
  377.         O-
  378.  
  379. RETURNS
  380.         <<scanf>> returns the number of input fields successfully
  381.         scanned, converted and stored; the return value does
  382.         not include scanned fields which were not stored.
  383.  
  384.         If <<scanf>> attempts to read at end-of-file, the return
  385.         value is <<EOF>>.
  386.  
  387.         If no fields were stored, the return value is <<0>>.
  388.  
  389.         <<scanf>> might stop scanning a particular field before
  390.         reaching the normal field end character, or may
  391.         terminate entirely.
  392.  
  393.         <<scanf>> stops scanning and storing the current field
  394.         and moves to the next input field (if any)
  395.         in any of the following situations:
  396.  
  397.         O+
  398.         o       The assignment suppressing character (<<*>>) appears
  399.         after the <<%>> in the format specification; the current
  400.         input field is scanned but not stored.
  401.  
  402.         o       <[width]> characters have been read (<[width]> is a
  403.         width specification, a positive decimal integer).
  404.  
  405.         o       The next character read cannot be converted
  406.         under the the current format (for example,
  407.         if a <<Z>> is read when the format is decimal).
  408.  
  409.         o       The next character in the input field does not appear
  410.         in the search set (or does appear in the inverted search set).
  411.         O-
  412.  
  413.         When <<scanf>> stops scanning the current input field for one of
  414.         these reasons, the next character is considered unread and
  415.         used as the first character of the following input field, or the
  416.         first character in a subsequent read operation on the input.
  417.  
  418.         <<scanf>> will terminate under the following circumstances:
  419.  
  420.         O+
  421.         o       The next character in the input field conflicts
  422.         with a corresponding non-whitespace character in the
  423.         format string.
  424.  
  425.         o       The next character in the input field is <<EOF>>.
  426.  
  427.         o       The format string has been exhausted.
  428.         O-
  429.  
  430.         When the format string contains a character sequence that is
  431.         not part of a format specification, the same character
  432.         sequence must appear in the input; <<scanf>> will
  433.         scan but not store the matched characters.  If a
  434.         conflict occurs, the first conflicting character remains in the input
  435.         as if it had never been read.
  436.  
  437. PORTABILITY
  438. <<scanf>> is ANSI C.
  439.  
  440. Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
  441. <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
  442. */
  443.  
  444. #include <_ansi.h>
  445. #include <reent.h>
  446. #include <stdio.h>
  447. #include <string.h>
  448. #ifdef _HAVE_STDC
  449. #include <stdarg.h>
  450. #else
  451. #include <varargs.h>
  452. #endif
  453. #include "local.h"
  454.  
  455. #ifndef _REENT_ONLY
  456.  
  457. #ifdef _HAVE_STDC
  458. int
  459. _DEFUN(sscanf, (str, fmt),
  460.        _CONST char *__restrict str _AND
  461.        _CONST char * fmt _DOTS)
  462. #else
  463. int
  464. sscanf(str, fmt, va_alist)
  465.        _CONST char *str;
  466.        _CONST char *fmt;
  467.        va_dcl
  468. #endif
  469. {
  470.   int ret;
  471.   va_list ap;
  472.   FILE f;
  473.  
  474.   f._flags = __SRD | __SSTR;
  475.   f._bf._base = f._p = (unsigned char *) str;
  476.   f._bf._size = f._r = strlen (str);
  477.   f._read = __seofread;
  478.   f._ub._base = NULL;
  479.   f._lb._base = NULL;
  480.   f._file = -1;  /* No file. */
  481. #ifdef _HAVE_STDC
  482.   va_start (ap, fmt);
  483. #else
  484.   va_start (ap);
  485. #endif
  486.   ret = __ssvfscanf_r (_REENT, &f, fmt, ap);
  487.   va_end (ap);
  488.   return ret;
  489. }
  490.  
  491. #ifdef _NANO_FORMATTED_IO
  492. int
  493. _EXFUN(siscanf, (const char *, const char *, ...)
  494.        _ATTRIBUTE ((__alias__("sscanf"))));
  495. #endif
  496.  
  497. #endif /* !_REENT_ONLY */
  498.  
  499. #ifdef _HAVE_STDC
  500. int
  501. _DEFUN(_sscanf_r, (ptr, str, fmt),
  502.        struct _reent *ptr _AND
  503.        _CONST char *__restrict str   _AND
  504.        _CONST char *__restrict fmt _DOTS)
  505. #else
  506. int
  507. _sscanf_r(ptr, str, fmt, va_alist)
  508.           struct _reent *ptr;
  509.           _CONST char *__restrict str;
  510.           _CONST char *__restrict fmt;
  511.           va_dcl
  512. #endif
  513. {
  514.   int ret;
  515.   va_list ap;
  516.   FILE f;
  517.  
  518.   f._flags = __SRD | __SSTR;
  519.   f._bf._base = f._p = (unsigned char *) str;
  520.   f._bf._size = f._r = strlen (str);
  521.   f._read = __seofread;
  522.   f._ub._base = NULL;
  523.   f._lb._base = NULL;
  524.   f._file = -1;  /* No file. */
  525. #ifdef _HAVE_STDC
  526.   va_start (ap, fmt);
  527. #else
  528.   va_start (ap);
  529. #endif
  530.   ret = __ssvfscanf_r (ptr, &f, fmt, ap);
  531.   va_end (ap);
  532.   return ret;
  533. }
  534.  
  535. #ifdef _NANO_FORMATTED_IO
  536. int
  537. _EXFUN(_siscanf_r, (struct _reent *, const char *, const char *, ...)
  538.        _ATTRIBUTE ((__alias__("_sscanf_r"))));
  539. #endif
  540.