PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
snprintf.c File Reference
#include "c.h"
#include <math.h>
Include dependency graph for snprintf.c:

Go to the source code of this file.

Data Structures

struct  PrintfTarget
 
union  PrintfArgValue
 

Macros

#define PG_NL_ARGMAX   31
 

Enumerations

enum  PrintfArgType {
  ATYPE_NONE = 0 , ATYPE_INT , ATYPE_LONG , ATYPE_LONGLONG ,
  ATYPE_DOUBLE , ATYPE_CHARPTR
}
 

Functions

static void flushbuffer (PrintfTarget *target)
 
static void dopr (PrintfTarget *target, const char *format, va_list args)
 
int pg_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
 
int pg_snprintf (char *str, size_t count, const char *fmt,...)
 
int pg_vsprintf (char *str, const char *fmt, va_list args)
 
int pg_sprintf (char *str, const char *fmt,...)
 
int pg_vfprintf (FILE *stream, const char *fmt, va_list args)
 
int pg_fprintf (FILE *stream, const char *fmt,...)
 
int pg_vprintf (const char *fmt, va_list args)
 
int pg_printf (const char *fmt,...)
 
static bool find_arguments (const char *format, va_list args, PrintfArgValue *argvalues)
 
static void fmtstr (const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
 
static void fmtptr (const void *value, PrintfTarget *target)
 
static void fmtint (long long value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
 
static void fmtchar (int value, int leftjust, int minlen, PrintfTarget *target)
 
static void fmtfloat (double value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
 
static void dostr (const char *str, int slen, PrintfTarget *target)
 
static void dopr_outch (int c, PrintfTarget *target)
 
static void dopr_outchmulti (int c, int slen, PrintfTarget *target)
 
static int adjust_sign (int is_negative, int forcesign, int *signvalue)
 
static int compute_padlen (int minlen, int vallen, int leftjust)
 
static void leading_pad (int zpad, int signvalue, int *padlen, PrintfTarget *target)
 
static void trailing_pad (int padlen, PrintfTarget *target)
 
static const char * strchrnul (const char *s, int c)
 
int pg_strfromd (char *str, size_t count, int precision, double value)
 

Macro Definition Documentation

◆ PG_NL_ARGMAX

#define PG_NL_ARGMAX   31

Definition at line 44 of file snprintf.c.

Enumeration Type Documentation

◆ PrintfArgType

Enumerator
ATYPE_NONE 
ATYPE_INT 
ATYPE_LONG 
ATYPE_LONGLONG 
ATYPE_DOUBLE 
ATYPE_CHARPTR 

Definition at line 142 of file snprintf.c.

143 {
144  ATYPE_NONE = 0,
145  ATYPE_INT,
146  ATYPE_LONG,
148  ATYPE_DOUBLE,
150 } PrintfArgType;
PrintfArgType
Definition: snprintf.c:143
@ ATYPE_LONGLONG
Definition: snprintf.c:147
@ ATYPE_INT
Definition: snprintf.c:145
@ ATYPE_LONG
Definition: snprintf.c:146
@ ATYPE_CHARPTR
Definition: snprintf.c:149
@ ATYPE_NONE
Definition: snprintf.c:144
@ ATYPE_DOUBLE
Definition: snprintf.c:148

Function Documentation

◆ adjust_sign()

static int adjust_sign ( int  is_negative,
int  forcesign,
int *  signvalue 
)
static

Definition at line 1467 of file snprintf.c.

1468 {
1469  if (is_negative)
1470  {
1471  *signvalue = '-';
1472  return true;
1473  }
1474  else if (forcesign)
1475  *signvalue = '+';
1476  return false;
1477 }

Referenced by fmtfloat(), and fmtint().

◆ compute_padlen()

static int compute_padlen ( int  minlen,
int  vallen,
int  leftjust 
)
static

Definition at line 1481 of file snprintf.c.

1482 {
1483  int padlen;
1484 
1485  padlen = minlen - vallen;
1486  if (padlen < 0)
1487  padlen = 0;
1488  if (leftjust)
1489  padlen = -padlen;
1490  return padlen;
1491 }

Referenced by fmtchar(), fmtfloat(), fmtint(), and fmtstr().

◆ dopr()

static void dopr ( PrintfTarget target,
const char *  format,
va_list  args 
)
static

Definition at line 376 of file snprintf.c.

377 {
378  int save_errno = errno;
379  const char *first_pct = NULL;
380  int ch;
381  bool have_dollar;
382  bool have_star;
383  bool afterstar;
384  int accum;
385  int longlongflag;
386  int longflag;
387  int pointflag;
388  int leftjust;
389  int fieldwidth;
390  int precision;
391  int zpad;
392  int forcesign;
393  int fmtpos;
394  int cvalue;
395  long long numvalue;
396  double fvalue;
397  const char *strvalue;
398  PrintfArgValue argvalues[PG_NL_ARGMAX + 1];
399 
400  /*
401  * Initially, we suppose the format string does not use %n$. The first
402  * time we come to a conversion spec that has that, we'll call
403  * find_arguments() to check for consistent use of %n$ and fill the
404  * argvalues array with the argument values in the correct order.
405  */
406  have_dollar = false;
407 
408  while (*format != '\0')
409  {
410  /* Locate next conversion specifier */
411  if (*format != '%')
412  {
413  /* Scan to next '%' or end of string */
414  const char *next_pct = strchrnul(format + 1, '%');
415 
416  /* Dump literal data we just scanned over */
417  dostr(format, next_pct - format, target);
418  if (target->failed)
419  break;
420 
421  if (*next_pct == '\0')
422  break;
423  format = next_pct;
424  }
425 
426  /*
427  * Remember start of first conversion spec; if we find %n$, then it's
428  * sufficient for find_arguments() to start here, without rescanning
429  * earlier literal text.
430  */
431  if (first_pct == NULL)
432  first_pct = format;
433 
434  /* Process conversion spec starting at *format */
435  format++;
436 
437  /* Fast path for conversion spec that is exactly %s */
438  if (*format == 's')
439  {
440  format++;
441  strvalue = va_arg(args, char *);
442  if (strvalue == NULL)
443  strvalue = "(null)";
444  dostr(strvalue, strlen(strvalue), target);
445  if (target->failed)
446  break;
447  continue;
448  }
449 
450  fieldwidth = precision = zpad = leftjust = forcesign = 0;
451  longflag = longlongflag = pointflag = 0;
452  fmtpos = accum = 0;
453  have_star = afterstar = false;
454 nextch2:
455  ch = *format++;
456  switch (ch)
457  {
458  case '-':
459  leftjust = 1;
460  goto nextch2;
461  case '+':
462  forcesign = 1;
463  goto nextch2;
464  case '0':
465  /* set zero padding if no nonzero digits yet */
466  if (accum == 0 && !pointflag)
467  zpad = '0';
468  /* FALL THRU */
469  case '1':
470  case '2':
471  case '3':
472  case '4':
473  case '5':
474  case '6':
475  case '7':
476  case '8':
477  case '9':
478  accum = accum * 10 + (ch - '0');
479  goto nextch2;
480  case '.':
481  if (have_star)
482  have_star = false;
483  else
484  fieldwidth = accum;
485  pointflag = 1;
486  accum = 0;
487  goto nextch2;
488  case '*':
489  if (have_dollar)
490  {
491  /*
492  * We'll process value after reading n$. Note it's OK to
493  * assume have_dollar is set correctly, because in a valid
494  * format string the initial % must have had n$ if * does.
495  */
496  afterstar = true;
497  }
498  else
499  {
500  /* fetch and process value now */
501  int starval = va_arg(args, int);
502 
503  if (pointflag)
504  {
505  precision = starval;
506  if (precision < 0)
507  {
508  precision = 0;
509  pointflag = 0;
510  }
511  }
512  else
513  {
514  fieldwidth = starval;
515  if (fieldwidth < 0)
516  {
517  leftjust = 1;
518  fieldwidth = -fieldwidth;
519  }
520  }
521  }
522  have_star = true;
523  accum = 0;
524  goto nextch2;
525  case '$':
526  /* First dollar sign? */
527  if (!have_dollar)
528  {
529  /* Yup, so examine all conversion specs in format */
530  if (!find_arguments(first_pct, args, argvalues))
531  goto bad_format;
532  have_dollar = true;
533  }
534  if (afterstar)
535  {
536  /* fetch and process star value */
537  int starval = argvalues[accum].i;
538 
539  if (pointflag)
540  {
541  precision = starval;
542  if (precision < 0)
543  {
544  precision = 0;
545  pointflag = 0;
546  }
547  }
548  else
549  {
550  fieldwidth = starval;
551  if (fieldwidth < 0)
552  {
553  leftjust = 1;
554  fieldwidth = -fieldwidth;
555  }
556  }
557  afterstar = false;
558  }
559  else
560  fmtpos = accum;
561  accum = 0;
562  goto nextch2;
563  case 'l':
564  if (longflag)
565  longlongflag = 1;
566  else
567  longflag = 1;
568  goto nextch2;
569  case 'z':
570 #if SIZEOF_SIZE_T == 8
571 #ifdef HAVE_LONG_INT_64
572  longflag = 1;
573 #elif defined(HAVE_LONG_LONG_INT_64)
574  longlongflag = 1;
575 #else
576 #error "Don't know how to print 64bit integers"
577 #endif
578 #else
579  /* assume size_t is same size as int */
580 #endif
581  goto nextch2;
582  case 'h':
583  case '\'':
584  /* ignore these */
585  goto nextch2;
586  case 'd':
587  case 'i':
588  if (!have_star)
589  {
590  if (pointflag)
591  precision = accum;
592  else
593  fieldwidth = accum;
594  }
595  if (have_dollar)
596  {
597  if (longlongflag)
598  numvalue = argvalues[fmtpos].ll;
599  else if (longflag)
600  numvalue = argvalues[fmtpos].l;
601  else
602  numvalue = argvalues[fmtpos].i;
603  }
604  else
605  {
606  if (longlongflag)
607  numvalue = va_arg(args, long long);
608  else if (longflag)
609  numvalue = va_arg(args, long);
610  else
611  numvalue = va_arg(args, int);
612  }
613  fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
614  precision, pointflag, target);
615  break;
616  case 'o':
617  case 'u':
618  case 'x':
619  case 'X':
620  if (!have_star)
621  {
622  if (pointflag)
623  precision = accum;
624  else
625  fieldwidth = accum;
626  }
627  if (have_dollar)
628  {
629  if (longlongflag)
630  numvalue = (unsigned long long) argvalues[fmtpos].ll;
631  else if (longflag)
632  numvalue = (unsigned long) argvalues[fmtpos].l;
633  else
634  numvalue = (unsigned int) argvalues[fmtpos].i;
635  }
636  else
637  {
638  if (longlongflag)
639  numvalue = (unsigned long long) va_arg(args, long long);
640  else if (longflag)
641  numvalue = (unsigned long) va_arg(args, long);
642  else
643  numvalue = (unsigned int) va_arg(args, int);
644  }
645  fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
646  precision, pointflag, target);
647  break;
648  case 'c':
649  if (!have_star)
650  {
651  if (pointflag)
652  precision = accum;
653  else
654  fieldwidth = accum;
655  }
656  if (have_dollar)
657  cvalue = (unsigned char) argvalues[fmtpos].i;
658  else
659  cvalue = (unsigned char) va_arg(args, int);
660  fmtchar(cvalue, leftjust, fieldwidth, target);
661  break;
662  case 's':
663  if (!have_star)
664  {
665  if (pointflag)
666  precision = accum;
667  else
668  fieldwidth = accum;
669  }
670  if (have_dollar)
671  strvalue = argvalues[fmtpos].cptr;
672  else
673  strvalue = va_arg(args, char *);
674  /* If string is NULL, silently substitute "(null)" */
675  if (strvalue == NULL)
676  strvalue = "(null)";
677  fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
678  target);
679  break;
680  case 'p':
681  /* fieldwidth/leftjust are ignored ... */
682  if (have_dollar)
683  strvalue = argvalues[fmtpos].cptr;
684  else
685  strvalue = va_arg(args, char *);
686  fmtptr((const void *) strvalue, target);
687  break;
688  case 'e':
689  case 'E':
690  case 'f':
691  case 'g':
692  case 'G':
693  if (!have_star)
694  {
695  if (pointflag)
696  precision = accum;
697  else
698  fieldwidth = accum;
699  }
700  if (have_dollar)
701  fvalue = argvalues[fmtpos].d;
702  else
703  fvalue = va_arg(args, double);
704  fmtfloat(fvalue, ch, forcesign, leftjust,
705  fieldwidth, zpad,
706  precision, pointflag,
707  target);
708  break;
709  case 'm':
710  {
711  char errbuf[PG_STRERROR_R_BUFLEN];
712  const char *errm = strerror_r(save_errno,
713  errbuf, sizeof(errbuf));
714 
715  dostr(errm, strlen(errm), target);
716  }
717  break;
718  case '%':
719  dopr_outch('%', target);
720  break;
721  default:
722 
723  /*
724  * Anything else --- in particular, '\0' indicating end of
725  * format string --- is bogus.
726  */
727  goto bad_format;
728  }
729 
730  /* Check for failure after each conversion spec */
731  if (target->failed)
732  break;
733  }
734 
735  return;
736 
737 bad_format:
738  errno = EINVAL;
739  target->failed = true;
740 }
int i
Definition: isn.c:72
static char format
#define PG_STRERROR_R_BUFLEN
Definition: port.h:256
#define strerror_r
Definition: port.h:255
static void dostr(const char *str, int slen, PrintfTarget *target)
Definition: snprintf.c:1377
static void dopr_outch(int c, PrintfTarget *target)
Definition: snprintf.c:1414
static void fmtint(long long value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
Definition: snprintf.c:1010
static void fmtptr(const void *value, PrintfTarget *target)
Definition: snprintf.c:996
static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
Definition: snprintf.c:1121
static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth, int pointflag, PrintfTarget *target)
Definition: snprintf.c:967
#define PG_NL_ARGMAX
Definition: snprintf.c:44
static void fmtfloat(double value, char type, int forcesign, int leftjust, int minlen, int zpad, int precision, int pointflag, PrintfTarget *target)
Definition: snprintf.c:1139
static bool find_arguments(const char *format, va_list args, PrintfArgValue *argvalues)
Definition: snprintf.c:749
static const char * strchrnul(const char *s, int c)
Definition: snprintf.c:350
bool failed
Definition: snprintf.c:133
long long ll
Definition: snprintf.c:156
char * cptr
Definition: snprintf.c:158

References generate_unaccent_rules::args, PrintfArgValue::cptr, PrintfArgValue::d, dopr_outch(), dostr(), PrintfTarget::failed, find_arguments(), fmtchar(), fmtfloat(), fmtint(), fmtptr(), fmtstr(), format, i, PrintfArgValue::i, PrintfArgValue::l, PrintfArgValue::ll, PG_NL_ARGMAX, PG_STRERROR_R_BUFLEN, strchrnul(), and strerror_r.

Referenced by pg_vfprintf(), pg_vsnprintf(), and pg_vsprintf().

◆ dopr_outch()

static void dopr_outch ( int  c,
PrintfTarget target 
)
static

Definition at line 1414 of file snprintf.c.

1415 {
1416  if (target->bufend != NULL && target->bufptr >= target->bufend)
1417  {
1418  /* buffer full, can we dump to stream? */
1419  if (target->stream == NULL)
1420  {
1421  target->nchars++; /* no, lose the data */
1422  return;
1423  }
1424  flushbuffer(target);
1425  }
1426  *(target->bufptr++) = c;
1427 }
char * c
static void flushbuffer(PrintfTarget *target)
Definition: snprintf.c:298
char * bufend
Definition: snprintf.c:129
char * bufptr
Definition: snprintf.c:127
FILE * stream
Definition: snprintf.c:131

