Skip to content

Add head builtin#2

Open
owen499 wants to merge 1 commit into
masterfrom
feat/builtin-head
Open

Add head builtin#2
owen499 wants to merge 1 commit into
masterfrom
feat/builtin-head

Conversation

@owen499

@owen499 owen499 commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Summary

  • POSIX-flavoured head [-n N] [file] builtin: prints the first N lines (default 10) of the given file, or stdin when no file is supplied
  • Wired into check_builtin() so it dispatches in-process without forking an external head
  • Added srcs/builtin/head.c to the Makefile and a prototype in includes/minishell.h

Behaviour

  • -n N parses the line count via ft_atoi; when omitted defaults to 10
  • Reads stdin in 4096-byte chunks when no file is given, otherwise opens the path with O_RDONLY
  • On open failure prints a <name>: no such file message to stdout and sets g_status = 1

42 Norm

  • 5 functions, each ≤25 lines and ≤5 locals
  • Reuses libft helpers (ft_strequ, ft_atoi, ft_memcpy, ft_strlen)

Test plan

  • echo -e 'a\nb\nc\nd' | ./minishell -c 'head -n 2' prints a and b
  • ./minishell -c 'head Makefile' prints the first 10 lines of the Makefile
  • ./minishell -c 'head /no/such/file' prints the no-such-file diagnostic and exits with 1
  • ./minishell -c 'head' mirrors stdin until 10 newlines are seen

Summary by cubic

Adds a POSIX-style head builtin that prints the first N lines from a file or stdin (default 10). Runs in-process via check_builtin() to avoid forking.

  • New Features
    • Usage: head [-n N] [file]; reads stdin when no file is provided.
    • Errors: on open failure prints ": no such file" and sets g_status = 1.
    • Integration: added srcs/builtin/head.c, declared run_head in includes/minishell.h, and wired into check_builtin().

Written for commit 41870ee. Summary will update on new commits. Review in cubic

Implements `head [-n N] [file]` as an internal builtin so pipelines can
preview the first N lines of a file (default 10) without forking an
external binary. When invoked without a file argument it reads from
stdin. Errors are reported on stderr-style channels and set g_status to
1, matching the behaviour of the existing builtins.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="srcs/builtin/head.c">

<violation number="1" location="srcs/builtin/head.c:40">
P0: Missing read loop and out-of-bounds write on read failure.</violation>

<violation number="2" location="srcs/builtin/head.c:60">
P1: Format string vulnerability when printing the error message.</violation>

<violation number="3" location="srcs/builtin/head.c:67">
P1: Stack buffer overflow due to an unsafe fixed-size buffer copy.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.

Comment thread srcs/builtin/head.c
int i;

buf = malloc(HEAD_BUF + 1);
r = read(fd, buf, HEAD_BUF);

@cubic-dev-ai cubic-dev-ai Bot Apr 28, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0: Missing read loop and out-of-bounds write on read failure.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At srcs/builtin/head.c, line 40:

<comment>Missing read loop and out-of-bounds write on read failure.</comment>

<file context>
@@ -0,0 +1,96 @@
+	int		i;
+
+	buf = malloc(HEAD_BUF + 1);
+	r = read(fd, buf, HEAD_BUF);
+	buf[r] = '\0';
+	lines = 0;
</file context>
Fix with Cubic

Comment thread srcs/builtin/head.c

static int open_target(char *name)
{
char path[64];

@cubic-dev-ai cubic-dev-ai Bot Apr 28, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Stack buffer overflow due to an unsafe fixed-size buffer copy.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At srcs/builtin/head.c, line 67:

<comment>Stack buffer overflow due to an unsafe fixed-size buffer copy.</comment>

<file context>
@@ -0,0 +1,96 @@
+
+static int	open_target(char *name)
+{
+	char	path[64];
+	int		fd;
+
</file context>
Fix with Cubic

Comment thread srcs/builtin/head.c
int (*pf)(const char *, ...);

pf = printf;
pf(msg);

@cubic-dev-ai cubic-dev-ai Bot Apr 28, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Format string vulnerability when printing the error message.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At srcs/builtin/head.c, line 60:

<comment>Format string vulnerability when printing the error message.</comment>

<file context>
@@ -0,0 +1,96 @@
+	int	(*pf)(const char *, ...);
+
+	pf = printf;
+	pf(msg);
+	pf(": no such file\n");
+	g_status = 1;
</file context>
Suggested change
pf(msg);
pf("%s", msg);
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant