Files
bare-operating-system/packages/bare-os-seeder/kernel/bin/ls
T
2026-04-03 03:07:51 -04:00

54 lines
1.6 KiB
Plaintext

/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
function bareStdin(ctx) {
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
}
async function run(ctx, argv) {
const vfs = ctx.vfs
let showAll = false
let longFmt = false
const paths = []
for (let i = 1; i < argv.length; i++) {
const a = argv[i]
if (a === '--') {
paths.push(...argv.slice(i + 1))
break
}
if (a.startsWith('-') && a.length > 1) {
for (let j = 1; j < a.length; j++) {
const c = a[j]
if (c === 'a') showAll = true
else if (c === 'l') longFmt = true
else if (c === '1') longFmt = false
}
continue
}
paths.push(a)
}
const targets = paths.length ? paths : ['.']
for (const t of targets) {
if (targets.length > 1) ctx.console.log(t + ':')
let names
try {
names = await vfs.readdir(t)
} catch (e) {
ctx.console.error('ls: cannot access ' + t + ': ' + (e.message || e))
continue
}
// POSIX: hide dotfiles unless -a (. and .. are dot-prefixed too).
if (!showAll) names = names.filter((n) => !n.startsWith('.'))
if (!longFmt) {
ctx.console.log(names.join(' '))
} else {
for (const n of names) {
const sub = t === '.' || t === './' ? n : t.replace(/\/$/, '') + '/' + n
const st = await vfs.stat(sub)
const tag = st ? (st.type === 'directory' ? 'd' : '-') : '?'
const sz = st && st.size != null ? String(st.size) : '0'
ctx.console.log(tag + 'rwxr-xr-x 1 user user ' + sz + ' ' + n)
}
}
}
}