References PrintfTarget::bufend, PrintfTarget::bufptr, flushbuffer(), PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by dopr(), dopr_outchmulti(), dostr(), fmtchar(), leading_pad(), and pg_strfromd().

◆ dopr_outchmulti()

static void dopr_outchmulti ( int  c,
int  slen,
PrintfTarget target 
)
static

Definition at line 1430 of file snprintf.c.

1431 {
1432  /* fast path for common case of slen == 1 */
1433  if (slen == 1)
1434  {
1435  dopr_outch(c, target);
1436  return;
1437  }
1438 
1439  while (slen > 0)
1440  {
1441  int avail;
1442 
1443  if (target->bufend != NULL)
1444  avail = target->bufend - target->bufptr;
1445  else
1446  avail = slen;
1447  if (avail <= 0)
1448  {
1449  /* buffer full, can we dump to stream? */
1450  if (target->stream == NULL)
1451  {
1452  target->nchars += slen; /* no, lose the data */
1453  return;
1454  }
1455  flushbuffer(target);
1456  continue;
1457  }
1458  avail = Min(avail, slen);
1459  memset(target->bufptr, c, avail);
1460  target->bufptr += avail;
1461  slen -= avail;
1462  }
1463 }
#define Min(x, y)
Definition: c.h:1009

References PrintfTarget::bufend, PrintfTarget::bufptr, dopr_outch(), flushbuffer(), Min, PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by fmtchar(), fmtfloat(), fmtint(), fmtstr(), leading_pad(), and trailing_pad().

◆ dostr()

static void dostr ( const char *  str,
int  slen,
PrintfTarget target 
)
static

Definition at line 1377 of file snprintf.c.

