65 lines
1.5 KiB
JavaScript
65 lines
1.5 KiB
JavaScript
async function run(ctx, argv) {
|
|
let posix = false
|
|
let strict = false
|
|
const paths = []
|
|
for (let i = 1; i < argv.length; i++) {
|
|
const a = argv[i]
|
|
if (a === '-p') {
|
|
posix = true
|
|
continue
|
|
}
|
|
if (a === '-P') {
|
|
strict = true
|
|
continue
|
|
}
|
|
if (a === '--') {
|
|
paths.push(...argv.slice(i + 1))
|
|
break
|
|
}
|
|
if (a.startsWith('-')) {
|
|
ctx.console.error('pathchk: unsupported option ' + a)
|
|
ctx.exitCode = 1
|
|
return
|
|
}
|
|
paths.push(a)
|
|
}
|
|
if (!paths.length) {
|
|
ctx.console.error('pathchk: missing operand')
|
|
ctx.exitCode = 1
|
|
return
|
|
}
|
|
for (const p of paths) {
|
|
if (strict && !p.length) {
|
|
ctx.console.error('pathchk: empty path name')
|
|
ctx.exitCode = 1
|
|
continue
|
|
}
|
|
if (!p.length) {
|
|
ctx.console.error('pathchk: empty path name')
|
|
continue
|
|
}
|
|
if (p.length > 4096) {
|
|
ctx.console.error('pathchk: path too long')
|
|
ctx.exitCode = 1
|
|
continue
|
|
}
|
|
if (p.includes('\0')) {
|
|
ctx.console.error('pathchk: NUL in path')
|
|
ctx.exitCode = 1
|
|
continue
|
|
}
|
|
if (strict && p.startsWith('-')) {
|
|
ctx.console.error('pathchk: leading hyphen in path')
|
|
ctx.exitCode = 1
|
|
continue
|
|
}
|
|
if (posix) {
|
|
const base = p.split('/').pop() || p
|
|
if (base !== '.' && base !== '..' && !/^[A-Za-z0-9._-]+$/.test(base)) {
|
|
ctx.console.error('pathchk: non-portable file name ' + base)
|
|
ctx.exitCode = 1
|
|
}
|
|
}
|
|
}
|
|
}
|