Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | --- stdarg.3.orig 2008-07-30 02:46:51.000000000 -0700 +++ stdarg.3 2008-07-30 04:06:35.000000000 -0700 @@ -74,13 +74,21 @@ .Pp The .Fn va_start -macro initializes -.Fa ap -for subsequent use by +macro must be called first, and it initializes +.Fa ap , +which can be passed to .Fn va_arg -and +for each argument to be processed. +Calling +.Fn va_end +signals that there are no further arguments, and causes +.Fa ap +to be invalidated. +Note that each call to +.Fn va_start +must be matched by a call to .Fn va_end , -and must be called first. +from within the same function. .Pp The parameter .Fa last @@ -93,10 +101,6 @@ function or an array type. .Pp The -.Fn va_start -macro returns no value. -.Pp -The .Fn va_arg macro expands to an expression that has the type and value of the next argument in the call. @@ -136,34 +140,38 @@ .Pp The .Fn va_copy -macro copies a variable argument list, previously initialized by +macro copies the state of the variable argument list, +.Fa src , +previously initialized by .Fn va_start , -from -.Fa src -to -.Fa dest . -The state is preserved such that it is equivalent to calling +to the variable argument list, +.Fa dest , +which must not have been previously initialized by +.Fn va_start , +without an intervening call to +.Fn va_end . +The state preserved in +.Fa dest +is equivalent to calling .Fn va_start -with the same second argument used with -.Fa src , -and calling +and .Fn va_arg -the same number of times as called with +on +.Fa dest +in the same way as was used on .Fa src . -.Pp -The -.Fn va_copy -macro returns no value. -.Pp -The +The copied variable argument list can subsequently be passed to +.Fn va_arg , +and must finally be passed to .Fn va_end -macro handles a normal return from the function whose variable argument -list was initialized by -.Fn va_start . +when through with it. .Pp -The -.Fn va_end -macro returns no value. +After a variable argument list is invalidated by +.Fn va_end , +it can be reinitialized with +.Fn va_start +or made a copy of another variable argument list with +.Fn va_copy . .Sh EXAMPLES The function .Em foo @@ -172,11 +180,12 @@ .Bd -literal -offset indent void foo(char *fmt, ...) { - va_list ap; + va_list ap, ap2; int d; char c, *s; va_start(ap, fmt); + va_copy(ap2, ap); while (*fmt) switch(*fmt++) { case 's': /* string */ @@ -194,6 +203,10 @@ break; } va_end(ap); + ... + /* use ap2 to iterate over the arguments again */ + ... + va_end(ap2); } .Ed .Sh COMPATIBILITY |