do not edit — generated by btf.
git.druid.rocksindexdruid520kaboomsrc/user/cp.nsc

src/user/cp.nsc


include "syscalls.nsh";
include "lib.nsh";
include "string.nsh";
include "stdio.nsh";
 
/* cp src dst -- both a bare name or a real path. reads src fully
 * (stdio.nsc's read_all, the same whole-file read cat uses -- sized to
 * the real file, no fixed cap) and writes it to dst via
 * sys_writefile, which creates dst if it doesn't exist yet. nothing is
 * written unless the whole source was read. */
global i32
main(i32 argc, ptr argv)
{
	ptr src;
	ptr dst;
	u64 src_len;
	u64 dst_len;
	i32 fd;
	i64 n;
	ptr buf;
 
	if(argc < 3)
	{
		fputs(2, "usage: cp src dst\n");
		return 1;
	}
 
	src = argv_get(argv, 1);
	src_len = strlen(src);
	dst = argv_get(argv, 2);
	dst_len = strlen(dst);
 
	fd = sys_open(src, src_len);
	if(fd < 0)
	{
		print_err(2, "cp", "source not found");
		return 1;
	}
 
	buf = read_all(fd, &n);
	sys_close(fd);
	if(buf == (ptr)0)
	{
		print_err(2, "cp", "read failed");
		return 1;
	}
 
	if(sys_writefile(dst, dst_len, buf, (u64)n) == 0)
	{
		print_err(2, "cp", "write failed");
		return 1;
	}
	return 0;
}
powered by btf.