1378 {
1379  /* fast path for common case of slen == 1 */
1380  if (slen == 1)
1381  {
1382  dopr_outch(*str, target);
1383  return;
1384  }
1385 
1386  while (slen > 0)
1387  {
1388  int avail;
1389 
1390  if (target->bufend != NULL)
1391  avail = target->bufend - target->bufptr;
1392  else
1393  avail = slen;
1394  if (avail <= 0)
1395  {
1396  /* buffer full, can we dump to stream? */
1397  if (target->stream == NULL)
1398  {
1399  target->nchars += slen; /* no, lose the data */
1400  return;
1401  }
1402  flushbuffer(target);
1403  continue;
1404  }
1405  avail = Min(avail, slen);
1406  memmove(target->bufptr, str, avail);
1407  target->bufptr += avail;
1408  str += avail;
1409  slen -= avail;
1410  }
1411 }
const char * str

References PrintfTarget::bufend, PrintfTarget::bufptr, dopr_outch(), flushbuffer(), Min, PrintfTarget::nchars, str, and PrintfTarget::stream.

Referenced by dopr(), fmtfloat(), fmtint(), fmtptr(), fmtstr(), and pg_strfromd().

◆ find_arguments()

static bool find_arguments ( const char *  format,
va_list  args,
PrintfArgValue argvalues 
)
static

Definition at line 749 of file snprintf.c.

751 {
752  int ch;
753  bool afterstar;
754  int accum;
755  int longlongflag;
756  int longflag;
757  int fmtpos;
758  int i;
759  int last_dollar = 0; /* Init to "no dollar arguments known" */
760  PrintfArgType argtypes[PG_NL_ARGMAX + 1] = {0};
761 
762  /*
763  * This loop must accept the same format strings as the one in dopr().
764  * However, we don't need to analyze them to the same level of detail.
765  *
766  * Since we're only called if there's a dollar-type spec somewhere, we can
767  * fail immediately if we find a non-dollar spec. Per the C99 standard,
768  * all argument references in the format string must be one or the other.
769  */
770  while (*format != '\0')
771  {
772  /* Locate next conversion specifier */
773  if (*format != '%')
774  {
775  /* Unlike dopr, we can just quit if there's no more specifiers */
776  format = strchr(format + 1, '%');
777  if (format == NULL)
778  break;
779  }
780 
781  /* Process conversion spec starting at *format */
782  format++;
783  longflag = longlongflag = 0;
784  fmtpos = accum = 0;
785  afterstar = false;
786 nextch1:
787  ch = *format++;
788  switch (ch)
789  {
790  case '-':
791  case '+':
792  goto nextch1;
793  case '0':
794  case '1':
795  case '2':
796  case '3':
797  case '4':
798  case '5':
799  case '6':
800  case '7':
801  case '8':
802  case '9':
803  accum = accum * 10 + (ch - '0');
804  goto nextch1;
805  case '.':
806  accum = 0;
807  goto nextch1;
808  case '*':
809  if (afterstar)
810  return false; /* previous star missing dollar */
811  afterstar = true;
812  accum = 0;
813  goto nextch1;
814  case '$':
815  if (accum <= 0 || accum > PG_NL_ARGMAX)
816  return false;
817  if (afterstar)
818  {
819  if (argtypes[accum] &&
820  argtypes[accum] != ATYPE_INT)
821  return false;
822  argtypes[accum] = ATYPE_INT;
823  last_dollar = Max(last_dollar, accum);
824  afterstar = false;
825  }
826  else
827  fmtpos = accum;
828  accum = 0;
829  goto nextch1;
830  case 'l':
831  if (longflag)
832  longlongflag = 1;
833  else
834  longflag = 1;
835  goto nextch1;
836  case 'z':
837 #if SIZEOF_SIZE_T == 8
838 #ifdef HAVE_LONG_INT_64
839  longflag = 1;
840 #elif defined(HAVE_LONG_LONG_INT_64)
841  longlongflag = 1;
842 #else
843 #error "Don't know how to print 64bit integers"
844 #endif
845 #else
846  /* assume size_t is same size as int */
847 #endif
848  goto nextch1;
849  case 'h':
850  case '\'':
851  /* ignore these */
852  goto nextch1;
853  case 'd':
854  case 'i':
855  case 'o':
856  case 'u':
857  case 'x':
858  case 'X':
859  if (fmtpos)
860  {
861  PrintfArgType atype;
862 
863  if (longlongflag)
864  atype = ATYPE_LONGLONG;
865  else if (longflag)
866  atype = ATYPE_LONG;
867  else
868  atype = ATYPE_INT;
869  if (argtypes[fmtpos] &&
870  argtypes[fmtpos] != atype)
871  return false;
872  argtypes[fmtpos] = atype;
873  last_dollar = Max(last_dollar, fmtpos);
874  }
875  else
876  return false; /* non-dollar conversion spec */
877  break;
878  case 'c':
879  if (fmtpos)
880  {
881  if (argtypes[fmtpos] &&
882  argtypes[fmtpos] != ATYPE_INT)
883  return false;
884  argtypes[fmtpos] = ATYPE_INT;
885  last_dollar = Max(last_dollar, fmtpos);
886  }
887  else
888  return false; /* non-dollar conversion spec */
889  break;
890  case 's':
891  case 'p':
892  if (fmtpos)
893  {
894  if (argtypes[fmtpos] &&
895  argtypes[fmtpos] != ATYPE_CHARPTR)
896  return false;
897  argtypes[fmtpos] = ATYPE_CHARPTR;
898  last_dollar = Max(last_dollar, fmtpos);
899  }
900  else
901  return false; /* non-dollar conversion spec */
902  break;
903  case 'e':
904  case 'E':
905  case 'f':
906  case 'g':
907  case 'G':
908  if (fmtpos)
909  {
910  if (argtypes[fmtpos] &&
911  argtypes[fmtpos] != ATYPE_DOUBLE)
912  return false;
913  argtypes[fmtpos] = ATYPE_DOUBLE;
914  last_dollar = Max(last_dollar, fmtpos);
915  }
916  else
917  return false; /* non-dollar conversion spec */
918  break;
919  case 'm':
920  case '%':
921  break;
922  default:
923  return false; /* bogus format string */
924  }
925 
926  /*
927  * If we finish the spec with afterstar still set, there's a
928  * non-dollar star in there.
929  */
930  if (afterstar)
931  return false; /* non-dollar conversion spec */
932  }
933 
934  /*
935  * Format appears valid so far, so collect the arguments in physical
936  * order. (Since we rejected any non-dollar specs that would have
937  * collected arguments, we know that dopr() hasn't collected any yet.)
938  */
939  for (i = 1; i <= last_dollar; i++)
940  {
941  switch (argtypes[i])
942  {
943  case ATYPE_NONE:
944  return false;
945  case ATYPE_INT:
946  argvalues[i].i = va_arg(args, int);
947  break;
948  case ATYPE_LONG:
949  argvalues[i].l = va_arg(args, long);
950  break;
951  case ATYPE_LONGLONG:
952  argvalues[i].ll = va_arg(args, long long);
953  break;
954  case ATYPE_DOUBLE:
955  argvalues[i].d = va_arg(args, double);
956  break;
957  case ATYPE_CHARPTR:
958  argvalues[i].cptr = va_arg(args, char *);
959  break;
960  }
961  }
962 
963  return true;
964 }
#define Max(x, y)
Definition: c.h:1003

