Fix the argv[0] == NULL bug

Signed-off-by: Gavin Howard <[email protected]>
This commit is contained in:
Gavin Howard 2022-02-05 10:13:21 -07:00
parent 52f7667c88
commit 81905ee8e2
Signed by: gavin
GPG Key ID: C08038BDF280D33E
1 changed files with 20 additions and 4 deletions

View File

@ -66,10 +66,26 @@ int main(int argc, char *argv[]) {
// Set the start pledge().
bc_pledge(bc_pledge_start, NULL);
// Figure out the name of the calculator we are using. We can't use basename
// because it's not portable, but yes, this is stripping off the directory.
name = strrchr(argv[0], BC_FILE_SEP);
vm.name = (name == NULL) ? argv[0] : name + 1;
// Sometimes, argv[0] can be NULL. Better make sure to be robust against it.
if (argv[0] != NULL) {
// Figure out the name of the calculator we are using. We can't use
// basename because it's not portable, but yes, this is stripping off
// the directory.
name = strrchr(argv[0], BC_FILE_SEP);
vm.name = (name == NULL) ? argv[0] : name + 1;
}
else
{
#if !DC_ENABLED
vm.name = "bc";
#elif !BC_ENABLED
vm.name = "dc";
#else
// Just default to bc in that case.
vm.name = "bc";
#endif
}
// If the name is longer than the length of the prefix, skip the prefix.
if (strlen(vm.name) > len) vm.name += len;