$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
[abcde]
[1.03]
[%]
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
[abcde]
[1.03]
[%]




[
abcde
]n[
1.03
]n[
%
]n




[       ...   L151   fwrite(start,   1,   fmt   - start, stdout);
abcde   ...   L289   PF(start, p);   =>   L87   (void)fputs(b, stdout);
]n[    ...   L151   fwrite(start,   1,   fmt   - start, stdout);
1.03    ...   L318   PF(start, p);   =>   L87   (void)fputs(b, stdout);
]n[    ...   L151   fwrite(start,   1,   fmt   - start, stdout);
%%      ...   L154   putchar('%');
]n     ...   L151   fwrite(start,   1, fmt - start, stdout);
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;
149       while (fmt < format + len) {
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */
154                    putchar('%');
155                    fmt += 2;
156               } else {
157                    fmt = doformat(fmt, &rval);
158                    if (fmt == NULL)
159                        return (1);
160                    end = 0;
161               }
162               start = fmt;
163           } else
164               fmt++;
165       }
166
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;
149       while (fmt < format + len) {
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */
154                    putchar('%');
155                    fmt += 2;
156               } else {
157                    fmt = doformat(fmt, &rval);
158                    if (fmt == NULL)
159                        return (1);
160                    end = 0;
161               }
162               start = fmt;
163           } else
164               fmt++;
165       }
166
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;
149       while (fmt < format + len) {
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153
154
                       /* %% prints a % */
                       putchar('%');
                                                                            [%s]n[%2.2f]n[%%]n
155                    fmt += 2;
156               } else {
157
158
                       fmt = doformat(fmt, &rval);
                       if (fmt == NULL)
                                                                            21
159                        return (1);
160                    end = 0;
161               }
162               start = fmt;
163           } else                                                         [%s]n
164               fmt++;
165       }                                                                  [%2.2f]n
166
167       if (end == 1) {                                                    [%%]n
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);                                                18
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;                                                      static char **gargv;
149       while (fmt < format + len) {
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */
154                    putchar('%');

                                                                                 “abcde”
155                    fmt += 2;
156               } else {
157                    fmt = doformat(fmt, &rval);
158                    if (fmt == NULL)
159                        return (1);
160                    end = 0;
161               }
162               start = fmt;
163           } else
164               fmt++;
165       }
166
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;
149       while (fmt < format + len) {
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */
154                    putchar('%');
155                    fmt += 2;
156               } else {
157                    fmt = doformat(fmt, &rval);
158                    if (fmt == NULL)
159                        return (1);
160                    end = 0;
161               }
162               start = fmt;
163           } else
164               fmt++;
165       }
166
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;
149       while (fmt < format + len) {
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */
154                    putchar('%');
155                    fmt += 2;
156               } else {
157                    fmt = doformat(fmt, &rval);
158                    if (fmt == NULL)
159
160
                           return (1);
                       end = 0;
                                                                                start:char *
161
162
                  }
                  start = fmt;
                                                                            [%s]n[%2.2f]n[%%]n
163           } else
164               fmt++;
165       }                                                                      fmt:char *
166
167       if (end == 1) {                                                   [%s]n[%2.2f]n[%%]n
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
[




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {
148       start = fmt;
149       while (fmt < format + len) {                                            start:char *
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);                      [%s]n[%2.2f]n[%%]n
152               if (fmt[1] == '%') {
153                    /* %% prints a % */
154                    putchar('%');
155                    fmt += 2;
156
157
                  } else {
                       fmt = doformat(fmt, &rval);
                                                                                   fmt:char *
158                    if (fmt == NULL)                                       [%s]n[%2.2f]n[%%]n
159                        return (1);
160                    end = 0;
161               }
162               start = fmt;                                                  fmt - start == 1
163           } else
164               fmt++;
165       }
166                                                                         fwrite(
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);                “[%s]n[%2.2f]n[%%]n”,
169
170       }
              return (1);
                                                                             1, 1, stdout
171
172
          fwrite(start, 1, fmt - start, stdout);
          if (chopped || !*gargv)
                                                                            );
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;                                                      => “[“
176       end = 1;
177   }
178   /* NOTREACHED */
[




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {                                                                  fmt:char *
148       start = fmt;
149       while (fmt < format + len) {                                       [%s]n[%2.2f]n[%%]n
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */                                  L157
154                    putchar('%');
155                    fmt += 2;                                              fmt = doformat(
156               } else {
157                    fmt = doformat(fmt, &rval);                                fmt, &rval
158                    if (fmt == NULL)
159                        return (1);                                        );
160                    end = 0;
161               }
162               start = fmt;
163           } else
164               fmt++;
165       }
166
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
[




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
185     static const char skip1[] = "#'-+ 0";
186     static const char skip2[] = "0123456789";                 %hogehoge
191     fmt = start + 1;
192     /* skip to field width */
193     fmt += strspn(fmt, skip1);
194     if (*fmt == '*') {
195         if (getint(&fieldwidth))
196              return (NULL);
197         havewidth = 1;
198         ++fmt;
199     } else {
200         havewidth = 0;
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);
204     }
205     if (*fmt == '.') {
206         /* precision present? */
207         ++fmt;
208         if (*fmt == '*') {
209             if (getint(&precision))
210                  return (NULL);
211             haveprec = 1;
212             ++fmt;
213         } else {
214             haveprec = 0;
215
216             /* skip to conversion char */
217             fmt += strspn(fmt, skip2);
218         }
219     } else
220         haveprec = 0;
[




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
185     static const char skip1[] = "#'-+ 0";
186     static const char skip2[] = "0123456789";

191     fmt = start + 1;
192     /* skip to field width */                                     start:char *
193     fmt += strspn(fmt, skip1);
194     if (*fmt == '*') {                                        [%s]n[%2.2f]n[%%]n
195         if (getint(&fieldwidth))
196              return (NULL);
197         havewidth = 1;
198         ++fmt;                                                     fmt:char *
199
200
        } else {
            havewidth = 0;
                                                                  [%s]n[%2.2f]n[%%]n
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);
204     }
205     if (*fmt == '.') {
206         /* precision present? */
207         ++fmt;
208         if (*fmt == '*') {
209             if (getint(&precision))
210                  return (NULL);
211             haveprec = 1;
212             ++fmt;
213         } else {
214             haveprec = 0;
215
216             /* skip to conversion char */
217             fmt += strspn(fmt, skip2);
218         }
219     } else
220         haveprec = 0;
[




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
                                                 convch:char == ‘s’
247     convch = *fmt;
248     nextch = *++fmt;                         nextch:char == ‘]’
249     *fmt = '0';

