Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions src/find.zig
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ fn parseArgs(allocator: Allocator, args: []const []const u8, stderr: anytype) !F
depth_first = true;
}

// -follow in expression position also enables symlink following
for (args[expr_start..]) |a| {
if (std.mem.eql(u8, a, "-follow")) {
follow_symlinks = true;
break;
}
}

// If no action, wrap with implicit -print
const result_expr = if (!has_action) blk: {
const print_expr = try allocExpr(allocator, .print, .{ .none = {} });
Expand Down Expand Up @@ -701,6 +709,11 @@ fn parsePrimary(allocator: Allocator, args: []const []const u8, pos: *usize, has
pos.* += 1;
return allocExpr(allocator, .true_expr, .{ .none = {} });
}
// -follow in expression position (deprecated GNU/macOS form)
if (std.mem.eql(u8, arg, "-follow")) {
pos.* += 1;
return allocExpr(allocator, .true_expr, .{ .none = {} });
}

if (std.mem.eql(u8, arg, "-name")) {
pos.* += 1;
Expand Down
33 changes: 24 additions & 9 deletions src/realpath.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ fn resolveLogical(allocator: Allocator, path: []const u8) ![]u8 {
return result;
}

/// Convert a Zig error to a POSIX-style error string (matching GNU coreutils output).
fn posixErrorName(err: anyerror) []const u8 {
return switch (err) {
error.AccessDenied => "Permission denied",
error.FileNotFound => "No such file or directory",
error.NotDir => "Not a directory",
error.NameTooLong => "File name too long",
error.SymLinkLoop => "Too many levels of symbolic links",
error.OutOfMemory => "Cannot allocate memory",
error.InvalidPath => "Invalid argument",
error.ReadOnlyFileSystem => "Read-only file system",
else => @errorName(err),
};
}

/// Process a single path and write the result
fn processPath(
allocator: Allocator,
Expand All @@ -110,22 +125,22 @@ fn processPath(
const resolved = if (opts.no_symlinks) blk: {
break :blk resolveLogical(allocator, path) catch |err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, posixErrorName(err) });
}
return false;
};
} else if (opts.canonicalize_missing) blk: {
break :blk path_utils.canonicalizeMissing(allocator, path) catch |err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, posixErrorName(err) });
}
return false;
};
} else if (opts.canonicalize_existing) blk: {
// -e: all components must exist
break :blk std.fs.cwd().realpathAlloc(allocator, path) catch |err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, posixErrorName(err) });
}
return false;
};
Expand All @@ -139,21 +154,21 @@ fn processPath(
const base = std.fs.path.basename(path);
const resolved_dir = std.fs.cwd().realpathAlloc(allocator, dir.?) catch |dir_err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, @errorName(dir_err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, posixErrorName(dir_err) });
}
return false;
};
defer allocator.free(resolved_dir);
break :blk std.fs.path.join(allocator, &.{ resolved_dir, base }) catch |join_err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, @errorName(join_err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, posixErrorName(join_err) });
}
return false;
};
}
}
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ path, posixErrorName(err) });
}
return false;
};
Expand All @@ -168,21 +183,21 @@ fn processPath(
const resolved_base = if (opts.no_symlinks)
resolveLogical(allocator, base_dir) catch |err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ base_dir, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ base_dir, posixErrorName(err) });
}
return false;
}
else if (opts.canonicalize_missing)
path_utils.canonicalizeMissing(allocator, base_dir) catch |err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ base_dir, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ base_dir, posixErrorName(err) });
}
return false;
}
else
std.fs.cwd().realpathAlloc(allocator, base_dir) catch |err| {
if (!opts.quiet) {
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ base_dir, @errorName(err) });
common.printErrorWithProgram(allocator, stderr_writer, "realpath", "{s}: {s}", .{ base_dir, posixErrorName(err) });
}
return false;
};
Expand Down
16 changes: 12 additions & 4 deletions src/stat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,19 @@ fn printDefaultFormat(
file_type,
});

// Line 3: Device, Inode, Links
// Line 3: Device, Inode, Links (GNU format: decimal major,minor)
const dev: u64 = @intCast(stat_buf.dev);
try writer.print("Device: {x}h/{d}d\tInode: {d: <12}Links: {d}\n", .{
dev,
dev,
const dev_major: u64 = if (builtin.os.tag == .macos or builtin.os.tag.isDarwin())
(dev >> 24) & 0xff
else
(dev >> 8) & 0xfff;
const dev_minor: u64 = if (builtin.os.tag == .macos or builtin.os.tag.isDarwin())
dev & 0xffffff
else
dev & 0xff;
try writer.print("Device: {d},{d}\tInode: {d: <12}Links: {d}\n", .{
dev_major,
dev_minor,
stat_buf.ino,
stat_buf.nlink,
});
Expand Down
Loading