mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Use FFstrbuf in emerge package counting
This commit is contained in:
parent
8a754f5b89
commit
8c6cb9405d
@ -45,7 +45,7 @@ Android, Arch, Arco, Artix, CachyOS, CentOS, Debian, Endeavour, Fedora, Garuda,
|
||||
|
||||
##### Package managers
|
||||
```
|
||||
Pacman, dpkg, rpm, xbps, Flatpak, Snap
|
||||
Pacman, dpkg, rpm, emerge, xbps, Flatpak, Snap
|
||||
```
|
||||
|
||||
##### WM themes
|
||||
|
@ -497,3 +497,9 @@ void ffPrintColor(const FFstrbuf* colorValue)
|
||||
ffStrbufWriteTo(colorValue, stdout);
|
||||
fputc('m', stdout);
|
||||
}
|
||||
|
||||
bool ffFileExists(const char* fileName, mode_t mode)
|
||||
{
|
||||
struct stat fileStat;
|
||||
return stat(fileName, &fileStat) == 0 && ((fileStat.st_mode & S_IFMT) == mode);
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ static inline void printCommandHelp(const char* command)
|
||||
}
|
||||
else if(strcasecmp(command, "packages-format") == 0)
|
||||
{
|
||||
constructAndPrintCommandHelpFormat("packages", "{2} (pacman){?3}[{3}]{?}, {4} (dpkg), {5} (rpm), {6} (xps), {7}, (flatpak), {8} (snap)", 8,
|
||||
constructAndPrintCommandHelpFormat("packages", "{2} (pacman){?3}[{3}]{?}, {4} (dpkg), {5} (rpm), {6} (xps), {7}, (flatpak), {8} (snap), {9} (emerge)", 9,
|
||||
"Number of all packages",
|
||||
"Number of pacman packages",
|
||||
"Pacman branch on manjaro",
|
||||
@ -103,7 +103,8 @@ static inline void printCommandHelp(const char* command)
|
||||
"Number of rpm packages",
|
||||
"Number of xbps packages",
|
||||
"Number of flatpak packages",
|
||||
"Number of snap packages"
|
||||
"Number of snap packages",
|
||||
"Number of emerge packages"
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(command, "shell-format") == 0)
|
||||
|
@ -10,8 +10,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include "fastfetch_config.h"
|
||||
@ -359,6 +361,8 @@ bool ffGetFileContent(const char* fileName, FFstrbuf* buffer);
|
||||
bool ffWriteFDContent(int fd, const FFstrbuf* content);
|
||||
void ffWriteFileContent(const char* fileName, const FFstrbuf* buffer);
|
||||
|
||||
bool ffFileExists(const char* fileName, mode_t mode);
|
||||
|
||||
// Not thread safe!
|
||||
void ffSuppressIO(bool suppress);
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define FF_PACKAGES_MODULE_NAME "Packages"
|
||||
#define FF_PACKAGES_NUM_FORMAT_ARGS 9
|
||||
@ -92,44 +91,35 @@ static uint32_t getNumStrings(const char* filename, const char* needle)
|
||||
return count;
|
||||
}
|
||||
|
||||
static const size_t BUFSIZE = PATH_MAX;
|
||||
static size_t filename_len_cache = 0;
|
||||
|
||||
// Make sure filename_len_cache is correct when calling this
|
||||
static uint32_t countFilesRecursive(const char* filename, char* dirBuffer, char* end)
|
||||
static uint32_t countFilesRecursive(FFstrbuf* baseDirPath, const char* filename)
|
||||
{
|
||||
if ((size_t)(end - dirBuffer) + filename_len_cache + 2 >= BUFSIZE)
|
||||
return 0;
|
||||
uint32_t baseDirPathLength = baseDirPath->length;
|
||||
|
||||
*end = '/';
|
||||
memcpy(end + 1, filename, filename_len_cache + 1); // Copy the NUL terminator too
|
||||
|
||||
struct stat exist;
|
||||
if (stat(dirBuffer, &exist) == 0 && (exist.st_mode & S_IFMT) == S_IFREG) // Found it, count it
|
||||
ffStrbufAppendC(baseDirPath, '/');
|
||||
ffStrbufAppendS(baseDirPath, filename);
|
||||
bool exists = ffFileExists(baseDirPath->chars, S_IFREG);
|
||||
ffStrbufSubstrBefore(baseDirPath, baseDirPathLength);
|
||||
if(exists)
|
||||
return 1;
|
||||
|
||||
*end = 0;
|
||||
|
||||
// Did not find it, search below
|
||||
DIR* dirp = opendir(dirBuffer);
|
||||
|
||||
if (!dirp)
|
||||
DIR* dirp = opendir(baseDirPath->chars);
|
||||
if(dirp == NULL)
|
||||
return 0;
|
||||
|
||||
*end = '/';
|
||||
ffStrbufAppendC(baseDirPath, '/');
|
||||
baseDirPathLength = baseDirPath->length;
|
||||
|
||||
uint32_t sum = 0;
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dirp)) != NULL) {
|
||||
while((entry = readdir(dirp)) != NULL) {
|
||||
// According to the PMS, neither category nor package name can begin with '.', so no need to check for . or .. specifically
|
||||
if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
|
||||
size_t len = strlen(entry->d_name);
|
||||
char *c = end + 1 + len;
|
||||
if ((size_t)(c - dirBuffer) < BUFSIZE - 1) {
|
||||
memcpy(end + 1, entry->d_name, len + 1); // Copy NUL terminator
|
||||
sum += countFilesRecursive(filename, dirBuffer, c);
|
||||
}
|
||||
}
|
||||
if(entry->d_type != DT_DIR || entry->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
ffStrbufAppendS(baseDirPath, entry->d_name);
|
||||
sum += countFilesRecursive(baseDirPath, filename);
|
||||
ffStrbufSubstrBefore(baseDirPath, baseDirPathLength);
|
||||
}
|
||||
|
||||
closedir(dirp);
|
||||
@ -138,17 +128,12 @@ static uint32_t countFilesRecursive(const char* filename, char* dirBuffer, char*
|
||||
|
||||
static uint32_t countFilesIn(const char* dirname, const char* filename)
|
||||
{
|
||||
char buffer[BUFSIZE];
|
||||
size_t len = strlen(dirname);
|
||||
|
||||
if (len >= BUFSIZE)
|
||||
return 0;
|
||||
|
||||
char* c = buffer + len;
|
||||
memcpy(buffer, dirname, len + 1); // Copy the NUL terminator too
|
||||
|
||||
filename_len_cache = strlen(filename); // Cache this
|
||||
return countFilesRecursive(filename, buffer, c);
|
||||
FFstrbuf baseDirPath;
|
||||
ffStrbufInitA(&baseDirPath, 128);
|
||||
ffStrbufAppendS(&baseDirPath, dirname);
|
||||
uint32_t result = countFilesRecursive(&baseDirPath, filename);
|
||||
ffStrbufDestroy(&baseDirPath);
|
||||
return result;
|
||||
}
|
||||
|
||||
void ffPrintPackages(FFinstance* instance)
|
||||
|
Loading…
x
Reference in New Issue
Block a user