@@ -505,6 +505,10 @@ longopts_to_optstring(const struct option opts[])
505
505
return result ;
506
506
}
507
507
508
+ /*
509
+ * Read options from environmental variables.
510
+ * Do not overwrite in option was already set via command line option.
511
+ */
508
512
static void
509
513
option_from_env (pgut_option options [])
510
514
{
@@ -513,24 +517,30 @@ option_from_env(pgut_option options[])
513
517
for (i = 0 ; options && options [i ].type ; i ++ )
514
518
{
515
519
pgut_option * opt = & options [i ];
516
- char name [256 ];
517
- size_t j ;
518
- const char * s ;
519
- const char * value ;
520
+ const char * value = NULL ;
520
521
522
+ /* If option was already set do not check env */
521
523
if (opt -> source > SOURCE_ENV || opt -> allowed < SOURCE_ENV )
522
524
continue ;
523
525
524
- for (s = opt -> lname , j = 0 ; * s && j < lengthof (name ) - 1 ; s ++ , j ++ )
526
+ if (strcmp (opt -> lname , "pgdata" ) == 0 )
527
+ value = getenv ("PGDATA" );
528
+ if (strcmp (opt -> lname , "port" ) == 0 )
529
+ value = getenv ("PGPORT" );
530
+ if (strcmp (opt -> lname , "host" ) == 0 )
531
+ value = getenv ("PGHOST" );
532
+ if (strcmp (opt -> lname , "username" ) == 0 )
533
+ value = getenv ("PGUSER" );
534
+ if (strcmp (opt -> lname , "dbname" ) == 0 )
525
535
{
526
- if (strchr ("-_ " , * s ))
527
- name [j ] = '_' ; /* - to _ */
528
- else
529
- name [j ] = toupper (* s );
536
+ value = getenv ("PGDATABASE" );
537
+ if (value == NULL )
538
+ value = getenv ("PGUSER" );
539
+ if (value == NULL )
540
+ value = get_username ();
530
541
}
531
- name [j ] = '\0' ;
532
542
533
- if (( value = getenv ( name )) != NULL )
543
+ if (value )
534
544
assign_option (opt , value , SOURCE_ENV );
535
545
}
536
546
}
@@ -564,10 +574,6 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
564
574
565
575
/* Read environment variables */
566
576
option_from_env (options );
567
- (void ) (pgut_dbname ||
568
- (pgut_dbname = getenv ("PGDATABASE" )) ||
569
- (pgut_dbname = getenv ("PGUSER" )) ||
570
- (pgut_dbname = get_username ()));
571
577
572
578
init_cancel_handler ();
573
579
atexit (on_cleanup );
0 commit comments