Skip to content

Commit

Permalink
use NOSHELL_DEBUG
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
viperML committed May 22, 2024
1 parent 8003113 commit 80df12b
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@

#include "noshell.h"

static bool DEBUG = false;

#define eprint_d(...) \
{ \
if (DEBUG) { \
fprintf(stderr, __VA_ARGS__); \
} \
};

#define eprint(...) \
{ fprintf(stderr, __VA_ARGS__); };

bool usable(char const* path) {
char real[PATH_MAX];
if (realpath(path, real) == NULL) {
Expand Down Expand Up @@ -67,7 +79,7 @@ char* getshell(void) {
}
}

fprintf(stderr, "WARN: Using fallback shell\n");
eprint("WARN: Using fallback shell\n");
return strdup(DEFAULT_SHELL);
}

Expand All @@ -77,7 +89,7 @@ char* getshell(void) {
void argv0_deref(char** argv0_p) {
struct stat sb = {};
if (lstat(*argv0_p, &sb) != 0) {
fprintf(stderr, "WARN: Failed to stat the shell\n");
eprint("WARN: Failed to stat the shell\n");
return;
}

Expand All @@ -89,7 +101,7 @@ void argv0_deref(char** argv0_p) {
if (S_ISLNK(sb.st_mode)) {
char buf[PATH_MAX + 1];
if (readlink(*argv0_p, buf, PATH_MAX) == -1) {
fprintf(stderr, "Failed to readlink\n");
eprint("Failed to readlink\n");
return;
}
*argv0_p = strdup(basename(buf));
Expand All @@ -99,14 +111,19 @@ void argv0_deref(char** argv0_p) {

int main(int argc, char* argv[]) {
(void)argc;

if (getenv("NOSHELL_DEBUG") != NULL) {
DEBUG = true;
}

char* shell = getshell();

if (shell == NULL) {
fprintf(stderr, "ERROR: Failed to detect shell");
eprint("ERROR: Failed to detect shell");
return EXIT_FAILURE;
}

fprintf(stderr, "INFO: Using %s\n", shell);
eprint_d("INFO: Using %s\n", shell);

bool login_dash = argv[0][0] == '-';

Expand Down

0 comments on commit 80df12b

Please sign in to comment.