Skip to content

Commit

Permalink
fix: use fileno(3) instead of STDOUT_FILENO defines
Browse files Browse the repository at this point in the history
this improves POSIX compliancy and facilitates the juggling of file
descriptors from callers
jaromil committed Dec 8, 2024
1 parent de9166f commit 88429a7
Showing 3 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/io.c
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ void _out(const char *fmt, ...) {
#if defined(__EMSCRIPTEN__)
EM_ASM_({Module.print(UTF8ToString($0))}, msg);
#else
write(STDOUT_FILENO, msg, len+1);
write(fileno(stdout), msg, len+1);
#endif
}

@@ -56,6 +56,6 @@ void _err(const char *fmt, ...) {
#elif defined(__ANDROID__)
__android_log_print(ANDROID_LOG_ERROR, "ZEN", "%s", msg);
#else
write(STDERR_FILENO,msg,len+1);
write(fileno(stderr),msg,len+1);
#endif
}
16 changes: 8 additions & 8 deletions src/kilo.c
Original file line number Diff line number Diff line change
@@ -228,7 +228,7 @@ void disableRawMode(int fd) {
void editorAtExit(void) {
if (!E.exiting)
return;
disableRawMode(STDIN_FILENO);
disableRawMode(fileno(stdin));
if ((!E.keep_scratchpad) && (E.filename)) {
char *cp1, *dir_nm;
cp1 = strdup(E.filename);
@@ -245,7 +245,7 @@ int enableRawMode(int fd) {
struct termios raw;

if (E.rawmode) return 0; /* Already enabled. */
if (!isatty(STDIN_FILENO)) goto fatal;
if (!isatty(fileno(stdin))) goto fatal;
if (tcgetattr(fd,&orig_termios) == -1) goto fatal;

raw = orig_termios; /* modify the original mode */
@@ -277,9 +277,9 @@ int enableRawMode(int fd) {
int enableGetCharMode(int fd) {
struct termios nocanon;

if (!isatty(STDIN_FILENO))
if (!isatty(fileno(stdin)))
goto fatal;
disableRawMode(STDIN_FILENO);
disableRawMode(fileno(stdin));
if (tcgetattr(fd,&orig_termios) == -1)
goto fatal;

@@ -1115,7 +1115,7 @@ void editorRefreshScreen(void) {
snprintf(buf,sizeof(buf),"\x1b[%d;%dH",E.cy+1,cx);
abAppend(&ab,buf,strlen(buf));
abAppend(&ab,"\x1b[?25h",6); /* Show cursor. */
write(STDOUT_FILENO,ab.b,ab.len);
write(fileno(stdout),ab.b,ab.len);
abFree(&ab);
/* Check for errors on new line */
if (E.check_cb) {
@@ -1385,11 +1385,11 @@ void editorProcessKeypress(int fd) {
E.dirty++;
editorSave();
strcat(ed_cmd, E.filename);
disableRawMode(STDIN_FILENO);
disableRawMode(fileno(stdin));
editorReset();
printf("Running %s\n", ed_cmd);
system(ed_cmd);
enableRawMode(STDIN_FILENO);
enableRawMode(fileno(stdin));
editorOpen();
break;
case CTRL_R: /* Ctrl-r */
@@ -1445,7 +1445,7 @@ int editorFileWasModified(void) {
}

void updateWindowSize(void) {
if (getWindowSize(STDIN_FILENO,STDOUT_FILENO,
if (getWindowSize(fileno(stdin),fileno(stdout),
&E.screenrows,&E.screencols) == -1) {
perror("Unable to query the screen for size (columns / rows)");
exit(1);
18 changes: 9 additions & 9 deletions src/repl.c
Original file line number Diff line number Diff line change
@@ -210,22 +210,22 @@ static int cjit_compile_buffer(void *tcs, char *code, int argc, char **argv)
TCCState *TCC = (TCCState *)tcs;
int res = 0;
char *err_msg;
disableRawMode(STDIN_FILENO);
disableRawMode(fileno(stdin));
/* Clear the screen */
write(STDOUT_FILENO, "\x1b[2J", 4);
write(fileno(stdout), "\x1b[2J", 4);
// run the code from main
res = cjit_compile_and_run(TCC, code, argc, argv, 1, &err_msg);
if (err_msg) {
_err(err_msg);
free(err_msg);
}
enableGetCharMode(STDIN_FILENO);
enableGetCharMode(fileno(stdin));
_err("\n\n\n\nPress any key to continue....\n");
getchar();
disableGetCharMode(STDIN_FILENO);
disableGetCharMode(fileno(stdin));
free(code);

enableRawMode(STDIN_FILENO);
enableRawMode(fileno(stdin));
editorRefreshScreen();
return res;
}
@@ -238,7 +238,7 @@ int cjit_check_buffer(void *tcs, char *code, char **err_msg) {
*err_msg = NULL;
// run the code from main
//
disableRawMode(STDIN_FILENO);
disableRawMode(fileno(stdin));
res = cjit_compile_and_run(TCC, code, 0, NULL, 0, err_msg);
if (res != 0) {
if(*err_msg) {
@@ -255,7 +255,7 @@ int cjit_check_buffer(void *tcs, char *code, char **err_msg) {
} else {
editorSetStatusMessage("No errors.");
}
enableRawMode(STDIN_FILENO);
enableRawMode(fileno(stdin));
return res;
}

@@ -305,15 +305,15 @@ int cjit_cli_kilo(TCCState *TCC) {
editorInsertRow(row++, editor_rows[i], strlen(editor_rows[i]));
}

enableRawMode(STDIN_FILENO);
enableRawMode(fileno(stdin));
editorSetStatusMessage(
"HELP: Cx-S = save | Cx-Q = quit | Cx-F = find | Cx-R = run | Cx-E = editor");
while(1) {
editorSetCompilerCallback(cjit_compile_buffer);
editorSetCheckCallback(cjit_check_buffer);
editorSetCompilerContext(TCC);
editorRefreshScreen();
editorProcessKeypress(STDIN_FILENO);
editorProcessKeypress(fileno(stdin));
}
}
return res;

0 comments on commit 88429a7

Please sign in to comment.