This commit is contained in:
Raven Scott
2026-04-04 00:01:11 -04:00
parent 9a4381ed7a
commit a14baabbc1
52 changed files with 1601 additions and 475 deletions
@@ -16,6 +16,7 @@ export const COREUTILS_COMMANDS = [
'cksum',
'clear',
'comm',
'cmp',
'cp',
'crontab',
'cut',
@@ -0,0 +1,16 @@
{
"name": "cmp",
"section": 1,
"title": "compare two files",
"synopsis": ["cmp [-s] FILE1 FILE2"],
"description": "Byte-wise comparison of two regular files. Exit 0 if identical, 1 if different, 2 on error. -s suppresses output.",
"options": [
{
"flag": "-s, --silent",
"meaning": "No output; only set exit status"
}
],
"keywords": ["cmp", "bare-os", "coreutils"],
"examples": [{ "caption": "basic", "code": "cmp a.txt b.txt" }],
"listCategory": "coreutils"
}
+46
View File
@@ -0,0 +1,46 @@
async function run(ctx, argv) {
const silent = argv.includes('-s') || argv.includes('--silent')
const args = argv.filter((a) => !a.startsWith('-'))
const a = args[0]
const b = args[1]
if (!a || !b) {
ctx.console.error('usage: cmp [-s] file1 file2')
ctx.exitCode = 2
return
}
const vfs = ctx.vfs
let x
let y
try {
x = await vfs.readFile(a)
y = await vfs.readFile(b)
} catch (e) {
ctx.console.error('cmp: ' + ((e && e.message) || String(e)))
ctx.exitCode = 2
return
}
if (!x && !y) {
ctx.exitCode = 0
return
}
if (!x || !y) {
if (!silent) ctx.console.error('cmp: EOF on ' + (!x ? a : b))
ctx.exitCode = 1
return
}
const bx = ctx.b4a.toString(x)
const by = ctx.b4a.toString(y)
if (bx === by) {
ctx.exitCode = 0
return
}
if (!silent) {
const lim = Math.min(bx.length, by.length)
let pos = 0
while (pos < lim && bx.charCodeAt(pos) === by.charCodeAt(pos)) pos++
ctx.console.error(
`${a} ${b} differ: char ${pos + 1}, line 1`
)
}
ctx.exitCode = 1
}