{"help", no_argument, NULL, 'h'},
{"script", required_argument, NULL, 's'},
{"noheader", required_argument, NULL, 'n'},
+ {"lock", required_argument, NULL, 'l'},
{NULL, 0, NULL, 0}
};
" -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");
}
#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"
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':
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);
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;
case 'a':
case 'c':
case 'h':
+ case 'l':
/* ignore common options */
break;
default: /* '?' */
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;
case 'a':
case 'c':
case 'h':
+ case 'l':
/* ignore common options */
break;
default: /* '?' */
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;
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);
}
}
+ if (env_opts.lockname)
+ free(lockname);
+
flock(lockfd, LOCK_UN);
close(lockfd);
return retval;