References generate_unaccent_rules::args, ATYPE_CHARPTR, ATYPE_DOUBLE, ATYPE_INT, ATYPE_LONG, ATYPE_LONGLONG, ATYPE_NONE, PrintfArgValue::cptr, PrintfArgValue::d, format, i, PrintfArgValue::i, PrintfArgValue::l, PrintfArgValue::ll, Max, and PG_NL_ARGMAX.

Referenced by dopr().

◆ flushbuffer()

static void flushbuffer ( PrintfTarget target)
static

Definition at line 298 of file snprintf.c.

299 {
300  size_t nc = target->bufptr - target->bufstart;
301 
302  /*
303  * Don't write anything if we already failed; this is to ensure we
304  * preserve the original failure's errno.
305  */
306  if (!target->failed && nc > 0)
307  {
308  size_t written;
309 
310  written = fwrite(target->bufstart, 1, nc, target->stream);
311  target->nchars += written;
312  if (written != nc)
313  target->failed = true;
314  }
315  target->bufptr = target->bufstart;
316 }
char * bufstart
Definition: snprintf.c:128

References PrintfTarget::bufptr, PrintfTarget::bufstart, PrintfTarget::failed, PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by dopr_outch(), dopr_outchmulti(), dostr(), and pg_vfprintf().

◆ fmtchar()

static void fmtchar ( int  value,
int  leftjust,
int  minlen,
PrintfTarget target 
)
static

Definition at line 1121 of file snprintf.c.

1122 {
1123  int padlen; /* amount to pad */
1124 
1125  padlen = compute_padlen(minlen, 1, leftjust);
1126 
1127  if (padlen > 0)
1128  {
1129  dopr_outchmulti(' ', padlen, target);
1130  padlen = 0;
1131  }
1132 
1133  dopr_outch(value, target);
1134 
1135  trailing_pad(padlen, target);
1136 }
static struct @160 value
static int compute_padlen(int minlen, int vallen, int leftjust)
Definition: snprintf.c:1481
static void trailing_pad(int padlen, PrintfTarget *target)
Definition: snprintf.c:1531
static void dopr_outchmulti(int c, int slen, PrintfTarget *target)
Definition: snprintf.c:1430

References compute_padlen(), dopr_outch(), dopr_outchmulti(), trailing_pad(), and value.

Referenced by dopr(), and rfmtlong().

◆ fmtfloat()

static void fmtfloat ( double  value,
char  type,
int  forcesign,
int  leftjust,
int  minlen,
int  zpad,
int  precision,
int  pointflag,
PrintfTarget target 
)
static

Definition at line 1139 of file snprintf.c.

