int main(int argc, char *argv[]) { char *p; char *cmdname = *argv; if ((p = strrchr (cmdname, '/')) != NULL) { cmdname = p + 1; } if (strcmp(cmdname, CMD_PRINTENV) == 0) { return fw_printenv (argc, argv); } else if (strcmp(cmdname, CMD_SETENV) == 0) { return fw_setenv (argc, argv); } fprintf (stderr, "Identity crisis - may be called as `" CMD_PRINTENV "' or as `" CMD_SETENV "' but not as `%s'\n", cmdname); return (EXIT_FAILURE); }
int main(int argc, char *argv[]) { char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; int lockfd = -1; int retval = EXIT_SUCCESS; char *_cmdname; _cmdname = *argv; if (strrchr(_cmdname, '/') != NULL) _cmdname = strrchr(_cmdname, '/') + 1; if (strcmp(_cmdname, CMD_PRINTENV) == 0) { do_printenv = 1; } else if (strcmp(_cmdname, CMD_SETENV) == 0) { do_printenv = 0; } else { fprintf(stderr, "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n", CMD_PRINTENV, CMD_SETENV, _cmdname); exit(EXIT_FAILURE); } if (do_printenv) { if (parse_printenv_args(argc, argv)) exit(EXIT_FAILURE); } else { if (parse_setenv_args(argc, argv)) exit(EXIT_FAILURE); } /* shift parsed flags, jump to non-option arguments */ 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); return EXIT_FAILURE; } if (-1 == flock(lockfd, LOCK_EX)) { fprintf(stderr, "Error locking file %s\n", lockname); close(lockfd); return EXIT_FAILURE; } if (do_printenv) { if (fw_printenv(argc, argv, noheader, &env_opts) != 0) retval = EXIT_FAILURE; } else { if (!script_file) { if (fw_env_set(argc, argv, &env_opts) != 0) retval = EXIT_FAILURE; } else { if (fw_parse_script(script_file, &env_opts) != 0) retval = EXIT_FAILURE; } } if (env_opts.lockname) free(lockname); flock(lockfd, LOCK_UN); close(lockfd); return retval; }