@@ -99,8 +99,9 @@ static const char *get_int(const char *s, int *x)
99
99
}
100
100
101
101
/* apply sign to result */
102
- if (negative != 0 )
102
+ if (negative != 0 ) {
103
103
* x = - * x ;
104
+ }
104
105
105
106
return s ;
106
107
}
@@ -143,12 +144,14 @@ static const char *get_flags(const char *s, int *flags)
143
144
}
144
145
145
146
/* Spec says that '-' has a higher priority than '0' */
146
- if ((* flags & PRINT_FLAG_LEFT_JUSTIFY ) != 0 )
147
+ if ((* flags & PRINT_FLAG_LEFT_JUSTIFY ) != 0 ) {
147
148
* flags &= ~PRINT_FLAG_PAD_ZERO ;
149
+ }
148
150
149
151
/* Spec says that '+' has a higher priority than ' ' */
150
- if ((* flags & PRINT_FLAG_SIGN ) != 0 )
152
+ if ((* flags & PRINT_FLAG_SIGN ) != 0 ) {
151
153
* flags &= ~PRINT_FLAG_SPACE ;
154
+ }
152
155
153
156
return s ;
154
157
}
@@ -167,15 +170,15 @@ static const char *get_length_modifier(const char *s,
167
170
* flags |= PRINT_FLAG_SHORT ;
168
171
* mask = 0x0000FFFF ;
169
172
}
170
- }
173
+ } else if ( * s == 'l' ) {
171
174
/* check for l[l] (long/long long) */
172
- else if (* s == 'l' ) {
173
175
s ++ ;
174
176
if (* s == 'l' ) {
175
177
* flags |= PRINT_FLAG_LONG_LONG ;
176
178
++ s ;
177
- } else
179
+ } else {
178
180
* flags |= PRINT_FLAG_LONG ;
181
+ }
179
182
}
180
183
181
184
return s ;
@@ -199,12 +202,14 @@ static int format_number(struct print_param *param)
199
202
width = param -> vars .valuelen + param -> vars .prefixlen ;
200
203
201
204
/* calculate additional characters for precision */
202
- if ((uint32_t )(param -> vars .precision ) > width )
205
+ if ((uint32_t )(param -> vars .precision ) > width ) {
203
206
p = param -> vars .precision - width ;
207
+ }
204
208
205
209
/* calculate additional characters for width */
206
- if ((uint32_t )(param -> vars .width ) > (width + p ))
210
+ if ((uint32_t )(param -> vars .width ) > (width + p )) {
207
211
w = param -> vars .width - (width + p );
212
+ }
208
213
209
214
/* handle case of right justification */
210
215
if ((param -> vars .flags & PRINT_FLAG_LEFT_JUSTIFY ) == 0 ) {
@@ -243,21 +248,24 @@ static int format_number(struct print_param *param)
243
248
/* emit prefix (if any), return early in case of an error */
244
249
res = param -> emit (PRINT_CMD_COPY , param -> vars .prefix ,
245
250
param -> vars .prefixlen , param -> data );
246
- if ((param -> vars .prefix != NULL ) && (res < 0 ))
251
+ if ((param -> vars .prefix != NULL ) && (res < 0 )) {
247
252
return res ;
253
+ }
248
254
249
255
/* insert additional 0's for precision, return early if an error
250
256
* occurred
251
257
*/
252
258
res = param -> emit (PRINT_CMD_FILL , "0" , p , param -> data );
253
- if (res < 0 )
259
+ if (res < 0 ) {
254
260
return res ;
261
+ }
255
262
256
263
/* emit the pre-calculated result, return early in case of an error */
257
264
res = param -> emit (PRINT_CMD_COPY , param -> vars .value ,
258
265
param -> vars .valuelen , param -> data );
259
- if (res < 0 )
266
+ if (res < 0 ) {
260
267
return res ;
268
+ }
261
269
262
270
/* handle left justification */
263
271
if ((param -> vars .flags & PRINT_FLAG_LEFT_JUSTIFY ) != 0 ) {
@@ -415,40 +423,46 @@ static int print_string(struct print_param *param, const char *s)
415
423
/* we need the length of the string if either width or precision is
416
424
* given
417
425
*/
418
- if ((param -> vars .precision != 0 )|| (param -> vars .width != 0 ))
426
+ if ((param -> vars .precision != 0 )|| (param -> vars .width != 0 )) {
419
427
len = strnlen_s (s , PRINT_STRING_MAX_LEN );
428
+ }
420
429
421
430
/* precision gives the max. number of characters to emit. */
422
- if ((param -> vars .precision != 0 ) && (len > param -> vars .precision ))
431
+ if ((param -> vars .precision != 0 ) && (len > param -> vars .precision )) {
423
432
len = param -> vars .precision ;
433
+ }
424
434
425
435
/* calculate the number of additional characters to get the required
426
436
* width
427
437
*/
428
- if (param -> vars .width > 0 && param -> vars .width > len )
438
+ if (param -> vars .width > 0 && param -> vars .width > len ) {
429
439
w = param -> vars .width - len ;
440
+ }
430
441
431
442
/* emit additional characters for width, return early if an error
432
443
* occurred
433
444
*/
434
445
if ((param -> vars .flags & PRINT_FLAG_LEFT_JUSTIFY ) == 0 ) {
435
446
res = param -> emit (PRINT_CMD_FILL , " " , w , param -> data );
436
- if (res < 0 )
447
+ if (res < 0 ) {
437
448
return res ;
449
+ }
438
450
}
439
451
440
452
/* emit the string, return early if an error occurred */
441
453
res = param -> emit (PRINT_CMD_COPY , s , len , param -> data );
442
- if (res < 0 )
454
+ if (res < 0 ) {
443
455
return res ;
456
+ }
444
457
445
458
/* emit additional characters on the right, return early if an error
446
459
* occurred
447
460
*/
448
461
if ((param -> vars .flags & PRINT_FLAG_LEFT_JUSTIFY ) != 0 ) {
449
462
res = param -> emit (PRINT_CMD_FILL , " " , w , param -> data );
450
- if (res < 0 )
463
+ if (res < 0 ) {
451
464
return res ;
465
+ }
452
466
}
453
467
454
468
return res ;
@@ -479,8 +493,9 @@ int do_print(const char *fmt, struct print_param *param,
479
493
*/
480
494
res = param -> emit (PRINT_CMD_COPY , start , fmt - start ,
481
495
param -> data );
482
- if (res < 0 )
496
+ if (res < 0 ) {
483
497
return res ;
498
+ }
484
499
485
500
/* continue only if the '%' character was found */
486
501
if (* fmt == '%' ) {
@@ -518,9 +533,8 @@ int do_print(const char *fmt, struct print_param *param,
518
533
if (ch == '%' ) {
519
534
res = param -> emit (PRINT_CMD_COPY , & ch , 1 ,
520
535
param -> data );
521
- }
536
+ } else if (( ch == 'd' ) || ( ch == 'i' )) {
522
537
/* decimal number */
523
- else if ((ch == 'd' ) || (ch == 'i' )) {
524
538
res = print_decimal (param ,
525
539
((param -> vars .flags &
526
540
PRINT_FLAG_LONG_LONG ) != 0 ) ?
@@ -556,8 +570,9 @@ int do_print(const char *fmt, struct print_param *param,
556
570
}
557
571
/* hexadecimal number */
558
572
else if ((ch == 'X' ) || (ch == 'x' )) {
559
- if (ch == 'X' )
573
+ if (ch == 'X' ) {
560
574
param -> vars .flags |= PRINT_FLAG_UPPER ;
575
+ }
561
576
res = print_pow2 (param ,
562
577
((param -> vars .flags &
563
578
PRINT_FLAG_LONG_LONG ) != 0 ) ?
@@ -572,8 +587,9 @@ int do_print(const char *fmt, struct print_param *param,
572
587
else if (ch == 's' ) {
573
588
const char * s = __builtin_va_arg (args , char * );
574
589
575
- if (s == NULL )
590
+ if (s == NULL ) {
576
591
s = "(null)" ;
592
+ }
577
593
res = print_string (param , s );
578
594
}
579
595
/* pointer argument */
@@ -601,8 +617,9 @@ int do_print(const char *fmt, struct print_param *param,
601
617
}
602
618
}
603
619
/* return if an error occurred */
604
- if (res < 0 )
620
+ if (res < 0 ) {
605
621
return res ;
622
+ }
606
623
}
607
624
608
625
/* done. Return the result of the last emit function call */
@@ -622,17 +639,19 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
622
639
if (cmd == PRINT_CMD_COPY ) {
623
640
if (sz < 0 ) {
624
641
while ((* s ) != 0 ) {
625
- if (n < param -> sz - param -> wrtn )
642
+ if (n < param -> sz - param -> wrtn ) {
626
643
* p = * s ;
644
+ }
627
645
p ++ ;
628
646
s ++ ;
629
647
n ++ ;
630
648
}
631
649
632
650
} else if (sz > 0 ) {
633
651
while (((* s ) != 0 ) && n < sz ) {
634
- if (n < param -> sz - param -> wrtn )
652
+ if (n < param -> sz - param -> wrtn ) {
635
653
* p = * s ;
654
+ }
636
655
p ++ ;
637
656
s ++ ;
638
657
n ++ ;
@@ -678,14 +697,17 @@ int vsnprintf(char *dst, int sz, const char *fmt, va_list args)
678
697
param .data = & snparam ;
679
698
680
699
/* execute the printf() */
681
- if (do_print (fmt , & param , args ) < 0 )
700
+ if (do_print (fmt , & param , args ) < 0 ) {
682
701
return -1 ;
702
+ }
683
703
684
704
/* ensure the written string is NULL terminated */
685
- if (snparam .wrtn < sz )
705
+ if (snparam .wrtn < sz ) {
686
706
snparam .dst [snparam .wrtn ] = '\0' ;
687
- else
707
+ }
708
+ else {
688
709
snparam .dst [sz - 1 ] = '\0' ;
710
+ }
689
711
690
712
/* return the number of chars which would be written */
691
713
res = snparam .wrtn ;
0 commit comments