static void block_update_plain_text(struct block *block, char *buf) { struct properties *props = &block->updated_props; char *lines = buf; linecpy(&lines, props->full_text, sizeof(props->full_text) - 1); linecpy(&lines, props->short_text, sizeof(props->short_text) - 1); linecpy(&lines, props->color, sizeof(props->color) - 1); }
static int update_block(struct block *block) { FILE *child_stdout; int child_status, code; char output[2048], *text = output; if (setup_env(block)) return 1; /* Pipe, fork and exec a shell for the block command line */ child_stdout = popen(block->command, "r"); if (!child_stdout) { perror("popen"); return 1; } /* Do not distinguish EOF or error, just read child's output */ memset(output, 0, sizeof(output)); fread(output, 1, sizeof(output) - 1, child_stdout); /* Wait for the child process to terminate */ child_status = pclose(child_stdout); if (child_status == -1) { perror("pclose"); return 1; } if (!WIFEXITED(child_status)) { fprintf(stderr, "child did not exit correctly\n"); return 1; } code = WEXITSTATUS(child_status); if (code != 0 && code != 127) { char reason[1024]; fprintf(stderr, "bad return code %d, skipping %s\n", code, block->name); sprintf(reason, "[%s] ERROR: bad return code %d", block->name, code); failed(reason, block); return 1; } /* From here, the update went ok so merge the output */ strncpy(block->urgent, code == 127 ? "true" : "false", sizeof(block->urgent) - 1); linecpy(&text, block->full_text, sizeof(block->full_text) - 1); linecpy(&text, block->short_text, sizeof(block->short_text) - 1); linecpy(&text, block->color, sizeof(block->color) - 1); block->last_update = time(NULL); return 0; }
void block_update(struct block *block) { FILE *child_stdout; int child_status, code; char output[2048], *text = output; if (setup_env(block)) return mark_as_failed(block, "failed to setup env", -1); /* Pipe, fork and exec a shell for the block command line */ child_stdout = popen(block->command, "r"); if (!child_stdout) { berrorx(block, "popen(%s)", block->command); return mark_as_failed(block, "failed to fork", -1); } /* Do not distinguish EOF or error, just read child's output */ memset(output, 0, sizeof(output)); fread(output, 1, sizeof(output) - 1, child_stdout); /* Wait for the child process to terminate */ child_status = pclose(child_stdout); if (child_status == -1) { berrorx(block, "pclose"); return mark_as_failed(block, "failed to wait", -1); } if (!WIFEXITED(child_status)) { berror(block, "child did not exit correctly"); return mark_as_failed(block, "command did not exit", -1); } code = WEXITSTATUS(child_status); if (code != 0 && code != '!') { char reason[1024] = { 0 }; berror(block, "bad exit code %d", code); linecpy(&text, reason, sizeof(reason) - 1); return mark_as_failed(block, reason, code); } /* From here, the update went ok so merge the output */ strncpy(block->urgent, code == '!' ? "true" : "false", sizeof(block->urgent) - 1); linecpy(&text, block->full_text, sizeof(block->full_text) - 1); linecpy(&text, block->short_text, sizeof(block->short_text) - 1); linecpy(&text, block->color, sizeof(block->color) - 1); block->last_update = time(NULL); bdebug(block, "updated successfully"); }
int findsub(char* substr, char* eldstr) { char* peldstr = malloc(5*sizeof(char)); char* tempstr = malloc(5*sizeof(char)); peldstr = eldstr; int lensub = strlength(substr); int leneld = strlength(eldstr); while (peldstr - eldstr + lensub <= leneld) { linecpy(peldstr++, tempstr); if (!linecmp(substr, _linecut(tempstr, lensub))) { return peldstr - eldstr; } } return 0; }
int main() { char* linexmpl = malloc(5*sizeof(char)); char* cpdline = malloc(5*sizeof(char)); char* subline = malloc(5*sizeof(char)); int pos = 0; inputline(linexmpl); ASSERT(linexmpl > 0); printf("\nLength of line '%s':%d\n\n", linexmpl, strlength(linexmpl)); linecpy(linexmpl, cpdline); printf("Result of copying:%s\n\n", cpdline); printf("Searching substring in '%s':", linexmpl); inputline(subline); ASSERT(subline > 0); "\n"; if (pos = findsub(subline, linexmpl)) printf("\nSubstring is found on position:%d\n", pos); else printf("\nSubstring is no found\n"); free(linexmpl); free(cpdline); free(subline); return 0; }