wrapper use time and not size

This commit is contained in:
Quinten 2023-05-06 11:38:55 +02:00 committed by GitHub
parent bf1217839d
commit 9c08f3fd5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 40 deletions

View File

@ -8,7 +8,7 @@
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
void print_latest_log(const char* log_directory, off_t* last_pos, off_t* last_size) { void print_latest_log(const char* log_directory, off_t* last_pos, time_t* last_time) {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
time_t latest_time = 0; time_t latest_time = 0;
@ -39,58 +39,57 @@ void print_latest_log(const char* log_directory, off_t* last_pos, off_t* last_si
return; return;
} }
if (latest_time <= *last_time) {
return;
}
int fd = open(latest_file, O_RDONLY); int fd = open(latest_file, O_RDONLY);
if (fd < 0) { if (fd < 0) {
printf("Error opening file %s\n", latest_file); printf("Error opening file %s\n", latest_file);
return; return;
} }
// Get the size of the file // Move the file pointer to the last printed position
off_t size = lseek(fd, 0, SEEK_END); lseek(fd, *last_pos, SEEK_SET);
// Check if the file size has increased since the last read char buffer[4096];
if (size > *last_size) { int bytes_read;
// Move the file pointer to the beginning of the file int last_line_printed = 0; // Flag to check whether we have printed the last line
lseek(fd, 0, SEEK_SET); do {
bytes_read = read(fd, buffer, sizeof(buffer));
char buffer[4096]; if (bytes_read > 0) {
int bytes_read; // Check if the last character is a newline
int last_line_printed = 0; // Flag to check whether we have printed the last line if (buffer[bytes_read - 1] == '\n') {
do { fwrite(buffer, 1, bytes_read, stdout);
bytes_read = read(fd, buffer, sizeof(buffer)); fflush(stdout);
if (bytes_read > 0) { } else {
// Check if the last character is a newline // If the last character is not a newline, add one
if (buffer[bytes_read - 1] == '\n') { char* temp = (char*) malloc(bytes_read + 1);
fwrite(buffer, 1, bytes_read, stdout); memcpy(temp, buffer, bytes_read);
fflush(stdout); temp[bytes_read] = '\n';
} else { fwrite(temp, 1, bytes_read + 1, stdout);
// If the last character is not a newline, add one fflush(stdout);
char* temp = (char*) malloc(bytes_read + 1); free(temp);
memcpy(temp, buffer, bytes_read);
temp[bytes_read] = '\n';
fwrite(temp, 1, bytes_read + 1, stdout);
fflush(stdout);
free(temp);
}
last_line_printed = (buffer[bytes_read - 1] == '\n');
} }
} while (bytes_read > 0); last_line_printed = (buffer[bytes_read - 1] == '\n');
// If the last line was not printed, print it now
if (!last_line_printed) {
printf("\n");
} }
} while (bytes_read > 0);
// Remember the last position and size that was read // If the last line was not printed, print it now
*last_pos = lseek(fd, 0, SEEK_CUR); if (!last_line_printed) {
*last_size = size; printf("\n");
} }
close(fd);
// Remember the last position and time that was read
*last_pos = lseek(fd, 0, SEEK_CUR);
*last_time = latest_time;
close(fd);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (argc < 2) { if (argc < 2) {
printf("Usage: winewrapper wine_path wine_args exe_path exe_args\n"); printf("Usage: winewrapper wine_path wine_args exe_path exe_args\n");