1142 {
1143  int signvalue = 0;
1144  int prec;
1145  int vallen;
1146  char fmt[8];
1147  char convert[1024];
1148  int zeropadlen = 0; /* amount to pad with zeroes */
1149  int padlen; /* amount to pad with spaces */
1150 
1151  /*
1152  * We rely on the regular C library's snprintf to do the basic conversion,
1153  * then handle padding considerations here.
1154  *
1155  * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
1156  * too wildly more than that with other hardware. In "f" format, snprintf
1157  * could therefore generate at most 308 characters to the left of the
1158  * decimal point; while we need to allow the precision to get as high as
1159  * 308+17 to ensure that we don't truncate significant digits from very
1160  * small values. To handle both these extremes, we use a buffer of 1024
1161  * bytes and limit requested precision to 350 digits; this should prevent
1162  * buffer overrun even with non-IEEE math. If the original precision
1163  * request was more than 350, separately pad with zeroes.
1164  *
1165  * We handle infinities and NaNs specially to ensure platform-independent
1166  * output.
1167  */
1168  if (precision < 0) /* cover possible overflow of "accum" */
1169  precision = 0;
1170  prec = Min(precision, 350);
1171 
1172  if (isnan(value))
1173  {
1174  strcpy(convert, "NaN");
1175  vallen = 3;
1176  /* no zero padding, regardless of precision spec */
1177  }
1178  else
1179  {
1180  /*
1181  * Handle sign (NaNs have no sign, so we don't do this in the case
1182  * above). "value < 0.0" will not be true for IEEE minus zero, so we
1183  * detect that by looking for the case where value equals 0.0
1184  * according to == but not according to memcmp.
1185  */
1186  static const double dzero = 0.0;
1187 
1188  if (adjust_sign((value < 0.0 ||
1189  (value == 0.0 &&
1190  memcmp(&value, &dzero, sizeof(double)) != 0)),
1191  forcesign, &signvalue))
1192  value = -value;
1193 
1194  if (isinf(value))
1195  {
1196  strcpy(convert, "Infinity");
1197  vallen = 8;
1198  /* no zero padding, regardless of precision spec */
1199  }
1200  else if (pointflag)
1201  {
1202  zeropadlen = precision - prec;
1203  fmt[0] = '%';
1204  fmt[1] = '.';
1205  fmt[2] = '*';
1206  fmt[3] = type;
1207  fmt[4] = '\0';
1208  vallen = snprintf(convert, sizeof(convert), fmt, prec, value);
1209  }
1210  else
1211  {
1212  fmt[0] = '%';
1213  fmt[1] = type;
1214  fmt[2] = '\0';
1215  vallen = snprintf(convert, sizeof(convert), fmt, value);
1216  }
1217  if (vallen < 0)
1218  goto fail;
1219 
1220  /*
1221  * Windows, alone among our supported platforms, likes to emit
1222  * three-digit exponent fields even when two digits would do. Hack
1223  * such results to look like the way everyone else does it.
1224  */
1225 #ifdef WIN32
1226  if (vallen >= 6 &&
1227  convert[vallen - 5] == 'e' &&
1228  convert[vallen - 3] == '0')
1229  {
1230  convert[vallen - 3] = convert[vallen - 2];
1231  convert[vallen - 2] = convert[vallen - 1];
1232  vallen--;
1233  }
1234 #endif
1235  }
1236 
1237  padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
1238 
1239  leading_pad(zpad, signvalue, &padlen, target);
1240 
1241  if (zeropadlen > 0)
1242  {
1243  /* If 'e' or 'E' format, inject zeroes before the exponent */
1244  char *epos = strrchr(convert, 'e');
1245 
1246  if (!epos)
1247  epos = strrchr(convert, 'E');
1248  if (epos)
1249  {
1250  /* pad before exponent */
1251  dostr(convert, epos - convert, target);
1252  dopr_outchmulti('0', zeropadlen, target);
1253  dostr(epos, vallen - (epos - convert), target);
1254  }
1255  else
1256  {
1257  /* no exponent, pad after the digits */
1258  dostr(convert, vallen, target);
1259  dopr_outchmulti('0', zeropadlen, target);
1260  }
1261  }
1262  else
1263  {
1264  /* no zero padding, just emit the number as-is */
1265  dostr(convert, vallen, target);
1266  }
1267 
1268  trailing_pad(padlen, target);
1269  return;
1270 
1271 fail:
1272  target->failed = true;
1273 }
static void const char * fmt
#define snprintf
Definition: port.h:238
static void leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
Definition: snprintf.c:1495
static int adjust_sign(int is_negative, int forcesign, int *signvalue)
Definition: snprintf.c:1467
const char * type
static void convert(const int32 val, char *const buf)
Definition: zic.c:1992

References adjust_sign(), compute_padlen(), convert(), dopr_outchmulti(), dostr(), PrintfTarget::failed, fmt, leading_pad(), Min, snprintf, trailing_pad(), type, and value.

Referenced by dopr().

◆ fmtint()

static void fmtint ( long long  value,
char  type,
int  forcesign,
int  leftjust,
int  minlen,
int  zpad,
int  precision,
int  pointflag,
PrintfTarget target 
)
static

Definition at line 1010 of file snprintf.c.

1013 {
1014  unsigned long long uvalue;
1015  int base;
1016  int dosign;
1017  const char *cvt = "0123456789abcdef";
1018  int signvalue = 0;
1019  char convert[64];
1020  int vallen = 0;
1021  int padlen; /* amount to pad */
1022  int zeropad; /* extra leading zeroes */
1023 
1024  switch (type)
1025  {
1026  case 'd':
1027  case 'i':
1028  base = 10;
1029  dosign = 1;
1030  break;
1031  case 'o':
1032  base = 8;
1033  dosign = 0;
1034  break;
1035  case 'u':
1036  base = 10;
1037  dosign = 0;
1038  break;
1039  case 'x':
1040  base = 16;
1041  dosign = 0;
1042  break;
1043  case 'X':
1044  cvt = "0123456789ABCDEF";
1045  base = 16;
1046  dosign = 0;
1047  break;
1048  default:
1049  return; /* keep compiler quiet */
1050  }
1051 
1052  /* disable MSVC warning about applying unary minus to an unsigned value */
1053 #ifdef _MSC_VER
1054 #pragma warning(push)
1055 #pragma warning(disable: 4146)
1056 #endif
1057  /* Handle +/- */
1058  if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
1059  uvalue = -(unsigned long long) value;
1060  else
1061  uvalue = (unsigned long long) value;
1062 #ifdef _MSC_VER
1063 #pragma warning(pop)
1064 #endif
1065 
1066  /*
1067  * SUS: the result of converting 0 with an explicit precision of 0 is no
1068  * characters
1069  */
1070  if (value == 0 && pointflag && precision == 0)
1071  vallen = 0;
1072  else
1073  {
1074  /*
1075  * Convert integer to string. We special-case each of the possible
1076  * base values so as to avoid general-purpose divisions. On most
1077  * machines, division by a fixed constant can be done much more
1078  * cheaply than a general divide.
1079  */
1080  if (base == 10)
1081  {
1082  do
1083  {
1084  convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 10];
1085  uvalue = uvalue / 10;
1086  } while (uvalue);
1087  }
1088  else if (base == 16)
1089  {
1090  do
1091  {
1092  convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 16];
1093  uvalue = uvalue / 16;
1094  } while (uvalue);
1095  }
1096  else /* base == 8 */
1097  {
1098  do
1099  {
1100  convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 8];
1101  uvalue = uvalue / 8;
1102  } while (uvalue);
1103  }
1104  }
1105 
1106  zeropad = Max(0, precision - vallen);
1107 
1108  padlen = compute_padlen(minlen, vallen + zeropad, leftjust);
1109 
1110  leading_pad(zpad, signvalue, &padlen, target);
1111 
1112  if (zeropad > 0)
1113  dopr_outchmulti('0', zeropad, target);
1114 
1115  dostr(convert + sizeof(convert) - vallen, vallen, target);
1116 
1117  trailing_pad(padlen, target);
1118 }

References adjust_sign(), compute_padlen(), convert(), dopr_outchmulti(), dostr(), leading_pad(), Max, trailing_pad(), type, and value.

Referenced by dopr().

◆ fmtptr()

