Manual Pages for Linux CentOS command on man va_copy
MyWebUniversity

Manual Pages for Linux CentOS command on man va_copy

STDARG(3) Linux Programmer's Manual STDARG(3)

NAME

stdarg, vastart, vaarg, vaend, vacopy - variable argument lists SYNOPSIS

#include void vastart(valist ap, last); type vaarg(valist ap, type); void vaend(valist ap); void vacopy(valist dest, valist src); DESCRIPTION A function may be called with a varying number of arguments of varying types. The include file declares a type valist and defines three macros for stepping through a list of arguments whose number and types are not known to the called function. The called function must declare an object of type valist which is used by the macros vastart(), vaarg(), and vaend(). vastart() The vastart() macro initializes ap for subsequent use by vaarg() and vaend(), and must be called first. The argument last is the name of the last argument before the variable argument list, that is, the last argument of which the calling function knows the type. Because the address of this argument may be used in the vastart() macro, it should not be declared as a register variable, or as a func‐ tion or an array type. vaarg() The vaarg() macro expands to an expression that has the type and value of the next argument in the call. The argument ap is the valist ap initialized by vastart(). Each call to vaarg() modifies ap so that the next call returns the next argument. The argument type is a type name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to type. The first use of the vaarg() macro after that of the vastart() macro returns the argument after last. Successive invocations return the values of the remaining arguments. If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), random errors will occur. If ap is passed to a function that uses vaarg(ap,type) then the value of ap is undefined after the return of that function. vaend() Each invocation of vastart() must be matched by a corresponding invo‐ cation of vaend() in the same function. After the call vaend(ap) the variable ap is undefined. Multiple traversals of the list, each brack‐ eted by vastart() and vaend() are possible. vaend() may be a macro or a function. vacopy() The vacopy() macro copies the (previously initialized) variable argu‐ ment list src to dest. The behavior is as if vastart() were applied to dest with the same last argument, followed by the same number of vaarg() invocations that was used to reach the current state of src. An obvious implementation would have a valist be a pointer to the stack frame of the variadic function. In such a setup (by far the most common) there seems nothing against an assignment valist aq = ap; Unfortunately, there are also systems that make it an array of pointers (of length 1), and there one needs valist aq; *aq = *ap; Finally, on systems where arguments are passed in registers, it may be necessary for vastart() to allocate memory, store the arguments there, and also an indication of which argument is next, so that vaarg() can step through the list. Now vaend() can free the allocated memory again. To accommodate this situation, C99 adds a macro vacopy(), so that the above assignment can be replaced by valist aq; vacopy(aq, ap); ... vaend(aq); Each invocation of vacopy() must be matched by a corresponding invoca‐ tion of vaend() in the same function. Some systems that do not supply vacopy() have vacopy instead, since that was the name used in the draft proposal. ATTRIBUTES For an explanation of the terms used in this section, see attributes(7). ┌──────────────────────┬───────────────┬─────────────────┐ │Interface │ Attribute │ Value │ ├──────────────────────┼───────────────┼─────────────────┤

│vastart(), vaend(), │ Thread safety │ MT-Safe │ │vacopy() │ │ │ ├──────────────────────┼───────────────┼─────────────────┤

│vaarg() │ Thread safety │ MT-Safe race:ap │ └──────────────────────┴───────────────┴─────────────────┘ CONFORMING TO The vastart(), vaarg(), and vaend() macros conform to C89. C99 defines the vacopy() macro. NOTES These macros are not compatible with the historic macros they replace.

A backward-compatible version can be found in the include file . The historic setup is:

#include void foo(vaalist) vadcl { valist ap; vastart(ap); while (...) { ... x = vaarg(ap, type); ... } vaend(ap); } On some systems, vaend contains a closing '}' matching a '{' in vastart, so that both macros must occur in the same function, and in a way that allows this. BUGS Unlike the varargs macros, the stdarg macros do not permit programmers to code a function with no fixed arguments. This problem generates work mainly when converting varargs code to stdarg code, but it also creates difficulties for variadic functions that wish to pass all of their arguments on to a function that takes a valist argument, such as vfprintf(3). EXAMPLE The function foo takes a string of format characters and prints out the argument associated with each format character based on the type.

#include

#include void foo(char *fmt, ...) { valist ap; int d; char c, *s; vastart(ap, fmt); while (*fmt) switch (*fmt++) { case 's': /* string */ s = vaarg(ap, char *);

printf("string %s\n", s); break; case 'd': /* int */ d = vaarg(ap, int);

printf("int %d\n", d); break; case 'c': /* char */ /* need a cast here since vaarg only takes fully promoted types */ c = (char) vaarg(ap, int);

printf("char %c\n", c); break; } vaend(ap); } COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

2013-03-15 STDARG(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™