Skip to content

Commit

Permalink
Flush stdout when needed
Browse files Browse the repository at this point in the history
This is required on certain machines (eg the iSH emulator on iOS)
  • Loading branch information
kabiroberai committed Jan 31, 2020
1 parent b02201d commit 58a7a86
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mcrcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ void packet_print(rc_packet *packet)

// print newline if string has no newline
if (packet->data[i-1] != 10 && packet->data[i-1] != 13) putchar('\n');

fflush(stdout);
}

rc_packet *packet_build(int id, int cmd, char *s1)
Expand Down Expand Up @@ -654,6 +656,7 @@ int run_terminal_mode(int sock)

while (global_connection_alive) {
putchar('>');
fflush(stdout);
int len = get_line(command, DATA_BUFFSIZE);

if ((strcasecmp(command, "exit") && strcasecmp(command, "quit")) == 0)
Expand Down

2 comments on commit 58a7a86

@unicodepepper
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this.

I am trying to set up a script that monitors the number of users in my server every minute, running with cron.

My first setup was simply adding /opt/minecraft/tools/mcrcon/mcrcon -p password "list" | head -n1 to the script and reading the output. However, a problem with that is that it adds two new lines to the server log every time it runs, and over the course of a day it gets very spammy.

I then managed to open an rcon session in tmux, then redirect the output to a file, and send commands using tmux send-keys -t 0 list. However, this showed another problem - the output is only written to disk after I kill mcrcon.

I was about to file a bug report but then I found your fix, and while I couldn't figure out how to clone it or merge it onto my copy of the repo, I did manage to simply edit the mcrcon.c file and add the fflush(stdout); lines where you placed them. This worked like a charm.

Hope you have a wonderful day.

@kabiroberai
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unicodepepper I’m glad I could help :)

Please sign in to comment.