static void fmtptr ( const void *  value,
PrintfTarget target 
)
static

Definition at line 996 of file snprintf.c.

997 {
998  int vallen;
999  char convert[64];
1000 
1001  /* we rely on regular C library's snprintf to do the basic conversion */
1002  vallen = snprintf(convert, sizeof(convert), "%p", value);
1003  if (vallen < 0)
1004  target->failed = true;
1005  else
1006  dostr(convert, vallen, target);
1007 }

References convert(), dostr(), PrintfTarget::failed, snprintf, and value.

Referenced by dopr().

◆ fmtstr()

static void fmtstr ( const char *  value,
int  leftjust,
int  minlen,
int  maxwidth,
int  pointflag,
PrintfTarget target 
)
static

Definition at line 967 of file snprintf.c.

969 {
970  int padlen,
971  vallen; /* amount to pad */
972 
973  /*
974  * If a maxwidth (precision) is specified, we must not fetch more bytes
975  * than that.
976  */
977  if (pointflag)
978  vallen = strnlen(value, maxwidth);
979  else
980  vallen = strlen(value);
981 
982  padlen = compute_padlen(minlen, vallen, leftjust);
983 
984  if (padlen > 0)
985  {
986  dopr_outchmulti(' ', padlen, target);
987  padlen = 0;
988  }
989 
990  dostr(value, vallen, target);
991 
992  trailing_pad(padlen, target);
993 }
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26

References compute_padlen(), dopr_outchmulti(), dostr(), strnlen(), trailing_pad(), and value.

Referenced by dopr(), dtcvfmtasc(), dttofmtasc(), dttofmtasc_replace(), and PGTYPEStimestamp_fmt_asc().

◆ leading_pad()

static void leading_pad ( int  zpad,
int  signvalue,
int *  padlen,
PrintfTarget target 
)
static

Definition at line 1495 of file snprintf.c.

1496 {
1497  int maxpad;
1498 
1499  if (*padlen > 0 && zpad)
1500  {
1501  if (signvalue)
1502  {
1503  dopr_outch(signvalue, target);
1504  --(*padlen);
1505  signvalue = 0;
1506  }
1507  if (*padlen > 0)
1508  {
1509  dopr_outchmulti(zpad, *padlen, target);
1510  *padlen = 0;
1511  }
1512  }
1513  maxpad = (signvalue != 0);
1514  if (*padlen > maxpad)
1515  {
1516  dopr_outchmulti(' ', *padlen - maxpad, target);
1517  *padlen = maxpad;
1518  }
1519  if (signvalue)
1520  {
1521  dopr_outch(signvalue, target);
1522  if (*padlen > 0)
1523  --(*padlen);
1524  else if (*padlen < 0)
1525  ++(*padlen);
1526  }
1527 }

References dopr_outch(), and dopr_outchmulti().

Referenced by fmtfloat(), and fmtint().

◆ pg_fprintf()

int pg_fprintf ( FILE *  stream,
const char *  fmt,
  ... 
)

Definition at line 264 of file snprintf.c.

265 {
266  int len;
267  va_list args;
268 
269  va_start(args, fmt);
270  len = pg_vfprintf(stream, fmt, args);
271  va_end(args);
272  return len;
273 }
va_end(args)
va_start(args, fmt)
const void size_t len
int pg_vfprintf(FILE *stream, const char *fmt, va_list args)
Definition: snprintf.c:242

References generate_unaccent_rules::args, fmt, len, pg_vfprintf(), va_end(), and va_start().

◆ pg_printf()

int pg_printf ( const char *  fmt,
  ... 
)

Definition at line 282 of file snprintf.c.

283 {
284  int len;
285  va_list args;
286 
287  va_start(args, fmt);
289  va_end(args);
290  return len;
291 }

References generate_unaccent_rules::args, fmt, len, pg_vfprintf(), generate_unaccent_rules::stdout, va_end(), and va_start().

◆ pg_snprintf()

int pg_snprintf ( char *  str,
size_t  count,
const char *  fmt,
  ... 
)

Definition at line 202 of file snprintf.c.

203 {
204  int len;
205  va_list args;
206 
207  va_start(args, fmt);
208  len = pg_vsnprintf(str, count, fmt, args);
209  va_end(args);
210  return len;
211 }
int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
Definition: snprintf.c:174

References generate_unaccent_rules::args, fmt, len, pg_vsnprintf(), str, va_end(), and va_start().

◆ pg_sprintf()

int pg_sprintf ( char *  str,
const char *  fmt,
  ... 
)

Definition at line 230 of file snprintf.c.

231 {
232  int len;
233  va_list args;
234 
235  va_start(args, fmt);
236  len = pg_vsprintf(str, fmt, args);
237  va_end(args);
238  return len;
239 }
int pg_vsprintf(char *str, const char *fmt, va_list args)
Definition: snprintf.c:214

References generate_unaccent_rules::args, fmt, len, pg_vsprintf(), str, va_end(), and va_start().

◆ pg_strfromd()

int pg_strfromd ( char *  str,
size_t  count,
int  precision,
double  value 
)

Definition at line 1285 of file snprintf.c.

