LCOV - code coverage report
Current view: top level - src/bin/pg_config - pg_config.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 40.5 % 79 32
Test Date: 2026-01-26 10:56:24 Functions: 50.0 % 4 2
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 68.2 % 22 15

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pg_config.c
       4                 :             :  *
       5                 :             :  * This program reports various pieces of information about the
       6                 :             :  * installed version of PostgreSQL.  Packages that interface to
       7                 :             :  * PostgreSQL can use it to configure their build.
       8                 :             :  *
       9                 :             :  * This is a C implementation of the previous shell script written by
      10                 :             :  * Peter Eisentraut <peter_e@gmx.net>, with adjustments made to
      11                 :             :  * accommodate the possibility that the installation has been relocated from
      12                 :             :  * the place originally configured.
      13                 :             :  *
      14                 :             :  * author of C translation: Andrew Dunstan         mailto:andrew@dunslane.net
      15                 :             :  *
      16                 :             :  * This code is released under the terms of the PostgreSQL License.
      17                 :             :  *
      18                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      19                 :             :  *
      20                 :             :  * src/bin/pg_config/pg_config.c
      21                 :             :  *
      22                 :             :  *-------------------------------------------------------------------------
      23                 :             :  */
      24                 :             : 
      25                 :             : #include "postgres_fe.h"
      26                 :             : 
      27                 :             : #include "common/config_info.h"
      28                 :             : 
      29                 :             : static const char *progname;
      30                 :             : 
      31                 :             : /*
      32                 :             :  * Table of known information items
      33                 :             :  *
      34                 :             :  * Be careful to keep this in sync with the help() display.
      35                 :             :  */
      36                 :             : typedef struct
      37                 :             : {
      38                 :             :         const char *switchname;
      39                 :             :         const char *configname;
      40                 :             : } InfoItem;
      41                 :             : 
      42                 :             : static const InfoItem info_items[] = {
      43                 :             :         {"--bindir", "BINDIR"},
      44                 :             :         {"--docdir", "DOCDIR"},
      45                 :             :         {"--htmldir", "HTMLDIR"},
      46                 :             :         {"--includedir", "INCLUDEDIR"},
      47                 :             :         {"--pkgincludedir", "PKGINCLUDEDIR"},
      48                 :             :         {"--includedir-server", "INCLUDEDIR-SERVER"},
      49                 :             :         {"--libdir", "LIBDIR"},
      50                 :             :         {"--pkglibdir", "PKGLIBDIR"},
      51                 :             :         {"--localedir", "LOCALEDIR"},
      52                 :             :         {"--mandir", "MANDIR"},
      53                 :             :         {"--sharedir", "SHAREDIR"},
      54                 :             :         {"--sysconfdir", "SYSCONFDIR"},
      55                 :             :         {"--pgxs", "PGXS"},
      56                 :             :         {"--configure", "CONFIGURE"},
      57                 :             :         {"--cc", "CC"},
      58                 :             :         {"--cppflags", "CPPFLAGS"},
      59                 :             :         {"--cflags", "CFLAGS"},
      60                 :             :         {"--cflags_sl", "CFLAGS_SL"},
      61                 :             :         {"--ldflags", "LDFLAGS"},
      62                 :             :         {"--ldflags_ex", "LDFLAGS_EX"},
      63                 :             :         {"--ldflags_sl", "LDFLAGS_SL"},
      64                 :             :         {"--libs", "LIBS"},
      65                 :             :         {"--version", "VERSION"},
      66                 :             :         {NULL, NULL}
      67                 :             : };
      68                 :             : 
      69                 :             : 
      70                 :             : static void
      71                 :           0 : help(void)
      72                 :             : {
      73                 :           0 :         printf(_("\n%s provides information about the installed version of PostgreSQL.\n\n"), progname);
      74                 :           0 :         printf(_("Usage:\n"));
      75                 :           0 :         printf(_("  %s [OPTION]...\n\n"), progname);
      76                 :           0 :         printf(_("Options:\n"));
      77                 :           0 :         printf(_("  --bindir              show location of user executables\n"));
      78                 :           0 :         printf(_("  --docdir              show location of documentation files\n"));
      79                 :           0 :         printf(_("  --htmldir             show location of HTML documentation files\n"));
      80                 :           0 :         printf(_("  --includedir          show location of C header files of the client\n"
      81                 :             :                          "                        interfaces\n"));
      82                 :           0 :         printf(_("  --pkgincludedir       show location of other C header files\n"));
      83                 :           0 :         printf(_("  --includedir-server   show location of C header files for the server\n"));
      84                 :           0 :         printf(_("  --libdir              show location of object code libraries\n"));
      85                 :           0 :         printf(_("  --pkglibdir           show location of dynamically loadable modules\n"));
      86                 :           0 :         printf(_("  --localedir           show location of locale support files\n"));
      87                 :           0 :         printf(_("  --mandir              show location of manual pages\n"));
      88                 :           0 :         printf(_("  --sharedir            show location of architecture-independent support files\n"));
      89                 :           0 :         printf(_("  --sysconfdir          show location of system-wide configuration files\n"));
      90                 :           0 :         printf(_("  --pgxs                show location of extension makefile\n"));
      91                 :           0 :         printf(_("  --configure           show options given to \"configure\" script when\n"
      92                 :             :                          "                        PostgreSQL was built\n"));
      93                 :           0 :         printf(_("  --cc                  show CC value used when PostgreSQL was built\n"));
      94                 :           0 :         printf(_("  --cppflags            show CPPFLAGS value used when PostgreSQL was built\n"));
      95                 :           0 :         printf(_("  --cflags              show CFLAGS value used when PostgreSQL was built\n"));
      96                 :           0 :         printf(_("  --cflags_sl           show CFLAGS_SL value used when PostgreSQL was built\n"));
      97                 :           0 :         printf(_("  --ldflags             show LDFLAGS value used when PostgreSQL was built\n"));
      98                 :           0 :         printf(_("  --ldflags_ex          show LDFLAGS_EX value used when PostgreSQL was built\n"));
      99                 :           0 :         printf(_("  --ldflags_sl          show LDFLAGS_SL value used when PostgreSQL was built\n"));
     100                 :           0 :         printf(_("  --libs                show LIBS value used when PostgreSQL was built\n"));
     101                 :           0 :         printf(_("  --version             show the PostgreSQL version\n"));
     102                 :           0 :         printf(_("  -?, --help            show this help, then exit\n"));
     103                 :           0 :         printf(_("\nWith no arguments, all known items are shown.\n\n"));
     104                 :           0 :         printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     105                 :           0 :         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     106                 :           0 : }
     107                 :             : 
     108                 :             : static void
     109                 :           0 : advice(void)
     110                 :             : {
     111                 :           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     112                 :           0 : }
     113                 :             : 
     114                 :             : static void
     115                 :           1 : show_item(const char *configname,
     116                 :             :                   ConfigData *configdata,
     117                 :             :                   size_t configdata_len)
     118                 :             : {
     119                 :           1 :         int                     i;
     120                 :             : 
     121         [ +  + ]:          24 :         for (i = 0; i < configdata_len; i++)
     122                 :             :         {
     123         [ +  + ]:          23 :                 if (strcmp(configname, configdata[i].name) == 0)
     124                 :           1 :                         printf("%s\n", configdata[i].setting);
     125                 :          23 :         }
     126                 :           1 : }
     127                 :             : 
     128                 :             : int
     129                 :           1 : main(int argc, char **argv)
     130                 :             : {
     131                 :           1 :         ConfigData *configdata;
     132                 :           1 :         size_t          configdata_len;
     133                 :           1 :         char            my_exec_path[MAXPGPATH];
     134                 :           1 :         int                     i;
     135                 :           1 :         int                     j;
     136                 :             : 
     137                 :           1 :         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_config"));
     138                 :             : 
     139                 :           1 :         progname = get_progname(argv[0]);
     140                 :             : 
     141                 :             :         /* check for --help */
     142         [ +  + ]:           2 :         for (i = 1; i < argc; i++)
     143                 :             :         {
     144         [ +  - ]:           1 :                 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-?") == 0)
     145                 :             :                 {
     146                 :           0 :                         help();
     147                 :           0 :                         exit(0);
     148                 :             :                 }
     149                 :           1 :         }
     150                 :             : 
     151         [ +  - ]:           1 :         if (find_my_exec(argv[0], my_exec_path) < 0)
     152                 :             :         {
     153                 :           0 :                 fprintf(stderr, _("%s: could not find own program executable\n"), progname);
     154                 :           0 :                 exit(1);
     155                 :             :         }
     156                 :             : 
     157                 :           1 :         configdata = get_configdata(my_exec_path, &configdata_len);
     158                 :             :         /* no arguments -> print everything */
     159         [ +  - ]:           1 :         if (argc < 2)
     160                 :             :         {
     161         [ #  # ]:           0 :                 for (i = 0; i < configdata_len; i++)
     162                 :           0 :                         printf("%s = %s\n", configdata[i].name, configdata[i].setting);
     163                 :           0 :                 exit(0);
     164                 :             :         }
     165                 :             : 
     166                 :             :         /* otherwise print requested items */
     167         [ +  + ]:           2 :         for (i = 1; i < argc; i++)
     168                 :             :         {
     169         [ -  + ]:          23 :                 for (j = 0; info_items[j].switchname != NULL; j++)
     170                 :             :                 {
     171         [ +  + ]:          23 :                         if (strcmp(argv[i], info_items[j].switchname) == 0)
     172                 :             :                         {
     173                 :           2 :                                 show_item(info_items[j].configname,
     174                 :           1 :                                                   configdata, configdata_len);
     175                 :           1 :                                 break;
     176                 :             :                         }
     177                 :          22 :                 }
     178         [ +  - ]:           1 :                 if (info_items[j].switchname == NULL)
     179                 :             :                 {
     180                 :           0 :                         fprintf(stderr, _("%s: invalid argument: %s\n"),
     181                 :           0 :                                         progname, argv[i]);
     182                 :           0 :                         advice();
     183                 :           0 :                         exit(1);
     184                 :             :                 }
     185                 :           1 :         }
     186                 :             : 
     187                 :           1 :         return 0;
     188                 :           1 : }
        

Generated by: LCOV version 2.3.2-1