tools/lib/subcmd/help.c
Source file repositories/reference/linux-study-clean/tools/lib/subcmd/help.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/subcmd/help.c- Extension
.c- Size
- 6291 bytes
- Lines
- 302
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hstdlib.hstring.hlinux/string.htermios.hsys/ioctl.hsys/types.hsys/stat.hunistd.hdirent.hassert.hsubcmd-util.hhelp.hexec-cmd.h
Detected Declarations
function add_cmdnamefunction clean_cmdnamesfunction cmdname_comparefunction uniqfunction exclude_cmdsfunction get_term_dimensionsfunction pretty_print_string_listfunction is_executablefunction has_extensionfunction list_commands_in_dirfunction load_command_listfunction list_commandsfunction is_in_cmdlist
Annotated Snippet
if (cmds->names[i]) {
if (i == j)
j++;
else
cmds->names[j++] = cmds->names[i];
}
}
cmds->cnt = j;
while (j < i)
cmds->names[j++] = NULL;
}
void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
{
size_t ci, cj, ei;
int cmp;
if (!excludes->cnt)
return;
ci = cj = ei = 0;
while (ci < cmds->cnt && ei < excludes->cnt) {
cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
if (cmp < 0) {
if (ci == cj) {
ci++;
cj++;
} else {
cmds->names[cj++] = cmds->names[ci];
cmds->names[ci++] = NULL;
}
} else if (cmp == 0) {
zfree(&cmds->names[ci]);
ci++;
ei++;
} else if (cmp > 0) {
ei++;
}
}
while (ci < cmds->cnt) {
if (ci != cj) {
cmds->names[cj] = cmds->names[ci];
cmds->names[ci] = NULL;
}
ci++;
cj++;
}
for (ci = cj; ci < cmds->cnt; ci++)
assert(cmds->names[ci] == NULL);
cmds->cnt = cj;
}
static void get_term_dimensions(struct winsize *ws)
{
char *s = getenv("LINES");
if (s != NULL) {
ws->ws_row = atoi(s);
s = getenv("COLUMNS");
if (s != NULL) {
ws->ws_col = atoi(s);
if (ws->ws_row && ws->ws_col)
return;
}
}
#ifdef TIOCGWINSZ
if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
ws->ws_row && ws->ws_col)
return;
#endif
ws->ws_row = 25;
ws->ws_col = 80;
}
static void pretty_print_string_list(struct cmdnames *cmds, int longest)
{
int cols = 1, rows;
int space = longest + 1; /* min 1 SP between words */
struct winsize win;
int max_cols;
int i, j;
get_term_dimensions(&win);
max_cols = win.ws_col - 1; /* don't print *on* the edge */
if (space < max_cols)
cols = max_cols / space;
rows = (cmds->cnt + cols - 1) / cols;
for (i = 0; i < rows; i++) {
Annotation
- Immediate include surface: `stdio.h`, `stdlib.h`, `string.h`, `linux/string.h`, `termios.h`, `sys/ioctl.h`, `sys/types.h`, `sys/stat.h`.
- Detected declarations: `function add_cmdname`, `function clean_cmdnames`, `function cmdname_compare`, `function uniq`, `function exclude_cmds`, `function get_term_dimensions`, `function pretty_print_string_list`, `function is_executable`, `function has_extension`, `function list_commands_in_dir`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.