Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions src/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,27 @@ void upperCase(char *str)
char *pipeRead(const char *exec)
{
FILE *pipe = popen(exec, "r");
if (pipe == NULL)
if (!pipe)
return NULL;
char *returnVal = malloc(256);
const int scanf_return = fscanf(pipe, "%[^\n]256s", returnVal);

char buffer[256];

if (!fgets(buffer, sizeof(buffer), pipe)) {
pclose(pipe);
return NULL;
}

pclose(pipe);
if (scanf_return == EOF) {
fprintf(stderr, "ERROR: scanf failed!\n");

buffer[strcspn(buffer, "\n")] = '\0';

char *result = strdup(buffer);
if (!result) {
perror("strdup");
exit(EXIT_FAILURE);
}
return returnVal;

return result;
}

void *kernel()
Expand Down Expand Up @@ -105,11 +116,14 @@ void *user()
void *shell()
{
char *shell = getenv("SHELL");
char *slash = strrchr(shell, '/');
if (slash) {
shell = slash + 1;
if (!shell) {
shellname = "unknown";
return NULL;
}
shellname = shell;

char *slash = strrchr(shell, '/');
shellname = slash ? slash + 1 : shell;

return NULL;
}

Expand All @@ -121,22 +135,19 @@ void *os()
/* This whole section could probably be rewritten - it seems
like a bit of a mess right now */
if (strncmp(sysInfo.sysname, "Linux", 5) == 0) {
char *osContents = malloc(512);
char *newContents = malloc(512);
int line = 0;
char osContents[512];
char newContents[512];
FILE *f = fopen("/etc/os-release", "rt");
if (f == NULL || osContents == NULL)
if (f == NULL)
return "Linux";
/* look through each line of /etc/os-release until we're on the
* NAME= line */
while (fgets(osContents, 512, f)) {
snprintf(newContents, 512, "%.*s", 511, osContents + 4);
if (strncmp(newContents, "=", 1) == 0)
break;
line++;
}
fclose(f);
free(osContents);
if (strncmp(newContents, "=", 1) == 0) {
int len = strlen(newContents);
for (int i = 0; i < len; i++) {
Expand All @@ -153,7 +164,6 @@ void *os()
if (osname == NULL)
osname = malloc(512);
strcpy(osname, newContents);
free(newContents);
/* end */
if (strncmp(osname, "Alpine Linux", 12) == 0) {
info.col1 = BBLUE "\n";
Expand Down Expand Up @@ -456,28 +466,26 @@ void *os()
info.col8 = BBLUE " `.__.-.__.' " BYELLOW;
if ((strncmp(sysInfo.machine, "iPhone", 6) == 0) || (strncmp(sysInfo.machine, "iPad", 4) == 0) || (strncmp(sysInfo.machine, "iPod", 4) == 0)) {
info.getPkgCount =
"dpkg -l | tail -n+6 | wc -l";
"dpkg -l | tail -n+6 | wc -l";

char *iosVer = malloc(1024);
strcpy(iosVer, "iOS ");
char *productVer = pipeRead("sw_vers -productVersion");

strcat(iosVer, productVer);
free(productVer);
osname = iosVer;
free(iosVer);
if (productVer) {
size_t len = strlen("iOS ") + strlen(productVer) + 1;
osname = malloc(len);
snprintf(osname, len, "iOS %s", productVer);
free(productVer);
}
} else {
info.getPkgCount =
"ls /usr/local/Cellar/* | grep ':' | wc -l | xargs";

char *macVer = malloc(64);
strcpy(macVer, "macOS ");
char *productVer = pipeRead("sw_vers -productVersion");

strcat(macVer, productVer);
free(productVer);
osname = macVer;
free(macVer);
if (productVer) {
size_t len = strlen("macOS ") + strlen(productVer) + 1;
osname = malloc(len);
snprintf(osname, len, "macOS %s", productVer);
free(productVer);
}
}
} else if (strncmp(sysInfo.sysname, "FreeBSD", 7) == 0) {
info.col1 = BRED "";
Expand Down Expand Up @@ -563,5 +571,6 @@ int main()
pthread_create(&threads[5], NULL, colourDraw, NULL);
pthread_join(threads[5], NULL);
printf("%s", RESET);

return 0;
}