Subject: Re: split into n chunks
To: None <tech-userlevel@netbsd.org>
From: Jachym Holecek <freza@NetBSD.org>
List: tech-userlevel
Date: 05/28/2007 21:37:09
Hello,
# Jan Schaumann 2007-05-28:
> I've had the need to split a file into N chunks. Now I could do the
> math myself by looking at the file size and the specifying "-b" with the
> appropriate number, but I figured it might be useful to let split(1) do
> this for me.
>
> The attached diff adds the "-n chunk_count" flag to split(1).
Looks useful to me.
> @@ -258,6 +271,26 @@
> }
>
> /*
> + * split3 --
> + * Split the input into specified number of chunks
> + */
> +static void
> +split3(off_t chunks)
> +{
> + struct stat sb;
> + off_t bcnt;
> +
> + if (fstat(ifd, &sb) == -1) {
> + err(1, "stat");
> + /* NOTREACHED */
> + }
> +
> + bcnt = sb.st_size/chunks;
> + bcnt += sb.st_size%chunks;
> + split1(bcnt);
> +}
Considering
st_size = 100
chunks = 6
yields
bcnt = 100/6 = 16
bcnt = 16 + 100%6 = 16 + 4 = 20
thus five files would be created -- not what the user asked for.
-- Jachym