From b4ae1e8162ed227ee3a41c6c562af41666855109 Mon Sep 17 00:00:00 2001 From: subrata05 Date: Fri, 3 Apr 2026 13:41:08 +0530 Subject: [PATCH] fix: resolve all compiler warnings in logger.c and tui.c - logger: add safe_write() wrapper, fix strncpy usage - tui: remove unused cols variable and visible_rows function --- src/features/logger.c | 185 +++++++++++++++++++++++++----------------- src/ui/tui.c | 2 + 2 files changed, 114 insertions(+), 73 deletions(-) diff --git a/src/features/logger.c b/src/features/logger.c index 3d17039..979eca0 100644 --- a/src/features/logger.c +++ b/src/features/logger.c @@ -1,21 +1,21 @@ /* * ComScope - Serial Port Terminal for Embedded Development * Copyright (c) 2026 Prakash Das - * + * * This file is part of ComScope. * ComScope is free software: you can redistribute it and/or modify * it under the terms of the MIT License. - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -35,10 +35,11 @@ #include #include #include +#include #include "logger.h" -#define LOG_BUFFER_SIZE 4096 // 4KB buffer -#define FSYNC_INTERVAL_SEC 1 // Sync to disk every 1 second +#define LOG_BUFFER_SIZE 4096 // 4KB buffer +#define FSYNC_INTERVAL_SEC 1 // Sync to disk every 1 second static int log_fd = -1; static char log_buffer[LOG_BUFFER_SIZE]; @@ -46,11 +47,30 @@ static int log_buffer_used = 0; static struct timespec last_fsync = {0, 0}; static char current_log_path[1024] = {0}; +/* Safe write with error handling */ +static int safe_write(int fd, const void *buf, size_t count, const char *context) +{ + ssize_t written = write(fd, buf, count); + if (written < 0) + { + fprintf(stderr, "Logger: Write error (%s): %s\n", context, strerror(errno)); + return -1; + } + if ((size_t)written != count) + { + fprintf(stderr, "Logger: Partial write (%s): %zd of %zu bytes\n", + context, written, count); + return -1; + } + return 0; +} + /* Create logs directory if it doesn't exist */ static int ensure_log_directory(void) { const char *home = getenv("HOME"); - if (!home) { + if (!home) + { fprintf(stderr, "Error: HOME environment variable not set\n"); return -1; } @@ -62,9 +82,11 @@ static int ensure_log_directory(void) snprintf(docs_dir, sizeof(docs_dir), "%s/Documents", home); mkdir(docs_dir, 0755); - if (mkdir(log_dir, 0755) < 0) { + if (mkdir(log_dir, 0755) < 0) + { struct stat st; - if (stat(log_dir, &st) != 0 || !S_ISDIR(st.st_mode)) { + if (stat(log_dir, &st) != 0 || !S_ISDIR(st.st_mode)) + { fprintf(stderr, "Error: Cannot create log directory: %s\n", log_dir); return -1; } @@ -77,7 +99,8 @@ static int ensure_log_directory(void) static int generate_log_filename(char *filename, size_t size) { const char *home = getenv("HOME"); - if (!home) { + if (!home) + { fprintf(stderr, "Error: HOME environment variable not set\n"); return -1; } @@ -85,17 +108,18 @@ static int generate_log_filename(char *filename, size_t size) time_t now = time(NULL); struct tm *t = localtime(&now); - int ret = snprintf(filename, size, - "%s/Documents/ComScope/ComScope_%04d-%02d-%02d_%02d-%02d-%02d.txt", - home, - t->tm_year + 1900, - t->tm_mon + 1, - t->tm_mday, - t->tm_hour, - t->tm_min, - t->tm_sec); - - if (ret < 0 || (size_t)ret >= size) { + int ret = snprintf(filename, size, + "%s/Documents/ComScope/ComScope_%04d-%02d-%02d_%02d-%02d-%02d.txt", + home, + t->tm_year + 1900, + t->tm_mon + 1, + t->tm_mday, + t->tm_hour, + t->tm_min, + t->tm_sec); + + if (ret < 0 || (size_t)ret >= size) + { fprintf(stderr, "Error: Log filename too long\n"); return -1; } @@ -106,36 +130,39 @@ static int generate_log_filename(char *filename, size_t size) /* Flush buffer to disk (internal use) */ static void flush_log_buffer(void) { - if (log_buffer_used > 0 && log_fd >= 0) { - ssize_t written = write(log_fd, log_buffer, log_buffer_used); - (void)written; // Best effort + if (log_buffer_used > 0 && log_fd >= 0) + { + safe_write(log_fd, log_buffer, log_buffer_used, "buffer"); log_buffer_used = 0; } } int logger_start(const char *path) { - (void)path; // Ignore passed path, use auto-generated + (void)path; // Ignore passed path, use auto-generated /* Reset state */ log_buffer_used = 0; clock_gettime(CLOCK_MONOTONIC, &last_fsync); - if (ensure_log_directory() < 0) { + if (ensure_log_directory() < 0) + { fprintf(stderr, "Warning: Could not create log directory\n"); return -1; } char log_filename[1024]; - if (generate_log_filename(log_filename, sizeof(log_filename)) < 0) { + if (generate_log_filename(log_filename, sizeof(log_filename)) < 0) + { fprintf(stderr, "Warning: Could not generate log filename\n"); return -1; } - strncpy(current_log_path, log_filename, sizeof(current_log_path) - 1); + strcpy(current_log_path, log_filename); log_fd = open(log_filename, O_WRONLY | O_CREAT | O_APPEND, 0644); - if (log_fd < 0) { + if (log_fd < 0) + { fprintf(stderr, "Warning: Could not open log file: %s\n", log_filename); return -1; } @@ -145,20 +172,21 @@ int logger_start(const char *path) struct tm *t = localtime(&now); char header[512]; int header_len = snprintf(header, sizeof(header), - "\n========================================\n" - "ComScope Session Started\n" - "Date: %04d-%02d-%02d\n" - "Time: %02d:%02d:%02d\n" - "========================================\n\n", - t->tm_year + 1900, - t->tm_mon + 1, - t->tm_mday, - t->tm_hour, - t->tm_min, - t->tm_sec); - - if (header_len > 0 && (size_t)header_len < sizeof(header)) { - write(log_fd, header, (size_t)header_len); + "\n========================================\n" + "ComScope Session Started\n" + "Date: %04d-%02d-%02d\n" + "Time: %02d:%02d:%02d\n" + "========================================\n\n", + t->tm_year + 1900, + t->tm_mon + 1, + t->tm_mday, + t->tm_hour, + t->tm_min, + t->tm_sec); + + if (header_len > 0 && (size_t)header_len < sizeof(header)) + { + safe_write(log_fd, header, (size_t)header_len, "header"); } /* Initial fsync for header */ @@ -170,30 +198,38 @@ int logger_start(const char *path) void logger_write(const char *buf, int n) { - if (log_fd < 0 || n <= 0) return; - + if (log_fd < 0 || n <= 0) + return; + /* If data is larger than buffer, flush existing and write directly */ - if (n > (int)(LOG_BUFFER_SIZE - log_buffer_used)) { + if (n > (int)(LOG_BUFFER_SIZE - log_buffer_used)) + { flush_log_buffer(); - - if (n > LOG_BUFFER_SIZE) { + + if (n > LOG_BUFFER_SIZE) + { /* Large write, bypass buffer */ - write(log_fd, buf, n); - } else { + safe_write(log_fd, buf, n, "large data"); // Fix ignored write + } + else + { memcpy(log_buffer + log_buffer_used, buf, n); log_buffer_used += n; } - } else { + } + else + { /* Buffer the data */ memcpy(log_buffer + log_buffer_used, buf, n); log_buffer_used += n; } - + /* Periodic fsync (every FSYNC_INTERVAL_SEC seconds) */ struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - - if (now.tv_sec > last_fsync.tv_sec + FSYNC_INTERVAL_SEC) { + + if (now.tv_sec > last_fsync.tv_sec + FSYNC_INTERVAL_SEC) + { flush_log_buffer(); fsync(log_fd); last_fsync = now; @@ -202,7 +238,8 @@ void logger_write(const char *buf, int n) void logger_stop(void) { - if (log_fd < 0) return; + if (log_fd < 0) + return; /* Flush remaining buffer */ flush_log_buffer(); @@ -212,23 +249,24 @@ void logger_stop(void) struct tm *t = localtime(&now); char footer[512]; int footer_len = snprintf(footer, sizeof(footer), - "\n========================================\n" - "ComScope Session Ended\n" - "Date: %04d-%02d-%02d\n" - "Time: %02d:%02d:%02d\n" - "========================================\n\n", - t->tm_year + 1900, - t->tm_mon + 1, - t->tm_mday, - t->tm_hour, - t->tm_min, - t->tm_sec); - - if (footer_len > 0 && (size_t)footer_len < sizeof(footer)) { - write(log_fd, footer, (size_t)footer_len); + "\n========================================\n" + "ComScope Session Ended\n" + "Date: %04d-%02d-%02d\n" + "Time: %02d:%02d:%02d\n" + "========================================\n\n", + t->tm_year + 1900, + t->tm_mon + 1, + t->tm_mday, + t->tm_hour, + t->tm_min, + t->tm_sec); + + if (footer_len > 0 && (size_t)footer_len < sizeof(footer)) + { + safe_write(log_fd, footer, (size_t)footer_len, "footer"); // Fix ignored write } - - fsync(log_fd); // Final sync + + fsync(log_fd); // Final sync close(log_fd); log_fd = -1; log_buffer_used = 0; @@ -242,8 +280,9 @@ int logger_active(void) const char *logger_get_log_dir(void) { const char *home = getenv("HOME"); - if (!home) return NULL; - + if (!home) + return NULL; + static char log_dir[1024]; snprintf(log_dir, sizeof(log_dir), "%s/Documents/ComScope", home); return log_dir; diff --git a/src/ui/tui.c b/src/ui/tui.c index 2a3bc5c..08a706f 100644 --- a/src/ui/tui.c +++ b/src/ui/tui.c @@ -63,6 +63,7 @@ static inline long long timespec_diff_ns(struct timespec *a, struct timespec *b) return (a->tv_sec - b->tv_sec) * 1000000000LL + (a->tv_nsec - b->tv_nsec); } +static int visible_rows(void) __attribute__((unused)); // avoid compilation warning static int visible_rows(void) { int rows, cols; @@ -200,6 +201,7 @@ void tui_refresh(void) int rows, cols; getmaxyx(stdscr, rows, cols); int display_rows = rows - 1; // Reserve bottom row for status + (void)cols; // Suppress unused warning clear();