1286 {
1287  PrintfTarget target;
1288  int signvalue = 0;
1289  int vallen;
1290  char fmt[8];
1291  char convert[64];
1292 
1293  /* Set up the target like pg_snprintf, but require nonempty buffer */
1294  Assert(count > 0);
1295  target.bufstart = target.bufptr = str;
1296  target.bufend = str + count - 1;
1297  target.stream = NULL;
1298  target.nchars = 0;
1299  target.failed = false;
1300 
1301  /*
1302  * We bound precision to a reasonable range; the combination of this and
1303  * the knowledge that we're using "g" format without padding allows the
1304  * convert[] buffer to be reasonably small.
1305  */
1306  if (precision < 1)
1307  precision = 1;
1308  else if (precision > 32)
1309  precision = 32;
1310 
1311  /*
1312  * The rest is just an inlined version of the fmtfloat() logic above,
1313  * simplified using the knowledge that no padding is wanted.
1314  */
1315  if (isnan(value))
1316  {
1317  strcpy(convert, "NaN");
1318  vallen = 3;
1319  }
1320  else
1321  {
1322  static const double dzero = 0.0;
1323 
1324  if (value < 0.0 ||
1325  (value == 0.0 &&
1326  memcmp(&value, &dzero, sizeof(double)) != 0))
1327  {
1328  signvalue = '-';
1329  value = -value;
1330  }
1331 
1332  if (isinf(value))
1333  {
1334  strcpy(convert, "Infinity");
1335  vallen = 8;
1336  }
1337  else
1338  {
1339  fmt[0] = '%';
1340  fmt[1] = '.';
1341  fmt[2] = '*';
1342  fmt[3] = 'g';
1343  fmt[4] = '\0';
1344  vallen = snprintf(convert, sizeof(convert), fmt, precision, value);
1345  if (vallen < 0)
1346  {
1347  target.failed = true;
1348  goto fail;
1349  }
1350 
1351 #ifdef WIN32
1352  if (vallen >= 6 &&
1353  convert[vallen - 5] == 'e' &&
1354  convert[vallen - 3] == '0')
1355  {
1356  convert[vallen - 3] = convert[vallen - 2];
1357  convert[vallen - 2] = convert[vallen - 1];
1358  vallen--;
1359  }
1360 #endif
1361  }
1362  }
1363 
1364  if (signvalue)
1365  dopr_outch(signvalue, &target);
1366 
1367  dostr(convert, vallen, &target);
1368 
1369 fail:
1370  *(target.bufptr) = '\0';
1371  return target.failed ? -1 : (target.bufptr - target.bufstart
1372  + target.nchars);
1373 }
#define Assert(condition)
Definition: c.h:863

References Assert, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, convert(), dopr_outch(), dostr(), PrintfTarget::failed, fmt, PrintfTarget::nchars, snprintf, str, PrintfTarget::stream, and value.

Referenced by float4out(), and float8out_internal().

◆ pg_vfprintf()

int pg_vfprintf ( FILE *  stream,
const char *  fmt,
va_list  args 
)

Definition at line 242 of file snprintf.c.

243 {
244  PrintfTarget target;
245  char buffer[1024]; /* size is arbitrary */
246 
247  if (stream == NULL)
248  {
249  errno = EINVAL;
250  return -1;
251  }
252  target.bufstart = target.bufptr = buffer;
253  target.bufend = buffer + sizeof(buffer); /* use the whole buffer */
254  target.stream = stream;
255  target.nchars = 0;
256  target.failed = false;
257  dopr(&target, fmt, args);
258  /* dump any remaining buffer contents */
259  flushbuffer(&target);
260  return target.failed ? -1 : target.nchars;
261 }
static void dopr(PrintfTarget *target, const char *format, va_list args)
Definition: snprintf.c:376

References generate_unaccent_rules::args, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, dopr(), PrintfTarget::failed, flushbuffer(), fmt, PrintfTarget::nchars, and PrintfTarget::stream.

Referenced by pg_fprintf(), pg_printf(), and pg_vprintf().

◆ pg_vprintf()

int pg_vprintf ( const char *  fmt,
va_list  args 
)

Definition at line 276 of file snprintf.c.

277 {
278  return pg_vfprintf(stdout, fmt, args);
279 }

References generate_unaccent_rules::args, fmt, pg_vfprintf(), and generate_unaccent_rules::stdout.

◆ pg_vsnprintf()

int pg_vsnprintf ( char *  str,
size_t  count,
const char *  fmt,
va_list  args 
)

Definition at line 174 of file snprintf.c.

175 {
176  PrintfTarget target;
177  char onebyte[1];
178 
179  /*
180  * C99 allows the case str == NULL when count == 0. Rather than
181  * special-casing this situation further down, we substitute a one-byte
182  * local buffer. Callers cannot tell, since the function result doesn't
183  * depend on count.
184  */
185  if (count == 0)
186  {
187  str = onebyte;
188  count = 1;
189  }
190  target.bufstart = target.bufptr = str;
191  target.bufend = str + count - 1;
192  target.stream = NULL;
193  target.nchars = 0;
194  target.failed = false;
195  dopr(&target, fmt, args);
196  *(target.bufptr) = '\0';
197  return target.failed ? -1 : (target.bufptr - target.bufstart
198  + target.nchars);
199 }

References generate_unaccent_rules::args, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, dopr(), PrintfTarget::failed, fmt, PrintfTarget::nchars, str, and PrintfTarget::stream.

Referenced by pg_snprintf().

◆ pg_vsprintf()

int pg_vsprintf ( char *  str,
const char *  fmt,
va_list  args 
)

Definition at line 214 of file snprintf.c.

215 {
216  PrintfTarget target;
217 
218  target.bufstart = target.bufptr = str;
219  target.bufend = NULL;
220  target.stream = NULL;
221  target.nchars = 0; /* not really used in this case */
222  target.failed = false;
223  dopr(&target, fmt, args);
224  *(target.bufptr) = '\0';
225  return target.failed ? -1 : (target.bufptr - target.bufstart
226  + target.nchars);
227 }

References generate_unaccent_rules::args, PrintfTarget::bufend, PrintfTarget::bufptr, PrintfTarget::bufstart, dopr(), PrintfTarget::failed, fmt, PrintfTarget::nchars, str, and PrintfTarget::stream.

Referenced by pg_sprintf().

◆ strchrnul()

static const char* strchrnul ( const char *  s,
int  c 
)
inlinestatic

Definition at line 350 of file snprintf.c.

351 {
352  while (*s != '\0' && *s != c)
353  s++;
354  return s;
355 }

Referenced by dopr().

◆ trailing_pad()

static void trailing_pad ( int  padlen,
PrintfTarget target 
)
static

Definition at line 1531 of file snprintf.c.

1532 {
1533  if (padlen < 0)
1534  dopr_outchmulti(' ', -padlen, target);
1535 }

References dopr_outchmulti().

Referenced by fmtchar(), fmtfloat(), fmtint(), and fmtstr().