+
+ sign = get_sign(&num, flags);
+ if (sign)
+ --field_width;
+
+ s = number(tmp_end, num, base, flags & SMALL);
+ len = tmp_end - s;
+ /* default precision is 1 */
+ if (precision < 0)
+ precision = 1;
+ /* precision is minimum number of digits to print */
+ if (precision < len)
+ precision = len;
+ if (flags & SPECIAL) {
+ /*
+ * For octal, a leading 0 is printed only if necessary,
+ * i.e. if it's not already there because of the
+ * precision.
+ */
+ if (base == 8 && precision == len)
+ ++precision;
+ /*
+ * For hexadecimal, the leading 0x is skipped if the
+ * output is empty, i.e. both the number and the
+ * precision are 0.
+ */
+ if (base == 16 && precision > 0)
+ field_width -= 2;
+ else
+ flags &= ~SPECIAL;
+ }
+ /*
+ * For zero padding, increase the precision to fill the field
+ * width.
+ */
+ if ((flags & ZEROPAD) && field_width > precision)
+ precision = field_width;
+
+output:
+ /* Calculate the padding necessary */
+ field_width -= precision;
+ /* Leading padding with ' ' */
+ if (!(flags & LEFT))
+ while (field_width-- > 0)
+ *str++ = ' ';
+ /* sign */
+ if (sign)
+ *str++ = sign;
+ /* 0x/0X for hexadecimal */
+ if (flags & SPECIAL) {
+ *str++ = '0';
+ *str++ = 'X' | (flags & SMALL);
+ }
+ /* Zero padding and excess precision */
+ while (precision-- > len)
+ *str++ = '0';
+ /* Actual output */
+ while (len-- > 0)
+ *str++ = *s++;
+ /* Trailing padding with ' ' */
+ while (field_width-- > 0)
+ *str++ = ' ';