$ 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 ...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backslas...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backslas...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backslas...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backslas...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backslas...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backslas...
[$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backsla...
[$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143   fmt = format = *argv;144   chopped = escape(fmt, 1, &len);      /* backsla...
[$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {185     static const c...
[$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {185     static const c...
[$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {                      ...
[$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {247     convch = *fmt;...
[abcde$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {247     convch = ...
[abcde$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {                 ...
[abcde]                                                                [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034149     w...
[abcde]                                                                                   [$ printf "[%s]n[%2.2f]n[%%]n" a...
[abcde]                                                                        [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.0341...
[abcde]                                                                        [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.0341...
[abcde]                                                                        [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.0341...
[abcde]                                                                        [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.0341...
[abcde]                                                                        [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.0341...
[abcde]                                                      [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *18...
[abcde]                                                                                   [$ printf "[%s]n[%2.2f]n[%%]n" a...
[abcde]                                                                                               [1.03$ printf "[%s]n...
[abcde]                                                                   [1.03$ printf "[%s]n[%2.2f]n[%%]n" abcdf 1.03418...
[abcde]                                                                [1.03]                                             ...
[abcde]                                                               [1.03]                                              ...
[abcde]                                                                [1.03]                                             ...
[abcde]                                                         [1.03]                                                    ...
$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034147   for (;;) {148       start = fmt;174       /* Restart at the beginning of th...
177     }178     /* NOTREACHED */179 }
Printf
Printf
Printf
Printf
Printf
Printf
Printf
Printf
Printf
Upcoming SlideShare
Loading in...5
×

Printf

1,068

Published on

コマンドなんでも読書会用資料

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,068
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Printf

  1. 1. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034[abcde][1.03][%]
  2. 2. $ 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);
  3. 3. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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 } else164 fmt++;165 }166167 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 */
  4. 4. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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 } else164 fmt++;165 }166167 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 */
  5. 5. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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] == %) {153154 /* %% prints a % */ putchar(%); [%s]n[%2.2f]n[%%]n155 fmt += 2;156 } else {157158 fmt = doformat(fmt, &rval); if (fmt == NULL) 21159 return (1);160 end = 0;161 }162 start = fmt;163 } else [%s]n164 fmt++;165 } [%2.2f]n166167 if (end == 1) { [%%]n168 warnx1("missing format character", NULL, NULL);169 return (1);170 }171 fwrite(start, 1, fmt - start, stdout);172 if (chopped || !*gargv)173 return (rval); 18174 /* Restart at the beginning of the format string. */175 fmt = format;176 end = 1;177 }178 /* NOTREACHED */
  6. 6. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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 } else164 fmt++;165 }166167 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 */
  7. 7. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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 } else164 fmt++;165 }166167 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 */
  8. 8. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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)159160 return (1); end = 0; start:char *161162 } start = fmt; [%s]n[%2.2f]n[%%]n163 } else164 fmt++;165 } fmt:char *166167 if (end == 1) { [%s]n[%2.2f]n[%%]n168 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 */
  9. 9. [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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[%%]n152 if (fmt[1] == %) {153 /* %% prints a % */154 putchar(%);155 fmt += 2;156157 } else { fmt = doformat(fmt, &rval); fmt:char *158 if (fmt == NULL) [%s]n[%2.2f]n[%%]n159 return (1);160 end = 0;161 }162 start = fmt; fmt - start == 1163 } else164 fmt++;165 }166 fwrite(167 if (end == 1) {168 warnx1("missing format character", NULL, NULL); “[%s]n[%2.2f]n[%%]n”,169170 } return (1); 1, 1, stdout171172 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 */
  10. 10. [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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[%%]n150 if (fmt[0] == %) {151 fwrite(start, 1, fmt - start, stdout);152 if (fmt[1] == %) {153 /* %% prints a % */ L157154 putchar(%);155 fmt += 2; fmt = doformat(156 } else {157 fmt = doformat(fmt, &rval); fmt, &rval158 if (fmt == NULL)159 return (1); );160 end = 0;161 }162 start = fmt;163 } else164 fmt++;165 }166167 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 */
  11. 11. [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {185 static const char skip1[] = "#-+ 0";186 static const char skip2[] = "0123456789"; %hogehoge191 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;201202 /* 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;215216 /* skip to conversion char */217 fmt += strspn(fmt, skip2);218 }219 } else220 haveprec = 0;
  12. 12. [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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[%%]n195 if (getint(&fieldwidth))196 return (NULL);197 havewidth = 1;198 ++fmt; fmt:char *199200 } else { havewidth = 0; [%s]n[%2.2f]n[%%]n201202 /* 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;215216 /* skip to conversion char */217 fmt += strspn(fmt, skip2);218 }219 } else220 haveprec = 0;
  13. 13. [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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[%%]n254 int getout;255285 case s: { start:char *286287 const char *p; [%s0n[%2.2f]n[%%]n288 p = getstr();289 PF(start, p);290 break;291 }
  14. 14. [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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;255285 case s: {286 const char *p;287288 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 }
  15. 15. [abcde$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {247 convch = *fmt;248 nextch = *++fmt;249 *fmt = 0;250251 switch (convch) { case b: { [%s0n[%2.2f]n[%%]n252 size_t len;253 char *p;254 int getout;255285 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)
  16. 16. [abcde$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 { nextch:char == ‘]’285 case s: {286 const char *p;287288 p = getstr(); fmt:char *289 PF(start, p);290 break; [%s0n[%2.2f]n[%%]n291 } ! ! !330 *fmt = nextch;331 return (fmt); [%s]n[%2.2f]n[%%]n332 }157 fmt = doformat(fmt, &rval);158 if (fmt == NULL)159 return (1); [%s]n[%2.2f]n[%%]n160 end = 0;161 }162 start = fmt; ! ! ! ! fmt:char * [%s]n[%2.2f]n[%%]n start:char * [%s]n[%2.2f]n[%%]n
  17. 17. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034149 while (fmt < format + len) {150 if (fmt[0] == %) {151 fwrite(start, 1, fmt - start, stdout);152 if (fmt[1] == %) {153 /* %% prints a % */ fmt:char *154155 putchar(%); fmt += 2; [%s]n[%2.2f]n[%%]n156 } else {157 fmt = doformat(fmt, &rval);158159 if (fmt == NULL) return (1); start:char *160 end = 0; [%s]n[%2.2f]n[%%]n161 }162 start = fmt;163 } else164 fmt++; fwrite(165 } start, // ]n[%2... 1, // 1 fmt - start, // 3 stdout ); => “]n[“
  18. 18. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034143 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[%%]n150 if (fmt[0] == %) {151 fwrite(start, 1, fmt - start, stdout);152 if (fmt[1] == %) {153 /* %% prints a % */ L157154 putchar(%);155 fmt += 2; fmt = doformat(156 } else {157 fmt = doformat(fmt, &rval); fmt, &rval158 if (fmt == NULL)159 return (1); );160 end = 0;161 }162 start = fmt;163 } else164 fmt++;165 }166167 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. 19. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval) start:char *184 {185 static const char skip1[] = "#-+ 0"; [%s]n[%2.2f]n[%%]n186 static const char skip2[] = "0123456789";191 fmt = start + 1;192 /* skip to field width */ fmt:char *193194 fmt += strspn(fmt, skip1); if (*fmt == *) { [%s]n[%2.2f]n[%%]n195 if (getint(&fieldwidth))196 return (NULL);197 havewidth = 1;198 ++fmt;199 } else {200 havewidth = 0;201202 /* 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;215216 /* skip to conversion char */217 fmt += strspn(fmt, skip2);218 }219 } else220 haveprec = 0;
  20. 20. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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[%%]n191 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;201202 /* 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;215216 /* skip to conversion char */217 fmt += strspn(fmt, skip2);218 }219 } else220 haveprec = 0;
  21. 21. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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[%%]n194 if (*fmt == *) {195 if (getint(&fieldwidth))196 return (NULL);197 havewidth = 1;198 ++fmt;199 } else {200 havewidth = 0;201202 /* 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[%%]n211 haveprec = 1;212 ++fmt;213 } else {214 haveprec = 0;215216 /* skip to conversion char */217 fmt += strspn(fmt, skip2);218 }219 } else220 haveprec = 0;
  22. 22. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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);197198 havewidth = 1; ++fmt; fmt:char*199200 } else { havewidth = 0; [%s]n[%2.2f]n[%%]n201202 /* skip to possible ., get following precision */203 fmt += strspn(fmt, skip2);204 }205 if (*fmt == .) {206 /* precision present? */207 ++fmt;208209 if (*fmt == *) { if (getint(&precision)) fmt:char*210 return (NULL); [%s]n[%2.2f]n[%%]n211 haveprec = 1;212 ++fmt;213 } else {214 haveprec = 0;215216217 /* skip to conversion char */ fmt += strspn(fmt, skip2); fmt:start*218 } [%s]n[%2.2f]n[%%]n219 } else220 haveprec = 0;
  23. 23. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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;201202 /* skip to possible ., get following precision */203 fmt += strspn(fmt, skip2); fmt:char*204 }205 if (*fmt == .) { [%s]n[%2.2f]n[%%]n206 /* precision present? */207 ++fmt;208 if (*fmt == *) {209 if (getint(&precision))210 return (NULL);211 haveprec = 1;212 ++fmt;213214 } else { haveprec = 0; fmt:char*215216 /* skip to conversion char */ [%s]n[%2.2f]n[%%]n217 fmt += strspn(fmt, skip2);218 }219 } else220 haveprec = 0;
  24. 24. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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[%%]n254 int getout;255285 case s: { start:char *286287 const char *p; [%s0n[%2.2f0n[%%]n288 p = getstr();289 PF(start, p);290 break;291 }
  25. 25. [abcde] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 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;255309 case e: case E:310 case f: case F:311 case g: case G:312 case a: case A: {313 long double p;314315 if (getfloating(&p, mod_ldbl))316 *rval = 1;317 if (mod_ldbl)318 PF(start, p);319 else320 PF(start, (double)p);321 break; 507 static int322 } 508 getfloating(long double *dp, int mod_ldbl) 509 { 510 char *ep; 511 int rval; 512 526 *dp = strtod(*gargv, &ep);
  26. 26. [abcde] [1.03$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034182 static char *183 doformat(char *start, int *rval)184 {247 convch = *fmt;248 nextch = *++fmt;249 *fmt = 0; [%s]n[%2.2f0n[%%]n250 switch (convch) {251 case b: {252 size_t len;253 char *p;254 int getout;255309310 case case e: case E: f: case F: 1.034311 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)
  27. 27. [abcde] [1.03$ printf "[%s]n[%2.2f]n[%%]n" abcdf 1.034182 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[%%]n315 if (getfloating(&p, mod_ldbl))316 *rval = 1; ! ! !317 if (mod_ldbl)318 PF(start, p); [%s]n[%2.2f]n[%%]n319 else320 PF(start, (double)p);321 break;322 }330 *fmt = nextch;331 return (fmt); [%s]n[%2.2f]n[%%]n332 }157158 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
  28. 28. [abcde] [1.03] [$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034149 while (fmt < format + len) {150 if (fmt[0] == %) {151 fwrite(start, 1, fmt - start, stdout);152 if (fmt[1] == %) {153 /* %% prints a % */ fmt:char *154155 putchar(%); fmt += 2; [%s]n[%2.2f]n[%%]n156 } else {157 fmt = doformat(fmt, &rval);158159 if (fmt == NULL) return (1); fm160 end = 0; [%s]n[%2.2f]n[%%]n161 }162 start = fmt;163 } else164 fmt++; fwrite(165 } start, // ]n[%%... 1, // 1 fmt - start, // 3 stdout ); => “]n[“
  29. 29. [abcde] [1.03] [%$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034149 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[%%]n160 end = 0;161 }162163 start = fmt; } else start:char *164165 } fmt++; [%s]n[%2.2f]n[%%]n
  30. 30. [abcde] [1.03] [%]$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034149 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 }162163 start = fmt; } else start:char *164165 } fmt++; [%s]n[%2.2f]n[%%]n171 fwrite(start, 1, fmt - start, stdout);172 if (chopped || !*gargv)173 return (rval);
  31. 31. [abcde] [1.03] [%]$ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034149 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 } else164 fmt++;165 } 0171 fwrite(start, 1, fmt - start, stdout);172 if (chopped || !*gargv)173 return (rval);
  32. 32. $ printf "[%s]n[%2.2f]n[%%]n" abcde 1.034147 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
  33. 33. 177 }178 /* NOTREACHED */179 }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×