Hacker News new | ask | show | jobs
by fintler 4154 days ago
I ended up writing a blog post on how to handle CP arguments a while back. I started coding before really thinking about it -- I mean, it's CP, how hard could it be (famous last words)?

http://blog.typeobject.com/thinking-out-loud-file-copy-tool-...

It turns out the arguments to CP are really tough to reason about if you just naively dive into it. The approach in this article is much better than mine. It feels like I was fumbling around in the dark.

If you're interested in seeing my implementation (it's buggy, but handles most cases), it can be found at:

https://github.com/hpc/dcp/blob/master/src/handle_args.c

The entry point to handle_args.c is DCOPY_parse_path_args, which is called by the main() in:

https://github.com/hpc/dcp/blob/master/src/dcp.c

1 comments

I thought about the same thing in regards to cp. I've been using OpenBSD a lot lately and having it's source installed wanted to take a look at their coding style and such. I picked cp as my first glance as I thought that should be simple enough to understand quickly.

As you say, it's option parsing makes up the bulk of the cp.c code, with utils.c doing most of the actual file copying. It turns out it's pretty tough to offer a lot of options with some of them being mutually exclusive. Obvious in hindsight but interesting to me at the time I was reading through it.

http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/bin/...

http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/bin/...