bbfdm/libbbfdm-api/version-2/bbfdm_system.c
2025-05-27 14:26:24 +05:30

90 lines
1.5 KiB
C

/*
* Copyright (C) 2025 iopsys Software Solutions AB
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
*
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/stat.h>
bool bbfdm_folder_exists(const char *path)
{
struct stat buffer;
if (!path)
return false;
return stat(path, &buffer) == 0 && S_ISDIR(buffer.st_mode);
}
bool bbfdm_file_exists(const char *path)
{
struct stat buffer;
if (!path)
return false;
return stat(path, &buffer) == 0;
}
bool bbfdm_file_nonzero(const char *path)
{
struct stat file_stats = {0};
int ret;
if (!path)
return false;
ret = stat(path, &file_stats);
if ((ret == 0) && (file_stats.st_size != 0)) {
return true;
}
return false;
}
bool bbfdm_is_regular_file(const char *path)
{
struct stat buffer;
if (!path)
return false;
return stat(path, &buffer) == 0 && S_ISREG(buffer.st_mode);
}
int bbfdm_create_empty_file(const char *path)
{
if (!path)
return -1;
// Skip creating the file if it already exists
if (bbfdm_file_exists(path))
return 0;
FILE *fp = fopen(path, "w");
if (fp == NULL)
return -1;
fclose(fp);
return 0;
}
void bbfdm_strncpy(char *dst, const char *src, size_t n)
{
if (dst == NULL || src == NULL)
return;
if (n > 1) {
strncpy(dst, src, n - 1);
dst[n - 1] = 0;
}
}