@ -46,6 +46,7 @@ static struct option long_options[] = {
{ " help " , no_argument , NULL , ' h ' } ,
{ " script " , required_argument , NULL , ' s ' } ,
{ " noheader " , required_argument , NULL , ' n ' } ,
{ " lock " , required_argument , NULL , ' l ' } ,
{ NULL , 0 , NULL , 0 }
} ;
@ -72,6 +73,7 @@ void usage_printenv(void)
" -c, --config configuration file, default: " CONFIG_FILE " \n "
# endif
" -n, --noheader do not repeat variable name in output \n "
" -l, --lock lock node, default:/var/lock \n "
" \n " ) ;
}
@ -88,6 +90,7 @@ void usage_setenv(void)
# ifdef CONFIG_FILE
" -c, --config configuration file, default: " CONFIG_FILE " \n "
# endif
" -l, --lock lock node, default:/var/lock \n "
" -s, --script batch mode to minimize writes \n "
" \n "
" Examples: \n "
@ -119,7 +122,7 @@ static void parse_common_args(int argc, char *argv[])
env_opts . config_file = CONFIG_FILE ;
# endif
while ( ( c = getopt_long ( argc , argv , " :a:c:h " , long_options , NULL ) ) ! =
while ( ( c = getopt_long ( argc , argv , " :a:c:l: h " , long_options , NULL ) ) ! =
EOF ) {
switch ( c ) {
case ' a ' :
@ -134,6 +137,9 @@ static void parse_common_args(int argc, char *argv[])
env_opts . config_file = optarg ;
break ;
# endif
case ' l ' :
env_opts . lockname = optarg ;
break ;
case ' h ' :
do_printenv ? usage_printenv ( ) : usage_setenv ( ) ;
exit ( EXIT_SUCCESS ) ;
@ -155,8 +161,8 @@ int parse_printenv_args(int argc, char *argv[])
parse_common_args ( argc , argv ) ;
while ( ( c = getopt_long ( argc , argv , " a:c:ns:h " , long_options , NULL ) ) ! =
EOF ) {
while ( ( c = getopt_long ( argc , argv , " a:c:ns:l: h " , long_options , NULL ) )
! = EOF ) {
switch ( c ) {
case ' n ' :
noheader = 1 ;
@ -164,6 +170,7 @@ int parse_printenv_args(int argc, char *argv[])
case ' a ' :
case ' c ' :
case ' h ' :
case ' l ' :
/* ignore common options */
break ;
default : /* '?' */
@ -181,8 +188,8 @@ int parse_setenv_args(int argc, char *argv[])
parse_common_args ( argc , argv ) ;
while ( ( c = getopt_long ( argc , argv , " a:c:ns:h " , long_options , NULL ) ) ! =
EOF ) {
while ( ( c = getopt_long ( argc , argv , " a:c:ns:l: h " , long_options , NULL ) )
! = EOF ) {
switch ( c ) {
case ' s ' :
script_file = optarg ;
@ -190,6 +197,7 @@ int parse_setenv_args(int argc, char *argv[])
case ' a ' :
case ' c ' :
case ' h ' :
case ' l ' :
/* ignore common options */
break ;
default : /* '?' */
@ -203,7 +211,7 @@ int parse_setenv_args(int argc, char *argv[])
int main ( int argc , char * argv [ ] )
{
const char * lockname = " /var/lock/ " CMD_PRINTENV " .lock " ;
char * lockname = " /var/lock/ " CMD_PRINTENV " .lock " ;
int lockfd = - 1 ;
int retval = EXIT_SUCCESS ;
char * _cmdname ;
@ -235,6 +243,18 @@ int main(int argc, char *argv[])
argc - = optind ;
argv + = optind ;
if ( env_opts . lockname ) {
lockname = malloc ( sizeof ( env_opts . lockname ) +
sizeof ( CMD_PRINTENV ) + 10 ) ;
if ( ! lockname ) {
fprintf ( stderr , " Unable allocate memory " ) ;
exit ( EXIT_FAILURE ) ;
}
sprintf ( lockname , " %s/%s.lock " ,
env_opts . lockname , CMD_PRINTENV ) ;
}
lockfd = open ( lockname , O_WRONLY | O_CREAT | O_TRUNC , 0666 ) ;
if ( - 1 = = lockfd ) {
fprintf ( stderr , " Error opening lock file %s \n " , lockname ) ;
@ -260,6 +280,9 @@ int main(int argc, char *argv[])
}
}
if ( env_opts . lockname )
free ( lockname ) ;
flock ( lockfd , LOCK_UN ) ;
close ( lockfd ) ;
return retval ;