From 3edaeb8343860ea89d2d7e65a8b199c4647fc0d7 Mon Sep 17 00:00:00 2001 From: radekchalupa Date: Tue, 21 Jan 2025 21:59:04 +0100 Subject: [PATCH] =?UTF-8?q?Nahr=C3=A1ny=20soubory=20do=20=E2=80=9E/?= =?UTF-8?q?=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 23 +++++++++ main.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 Makefile create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..419ff91 --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +CC = gcc +CFLAGS = -Wall -pedantic -std=c99 +target = rc-file-info +source = main.c +release = -O2 +valgrind = -ggdb3 + +$(target): Makefile $(source) +# @echo "sestavuji" + $(CC) -o $(target) $(source) $(release) $(CFLAGS) + +clean: +# @echo "provadim clean" + rm -f $(target) + +install: $(target) + @echo "Instaluji" + cp -f $(target) /usr/local/bin + sync + +valgrind: Makefile $(source) + @echo "valgrind" + $(CC) -o $(target) $(source) $(valgrind) $(CFLAGS) diff --git a/main.c b/main.c new file mode 100644 index 0000000..87027bb --- /dev/null +++ b/main.c @@ -0,0 +1,154 @@ +#define _POSIX_C_SOURCE 2 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define RC_OK 0 +#define RC_ERROR -1 + +static const char* _err_invalid_arg = "nebyl zadán platný soubor"; + +static int get_command_output_line(const char* cmd, + char* outbuff, size_t bufsize) +{ + FILE* pf = popen(cmd, "r"); + if (NULL == pf) { + return RC_ERROR; + } + int retval = RC_OK; + if (!feof(pf)) { + if (NULL == fgets(outbuff, bufsize, pf)) { + *outbuff = 0; + retval = RC_ERROR; + } + } + else + retval = RC_ERROR; + pclose(pf); + char* pnl = strrchr(outbuff, '\n'); + if (pnl) + *pnl = 0; + return retval; +} + +static int get_mime_type(const char* file_path, + char* outbuff, size_t buffsize) +{ + char* cmdbuff = (char*)malloc(strlen(file_path) + 64); + if (NULL == cmdbuff) + abort(); + strcpy(cmdbuff, "xdg-mime query filetype \""); + strcat(cmdbuff, file_path); + strcat(cmdbuff, "\""); + if (RC_OK != get_command_output_line(cmdbuff, outbuff, buffsize)) { + free(cmdbuff); + return RC_ERROR; + } + free(cmdbuff); + return RC_OK; +} + +static int get_app_name(const char* desktop_file, + char* outbuff, size_t buffsize) +{ + char filepath[PATH_MAX]; + strcpy(filepath, "/usr/share/applications/"); + strncat(filepath, desktop_file, sizeof(filepath) - strlen(filepath) - 1); + FILE* pf = fopen(filepath, "r"); + if (NULL == pf) + return RC_ERROR; + char line[512]; + *outbuff = 0; + while (fgets(line, sizeof(line), pf)) { + if (sscanf(line, "Name=%s", outbuff) == 1) + break; + } + fclose(pf); + return (0 == *outbuff) ? RC_ERROR : RC_OK; +} + +static int get_mime_opener(const char* mimetype, + char* outbuff, size_t buffsize) +{ + char cmdbuff[256]; + strcpy(cmdbuff, "xdg-mime query default "); + strncat(cmdbuff, mimetype, sizeof(cmdbuff) - strlen(cmdbuff) - 1); + return get_command_output_line(cmdbuff, outbuff, buffsize); +} + +static int show_file_info(const char* file_path) +{ + struct stat fst; + char buff[256]; + struct passwd* pwd; + if (0 != stat(file_path, &fst)) { + return errno; + } + printf("velikost: %lu MiB (%lu bytů)\n", + fst.st_size / 1024 / 1024, fst.st_size); + printf("na disku: %lu MiB (%lu bytů)\n", + (fst.st_blocks * 512) / 1024 / 1024, fst.st_blocks * 512); + printf("pevných odkazů: %lu\n", fst.st_nlink); + strftime(buff, sizeof(buff), "poslední přístup: %d.%m.%Y %H:%M:%S", + localtime((const time_t*)&fst.st_atime)); + puts(buff); + strftime(buff, sizeof(buff), " poslední úprava: %d.%m.%Y %H:%M:%S", + localtime((const time_t*)&fst.st_mtime)); + puts(buff); + strftime(buff, sizeof(buff), " změna stavu: %d.%m.%Y %H:%M:%S", + localtime((const time_t*)&fst.st_ctime)); + puts(buff); + char permission[4]; + unsigned int uperm; + uperm = 100 * (0x7 & (fst.st_mode >> 6)) + + 10 * (0x7 & (fst.st_mode >> 3)) + + (0x7 & fst.st_mode); + permission[3] = 0; + permission[0] = (S_IRUSR & fst.st_mode) ? 'r' : '-'; + permission[1] = (S_IWUSR & fst.st_mode) ? 'w' : '-'; + permission[2] = (S_IXUSR & fst.st_mode) ? 'x' : '-'; + if ((pwd = getpwuid(fst.st_uid)) != NULL) { + printf("vlastník: %s (%d)\n", pwd->pw_name, pwd->pw_uid); + } + else { + printf("User ID: %u\n", fst.st_uid); + } + printf("oprávnění: %u\n", uperm); + printf("uživatel: %s", permission); + permission[0] = (S_IRGRP & fst.st_mode) ? 'r' : '-'; + permission[1] = (S_IWGRP & fst.st_mode) ? 'w' : '-'; + permission[2] = (S_IXGRP & fst.st_mode) ? 'x' : '-'; + printf(" skupina: %s", permission); + permission[0] = (S_IROTH & fst.st_mode) ? 'r' : '-'; + permission[1] = (S_IWOTH & fst.st_mode) ? 'w' : '-'; + permission[2] = (S_IXOTH & fst.st_mode) ? 'x' : '-'; + printf(" ostatní: %s\n", permission); + if (RC_OK == get_mime_type(file_path, buff, sizeof(buff))) { + printf("mime typ: %s\n", buff); + if (RC_OK == get_mime_opener(buff, buff, sizeof(buff))) { + printf("výchozí aplikace: %s", buff); + if (RC_OK == get_app_name(buff, buff, sizeof(buff))) { + printf(" (%s)", buff); + } + printf("\n"); + } + } + printf("\nstiskni enter pro ukončení..."); + getchar(); + return EXIT_SUCCESS; +} + +int main(int argc, char** argv) +{ + if (argc < 2) { + puts(_err_invalid_arg); + return EXIT_FAILURE; + } + return show_file_info(argv[1]); +}