250     switch (convch) {
251     case 'b': {                                  fmt:char *
252         size_t len;
253         char *p;                           [%s0n[%2.2f]n[%%]n
254         int getout;
255

285     case 's': {
                                                    start:char *
286
287
            const char *p;                     [%s0n[%2.2f]n[%%]n
288         p = getstr();
289         PF(start, p);
290         break;
291     }
[




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {

247     convch = *fmt;
248     nextch = *++fmt;
249     *fmt = '0';

250     switch (convch) {
251     case 'b': {
252         size_t len;
253         char *p;
254         int getout;
255

285     case 's': {
286         const char *p;
287
288         p = getstr();
289         PF(start, p);
290         break;
291     }             442    static const char *
                      443    getstr(void)
                      444    {
                      445        if (!*gargv)
                      446            return ("");
                      447        return (*gargv++);
                      448    }
[abcde




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {

247     convch = *fmt;
248     nextch = *++fmt;
249     *fmt = '0';

250
251
        switch (convch) {
        case 'b': {
                                       [%s0n[%2.2f]n[%%]n
252         size_t len;
253         char *p;
254         int getout;
255

285     case 's': {
286         const char *p;
287                                                 “abcde”
288         p = getstr();
289         PF(start, p);
290         break;           75 #define PF(f, func) do { 
291     }                    76     char *b = NULL; 
                             77     if (havewidth) 
                             78         if (haveprec) 
                             79             (void)asprintf(&b, f, fieldwidth, precision, func); 
                             80         else 
                             81             (void)asprintf(&b, f, fieldwidth, func); 
                             82     else if (haveprec) 
                             83         (void)asprintf(&b, f, precision, func); 
                             84     else 
                             85         (void)asprintf(&b, f, func); 
                             86     if (b) { 
                             87
                             88
                                        (void)fputs(b, stdout); 
                                        free(b);                                            [abcde
                             89     } 
                             90 } while (0)
[abcde




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
                                                         nextch:char == ‘]’
285     case 's': {
286         const char *p;
287
288         p = getstr();                                    fmt:char *
289         PF(start, p);
290         break;                                     [%s0n[%2.2f]n[%%]n
291     }
                                                               ! ! !
330     *fmt = nextch;
331     return (fmt);                                  [%s]n[%2.2f]n[%%]n
332 }


157                      fmt = doformat(fmt, &rval);
158                      if (fmt == NULL)
159                          return (1);               [%s]n[%2.2f]n[%%]n
160                      end = 0;
161                }
162                start = fmt;
                                                          !   !            !   !


                                                            fmt:char *
                                                       [%s]n[%2.2f]n[%%]n
                                                           start:char *
                                                       [%s]n[%2.2f]n[%%]n
[abcde]
                                                                [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
149     while (fmt < format + len) {
150         if (fmt[0] == '%') {
151             fwrite(start, 1, fmt - start, stdout);
152             if (fmt[1] == '%') {
153                 /* %% prints a % */                       fmt:char *
154
155
                    putchar('%');
                    fmt += 2;
                                                         [%s]n[%2.2f]n[%%]n
156             } else {
157                 fmt = doformat(fmt, &rval);
158
159
                    if (fmt == NULL)
                         return (1);
                                                             start:char *
160                 end = 0;                             [%s]n[%2.2f]n[%%]n
161             }
162             start = fmt;
163         } else
164             fmt++;                                   fwrite(
165     }
                                                            start, // ]n[%2...
                                                            1,     // 1
                                                            fmt - start, // 3
                                                            stdout
                                                         );

                                                         => “]n[“
[abcde]
                                                                                   [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
143   fmt = format = *argv;
144   chopped = escape(fmt, 1, &len);      /* backslash interpretation */
145   rval = end = 0;
146   gargv = ++argv;
147   for (;;) {                                                                  fmt:char *
148       start = fmt;
149       while (fmt < format + len) {                                       [%s]n[%2.2f]n[%%]n
150           if (fmt[0] == '%') {
151               fwrite(start, 1, fmt - start, stdout);
152               if (fmt[1] == '%') {
153                    /* %% prints a % */                                  L157
154                    putchar('%');
155                    fmt += 2;                                              fmt = doformat(
156               } else {
157                    fmt = doformat(fmt, &rval);                                fmt, &rval
158                    if (fmt == NULL)
159                        return (1);                                        );
160                    end = 0;
161               }
162               start = fmt;
163           } else
164               fmt++;
165       }
166
167       if (end == 1) {
168           warnx1("missing format character", NULL, NULL);
169           return (1);
170       }
171       fwrite(start, 1, fmt - start, stdout);
172       if (chopped || !*gargv)
173           return (rval);
174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }
178   /* NOTREACHED */
[abcde]
                                                                        [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)                                  start:char *
184 {
185     static const char skip1[] = "#'-+ 0";                     [%s]n[%2.2f]n[%%]n
186     static const char skip2[] = "0123456789";

191     fmt = start + 1;
192     /* skip to field width */                                      fmt:char *
193
194
        fmt += strspn(fmt, skip1);
        if (*fmt == '*') {
                                                                  [%s]n[%2.2f]n[%%]n
195         if (getint(&fieldwidth))
196              return (NULL);
197         havewidth = 1;
198         ++fmt;
199     } else {
200         havewidth = 0;
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);
204     }
205     if (*fmt == '.') {
206         /* precision present? */
207         ++fmt;
208         if (*fmt == '*') {
209             if (getint(&precision))
210                  return (NULL);
211             haveprec = 1;
212             ++fmt;
213         } else {
214             haveprec = 0;
215
216             /* skip to conversion char */
217             fmt += strspn(fmt, skip2);
218         }
219     } else
220         haveprec = 0;
[abcde]
                                                                        [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
185     static const char skip1[] = "#'-+ 0";                          fmt:char *
186     static const char skip2[] = "0123456789";
                                                                  [%s]n[%2.2f]n[%%]n
191     fmt = start + 1;
192     /* skip to field width */
193     fmt += strspn(fmt, skip1);
194     if (*fmt == '*') {
195         if (getint(&fieldwidth))                                skip1 [#’-+ 0]
196              return (NULL);
197         havewidth = 1;
198         ++fmt;
199     } else {
200         havewidth = 0;
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);
204     }
205     if (*fmt == '.') {
206         /* precision present? */
207         ++fmt;
208         if (*fmt == '*') {
209             if (getint(&precision))
210                  return (NULL);
211             haveprec = 1;
212             ++fmt;
213         } else {
214             haveprec = 0;
215
216             /* skip to conversion char */
217             fmt += strspn(fmt, skip2);
218         }
219     } else
220         haveprec = 0;
[abcde]
                                                                        [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
185     static const char skip1[] = "#'-+ 0";
186     static const char skip2[] = "0123456789";

191     fmt = start + 1;                                                fmt:char*
192     /* skip to field width */
193     fmt += strspn(fmt, skip1);                                [%s]n[%2.2f]n[%%]n
194     if (*fmt == '*') {
195         if (getint(&fieldwidth))
196              return (NULL);
197         havewidth = 1;
198         ++fmt;
199     } else {
200         havewidth = 0;
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);
204     }
205     if (*fmt == '.') {
206         /* precision present? */
207         ++fmt;
208         if (*fmt == '*') {                                          fmt:char*
209             if (getint(&precision))
210                  return (NULL);                               [%s]n[%2.2f]n[%%]n
211             haveprec = 1;
212             ++fmt;
213         } else {
214             haveprec = 0;
215
216             /* skip to conversion char */
217             fmt += strspn(fmt, skip2);
218         }
219     } else
220         haveprec = 0;
[abcde]
                                                                        [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
185     static const char skip1[] = "#'-+ 0";
186     static const char skip2[] = "0123456789";

191     fmt = start + 1;
192     /* skip to field width */
193     fmt += strspn(fmt, skip1);
194     if (*fmt == '*') {
195         if (getint(&fieldwidth))
196              return (NULL);
197
198
            havewidth = 1;
            ++fmt;
                                                                        fmt:char*
199
200
        } else {
            havewidth = 0;
                                                                  [%s]n[%2.2f]n[%%]n
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);
204     }
205     if (*fmt == '.') {
206         /* precision present? */
207         ++fmt;
208
209
            if (*fmt == '*') {
                if (getint(&precision))
                                                                        fmt:char*
210                  return (NULL);                               [%s]n[%2.2f]n[%%]n
211             haveprec = 1;
212             ++fmt;
213         } else {
214             haveprec = 0;
215
216
217
                /* skip to conversion char */
                fmt += strspn(fmt, skip2);
                                                                       fmt:start*
218         }                                                     [%s]n[%2.2f]n[%%]n
219     } else
220         haveprec = 0;
[abcde]
                                                                        [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
185     static const char skip1[] = "#'-+ 0";
186     static const char skip2[] = "0123456789";

191     fmt = start + 1;
192     /* skip to field width */
193     fmt += strspn(fmt, skip1);
194     if (*fmt == '*') {
195         if (getint(&fieldwidth))
196              return (NULL);
197         havewidth = 1;
198         ++fmt;
199     } else {
200         havewidth = 0;
201
202         /* skip to possible '.', get following precision */
203         fmt += strspn(fmt, skip2);                                  fmt:char*
204     }
205     if (*fmt == '.') {                                        [%s]n[%2.2f]n[%%]n
206         /* precision present? */
207         ++fmt;
208         if (*fmt == '*') {
209             if (getint(&precision))
210                  return (NULL);
211             haveprec = 1;
212             ++fmt;
213
214
            } else {
                haveprec = 0;
                                                                        fmt:char*
215
216             /* skip to conversion char */
                                                                  [%s]n[%2.2f]n[%%]n
217             fmt += strspn(fmt, skip2);
218         }
219     } else
220         haveprec = 0;
[abcde]
                                                      [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
                                                 convch:char == ‘f’
247     convch = *fmt;
248     nextch = *++fmt;                         nextch:char == ‘]’
249     *fmt = '0';

250     switch (convch) {
251     case 'b': {                                  fmt:char *
252         size_t len;
253         char *p;                           [%s]n[%2.2f0n[%%]n
254         int getout;
255

285     case 's': {
                                                    start:char *
286
287
            const char *p;                     [%s0n[%2.2f0n[%%]n
288         p = getstr();
289         PF(start, p);
290         break;
291     }
[abcde]
                                                                                   [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {

247     convch = *fmt;
248     nextch = *++fmt;
249     *fmt = '0';

250     switch (convch) {
251     case 'b': {
252         size_t len;
253         char *p;
254         int getout;
255

309     case    'e': case 'E':
310     case    'f': case 'F':
311     case    'g': case 'G':
312     case    'a': case 'A': {
313            long double p;
314
315            if (getfloating(&p, mod_ldbl))
316                 *rval = 1;
317            if (mod_ldbl)
318                 PF(start, p);
319            else
320                 PF(start, (double)p);
321            break;                     507 static int
322     }                                 508 getfloating(long double *dp, int
                                          mod_ldbl)
                                          509 {
                                          510     char *ep;
                                          511     int rval;
                                          512

                                          526         *dp = strtod(*gargv, &ep);
[abcde]
                                                                                               [1.03




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {

247     convch = *fmt;
248     nextch = *++fmt;
249     *fmt = '0';
                                   [%s]n[%2.2f0n[%%]n
250     switch (convch) {
251     case 'b': {
252         size_t len;
253         char *p;
254         int getout;
255

309
310
        case
        case
                'e': case 'E':
                'f': case 'F':
                                                1.034
311     case    'g': case 'G':
312     case    'a': case 'A': {
313            long double p;                     75 #define PF(f, func) do { 
314                                               76     char *b = NULL; 
315            if (getfloating(&p, mod_ldbl))     77     if (havewidth) 
316                 *rval = 1;                    78         if (haveprec) 
317            if (mod_ldbl)                      79             (void)asprintf(&b, f, fieldwidth, precision, func); 
318                 PF(start, p);                 80         else 
319            else                               81             (void)asprintf(&b, f, fieldwidth, func); 
320                 PF(start, (double)p);         82     else if (haveprec) 
321            break;                             83         (void)asprintf(&b, f, precision, func); 
322     }                                         84     else 
                                                  85         (void)asprintf(&b, f, func); 
                                                  86     if (b) { 
                                                  87         (void)fputs(b, stdout); 
                                                  88         free(b); 
                                                  89     } 
                                                  90 } while (0)
[abcde]
                                                                   [1.03




$ printf "[%s]n[%2.2f]n[%%]n" abcdf 1.034
182 static char *
183 doformat(char *start, int *rval)
184 {
                                                          nextch:char == ‘]’
309     case    'e': case 'E':
310     case    'f': case 'F':
311     case    'g': case 'G':
312     case    'a': case 'A': {                              fmt:char *
313            long double p;
314                                                     [%s]n[%2.2f0n[%%]n
315            if (getfloating(&p, mod_ldbl))
316                 *rval = 1;                                  ! ! !
317            if (mod_ldbl)
318                 PF(start, p);                       [%s]n[%2.2f]n[%%]n
319            else
320                 PF(start, (double)p);
321            break;
322     }
330     *fmt = nextch;
331     return (fmt);                                   [%s]n[%2.2f]n[%%]n
332 }


157
158
                          fmt = doformat(fmt, &rval);
                          if (fmt == NULL)
                                                           !   !             !   !
159                           return (1);
160                       end = 0;
161                   }
162                   start = fmt;                           fmt:char *
                                                        [%s]n[%2.2f]n[%%]n
                                                            start:char *
                                                        [%s]n[%2.2f]n[%%]n
[abcde]
                                                                [1.03]
                                                                [




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
149     while (fmt < format + len) {
150         if (fmt[0] == '%') {
151             fwrite(start, 1, fmt - start, stdout);
152             if (fmt[1] == '%') {
153                 /* %% prints a % */                       fmt:char *
154
155
                    putchar('%');
                    fmt += 2;
                                                         [%s]n[%2.2f]n[%%]n
156             } else {
157                 fmt = doformat(fmt, &rval);
158
159
                    if (fmt == NULL)
                         return (1);
                                                                  fm
160                 end = 0;                             [%s]n[%2.2f]n[%%]n
161             }
162             start = fmt;
163         } else
164             fmt++;                                   fwrite(
165     }
                                                            start, // ]n[%%...
                                                            1,     // 1
                                                            fmt - start, // 3
                                                            stdout
                                                         );

                                                         => “]n[“
[abcde]
                                                               [1.03]
                                                               [%




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
149     while (fmt < format + len) {
150         if (fmt[0] == '%') {
151             fwrite(start, 1, fmt - start, stdout);
152             if (fmt[1] == '%') {
153                 /* %% prints a % */
154                 putchar('%');
155                 fmt += 2;
156             } else {
157                 fmt = doformat(fmt, &rval);               fmt:char *
158                 if (fmt == NULL)
159                      return (1);                     [%s]n[%2.2f]n[%%]n
160                 end = 0;
161             }
162
163
                start = fmt;
            } else
                                                             start:char *
164
165     }
                fmt++;
                                                         [%s]n[%2.2f]n[%%]n
[abcde]
                                                                [1.03]
                                                                [%]




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
149     while (fmt < format + len) {
150         if (fmt[0] == '%') {
151             fwrite(start, 1, fmt - start, stdout);
152             if (fmt[1] == '%') {
153                 /* %% prints a % */
154                 putchar('%');
155                 fmt += 2;
156             } else {
157                 fmt = doformat(fmt, &rval);                fmt:char *
158                 if (fmt == NULL)
159                      return (1);                     [%s]n[%2.2f]n[%%]n?
160                 end = 0;
161             }
162
163
                start = fmt;
            } else
                                                             start:char *
164
165     }
                fmt++;
                                                         [%s]n[%2.2f]n[%%]n
171     fwrite(start, 1, fmt - start, stdout);
172     if (chopped || !*gargv)
173         return (rval);
[abcde]
                                                         [1.03]
                                                         [%]




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034
149     while (fmt < format + len) {
150         if (fmt[0] == '%') {
151             fwrite(start, 1, fmt - start, stdout);
152             if (fmt[1] == '%') {
153                 /* %% prints a % */
154                 putchar('%');
155                 fmt += 2;
156             } else {
157                 fmt = doformat(fmt, &rval);
158                 if (fmt == NULL)
159                      return (1);
160                 end = 0;
161             }
162             start = fmt;
163         } else
164             fmt++;
165     }
                                                               0
171     fwrite(start, 1, fmt - start, stdout);
172     if (chopped || !*gargv)
173         return (rval);
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034




147   for (;;) {
148       start = fmt;

174       /* Restart at the beginning of the format string. */
175       fmt = format;
176       end = 1;
177   }




$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 fghij 23.4
[abcde]
1.03
[fghij]
23.40
177     }
178     /* NOTREACHED */
179 }
Printf

Printf

  • 9.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 [abcde] [1.03] [%]
  • 10.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 [abcde] [1.03] [%] [ abcde ]n[ 1.03 ]n[ % ]n [ ... L151 fwrite(start, 1, fmt - start, stdout); abcde ... L289 PF(start, p); => L87 (void)fputs(b, stdout); ]n[ ... L151 fwrite(start, 1, fmt - start, stdout); 1.03 ... L318 PF(start, p); => L87 (void)fputs(b, stdout); ]n[ ... L151 fwrite(start, 1, fmt - start, stdout); %% ... L154 putchar('%'); ]n ... L151 fwrite(start, 1, fmt - start, stdout);
  • 11.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 return (1); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 166 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 12.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 return (1); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 166 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 13.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 154 /* %% prints a % */ putchar('%'); [%s]n[%2.2f]n[%%]n 155 fmt += 2; 156 } else { 157 158 fmt = doformat(fmt, &rval); if (fmt == NULL) 21 159 return (1); 160 end = 0; 161 } 162 start = fmt; 163 } else [%s]n 164 fmt++; 165 } [%2.2f]n 166 167 if (end == 1) { [%%]n 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 18 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 14.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; static char **gargv; 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); “abcde” 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 return (1); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 166 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 15.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 return (1); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 166 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 16.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 160 return (1); end = 0; start:char * 161 162 } start = fmt; [%s]n[%2.2f]n[%%]n 163 } else 164 fmt++; 165 } fmt:char * 166 167 if (end == 1) { [%s]n[%2.2f]n[%%]n 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 17.
    [ $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { 148 start = fmt; 149 while (fmt < format + len) { start:char * 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); [%s]n[%2.2f]n[%%]n 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 157 } else { fmt = doformat(fmt, &rval); fmt:char * 158 if (fmt == NULL) [%s]n[%2.2f]n[%%]n 159 return (1); 160 end = 0; 161 } 162 start = fmt; fmt - start == 1 163 } else 164 fmt++; 165 } 166 fwrite( 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); “[%s]n[%2.2f]n[%%]n”, 169 170 } return (1); 1, 1, stdout 171 172 fwrite(start, 1, fmt - start, stdout); if (chopped || !*gargv) ); 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; => “[“ 176 end = 1; 177 } 178 /* NOTREACHED */
  • 18.
    [ $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { fmt:char * 148 start = fmt; 149 while (fmt < format + len) { [%s]n[%2.2f]n[%%]n 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ L157 154 putchar('%'); 155 fmt += 2; fmt = doformat( 156 } else { 157 fmt = doformat(fmt, &rval); fmt, &rval 158 if (fmt == NULL) 159 return (1); ); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 166 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 19.
    [ $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 185 static const char skip1[] = "#'-+ 0"; 186 static const char skip2[] = "0123456789"; %hogehoge 191 fmt = start + 1; 192 /* skip to field width */ 193 fmt += strspn(fmt, skip1); 194 if (*fmt == '*') { 195 if (getint(&fieldwidth)) 196 return (NULL); 197 havewidth = 1; 198 ++fmt; 199 } else { 200 havewidth = 0; 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); 204 } 205 if (*fmt == '.') { 206 /* precision present? */ 207 ++fmt; 208 if (*fmt == '*') { 209 if (getint(&precision)) 210 return (NULL); 211 haveprec = 1; 212 ++fmt; 213 } else { 214 haveprec = 0; 215 216 /* skip to conversion char */ 217 fmt += strspn(fmt, skip2); 218 } 219 } else 220 haveprec = 0;
  • 20.
    [ $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 185 static const char skip1[] = "#'-+ 0"; 186 static const char skip2[] = "0123456789"; 191 fmt = start + 1; 192 /* skip to field width */ start:char * 193 fmt += strspn(fmt, skip1); 194 if (*fmt == '*') { [%s]n[%2.2f]n[%%]n 195 if (getint(&fieldwidth)) 196 return (NULL); 197 havewidth = 1; 198 ++fmt; fmt:char * 199 200 } else { havewidth = 0; [%s]n[%2.2f]n[%%]n 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); 204 } 205 if (*fmt == '.') { 206 /* precision present? */ 207 ++fmt; 208 if (*fmt == '*') { 209 if (getint(&precision)) 210 return (NULL); 211 haveprec = 1; 212 ++fmt; 213 } else { 214 haveprec = 0; 215 216 /* skip to conversion char */ 217 fmt += strspn(fmt, skip2); 218 } 219 } else 220 haveprec = 0;
  • 21.
    [ $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { convch:char == ‘s’ 247 convch = *fmt; 248 nextch = *++fmt; nextch:char == ‘]’ 249 *fmt = '0'; 250 switch (convch) { 251 case 'b': { fmt:char * 252 size_t len; 253 char *p; [%s0n[%2.2f]n[%%]n 254 int getout; 255 285 case 's': { start:char * 286 287 const char *p; [%s0n[%2.2f]n[%%]n 288 p = getstr(); 289 PF(start, p); 290 break; 291 }
  • 22.
    [ $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 247 convch = *fmt; 248 nextch = *++fmt; 249 *fmt = '0'; 250 switch (convch) { 251 case 'b': { 252 size_t len; 253 char *p; 254 int getout; 255 285 case 's': { 286 const char *p; 287 288 p = getstr(); 289 PF(start, p); 290 break; 291 } 442 static const char * 443 getstr(void) 444 { 445 if (!*gargv) 446 return (""); 447 return (*gargv++); 448 }
  • 23.
    [abcde $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 247 convch = *fmt; 248 nextch = *++fmt; 249 *fmt = '0'; 250 251 switch (convch) { case 'b': { [%s0n[%2.2f]n[%%]n 252 size_t len; 253 char *p; 254 int getout; 255 285 case 's': { 286 const char *p; 287 “abcde” 288 p = getstr(); 289 PF(start, p); 290 break; 75 #define PF(f, func) do { 291 } 76 char *b = NULL; 77 if (havewidth) 78 if (haveprec) 79 (void)asprintf(&b, f, fieldwidth, precision, func); 80 else 81 (void)asprintf(&b, f, fieldwidth, func); 82 else if (haveprec) 83 (void)asprintf(&b, f, precision, func); 84 else 85 (void)asprintf(&b, f, func); 86 if (b) { 87 88 (void)fputs(b, stdout); free(b); [abcde 89 } 90 } while (0)
  • 24.
    [abcde $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { nextch:char == ‘]’ 285 case 's': { 286 const char *p; 287 288 p = getstr(); fmt:char * 289 PF(start, p); 290 break; [%s0n[%2.2f]n[%%]n 291 } ! ! ! 330 *fmt = nextch; 331 return (fmt); [%s]n[%2.2f]n[%%]n 332 } 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 return (1); [%s]n[%2.2f]n[%%]n 160 end = 0; 161 } 162 start = fmt; ! ! ! ! fmt:char * [%s]n[%2.2f]n[%%]n start:char * [%s]n[%2.2f]n[%%]n
  • 25.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ fmt:char * 154 155 putchar('%'); fmt += 2; [%s]n[%2.2f]n[%%]n 156 } else { 157 fmt = doformat(fmt, &rval); 158 159 if (fmt == NULL) return (1); start:char * 160 end = 0; [%s]n[%2.2f]n[%%]n 161 } 162 start = fmt; 163 } else 164 fmt++; fwrite( 165 } start, // ]n[%2... 1, // 1 fmt - start, // 3 stdout ); => “]n[“
  • 26.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 143 fmt = format = *argv; 144 chopped = escape(fmt, 1, &len); /* backslash interpretation */ 145 rval = end = 0; 146 gargv = ++argv; 147 for (;;) { fmt:char * 148 start = fmt; 149 while (fmt < format + len) { [%s]n[%2.2f]n[%%]n 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ L157 154 putchar('%'); 155 fmt += 2; fmt = doformat( 156 } else { 157 fmt = doformat(fmt, &rval); fmt, &rval 158 if (fmt == NULL) 159 return (1); ); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 166 167 if (end == 1) { 168 warnx1("missing format character", NULL, NULL); 169 return (1); 170 } 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval); 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } 178 /* NOTREACHED */
  • 27.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) start:char * 184 { 185 static const char skip1[] = "#'-+ 0"; [%s]n[%2.2f]n[%%]n 186 static const char skip2[] = "0123456789"; 191 fmt = start + 1; 192 /* skip to field width */ fmt:char * 193 194 fmt += strspn(fmt, skip1); if (*fmt == '*') { [%s]n[%2.2f]n[%%]n 195 if (getint(&fieldwidth)) 196 return (NULL); 197 havewidth = 1; 198 ++fmt; 199 } else { 200 havewidth = 0; 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); 204 } 205 if (*fmt == '.') { 206 /* precision present? */ 207 ++fmt; 208 if (*fmt == '*') { 209 if (getint(&precision)) 210 return (NULL); 211 haveprec = 1; 212 ++fmt; 213 } else { 214 haveprec = 0; 215 216 /* skip to conversion char */ 217 fmt += strspn(fmt, skip2); 218 } 219 } else 220 haveprec = 0;
  • 28.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 185 static const char skip1[] = "#'-+ 0"; fmt:char * 186 static const char skip2[] = "0123456789"; [%s]n[%2.2f]n[%%]n 191 fmt = start + 1; 192 /* skip to field width */ 193 fmt += strspn(fmt, skip1); 194 if (*fmt == '*') { 195 if (getint(&fieldwidth)) skip1 [#’-+ 0] 196 return (NULL); 197 havewidth = 1; 198 ++fmt; 199 } else { 200 havewidth = 0; 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); 204 } 205 if (*fmt == '.') { 206 /* precision present? */ 207 ++fmt; 208 if (*fmt == '*') { 209 if (getint(&precision)) 210 return (NULL); 211 haveprec = 1; 212 ++fmt; 213 } else { 214 haveprec = 0; 215 216 /* skip to conversion char */ 217 fmt += strspn(fmt, skip2); 218 } 219 } else 220 haveprec = 0;
  • 29.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 185 static const char skip1[] = "#'-+ 0"; 186 static const char skip2[] = "0123456789"; 191 fmt = start + 1; fmt:char* 192 /* skip to field width */ 193 fmt += strspn(fmt, skip1); [%s]n[%2.2f]n[%%]n 194 if (*fmt == '*') { 195 if (getint(&fieldwidth)) 196 return (NULL); 197 havewidth = 1; 198 ++fmt; 199 } else { 200 havewidth = 0; 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); 204 } 205 if (*fmt == '.') { 206 /* precision present? */ 207 ++fmt; 208 if (*fmt == '*') { fmt:char* 209 if (getint(&precision)) 210 return (NULL); [%s]n[%2.2f]n[%%]n 211 haveprec = 1; 212 ++fmt; 213 } else { 214 haveprec = 0; 215 216 /* skip to conversion char */ 217 fmt += strspn(fmt, skip2); 218 } 219 } else 220 haveprec = 0;
  • 30.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 185 static const char skip1[] = "#'-+ 0"; 186 static const char skip2[] = "0123456789"; 191 fmt = start + 1; 192 /* skip to field width */ 193 fmt += strspn(fmt, skip1); 194 if (*fmt == '*') { 195 if (getint(&fieldwidth)) 196 return (NULL); 197 198 havewidth = 1; ++fmt; fmt:char* 199 200 } else { havewidth = 0; [%s]n[%2.2f]n[%%]n 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); 204 } 205 if (*fmt == '.') { 206 /* precision present? */ 207 ++fmt; 208 209 if (*fmt == '*') { if (getint(&precision)) fmt:char* 210 return (NULL); [%s]n[%2.2f]n[%%]n 211 haveprec = 1; 212 ++fmt; 213 } else { 214 haveprec = 0; 215 216 217 /* skip to conversion char */ fmt += strspn(fmt, skip2); fmt:start* 218 } [%s]n[%2.2f]n[%%]n 219 } else 220 haveprec = 0;
  • 31.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 185 static const char skip1[] = "#'-+ 0"; 186 static const char skip2[] = "0123456789"; 191 fmt = start + 1; 192 /* skip to field width */ 193 fmt += strspn(fmt, skip1); 194 if (*fmt == '*') { 195 if (getint(&fieldwidth)) 196 return (NULL); 197 havewidth = 1; 198 ++fmt; 199 } else { 200 havewidth = 0; 201 202 /* skip to possible '.', get following precision */ 203 fmt += strspn(fmt, skip2); fmt:char* 204 } 205 if (*fmt == '.') { [%s]n[%2.2f]n[%%]n 206 /* precision present? */ 207 ++fmt; 208 if (*fmt == '*') { 209 if (getint(&precision)) 210 return (NULL); 211 haveprec = 1; 212 ++fmt; 213 214 } else { haveprec = 0; fmt:char* 215 216 /* skip to conversion char */ [%s]n[%2.2f]n[%%]n 217 fmt += strspn(fmt, skip2); 218 } 219 } else 220 haveprec = 0;
  • 32.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { convch:char == ‘f’ 247 convch = *fmt; 248 nextch = *++fmt; nextch:char == ‘]’ 249 *fmt = '0'; 250 switch (convch) { 251 case 'b': { fmt:char * 252 size_t len; 253 char *p; [%s]n[%2.2f0n[%%]n 254 int getout; 255 285 case 's': { start:char * 286 287 const char *p; [%s0n[%2.2f0n[%%]n 288 p = getstr(); 289 PF(start, p); 290 break; 291 }
  • 33.
    [abcde] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 247 convch = *fmt; 248 nextch = *++fmt; 249 *fmt = '0'; 250 switch (convch) { 251 case 'b': { 252 size_t len; 253 char *p; 254 int getout; 255 309 case 'e': case 'E': 310 case 'f': case 'F': 311 case 'g': case 'G': 312 case 'a': case 'A': { 313 long double p; 314 315 if (getfloating(&p, mod_ldbl)) 316 *rval = 1; 317 if (mod_ldbl) 318 PF(start, p); 319 else 320 PF(start, (double)p); 321 break; 507 static int 322 } 508 getfloating(long double *dp, int mod_ldbl) 509 { 510 char *ep; 511 int rval; 512 526 *dp = strtod(*gargv, &ep);
  • 34.
    [abcde] [1.03 $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { 247 convch = *fmt; 248 nextch = *++fmt; 249 *fmt = '0'; [%s]n[%2.2f0n[%%]n 250 switch (convch) { 251 case 'b': { 252 size_t len; 253 char *p; 254 int getout; 255 309 310 case case 'e': case 'E': 'f': case 'F': 1.034 311 case 'g': case 'G': 312 case 'a': case 'A': { 313 long double p; 75 #define PF(f, func) do { 314 76 char *b = NULL; 315 if (getfloating(&p, mod_ldbl)) 77 if (havewidth) 316 *rval = 1; 78 if (haveprec) 317 if (mod_ldbl) 79 (void)asprintf(&b, f, fieldwidth, precision, func); 318 PF(start, p); 80 else 319 else 81 (void)asprintf(&b, f, fieldwidth, func); 320 PF(start, (double)p); 82 else if (haveprec) 321 break; 83 (void)asprintf(&b, f, precision, func); 322 } 84 else 85 (void)asprintf(&b, f, func); 86 if (b) { 87 (void)fputs(b, stdout); 88 free(b); 89 } 90 } while (0)
  • 35.
    [abcde] [1.03 $ printf "[%s]n[%2.2f]n[%%]n" abcdf 1.034 182 static char * 183 doformat(char *start, int *rval) 184 { nextch:char == ‘]’ 309 case 'e': case 'E': 310 case 'f': case 'F': 311 case 'g': case 'G': 312 case 'a': case 'A': { fmt:char * 313 long double p; 314 [%s]n[%2.2f0n[%%]n 315 if (getfloating(&p, mod_ldbl)) 316 *rval = 1; ! ! ! 317 if (mod_ldbl) 318 PF(start, p); [%s]n[%2.2f]n[%%]n 319 else 320 PF(start, (double)p); 321 break; 322 } 330 *fmt = nextch; 331 return (fmt); [%s]n[%2.2f]n[%%]n 332 } 157 158 fmt = doformat(fmt, &rval); if (fmt == NULL) ! ! ! ! 159 return (1); 160 end = 0; 161 } 162 start = fmt; fmt:char * [%s]n[%2.2f]n[%%]n start:char * [%s]n[%2.2f]n[%%]n
  • 36.
    [abcde] [1.03] [ $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ fmt:char * 154 155 putchar('%'); fmt += 2; [%s]n[%2.2f]n[%%]n 156 } else { 157 fmt = doformat(fmt, &rval); 158 159 if (fmt == NULL) return (1); fm 160 end = 0; [%s]n[%2.2f]n[%%]n 161 } 162 start = fmt; 163 } else 164 fmt++; fwrite( 165 } start, // ]n[%%... 1, // 1 fmt - start, // 3 stdout ); => “]n[“
  • 37.
    [abcde] [1.03] [% $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); fmt:char * 158 if (fmt == NULL) 159 return (1); [%s]n[%2.2f]n[%%]n 160 end = 0; 161 } 162 163 start = fmt; } else start:char * 164 165 } fmt++; [%s]n[%2.2f]n[%%]n
  • 38.
    [abcde] [1.03] [%] $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); fmt:char * 158 if (fmt == NULL) 159 return (1); [%s]n[%2.2f]n[%%]n? 160 end = 0; 161 } 162 163 start = fmt; } else start:char * 164 165 } fmt++; [%s]n[%2.2f]n[%%]n 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval);
  • 39.
    [abcde] [1.03] [%] $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 149 while (fmt < format + len) { 150 if (fmt[0] == '%') { 151 fwrite(start, 1, fmt - start, stdout); 152 if (fmt[1] == '%') { 153 /* %% prints a % */ 154 putchar('%'); 155 fmt += 2; 156 } else { 157 fmt = doformat(fmt, &rval); 158 if (fmt == NULL) 159 return (1); 160 end = 0; 161 } 162 start = fmt; 163 } else 164 fmt++; 165 } 0 171 fwrite(start, 1, fmt - start, stdout); 172 if (chopped || !*gargv) 173 return (rval);
  • 40.
    $ printf "[%s]n[%2.2f]n[%%]n"abcde 1.034 147 for (;;) { 148 start = fmt; 174 /* Restart at the beginning of the format string. */ 175 fmt = format; 176 end = 1; 177 } $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034 fghij 23.4 [abcde] 1.03 [fghij] 23.40
  • 41.
    177 } 178 /* NOTREACHED */ 179 }