Re: [dev] ncurses or ...

2014-01-31 Thread sin
On Fri, Jan 31, 2014 at 06:01:48PM +0200, Dimitris Zervas wrote:
> hmm, fifo interface for Xlib.
> Sounds like a good way to get my hands dirty with Xlib.
> 
> Do I have to interface all the features of xlib, or we need some specific?

ALL OF THEM.



Re: [dev] [PATCH] [sbase] printf: Avoid casts for better portability

2014-01-31 Thread sin
On Fri, Jan 31, 2014 at 07:04:46PM +0100, FRIGN wrote:
> On Fri, 31 Jan 2014 18:43:50 +0100
> FRIGN  wrote:
> 
> > Using the C99 PRIu32-macro and %zu-format-specifier increase
> > portability and avoid these unnecessary casts.
> 
> Argh, I forgot C90 didn't support the z-format-specifier.
> So, we won't get around casting len to unsigned long -.-.
> 
> Please let me know if I should check the code for more of those cases.

I was thinking about exactly the same issues today when I did this patch.

Do you guys think it would make sense to move sbase to C99?
We are already using long long in the code.



Re: [dev] ncurses or ...

2014-02-01 Thread sin
On Sat, Feb 01, 2014 at 07:33:54PM +0200, Dimitris Zervas wrote:
> @Charlie:
> The best automation in an editor for me, is autocompletion. It saves
> very much time and does not brake the whole world down.

Autocompletion is mostly useless.



Re: [dev] [PATCH] [sbase] printf: Avoid casts for better portability

2014-02-01 Thread sin
On Fri, Jan 31, 2014 at 07:04:46PM +0100, FRIGN wrote:
> On Fri, 31 Jan 2014 18:43:50 +0100
> FRIGN  wrote:
> 
> > Using the C99 PRIu32-macro and %zu-format-specifier increase
> > portability and avoid these unnecessary casts.
> 
> Argh, I forgot C90 didn't support the z-format-specifier.
> So, we won't get around casting len to unsigned long -.-.

Applied, thanks.

In the future, generate patches using git-format-patch(1).



Re: [dev] [PATCH] [sbase] printf: Avoid casts for better portability

2014-02-01 Thread sin
On Fri, Jan 31, 2014 at 07:04:46PM +0100, FRIGN wrote:
> Please let me know if I should check the code for more of those cases.

Would be nice! :)



[dev] Announcing sinit - the suckless init

2014-02-06 Thread sin
Hi all,

As part of experimenting with a toy distro I wanted to get rid of
busybox's init, so I hacked together sinit[1].  sinit is based on Strake's
init[2].

It is currently controlled via a FIFO.  It supports only two commands (reboot
and poweroff).

It follows the classic style of config.def.h and it should work with any
init scripts.  I've been testing it with my init scripts[3].

Let me know what you guys think, I am looking forward to use this with sta.li.

Thanks,
sin

[1] http://git.2f30.org/sinit
[2] https://github.com/strake/init
[3] http://git.2f30.org/fs/



Re: [dev] Announcing sinit - the suckless init

2014-02-06 Thread sin
On Thu, Feb 06, 2014 at 02:46:43PM +0100, Eckehard Berns wrote:
> > As part of experimenting with a toy distro I wanted to get rid of
> > busybox's init, so I hacked together sinit[1].  sinit is based on Strake's
> > init[2].
> > 
> > It is currently controlled via a FIFO.  It supports only two commands 
> > (reboot
> > and poweroff).
> 
> Why are those included? sinit does nothing but reaping zombies. Using
> it to spawn shutdown scripts seems kinda wrong to me. Why not call the
> shutdown scripts directly to poweroff or reboot the system?

Yes, you can call the shutdown script directly.  I was floating between
this decision myself.

I might just as well remove support for that.



Re: [dev] Announcing sinit - the suckless init

2014-02-06 Thread sin
On Thu, Feb 06, 2014 at 07:54:17PM +0100, YpN wrote:
> > Let me know what you guys think, I am looking forward to use this with 
> > sta.li.
> 
> I will probably try it this month. And you should add it on
> the "rocks" page. For now, I don't have any advices, but it
> could be good to "support" daemons.

daemons are handled separately and outside of init.
In my setup I have some simple scripts in /bin/rc.d.

In the future I am planning to use svc[1].

[1] http://git.r-36.net/svc/



Re: [dev] Announcing sinit - the suckless init

2014-02-06 Thread sin
On Thu, Feb 06, 2014 at 09:36:23PM +0100, Eckehard Berns wrote:
> On Thu, Feb 06, 2014 at 12:32:59PM +0000, sin wrote:
> > As part of experimenting with a toy distro I wanted to get rid of
> > busybox's init, so I hacked together sinit[1].  sinit is based on Strake's
> > init[2].
> > [...]
> > Let me know what you guys think, I am looking forward to use this with 
> > sta.li.
> 
> I now had time to test sinit. I first had trouble getting the FIFO
> to work. When booting with the root fs mounted read-only, the child
> dealing with the FIFO would segfault. When booting with my root fs
> mounted read-write my rcinitcmd would become a zombe since the FIFO code
> never reaps children.

Thanks for testing!

I think the segfault could have been caused because there was no error
checking for open(2).  I added that in later.

Hm yes, you are right, the FIFO code never reaps children.  We could
probably use the double fork trick + killing the parent to force it to
be reaped by the original process (the parent of the FIFO code).

> So I changed spawn() to wait for its child. Now I can mount my root fs
> read-write in my rcinitcmd or even setup a tmpfs, and my rcinitcmd will
> have its exit status checked and not become a zombie. This means though
> that you can't use the FIFO until the rcinitcmd exited (which means
> you might want to chane your rc.svc to background the agetty loop).

Yeah something did not feel right with having the rc script block.
I presume if we use the aforementioned double fork trick, we'll be able
to continue even if rc.svc doesn't exit (I may be hallucinating right
now, I will check tomorrow if what I am saying makes sense).

> Also, would it be worth it to deal with x86 Linux's ctrl-alt-del? It would
> pull in OS specific code, and maybe people don't care for ctrl-alt-del
> on the console, since everybody lives in X anyway.

Hm, not sure.  What you guys think?





Re: [dev] Announcing sinit - the suckless init

2014-02-06 Thread sin
On Thu, Feb 06, 2014 at 09:03:38PM +0100, Hadrian Węgrzynowski wrote:
> Dnia 2014-02-06, o godz. 12:32:59
> Hey.
> 
> Did you saw qinit[1] from David Galos?

Yes, it does a bit more than what I'd expect from init.
It is also a bit racy, I have a patched version somewhere that sort of
works.



Re: [dev] Announcing sinit - the suckless init

2014-02-07 Thread sin
On Thu, Feb 06, 2014 at 11:40:22PM +0100, Eckehard Berns wrote:
> On Thu, Feb 06, 2014 at 09:54:44PM +0000, sin wrote:
> > Hm yes, you are right, the FIFO code never reaps children.  We could
> > probably use the double fork trick + killing the parent to force it to
> > be reaped by the original process (the parent of the FIFO code).
> 
> Double forking would take care of the spawned process. But you'd still be
> forced to boot with the file system mounted read-write. I don't know if
> that's the way to go. I think Arch wants you to boot with a read-writable
> root fs nowadays, but I don't know what the benefit should be. Even more
> so since you need to remount it read-only during system initialisation
> anyway before checking it.

I've fixed the issues you mentioned except the case when rootfs is mounted
as ro.

How would you tackle that?

I am considering setting up a SIGHUP handler in init and then sending that
signal at the end of the boot process when we are basically ready.

What do you think?

Cheers,
sin



Re: [dev] Announcing sinit - the suckless init

2014-02-07 Thread sin
On Fri, Feb 07, 2014 at 01:42:36PM +0100, Eckehard Berns wrote:
> On Fri, Feb 07, 2014 at 12:03:03PM +0000, sin wrote:
> > I've fixed the issues you mentioned except the case when rootfs is mounted
> > as ro.
> > 
> > How would you tackle that?
> 
> I thought about this a bit. If the fifo is present when booting the
> ro root fs everything should be fine. You wouldn't be able to unlink
> fifopath or create it, but since it's already there the call to open()
> should succeed. You'd have to ignore errors for unlink() and mkfifo()
> and only check open() for errors. I can't test this at the moment, but I
> think this might work.

Hi,

I refactored the code and this is what I have now:

http://git.2f30.org/sinit/commit/?id=8194f460daaa9343ce9863838b14132d0ad8037c

>From the commit message:

"We no longer have two processes, one for reaping children and
another for handling the FIFO.

Use signalfd() to multiplex signal handling and reading from the
FIFO.

This setup will also allow us to capture ctrl-alt-del and to work
with a read-only rootfs.

To create the FIFO, we send a SIGHUP signal to init from our init
scripts."

Yes, I realize signalfd() is Linux specific... aw well.

Cheers,
sin



Re: [dev] Announcing sinit - the suckless init

2014-02-07 Thread sin
On Fri, Feb 07, 2014 at 01:42:36PM +0100, Eckehard Berns wrote:
> On Fri, Feb 07, 2014 at 12:03:03PM +0000, sin wrote:
> > I've fixed the issues you mentioned except the case when rootfs is mounted
> > as ro.
> > 
> > How would you tackle that?
> 
> I thought about this a bit. If the fifo is present when booting the
> ro root fs everything should be fine. You wouldn't be able to unlink
> fifopath or create it, but since it's already there the call to open()
> should succeed. You'd have to ignore errors for unlink() and mkfifo()
> and only check open() for errors. I can't test this at the moment, but I
> think this might work.

Well, I completely removed the FIFO code :)



Re: [dev] Announcing sinit - the suckless init

2014-02-07 Thread sin
On Fri, Feb 07, 2014 at 09:56:17PM +0100, Eckehard Berns wrote:
> On Fri, Feb 07, 2014 at 04:24:03PM +0000, sin wrote:
> > Well, I completely removed the FIFO code :)
> 
> That simplifies things :)
> 
> I tested v0.3 and besides some glitches due to my system everything
> worked fine.  I'm using fgetty (yeah, freeing a couple more kb might
> not be worth using it, but I tried it some time ago and kept it) and
> it complains about file descriptor 3 when I try to log in. Aparently
> (haven't checked the fgetty code) it uses file descriptor 3 for internal
> communication of some sort and that would be sinit's sigfd. I added
> a line to spawn() to close sigfd in the first child (if sigfd >= 0)
> and I could log in again.

Aha, I see.  I will try testing with fgetty as well.

> Speaking of children, I think you could drop the double fork in spawn
> now, since pid 1 already waits for children.

Makes sense.

> And since sinit uses Linux specific code anyway you might consider
> calling reboot(0) to tell the kernel to send SIGINT to pid 1 on
> ctrl-alt-del.

I implemented ctrlaltdel(8) in ubase and I call it in my init scripts
to set the ctrl-alt-del behaviour.



Re: [dev] Announcing sinit - the suckless init

2014-02-07 Thread sin
On Fri, Feb 07, 2014 at 05:26:54PM +0100, Szabolcs Nagy wrote:
> * sin  [2014-02-06 12:32:59 +]:
> > As part of experimenting with a toy distro I wanted to get rid of
> > busybox's init, so I hacked together sinit[1].  sinit is based on Strake's
> > init[2].
> 
> note that strake got that init code is from Rich Felker
> and there is more to it than that code.. (you may find
> related discussions on the musl mailing list archive,
> he also planned to write about reliable init systems on
> ewontfix.com so you may want to check that out as well)
> 
> > [2] https://github.com/strake/init
> 
> compare to
> https://gist.github.com/rofl0r/6168719

nsz, thanks for clarifying!  I will give proper credit
to Rich Felker and check out the discussions.



Re: [dev] Announcing sinit - the suckless init

2014-02-07 Thread sin
On Fri, Feb 07, 2014 at 09:56:17PM +0100, Eckehard Berns wrote:
> I tested v0.3 and besides some glitches due to my system everything
> worked fine.  I'm using fgetty (yeah, freeing a couple more kb might
> not be worth using it, but I tried it some time ago and kept it) and
> it complains about file descriptor 3 when I try to log in. Aparently
> (haven't checked the fgetty code) it uses file descriptor 3 for internal
> communication of some sort and that would be sinit's sigfd. I added
> a line to spawn() to close sigfd in the first child (if sigfd >= 0)
> and I could log in again.

I use SFD_CLOEXEC now - should work.

> Speaking of children, I think you could drop the double fork in spawn
> now, since pid 1 already waits for children.

Done.



Re: [dev] Announcing sinit - the suckless init

2014-02-08 Thread sin
On Sat, Feb 08, 2014 at 09:27:36AM +0100, Truls Becken wrote:
> Hi,
> 
> Wrapping the argument to spawn in Arg union is useless now.
> Probably a leftover from the design with FIFO.

Yeah, indeed.  Will remove.



Re: [dev][ubase][PATCH] swapon/swapoff -a and -p

2014-02-09 Thread sin
On Sat, Feb 08, 2014 at 10:34:31PM -0500, Carlos Torres wrote:
> Hi sin,
> attached are two patches for the -a flag on swapon and swapoff.  there
> is room for improvement. and cleaning up, but i wasn't sure how you
> might want it organized.   i altered the error handling a little bit,
> since it felt funny how you let the loop continue on error and
> possibly removing the EXIT_FAILURE return code if two devices are to
> be swapped on/off and the first failed but the second succeeds.  I
> make it so that once EXIT_FAILURE is set it doesn't go away.

Thanks a lot for your patches.  They have been applied.

(You are right btw about setting the exit value.)

Cheers,
sin



Re: [dev] Announcing sinit - the suckless init

2014-02-09 Thread sin
On Sun, Feb 09, 2014 at 01:36:21PM -0500, Bryan Bennett wrote:
> Well, damnit. Gmail's web interface strikes again...
> 
> As I was saying...
> 
> I've not had the chance to try this out myself, but I'm curious what
> this provides
> (or doesn't provide, given the collective mindsets on this mailinglist, myself
> included) that something like minirc[1] does or doesn't do.

Thanks for pointing out minirc, but sinit is just an implementation of the
init(8) program, it is not tied to any particular set of init scripts
like minirc.

Without hacking minirc, sinit would not work with it as I do not want
to support an inittab file.  sinit is entirely configured via config.h.

minirc has been tested with busybox init.  I am trying hard to get rid
of busybox from my systems and this is my attempt at implementing a simple
init system that does the bare minimum needed.  Everything else comes either
as a separate tool or as part of the init scripts (like sanitizing
PATH etc.)



Re: [dev][ubase][PATCH] alternative - return exit codes like util-linux

2014-02-10 Thread sin
On Sun, Feb 09, 2014 at 09:17:07PM -0500, Carlos Torres wrote:
> Hello,
>   This patch is food for thought.  i looked closer at what util-linux
> does (ugh) and found that their exit codes are what swapon/swapoff
> return.  so this is an alternative patch that behaves the same way.  I
> thought its possible there are shell scripts out there that are
> interested in the exit  value of these...???

Hi Carlos,

>From the manpage of swapon/swapoff.

"   On success, zero is returned.  On error, -1 is returned, and
errno is set appropriately."

So I don't see how or-ing the return values actually does anything.
It can only be -1 or 0.

Am I missing something here?



Re: [dev] Announcing sinit - the suckless init

2014-02-10 Thread sin
On Sat, Feb 08, 2014 at 11:58:47PM +0100, Eckehard Berns wrote:
> On Fri, Feb 07, 2014 at 09:36:01PM +0000, sin wrote:
> > On Fri, Feb 07, 2014 at 09:56:17PM +0100, Eckehard Berns wrote:
> > > I tested v0.3 and besides some glitches due to my system everything
> > > worked fine.  I'm using fgetty (yeah, freeing a couple more kb might
> > > not be worth using it, but I tried it some time ago and kept it) and
> > > it complains about file descriptor 3 when I try to log in. Aparently
> > > (haven't checked the fgetty code) it uses file descriptor 3 for internal
> > > communication of some sort and that would be sinit's sigfd. I added
> > > a line to spawn() to close sigfd in the first child (if sigfd >= 0)
> > > and I could log in again.
> > 
> > I use SFD_CLOEXEC now - should work.
> 
> Just had a chance to test v0.4-4-gebccd1b and everything works
> beautifully.

Hi there,

I just pushed a simple implementation of getty[1] to ubase.  Would
be nice to see if that works ok with your setup (and maybe get rid of
fgetty ;-)).


[1] http://git.2f30.org/ubase/tree/getty.c



Re: [dev] Announcing sinit - the suckless init

2014-02-10 Thread sin
On Mon, Feb 10, 2014 at 02:44:11PM +0100, Eckehard Berns wrote:
> On Mon, Feb 10, 2014 at 12:31:59PM +0000, sin wrote:
> > I just pushed a simple implementation of getty[1] to ubase.  Would
> > be nice to see if that works ok with your setup (and maybe get rid of
> > fgetty ;-)).
> 
> I didn't dare to ask if there was a suckless getty :) Thanks for that!
> 
> I could only test it briefly. At first it seemed to work as expected.
> Then I realized that you cheated your way around asking for the
> username. On Slackware /bin/login times out after 60 seconds which will
> clutter the vt after a couple of minutes with timeout messages and login
> prompts. So it seems that getty should ask for the username first even
> if login could do it as well.

Fixed in upstream ubase.  It should work ok now.



Re: [dev][ubase][PATCH] alternative - return exit codes like util-linux

2014-02-10 Thread sin
On Mon, Feb 10, 2014 at 09:19:10AM -0500, Carlos Torres wrote:
> Hello,
> 
> On 2/10/14, sin  wrote:
> > So I don't see how or-ing the return values actually does anything.
> > It can only be -1 or 0.
> >
> > Am I missing something here?
> 
> No sir.  this patch simply emulates whats wrong with util-linux's
> swapon/swapoff.
> Its not necessary unless you want full compatibility.

I'd like to avoid it as much as possible.

A patch that factors out the function however, without playing
tricks with the return value would be nice though,



Re: [dev] Announcing sinit - the suckless init

2014-02-10 Thread sin
On Mon, Feb 10, 2014 at 08:53:16PM +0100, Krol, Willem van de wrote:
> On Fri, Feb 7, 2014 at 2:02 PM, sin  wrote:
> > Yes, I realize signalfd() is Linux specific... aw well.
> 
> Why did you choose signalfd() over sigwait()? The only advantage of
> signalfd() seems to be poll()-ing or similar actions, which are not
> used in sinit.

It is a left-over from when I was multiplexing the file descriptors of
the FIFO and the signal dispatching.

I will fix it, thanks for pointing out.



Re: [dev] Announcing sinit - the suckless init

2014-02-10 Thread sin
On Mon, Feb 10, 2014 at 08:53:16PM +0100, Krol, Willem van de wrote:
> On Fri, Feb 7, 2014 at 2:02 PM, sin  wrote:
> > Yes, I realize signalfd() is Linux specific... aw well.
> 
> Why did you choose signalfd() over sigwait()? The only advantage of
> signalfd() seems to be poll()-ing or similar actions, which are not
> used in sinit.

Fixed :)



Re: [dev] Reasonable Makefiles

2014-02-11 Thread sin
On Tue, Feb 11, 2014 at 01:28:25PM +0100, Markus Teich wrote:
> Nick wrote:
> > I was reading the opengroup specifications for make(1) recently[0],
> > and found that even our standard makefile practise of using 'include'
> > for config variables is nonstandard, as far as they're concerned.
> > Needless to say I think 'include' is a perfectly reasonable feature
> > to use, and it evidently works everywhere that people care about.
> 
> Heyho,
> 
> Regarding the include config.mk used in various suckless projects: What is the
> benefit? If a user needs to adapt it to his system, he effectively has to 
> edit a
> file. Would there be a problem if this file would be the Makefile instead of 
> the
> config.mk file?

It is clear separation between user-configured variables and generic
code.

It also makes it easy to have a configure script like as shown below:

#!/bin/sh

case `uname` in
OpenBSD)
ln config.bsd config.mk
;;
*)
ln config.posix config.mk
;;
esac

This was taken from utmp[1].
This doesn't duplicate the entire Makefile.

[1] http://git.suckless.org/utmp

cheers,
sin



Re: [dev] Reasonable Makefiles

2014-02-11 Thread sin
On Tue, Feb 11, 2014 at 01:28:25PM +0100, Markus Teich wrote:
> Nick wrote:
> > I was reading the opengroup specifications for make(1) recently[0],
> > and found that even our standard makefile practise of using 'include'
> > for config variables is nonstandard, as far as they're concerned.
> > Needless to say I think 'include' is a perfectly reasonable feature
> > to use, and it evidently works everywhere that people care about.
> 
> Heyho,
> 
> Regarding the include config.mk used in various suckless projects: What is the
> benefit? If a user needs to adapt it to his system, he effectively has to 
> edit a
> file. Would there be a problem if this file would be the Makefile instead of 
> the
> config.mk file?

It also makes it possible to modify the Makefile at will without causing
potential conflict headaches for users.

Sure that's also possible with config.mk but it is much less likely
that it will change.

Almost no one gets Makefiles right the first time, so they are subject to
change.



Re: *** GMX Spamverdacht *** [dev] Reasonable Makefiles

2014-02-11 Thread sin
On Tue, Feb 11, 2014 at 07:16:35PM +0100, Markus Wichmann wrote:
> On Tue, Feb 11, 2014 at 12:19:59PM +, Nick wrote:
> > I was reading the opengroup specifications for make(1) recently[0],
> > and found that even our standard makefile practise of using 'include'
> > for config variables is nonstandard, as far as they're concerned.
> > Needless to say I think 'include' is a perfectly reasonable feature
> > to use, and it evidently works everywhere that people care about.
> > 
> > But it got me thinking about what other features of make are worth
> > using. Basically because I'm replacing a autotools horrorshow with
> > plain make, but am not sure what the nicest way of allowing compile-
> > time feature disabling is. Can 'ifdef' be relied upon, and does it
> > tend to produce unreadable and buggy makefiles in anyone's
> > experience? Are there other options, beyond asking people to comment
> > out certain lines in a config.mk, to e.g. disable some LDFLAGS?
> > 
> 
> If you want a nice looking user interface for choices, have a look at
> kconf. Otherwise just assume that people know to look for the correct
> variable to change what they need. Using autoconf just to spare people
> the pain of opening an editor is really not worth it.

kconf? really?

> By the way, I usually write my Makefiles for GNU make. Its
> implementation may be bad, but its ideas are also used in mk.

Good luck.

> Why did make become the horrible cancer that it is today? Because
> developers thought it inadequate in some respects and made
> vendor-specific changes. Who's to say that won't happen for mk? I mean,
> assuming it ever gets sufficient exposure (sorry guys, but technological
> superiority doesn't guarentee market share, which is the primary reason
> for autohell, cmake, and qmake), someone will want to change
> something about it. Things will grow on it. mk will become just as
> bloated as make, come 20 years or so.

Because people like you choose to be insane.

> Why? If Linux wasn't as configurable as it is, it would be completely
> unusable, or would you rather compiler _all_ the drivers all the time?
> Same for other software.

Yes.



Re: [dev] Announcing sinit - the suckless init

2014-02-16 Thread sin
On Sat, Feb 15, 2014 at 11:20:08PM +0100, Truls Becken wrote:
> Rich Felker included a really minimalistic init in his recent blog post
> "Broken by design: systemd" [1].

It was posted here to this thread by nsz.  sinit was initially inspired
by Rich Felker's init.

> [1] http://ewontfix.com/14/



Re: [dev] Re: Article about suckless on root.cz

2014-02-17 Thread sin
On Mon, Feb 17, 2014 at 07:57:37AM +0100, Martin Kopta wrote:
> Hi guys,
> 
> just wanted to point out my article about Stali [0] which came out today as a
> third part of the Suckless series on root.cz. First article was about Suckless
> in general [1], second was about dwm [2].

Cool! :)



[dev] suckless conf this year?

2014-02-27 Thread sin
Hi all,

Are there any plans for a suckless conference this year?

Cheers,
sin



Re: [dev] suckless conf this year?

2014-02-27 Thread sin
I am planning to travel from Scotland :)

I'd also like to talk about sbase/ubase.



[dev] [9base] [patch] Simplify preprocessor conditions to enable building on musl-libc

2014-03-03 Thread sin
Hi,

This is a fix for 9base to enable building on musl.
>From 8fccc638b7d48702e482b1e1627c8e83770dfa33 Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 3 Mar 2014 10:59:34 +
Subject: [PATCH] Simplify preprocessor conditions to enable building on
 musl-libc

musl-libc does not export __MUSL__ so we cannot use that.  I've
chosen to go with the common solution here that works on uClibc,
glibc and musl-libc by just using syscall(2) and getdents(2).

To build 9base, you will need the appropriate kernel headers in
place.
---
 lib9/_p9dir.c   |2 +-
 lib9/dirread.c  |   22 +-
 lib9/readcons.c |3 ---
 3 files changed, 2 insertions(+), 25 deletions(-)

diff --git a/lib9/_p9dir.c b/lib9/_p9dir.c
index 706130c..b49cd4d 100644
--- a/lib9/_p9dir.c
+++ b/lib9/_p9dir.c
@@ -61,7 +61,7 @@ disksize(int fd, struct stat *st)
return (vlong)lab.d_partitions[n].p_size * lab.d_secsize;
 }
 
-#elif defined(__linux__) && !defined(__MUSL__)
+#elif defined(__linux__)
 #include 
 #include 
 #include 
diff --git a/lib9/dirread.c b/lib9/dirread.c
index aad4c36..99d5597 100644
--- a/lib9/dirread.c
+++ b/lib9/dirread.c
@@ -4,7 +4,7 @@
 #include 
 #include 
 
-#if defined (__UCLIBC__)
+#if defined (__linux__)
 # include 
 # if defined (__USE_LARGEFILE64)
 #  define getdents SYS_getdents64
@@ -16,30 +16,10 @@
 extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*);
 
 #if defined(__linux__)
-# if defined (__UCLIBC__)
 static int
 mygetdents(int fd, struct dirent *buf, int n) {
   return syscall (getdents, fd, (void*) buf, n);
 }
-# elif defined(__MUSL__)
-static int
-mygetdents(int fd, struct dirent *buf, int n)
-{
-   return getdents(fd, (void*)buf, n);
-}
-# else
-static int
-mygetdents(int fd, struct dirent *buf, int n)
-{
-   off_t off;
-   int nn;
-
-   /* This doesn't match the man page, but it works in Debian with a 2.2 
kernel */
-   off = p9seek(fd, 0, 1);
-   nn = getdirentries(fd, (void*)buf, n, &off);
-   return nn;
-}
-# endif
 #elif defined(__APPLE__) || defined(__FreeBSD__)
 static int
 mygetdents(int fd, struct dirent *buf, int n)
diff --git a/lib9/readcons.c b/lib9/readcons.c
index fb7b0f3..0c54a4b 100644
--- a/lib9/readcons.c
+++ b/lib9/readcons.c
@@ -2,9 +2,6 @@
 #define NOPLAN9DEFINES
 #include 
 #include 
-#ifndef __MUSL__
-# include 
-#endif
 
 static int
 rawx(int fd, int echoing)
-- 
1.7.10.4



[dev] [9base] [patch] Fix building on OpenBSD 5.5

2014-03-03 Thread sin

>From 9fcdca78649a1b7a6f19db4729e621388ad89814 Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 3 Mar 2014 13:57:30 +
Subject: [PATCH] Fix building on OpenBSD 5.5

getdirentries() has been removed and instead we should use
getdents().
---
 lib9/dirread.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/lib9/dirread.c b/lib9/dirread.c
index 99d5597..f114195 100644
--- a/lib9/dirread.c
+++ b/lib9/dirread.c
@@ -28,12 +28,21 @@ mygetdents(int fd, struct dirent *buf, int n)
return getdirentries(fd, (void*)buf, n, &off);
 }
 #elif defined(__OpenBSD__)
+#include 
+# if OpenBSD < 201405 /* for OpenBSD 5.4 and earlier */
 static int
 mygetdents(int fd, struct dirent *buf, int n)
 {
off_t off;
return getdirentries(fd, (void*)buf, n, &off);
 }
+# else
+static int
+mygetdents(int fd, struct dirent *buf, int n)
+{
+   return getdents(fd, (void*)buf, n);
+}
+# endif
 #elif defined(__sun__) || defined(__NetBSD__)
 static int
 mygetdents(int fd, struct dirent *buf, int n)
-- 
1.8.5.3



Re: [dev][PATCH][quark] Clean up the log-facility

2014-03-03 Thread sin
On Mon, Mar 03, 2014 at 07:39:13PM +0100, FRIGN wrote:
> I'm currently working on quark and would like to propose a patch
> simplifying the logmsg-, logerrmsg- and die-functions in quark.
> There's more to come!

Why can't we not use err(), warn(), etc. from err.h?




Re: [dev][PATCH][quark] Clean up the log-facility [fixed-PATCH]

2014-03-03 Thread sin
On Mon, Mar 03, 2014 at 09:53:20PM +0100, FRIGN wrote:
> On Mon, 3 Mar 2014 21:16:46 +
> sin  wrote:
> 
> > Why can't we not use err(), warn(), etc. from err.h?
> 
> It's not necessary and introduces more problems, as it forces a certain
> format on the error-messages, always beginning with "quark:".
> I'm all for using built-in functions, but in this case, it's not for
> the better.

I don't agree.  At the very least have a look at eprintf() from the
'The practice of programming'.  We are using similar functions in sbase
and ubase.

I do not like that the type of log is passed in as an argument, it should
just be a separate function.



Re: [dev] What is bad with Python

2014-03-04 Thread sin
On Tue, Mar 04, 2014 at 09:25:26AM +0100, FRIGN wrote:
> A question to everyone on this list: What do you think about the
> Go-language?

I have no experience with python, but I use golang a lot.

Lua is also quite fun to code in.



Re: [dev][smdev] Fixed Makefile to create bin folder

2014-04-06 Thread sin
On Sun, Apr 06, 2014 at 12:37:54AM +0300, Ari Malinen wrote:
> Its hard to write ebuilds for smdev because this line is missing from smdev:
> @mkdir -p ${DESTDIR}${PREFIX}/bin
> 
> Here is patch:
> http://koti.kapsi.fi/~deferi/patches/smdev-git-create-bin.diff

This has been fixed, thanks for reporting.



Re: [dev] lock (1) - where does it go?

2014-04-11 Thread sin
On Fri, Apr 11, 2014 at 10:25:48AM -0400, Calvin Morrison wrote:
> 1) submit it to the suckless repository, if the community so desires

Personally I'd like to see something like miscutils, rutils (random utils)
or similar under MIT/X.  I am sure more programs will pop up.



Re: [dev][sbase][RFC] "tr" with -d option or without?

2014-04-11 Thread sin
On Fri, Apr 11, 2014 at 09:07:10PM +0200, Silvan Jegen wrote:
> I can see the need for a '-d' flag for compatibility reasons but my
> preference would be to just ignore the flag if it is specified. What are
> peoples opinion on the '-d' flag?

I suspect we can do that yes, Hiltjo might be able to shed some more
light on this but I do not see why what you suggest would not work.



Re: [dev][sbase][RFC] "tr" with -d option or without?

2014-04-11 Thread sin
On Fri, Apr 11, 2014 at 09:07:10PM +0200, Silvan Jegen wrote:
> I can see the need for a '-d' flag for compatibility reasons but my
> preference would be to just ignore the flag if it is specified. What are
> peoples opinion on the '-d' flag?

One thing that is different is the breaking conditions for when to consider
the invocation as invalid usage (in the -d case we check for >= 2 in
any other case we check for != 2).  So in that case we cannot consider -d
as a no-op as far as I uderstand it.

We can still share the rest of the code though. 



Re: [dev] lock (1) - where does it go?

2014-04-11 Thread sin
On Fri, Apr 11, 2014 at 06:02:55PM +0200, Christoph Lohmann wrote:
> Greetings.
> 
> On Fri, 11 Apr 2014 18:02:55 +0200 Calvin Morrison  
> wrote:
> > I'd like lock to belong somewhere. FRIGN and I privately discussed
> > submitting it to moreutils, a package which contains unix utilities
> > which didn't quite make it into coreutils, but are still useful.
> 
> Submit it to ubase.

I guess we can do that yes.  So Calvin, send me a patch for lock to be included
in ubase and I will apply it.

Cheers,
sin



Re: [dev][sbase][RFC] "tr" with -d option or without?

2014-04-12 Thread sin
On Sat, Apr 12, 2014 at 11:08:29AM +0200, Martti Kühne wrote:
> On Fri, Apr 11, 2014 at 9:07 PM, Silvan Jegen  wrote:
> > I can see the need for a '-d' flag for compatibility reasons but my
> > preference would be to just ignore the flag if it is specified. What are
> > peoples opinion on the '-d' flag?
> >
> > In any case, the tr.1 man page should be updated and the TODO item
> > removed which I will do as soon as the decision has been made.
> >
> 
> Wouldn't that be a case for some makefile magic? reverting the -d
> patch and removing it from the manpage...

I don't follow, care to elaborate?



Re: [dev] [quark] [PATCH] correct information in README

2014-04-12 Thread sin
On Thu, Apr 10, 2014 at 08:55:08PM +0200, Jakob Kramer wrote:
> There is wrong information about the installation directory and about
> how to run quark in the README.

Applied, thanks.



Re: [dev][sbase][RFC] "tr" with -d option or without?

2014-04-12 Thread sin
On Sat, Apr 12, 2014 at 09:02:05PM +0200, Silvan Jegen wrote:
> True. I would suggest just adding checks and bailing out when the return
> code is <0. Maybe something like the following?

Looks good.  Can you resend with an attachment?



Re: [dev][sbase][RFC] "tr" with -d option or without?

2014-04-12 Thread sin
On Sat, Apr 12, 2014 at 10:20:14PM +0200, Silvan Jegen wrote:
> On Sat, Apr 12, 2014 at 08:42:22PM +0100, sin wrote:
> > On Sat, Apr 12, 2014 at 09:02:05PM +0200, Silvan Jegen wrote:
> > > True. I would suggest just adding checks and bailing out when the return
> > > code is <0. Maybe something like the following?
> > 
> > Looks good.  Can you resend with an attachment?
> 
> Can do!

Applied, thanks.



Re: [dev][ubase] Implement switch_root

2014-04-13 Thread sin
On Sun, Apr 13, 2014 at 02:10:51PM +0200, FRIGN wrote:
> Good day,
> 
> sometimes, you depend on an initramfs to do stuff for you before
> the rootfs is available.
> Busybox has become the standard for "all your initramfs needs", but
> tbh, I hate working with it.
> Statically linking sbase and ubase and choosing the tools you need for
> the job almost solves the problem, but the lack of switch_root, which
> almost every initramfs depends on, forces you to use busybox (because
> util-linux doesn't link statically).
> 
> Now, I would've already sent in a patch, but I am pretty sure somebody
> here already hacked it together before. So, to save my time, I better
> ask now.

I am not aware of any such patches.  Feel free to implement the tool
and send me a patch :)

Cheers,
sin



Re: [dev][ubase] Implement switch_root

2014-04-17 Thread sin
On Thu, Apr 17, 2014 at 12:26:09PM +0200, FRIGN wrote:
> On Wed, 16 Apr 2014 23:10:23 +0100
> Dimitris Papastamos  wrote:
> 
> > > i just note that pivot_root is a linux system call
> > > so implementing that tool is a one-liner in c
> 
> We don't need this here, given we don't give a damn about preserving
> the initramfs somewhere.
> I would agree on using syscalls to avoid long hacks and system-specific
> behaviour, but wiping / and mounting something over it is trivial.
> Besides, I often made the experience that syscalls can turn out to be
> very complex and intransparent.
> It may be a one-liner in C, but who knows what happens behind closed
> doors?

http://git.suckless.org/ubase/tree/pivot_root.c

I am pretty sure you've seen this, but this is our implementation.  It is
very trivial.

> > We already have pivot_root in ubase.  Markus is basically asking
> > why we implemented switch_root in C instead of in sh (as demonstrated
> > in his e-mail).
> 
> Well, sh doesn't work when you don't have a shell on your initrd (like
> I do), but you already explained that to him afaik.
> However, I think Markus just sketched the functionality in sh and
> implied it to be implemented in C regardless.

But, you implemented it :)

In any case as already mentioned, ubase-box breaks if we add shell scripts
to ubase and this cannot be fixed.  Someome might suggest adding some useful
shell scripts in a directory misc/ or so in ubase but that's not the
right place and I'd like to keep sbase and ubase free of non-C code
and external runtime dependencies.




Re: [dev][ubase] Implement switch_root

2014-04-17 Thread sin
On Thu, Apr 17, 2014 at 02:47:37PM +0200, FRIGN wrote:
> On Thu, 17 Apr 2014 12:57:24 +0100
> sin  wrote:
> 
> >Someome might suggest adding some useful
> > shell scripts in a directory misc/ or so in ubase but that's not the
> > right place and I'd like to keep sbase and ubase free of non-C code
> > and external runtime dependencies.
> 
> That's exactly the point!
> One reason why I use ubase in my initramfs is that it doesn't fuck
> around with this crap.
> If you want to be in complete control of your system, shell-files
> aren't the way to go, given you depend on a shell-implementation, which
> itself might invoke dynamic dependencies and other quirks.

Yup! :)

BTW, would be really nice if you could document your initrd setup and post
it to the wiki.

Cheers,
sin



Re: [dev] lock (1) - where does it go?

2014-04-17 Thread sin
On Thu, Apr 17, 2014 at 11:21:45AM -0400, Calvin Morrison wrote:
> when are you planning a release?

1st of May or so



[dev] [sinit] 0.9 release

2014-04-18 Thread sin
Greetings everyone.

sinit 0.9[0] has been released today on suckless.org.

The code is hosted on git[1] and we also have a dedicated
tools page[2].

You'll find everything you need to know in the code and in
the provided README.  I am planning to post some usage information
in the wiki.

Please note that sinit is considered stable and complete.  If
you have any patches, feel free to submit them to the wiki.

Cheers,
sin

[0] http://dl.suckless.org/sinit/sinit-0.9.tar.gz
[1] http://git.suckless.org/sinit
[2] http://tools.suckless.org/sinit/



Re: [dev] [st] [patch] misplaced parenthesis in LEN macro

2014-04-20 Thread sin
On Sun, Apr 20, 2014 at 04:07:12PM +0400, non...@inventati.org wrote:
> On Sun, Apr 20, 2014 at 01:53:33PM +0200, Alexander Huemer wrote:
> > Hi,
> > 
> > On Sun, Apr 20, 2014 at 03:41:40PM +0400, non...@inventati.org wrote:
> > > […]
> > > -#define LEN(a) (sizeof(a) / sizeof(a[0]))
> > > +#define LEN(a) (sizeof(a) / sizeof(a)[0])
> > > […]
> > 
> > Why parenthesis anyway? a[0] is an expression, not a type, and there is 
> > nothing to group here. sizeof is not a function!
> 
> Because it is a macro and there can be another expression (such as m+1
> where m is an array).  Macro would expand to
>   (sizeof(m+1) / sizeof(m+1[0]))
> instead of
>   (sizeof(m+1) / sizeof(m+1)[0])

Yup.

#define LEN(x) (sizeof (x) / sizeof *(x))

that's more readable imho.



Re: [dev] [st] [patch] misplaced parenthesis in LEN macro

2014-04-20 Thread sin
On Sun, Apr 20, 2014 at 08:58:23PM +0200, Roberto E. Vargas Caballero wrote:
> > #define LEN(x) (sizeof (x) / sizeof *(x))
> 
> I am used to read the other form, but I thing it is only a question
> of personal taste, and since the other form was sent before your suggestion
> I'll apply it.

Yes, personal preference.

In any case, it does not matter match - people are not going to spend
their entire lives reading that macro anyhow.



Re: [dev] [st] [patch] misplaced parenthesis in LEN macro

2014-04-20 Thread sin
On Sun, Apr 20, 2014 at 02:43:53PM +0200, FRIGN wrote:
> On Sun, 20 Apr 2014 13:53:33 +0200
> Alexander Huemer  wrote:
> 
> > > […]
> > > -#define LEN(a) (sizeof(a) / sizeof(a[0]))
> > > +#define LEN(a) (sizeof(a) / sizeof(a)[0])
> > > […]
> > 
> > Why parenthesis anyway? a[0] is an expression, not a type, and there is 
> > nothing to group here. sizeof is not a function!
> 
> #define LEN(a) (sizeof a / sizeof *a)
> 
> is the right way to do it.

You are missing the parentheses there.

Your macro provides the wrong answer for something like:

  int a[] = { 1, 2, 3, 4, 5 };
  printf("%zu\n", LEN(a + 2));

Cheers,
sin



[dev] [ubase] [patch] RFC: fifo listener mode for respawn

2014-04-21 Thread sin
Hi all,

Based on [0] and in particular:

"Despite it being against the "spirit" of systemd, this is perhaps the
cleanest and most reliable: have systemd poll whatever service the
daemon is supposed to provide. For example, if the service is starting
sshd on port 22, systemd could repeatedly try connecting to port 22,
with exponential backoff, until it succeeds. This approach requires no
modification to existing daemons, and if implemented correctly, would
have minimal cost (only at daemon start time) in cpu load and startup latency."

I added a fifo listener mode for respawn.  It will sit there waiting for
the specified fifo to become readable and it will restart the given program.
It discards all input on the fifo.

The monitoring tool does not exist yet but it can be written separately
and then simply do a dummy write to the respawn fifo.

Rate-limiting might have to happen somewhere but I'd like to keep respawn
dumb.

The number of writes do not correspond 1-1 with the number of invocations.
If the monitoring program writes too fast, the writes will most
probably be coalesced as far as the respawn tool is concerned.  So it is
likely for N writes very close together that it will trigger only 1
invocation of the program.

What do you think?

Cheers,
sin

[0] http://ewontfix.com/15/
>From bcb98c9736725eccfdaf23dfb53cbebc2d7973be Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 21 Apr 2014 11:48:09 +0100
Subject: [PATCH] Implement fifo listener mode for respawn

---
 respawn.1 |  8 +++-
 respawn.c | 44 +---
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/respawn.1 b/respawn.1
index f9d68a1..05ad86b 100644
--- a/respawn.1
+++ b/respawn.1
@@ -2,7 +2,7 @@
 .SH NAME
 \fBrespawn\fR - Spawn the given command repeatedly
 .SH SYNOPSIS
-\fBrespawn\fR [\fB-d\fI N\fR] \fIcmd\fR [\fIargs...\fR]
+\fBrespawn\fR [\fB-l\fI fifo\fR] [\fB-d\fI N\fR] \fIcmd\fR [\fIargs...\fR]
 .SH DESCRIPTION
 \fBrespawn\fR spawns the given \fIcmd\fR in a new session
 repeatedly.
@@ -10,3 +10,9 @@ repeatedly.
 .TP
 \fB-d\fR
 Set the delay between invocations of \fIcmd\fR.  It defaults to 0.
+.TP
+\fB-l\fR
+Listen on the specified \fIfifo\fR for writes.  For each write
+spawn a new instance of \fIcmd\fR.  This can be used in conjunction
+with a process supervisor to restart a particular program.  The \fB-l\fR
+and \fB-d\fR options are incompatible.  All writes are discarded.
diff --git a/respawn.c b/respawn.c
index 453c534..30ca01d 100644
--- a/respawn.c
+++ b/respawn.c
@@ -1,6 +1,12 @@
 /* See LICENSE file for copyright and license details. */
+#include 
 #include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -9,20 +15,28 @@
 static void
 usage(void)
 {
-   eprintf("usage: respawn [-d N] cmd [args...]\n");
+   eprintf("usage: respawn [-l fifo] [-d N] cmd [args...]\n");
 }
 
 int
 main(int argc, char *argv[])
 {
+   char *fifo = NULL;
+   unsigned int delay = 0;
pid_t pid;
+   char buf[BUFSIZ];
int savederrno;
-   unsigned int delay = 0;
+   int fd;
+   ssize_t n;
+   fd_set rdfd;
 
ARGBEGIN {
case 'd':
delay = estrtol(EARGF(usage()), 0);
break;
+   case 'l':
+   fifo = EARGF(usage());
+   break;
default:
usage();
} ARGEND;
@@ -30,7 +44,30 @@ main(int argc, char *argv[])
if(argc < 1)
usage();
 
+   if (fifo && delay > 0)
+   usage();
+
+   if (fifo) {
+   fd = open(fifo, O_RDWR | O_NONBLOCK);
+   if (fd < 0)
+   eprintf("open %s:", fifo);
+   }
+
while (1) {
+   if (fifo) {
+   FD_ZERO(&rdfd);
+   FD_SET(fd, &rdfd);
+   n = select(fd + 1, &rdfd, NULL, NULL, NULL);
+   if (n < 0)
+   eprintf("select:");
+   if (n == 0 || FD_ISSET(fd, &rdfd) == 0)
+   continue;
+   while ((n = read(fd, buf, sizeof(buf))) > 0)
+   ;
+   if (n < 0)
+   if (errno != EAGAIN)
+   eprintf("read %s:", fifo);
+   }
pid = fork();
if (pid < 0)
eprintf("fork:");
@@ -47,7 +84,8 @@ main(int argc, char *argv[])
waitpid(pid, NULL, 0);
break;
}
-   sleep(delay);
+   if (!fifo)
+   sleep(delay);
}
/* not reachable */
return EXIT_SUCCESS;
-- 
1.9.2



Re: [dev] [sinit] 0.9 release

2014-04-22 Thread sin
On Tue, Apr 22, 2014 at 09:09:31AM +0200, Martti Kühne wrote:
> On Sat, Apr 19, 2014 at 9:13 AM, Krol, Willem van de <008...@jfc.nl> wrote:
> > Isn't setpgid(0, 0) after setsid() redundant?
> >
> 
> No. [0] setsid() creates a new session.

The issue is related to setsid() - in particular if you use setpgid(0, 0)
to move a process from one process group to another the pgid specifies an
existing process group to be joined and the session ID of that group
must match the session ID of the joining process.

So if you try the code we had (with setstid() + setpgid(0, 0)) you will
see that the setpgid() call returns -EPERM.

So it is not redundant, it is plain wrong.

That is if I understand everything correctly :P

Cheers,
sin



Re: [dev] lock (1) - where does it go?

2014-04-22 Thread sin
On Tue, Apr 22, 2014 at 05:20:29PM -0400, Calvin Morrison wrote:
> > How are you getting on with this?  I am planning to do a bit more polishing
> > to ubase + add 1-2 tools and then make an initial v0.1 release.  Would
> 
> wouldn't this fit better in sbase?

depends... a naive implementation can be done portably and fit in sbase.
if we put it in ubase, I suspect we'll fix it to work with inotify instead.



Re: [dev] lock (1) - where does it go?

2014-04-22 Thread sin
On Tue, Apr 22, 2014 at 05:34:47PM -0400, Calvin Morrison wrote:
> On 22 April 2014 17:24, sin  wrote:
> > On Tue, Apr 22, 2014 at 05:20:29PM -0400, Calvin Morrison wrote:
> >> > How are you getting on with this?  I am planning to do a bit more 
> >> > polishing
> >> > to ubase + add 1-2 tools and then make an initial v0.1 release.  Would
> >>
> >> wouldn't this fit better in sbase?
> >
> > depends... a naive implementation can be done portably and fit in sbase.
> > if we put it in ubase, I suspect we'll fix it to work with inotify instead.
> >
> 
> might as well use inotify tools then
> 
>  inotifywait -e delete lock_dir

the point is to have something light and self-contained.

you might as well use gnu coreutils then.



Re: [dev] [sbase] [patch] Add comm

2013-06-18 Thread sin
On Tue, Jun 18, 2013 at 10:41:29AM -0400, Galos, David wrote:
> > Implemented comm(1).  I have not tested this very thoroughly so take it with
> > a grain of salt.
> 
> Dangit! You beat me to it! I wrote this just last night, and was about
> to make the manpage.

Damn!  I have not looked at your implementation yet but if it is better/simpler
we should include that in sbase.

(sorry don't have access to my other e-mail acct atm.)

Thanks,
stateless



Re: [dev] [sbase] [patch v2] Add md5sum

2013-06-19 Thread sin
On Wed, Jun 19, 2013 at 12:51:33PM +0200, Szabolcs Nagy wrote:
> * stateless  [2013-06-19 11:38:00 +0100]:
> > This is a version of md5sum(1) using the md5 routines from 9base - slightly
> > adapted to compile.
> > 
> 
> be careful with integer arithmetics in crypto code
> 
> your code invokes undefined behaviour because of
> signed int overflow:
> 
>  unsigned f(unsigned char c) { return c<<24; }
> 
> c is promoted to int not unsigned in the left shift
> 
> it will work in practice (usually) but that's only
> by accident
> 
> you should cast c to the right type

You are right.  I will send some incremental patches
to fix this (and for 9base too).
I presume this is in the utils/md5*.c code which I have not
looked at in anger yet.

Integer promotion rules are nasty!  I think something like
the following would still be ok?

unsigned f(unsigned int c) { return c<<24U; }

Thanks,
stateless



Re: [dev] [sbase] [patch v2] Add md5sum

2013-06-19 Thread sin
On Wed, Jun 19, 2013 at 02:55:12PM +0300, sin wrote:
> On Wed, Jun 19, 2013 at 12:51:33PM +0200, Szabolcs Nagy wrote:
> > * stateless  [2013-06-19 11:38:00 +0100]:
> > > This is a version of md5sum(1) using the md5 routines from 9base - 
> > > slightly
> > > adapted to compile.
> > > 
> > 
> > be careful with integer arithmetics in crypto code
> > 
> > your code invokes undefined behaviour because of
> > signed int overflow:
> > 
> >  unsigned f(unsigned char c) { return c<<24; }
> > 
> > c is promoted to int not unsigned in the left shift
> > 
> > it will work in practice (usually) but that's only
> > by accident
> > 
> > you should cast c to the right type
> 
> You are right.  I will send some incremental patches
> to fix this (and for 9base too).
> I presume this is in the utils/md5*.c code which I have not
> looked at in anger yet.
> 
> Integer promotion rules are nasty!  I think something like
> the following would still be ok?
> 
> unsigned f(unsigned int c) { return c<<24U; }

Although in this case we still have undefined behaviour
because unsigned int can be 2 bytes by the standard.

Depending on the ABI this might or might not be an issue.



Re: [dev] [sbase] [patch v2] Add md5sum

2013-06-19 Thread sin
On Wed, Jun 19, 2013 at 04:47:02PM +0200, Szabolcs Nagy wrote:
> * sin  [2013-06-19 15:00:43 +0300]:
> > > Integer promotion rules are nasty!  I think something like
> > > the following would still be ok?
> > > 
> > > unsigned f(unsigned int c) { return c<<24U; }
> > 
> > Although in this case we still have undefined behaviour
> > because unsigned int can be 2 bytes by the standard.
> > 
> > Depending on the ABI this might or might not be an issue.
> 
> don't worry about 2byte int, that does not work on linux
> (and posix platforms in general)
> 
> bit shift does not do arithmetic conversion:
> the signedness of the right operand does not matter,
> integer promotion is applied independently to the two
> sides
> 
> the usual way to do the char shift is
> 
>   (uint32_t)c<<24
> 
> or
> 
>   uint32_t w = c;
>   w << 24

Aha, I see - thanks :)



[dev] [sbase] [patch] Explicitly cast len to unsigned long for %lu

2013-06-28 Thread sin
Fix warning
>From 68b3e95270dcf54d55680f0184e6bd42ae2dfa4d Mon Sep 17 00:00:00 2001
From: stateless 
Date: Fri, 28 Jun 2013 23:30:59 +0100
Subject: [PATCH] Explicitly cast len to unsigned long for %lu

---
 cksum.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cksum.c b/cksum.c
index efe241b..d08ca31 100644
--- a/cksum.c
+++ b/cksum.c
@@ -100,7 +100,7 @@ cksum(int fd, const char *s)
for(i = len; i > 0; i >>= 8)
ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)];
 
-   printf("%u %lu", ~ck, len);
+   printf("%u %lu", ~ck, (unsigned long)len);
if(s != NULL)
printf(" %s", s);
putchar('\n');
-- 
1.7.12.1



Re: [dev] [sbase] chvt.c does not need to be executable

2013-07-02 Thread sin
On Tue, Jul 02, 2013 at 12:08:27PM -0400, Galos, David wrote:
> > so why keep it executable...
> 
> Indeed. chvt.c is now 644.

Hi David,

Could you also have a look at the two patches I've sent:

"Explicitly cast len to unsigned long for %lu" and
"v3 Add md5sum"

Thanks,
stateless



Re: [dev] [sbase] [patch] Explicitly cast len to unsigned long for %lu

2013-07-02 Thread sin
On Tue, Jul 02, 2013 at 02:17:30PM -0400, Galos, David wrote:
> > Fix warning
> 
> I have applied the patch for now. It is unfortunate that c89 does not
> support '%zu', because that really would have been the right thing to
> do.
> 
> David Galos

Yeah that was the first thing I did then I realized the code is compiled
with c89 compliance.



Re: [dev] [sbase] [patch v3] Add md5sum

2013-07-03 Thread sin
On Wed, Jul 03, 2013 at 01:16:35PM +0200, Szabolcs Nagy wrote:
> * Robert Ransom  [2013-07-03 10:26:03 +]:
> > On 7/3/13, Galos, David  wrote:
> > >> Added LICENSE.lpl as well and updated the license and copyright details
> > >> for
> > >> the md5 code.
> > >
> > > I am considering applying this patch. The license is why I have to take
> > > time to think. I'm worried about setting a precedent which allows external
> > > differently-licensed software to be swept into sbase. It is clean code,
> > > which is a large part of why I am considering it.
> > 
> > Why not use the public-domain MD5 implementation from libtomcrypt?
> > 
> 
> here is a cleaned up version
> 
> http://git.musl-libc.org/cgit/musl/tree/src/crypt/crypt_md5.c
> 
> the libtomcrypt version has gratuitous optimizations
> ifdef configurations and useless failure modes

That's cool.  I will spend some time today or tomorrow to re-write the
code and send in patch v4.

If that's fine then I can also proceed with sha1sum(1).

Thanks,
stateless



[dev] [sbase] [patch v4] Add md5sum

2013-07-04 Thread sin
Hi,

This is using nsz's stripped down md5 implementation
based on rfc1321 and libtomcrypt.

I will send a patch incrementally at some point
to implement the -c option.

Thanks,
stateless
>From ac8a2f508ca8cc5e66ee8b81b0ff4d16196e1dee Mon Sep 17 00:00:00 2001
From: stateless 
Date: Wed, 19 Jun 2013 09:54:52 +0100
Subject: [PATCH v4] Add md5sum

No support for -c at the moment.
---
 Makefile   |   2 +
 md5.h  |  18 
 md5sum.1   |   8 
 md5sum.c   |  67 +
 util/md5.c | 139 +
 5 files changed, 234 insertions(+)
 create mode 100644 md5.h
 create mode 100644 md5sum.1
 create mode 100644 md5sum.c
 create mode 100644 util/md5.c

diff --git a/Makefile b/Makefile
index 26c831f..a54f902 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ LIB = \
util/estrtol.o   \
util/fnck.o  \
util/getlines.o  \
+   util/md5.o   \
util/putword.o   \
util/recurse.o   \
util/rm.o
@@ -45,6 +46,7 @@ SRC = \
ln.c   \
ls.c   \
mc.c   \
+   md5sum.c   \
mkdir.c\
mkfifo.c   \
mknod.c\
diff --git a/md5.h b/md5.h
new file mode 100644
index 000..0895d3e
--- /dev/null
+++ b/md5.h
@@ -0,0 +1,18 @@
+/* public domain md5 implementation based on rfc1321 and libtomcrypt */
+
+struct md5 {
+   uint64_t len;/* processed message length */
+   uint32_t h[4];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { MD5_DIGEST_LENGTH = 16 };
+
+/* reset state */
+void md5_init(struct md5 *s);
+/* process message */
+void md5_update(struct md5 *s, const void *m, unsigned long len);
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
diff --git a/md5sum.1 b/md5sum.1
new file mode 100644
index 000..93ecb2f
--- /dev/null
+++ b/md5sum.1
@@ -0,0 +1,8 @@
+.TH MD5SUM 1 sbase\-VERSION
+.SH NAME
+md5sum \- compute MD5 message digest
+.SH SYNOPSIS
+.B md5sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print MD5 (128-bit) checksums. With no file, read standard input.
diff --git a/md5sum.c b/md5sum.c
new file mode 100644
index 000..722416f
--- /dev/null
+++ b/md5sum.c
@@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+#include "md5.h"
+
+static void md5sum(int fd, const char *f);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   int fd;
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   md5sum(STDIN_FILENO, "");
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fd = open(*argv, O_RDONLY)) < 0)
+   eprintf("open %s:", *argv);
+   md5sum(fd, *argv);
+   close(fd);
+   argv++;
+   }
+   }
+
+   return 0;
+}
+
+static void
+md5sum(int fd, const char *f)
+{
+   unsigned char buf[BUFSIZ];
+   unsigned char digest[MD5_DIGEST_LENGTH];
+   struct md5 s;
+   ssize_t n;
+   int i;
+
+   md5_init(&s);
+   while ((n = read(fd, buf, sizeof buf)) > 0)
+   md5_update(&s, buf, n);
+   if (n < 0) {
+   eprintf("%s: read error:", f);
+   return;
+   }
+
+   md5_sum(&s, digest);
+
+   for (i = 0; i < sizeof(digest); i++)
+   printf("%02x", digest[i]);
+   printf("  %s\n", f);
+}
diff --git a/util/md5.c b/util/md5.c
new file mode 100644
index 000..0d8353d
--- /dev/null
+++ b/util/md5.c
@@ -0,0 +1,139 @@
+/* public domain md5 implementation based on rfc1321 and libtomcrypt */
+#include 
+#include 
+#include "../md5.h"
+
+static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
+#define F(x,y,z) (z ^ (x & (y ^ z)))
+#define G(x,y,z) (y ^ (z & (y ^ x)))
+#define H(x,y,z) (x ^ y ^ z)
+#define I(x,y,z) (y ^ (x | ~z))
+#define FF(a,b,c,d,w,s,t) a += F(b,c,d) + w + t; a = rol(a,s) + b
+#define GG(a,b,c,d,w,s,t) a += G(b,c,d) + w + t; a = rol(a,s) + b
+#define HH(a,b,c,d,w,s,t) a += H(b,c,d) + w + t; a = rol(a,s) + b
+#define II(a,b,c,d,w,s,t) a += I(b,c,d) + w + t; a = rol(a,s) + b
+
+static const uint32_t tab[64] = {
+   0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 
0xa8304613, 0xfd469501,
+   0x698098d8, 0x8b44f7af, 0x5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 
0xa679438e, 0x49b40821,
+   0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 
0xd8a1e681, 0xe7d3fbc8,
+   0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 

[dev] [sbase] [patch] Add sha1sum

2013-07-04 Thread sin
Hi,

Added sha1sum(1) based on nsz's stripped down version
of libtomcrypt from http://port70.net/~nsz/crypt/.

Thanks,
stateless
>From a1714a24098fe363fc244f28e07ab0cd0814de2e Mon Sep 17 00:00:00 2001
From: stateless 
Date: Thu, 4 Jul 2013 12:14:14 +0100
Subject: [PATCH] Add sha1sum

No support for -c at the moment.
---
 Makefile|   4 +-
 sha1.h  |  18 
 sha1sum.1   |   8 
 sha1sum.c   |  67 ++
 util/sha1.c | 134 
 5 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 sha1.h
 create mode 100644 sha1sum.1
 create mode 100644 sha1sum.c
 create mode 100644 util/sha1.c

diff --git a/Makefile b/Makefile
index a54f902..f825ec4 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,8 @@ LIB = \
util/md5.o   \
util/putword.o   \
util/recurse.o   \
-   util/rm.o
+   util/rm.o\
+   util/sha1.o
 
 SRC = \
basename.c \
@@ -76,6 +77,7 @@ SRC = \
uniq.c \
unlink.c   \
seq.c  \
+   sha1sum.c  \
wc.c   \
who.c  \
yes.c
diff --git a/sha1.h b/sha1.h
new file mode 100644
index 000..e11f49e
--- /dev/null
+++ b/sha1.h
@@ -0,0 +1,18 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+
+struct sha1 {
+   uint64_t len;/* processed message length */
+   uint32_t h[5];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { SHA1_DIGEST_LENGTH = 20 };
+
+/* reset state */
+void sha1_init(struct sha1 *s);
+/* process message */
+void sha1_update(struct sha1 *s, const void *m, unsigned long len);
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha1_sum(struct sha1 *s, uint8_t md[SHA1_DIGEST_LENGTH]);
diff --git a/sha1sum.1 b/sha1sum.1
new file mode 100644
index 000..10ac3c5
--- /dev/null
+++ b/sha1sum.1
@@ -0,0 +1,8 @@
+.TH SHA1SUM 1 sbase\-VERSION
+.SH NAME
+sha1sum \- compute SHA1 message digest
+.SH SYNOPSIS
+.B sha1sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SH1 (160-bit) checksums. With no file, read standard input.
diff --git a/sha1sum.c b/sha1sum.c
new file mode 100644
index 000..e38346a
--- /dev/null
+++ b/sha1sum.c
@@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+#include "sha1.h"
+
+static void sha1sum(int fd, const char *f);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   int fd;
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   sha1sum(STDIN_FILENO, "");
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fd = open(*argv, O_RDONLY)) < 0)
+   eprintf("open %s:", *argv);
+   sha1sum(fd, *argv);
+   close(fd);
+   argv++;
+   }
+   }
+
+   return 0;
+}
+
+static void
+sha1sum(int fd, const char *f)
+{
+   unsigned char buf[BUFSIZ];
+   unsigned char digest[SHA1_DIGEST_LENGTH];
+   struct sha1 s;
+   ssize_t n;
+   int i;
+
+   sha1_init(&s);
+   while ((n = read(fd, buf, sizeof buf)) > 0)
+   sha1_update(&s, buf, n);
+   if (n < 0) {
+   eprintf("%s: read error:", f);
+   return;
+   }
+
+   sha1_sum(&s, digest);
+
+   for (i = 0; i < sizeof(digest); i++)
+   printf("%02x", digest[i]);
+   printf("  %s\n", f);
+}
diff --git a/util/sha1.c b/util/sha1.c
new file mode 100644
index 000..f828395
--- /dev/null
+++ b/util/sha1.c
@@ -0,0 +1,134 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+#include 
+#include 
+#include "../sha1.h"
+
+static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
+#define F0(b,c,d) (d ^ (b & (c ^ d)))
+#define F1(b,c,d) (b ^ c ^ d)
+#define F2(b,c,d) ((b & c) | (d & (b | c)))
+#define F3(b,c,d) (b ^ c ^ d)
+#define G0(a,b,c,d,e,i) e += rol(a,5)+F0(b,c,d)+W[i]+0x5A827999; b = rol(b,30)
+#define G1(a,b,c,d,e,i) e += rol(a,5)+F1(b,c,d)+W[i]+0x6ED9EBA1; b = rol(b,30)
+#define G2(a,b,c,d,e,i) e += rol(a,5)+F2(b,c,d)+W[i]+0x8F1BBCDC; b = rol(b,30)
+#define G3(a,b,c,d,e,i) e += rol(a,5)+F3(b,c,d)+W[i]+0xCA62C1D6; b = rol(b,30)
+
+static void processblock(struct sha1 *s, const uint8_t *buf)
+{
+   uint32_t W[80], a, b, c, d, e;
+   int i;
+
+   for (i = 0; i < 16; i++) {
+   W[i] = (uint32_t)buf[4*i]<<24;
+   W[i] |= (uint32_t)buf[4*i+1]<<16;
+   W[i] |= (uint32_t)buf[4*i+2]<<8;
+   W[i] |= buf[4*i+3];
+   }
+   for (; i < 80; i++)
+  

Re: [dev] [sbase] [patch] Add sha1sum

2013-07-04 Thread sin
On Thu, Jul 04, 2013 at 12:34:14PM +, Robert Ransom wrote:
> On 7/4/13, sin  wrote:
> > Hi,
> >
> > Added sha1sum(1) based on nsz's stripped down version
> > of libtomcrypt from http://port70.net/~nsz/crypt/.
> 
> It's "SHA-1", not "SH1".

Oops, that's a typo - thanks for spotting.  Will re-send.

> sha1sum.c is very similar to md5sum.c; ideally, more of the common
> code between those programs would be in a library routine.

Yeah they are very similar, however, the code is very simple and
there is not going to be more programs in sbase that will use that code
as far as I know.  If that is indeed the case then I can factor out
the common bits incrementally when we need it.

Both the md5.[ch] and sha1.[ch] code is very simple to use.

Thanks,
stateless



[dev] [sbase] [patch v2] Add sha1sum

2013-07-04 Thread sin
Hi, 

Added sha1sum(1) based on nsz's stripped down version 
of libtomcrypt from http://port70.net/~nsz/crypt/.

No support for -c at the moment.

Thanks,
stateless
>From 71ffbed273a508345c5f4771c50892a4fcf4b057 Mon Sep 17 00:00:00 2001
From: stateless 
Date: Thu, 4 Jul 2013 12:14:14 +0100
Subject: [PATCH] Add sha1sum

No support for -c at the moment.
---
 Makefile|   4 +-
 sha1.h  |  18 
 sha1sum.1   |   8 
 sha1sum.c   |  67 ++
 util/sha1.c | 134 
 5 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 sha1.h
 create mode 100644 sha1sum.1
 create mode 100644 sha1sum.c
 create mode 100644 util/sha1.c

diff --git a/Makefile b/Makefile
index a54f902..f825ec4 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,8 @@ LIB = \
util/md5.o   \
util/putword.o   \
util/recurse.o   \
-   util/rm.o
+   util/rm.o\
+   util/sha1.o
 
 SRC = \
basename.c \
@@ -76,6 +77,7 @@ SRC = \
uniq.c \
unlink.c   \
seq.c  \
+   sha1sum.c  \
wc.c   \
who.c  \
yes.c
diff --git a/sha1.h b/sha1.h
new file mode 100644
index 000..e11f49e
--- /dev/null
+++ b/sha1.h
@@ -0,0 +1,18 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+
+struct sha1 {
+   uint64_t len;/* processed message length */
+   uint32_t h[5];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { SHA1_DIGEST_LENGTH = 20 };
+
+/* reset state */
+void sha1_init(struct sha1 *s);
+/* process message */
+void sha1_update(struct sha1 *s, const void *m, unsigned long len);
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha1_sum(struct sha1 *s, uint8_t md[SHA1_DIGEST_LENGTH]);
diff --git a/sha1sum.1 b/sha1sum.1
new file mode 100644
index 000..8f8a2df
--- /dev/null
+++ b/sha1sum.1
@@ -0,0 +1,8 @@
+.TH SHA1SUM 1 sbase\-VERSION
+.SH NAME
+sha1sum \- compute SHA-1 message digest
+.SH SYNOPSIS
+.B sha1sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SHA-1 (160-bit) checksums. With no file, read standard input.
diff --git a/sha1sum.c b/sha1sum.c
new file mode 100644
index 000..e38346a
--- /dev/null
+++ b/sha1sum.c
@@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+#include "sha1.h"
+
+static void sha1sum(int fd, const char *f);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   int fd;
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   sha1sum(STDIN_FILENO, "");
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fd = open(*argv, O_RDONLY)) < 0)
+   eprintf("open %s:", *argv);
+   sha1sum(fd, *argv);
+   close(fd);
+   argv++;
+   }
+   }
+
+   return 0;
+}
+
+static void
+sha1sum(int fd, const char *f)
+{
+   unsigned char buf[BUFSIZ];
+   unsigned char digest[SHA1_DIGEST_LENGTH];
+   struct sha1 s;
+   ssize_t n;
+   int i;
+
+   sha1_init(&s);
+   while ((n = read(fd, buf, sizeof buf)) > 0)
+   sha1_update(&s, buf, n);
+   if (n < 0) {
+   eprintf("%s: read error:", f);
+   return;
+   }
+
+   sha1_sum(&s, digest);
+
+   for (i = 0; i < sizeof(digest); i++)
+   printf("%02x", digest[i]);
+   printf("  %s\n", f);
+}
diff --git a/util/sha1.c b/util/sha1.c
new file mode 100644
index 000..f828395
--- /dev/null
+++ b/util/sha1.c
@@ -0,0 +1,134 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+#include 
+#include 
+#include "../sha1.h"
+
+static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
+#define F0(b,c,d) (d ^ (b & (c ^ d)))
+#define F1(b,c,d) (b ^ c ^ d)
+#define F2(b,c,d) ((b & c) | (d & (b | c)))
+#define F3(b,c,d) (b ^ c ^ d)
+#define G0(a,b,c,d,e,i) e += rol(a,5)+F0(b,c,d)+W[i]+0x5A827999; b = rol(b,30)
+#define G1(a,b,c,d,e,i) e += rol(a,5)+F1(b,c,d)+W[i]+0x6ED9EBA1; b = rol(b,30)
+#define G2(a,b,c,d,e,i) e += rol(a,5)+F2(b,c,d)+W[i]+0x8F1BBCDC; b = rol(b,30)
+#define G3(a,b,c,d,e,i) e += rol(a,5)+F3(b,c,d)+W[i]+0xCA62C1D6; b = rol(b,30)
+
+static void processblock(struct sha1 *s, const uint8_t *buf)
+{
+   uint32_t W[80], a, b, c, d, e;
+   int i;
+
+   for (i = 0; i < 16; i++) {
+   W[i] = (uint32_t)buf[4*i]<<24;
+   W[i] |= (uint32_t)buf[4*i+1]<<16;
+   W[i] |= (uint32_t)buf[4*i+2]<<8;
+   W[i] |= buf[4*i+3];
+ 

[dev] [sbase] [patch] Add crypt.[ch] and update md5sum and sha1sum

2013-07-07 Thread sin
Hi,

Let me know if this makes sense.

Thanks,
stateless
>From 6b7835c72d91bae7670ec7fab1d7ad1e1f0827cb Mon Sep 17 00:00:00 2001
From: sin 
Date: Sun, 7 Jul 2013 15:29:45 +0100
Subject: [PATCH] Add crypt.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
---
 Makefile |  1 +
 crypt.h  |  9 +++
 md5sum.c | 78 +++-
 sha1sum.c| 78 +++-
 util/crypt.c | 25 +++
 5 files changed, 137 insertions(+), 54 deletions(-)
 create mode 100644 crypt.h
 create mode 100644 util/crypt.c

diff --git a/Makefile b/Makefile
index f825ec4..c58fb86 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ LIB = \
util/apathmax.o  \
util/concat.o\
util/cp.o\
+   util/crypt.o \
util/enmasse.o   \
util/eprintf.o   \
util/estrtol.o   \
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 000..ad0392e
--- /dev/null
+++ b/crypt.h
@@ -0,0 +1,9 @@
+struct crypt_ops {
+   void (*init)(struct crypt_ops *);
+   void (*update)(struct crypt_ops *, const void *, unsigned long);
+   void (*sum)(struct crypt_ops *);
+   void (*print)(struct crypt_ops *, const char *);
+   void *priv;
+};
+
+int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f);
diff --git a/md5sum.c b/md5sum.c
index 722416f..c66909c 100644
--- a/md5sum.c
+++ b/md5sum.c
@@ -1,15 +1,28 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 #include "util.h"
+#include "crypt.h"
 #include "md5.h"
 
-static void md5sum(int fd, const char *f);
+static void crypt_md5_init(struct crypt_ops *);
+static void crypt_md5_update(struct crypt_ops *, const void *, unsigned long);
+static void crypt_md5_sum(struct crypt_ops *);
+static void crypt_md5_print(struct crypt_ops *, const char *);
+
+struct md5_priv {
+   struct md5 s;
+   unsigned char digest[MD5_DIGEST_LENGTH];
+} md5_priv;
+
+struct crypt_ops md5_ops = {
+   crypt_md5_init,
+   crypt_md5_update,
+   crypt_md5_sum,
+   crypt_md5_print,
+   &md5_priv,
+};
 
 static void
 usage(void)
@@ -20,7 +33,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   int fd;
+   FILE *fp;
 
ARGBEGIN {
default:
@@ -28,13 +41,13 @@ main(int argc, char *argv[])
} ARGEND;
 
if (argc == 0) {
-   md5sum(STDIN_FILENO, "");
+   cryptsum(&md5_ops, stdin, "");
} else {
for (; argc > 0; argc--) {
-   if ((fd = open(*argv, O_RDONLY)) < 0)
-   eprintf("open %s:", *argv);
-   md5sum(fd, *argv);
-   close(fd);
+   if ((fp = fopen(*argv, "r"))  == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&md5_ops, fp, *argv);
+   fclose(fp);
argv++;
}
}
@@ -43,25 +56,36 @@ main(int argc, char *argv[])
 }
 
 static void
-md5sum(int fd, const char *f)
+crypt_md5_init(struct crypt_ops *ops)
 {
-   unsigned char buf[BUFSIZ];
-   unsigned char digest[MD5_DIGEST_LENGTH];
-   struct md5 s;
-   ssize_t n;
-   int i;
+   struct md5_priv *priv = ops->priv;
 
-   md5_init(&s);
-   while ((n = read(fd, buf, sizeof buf)) > 0)
-   md5_update(&s, buf, n);
-   if (n < 0) {
-   eprintf("%s: read error:", f);
-   return;
-   }
+   md5_init(&priv->s);
+}
+
+static void
+crypt_md5_update(struct crypt_ops *ops, const void *m, unsigned long len)
+{
+   struct md5_priv *priv = ops->priv;
+
+   md5_update(&priv->s, m, len);
+}
+
+static void
+crypt_md5_sum(struct crypt_ops *ops)
+{
+   struct md5_priv *priv = ops->priv;
 
-   md5_sum(&s, digest);
+   md5_sum(&priv->s, priv->digest);
+}
+
+static void
+crypt_md5_print(struct crypt_ops *ops, const char *f)
+{
+   struct md5_priv *priv = ops->priv;
+   int i;
 
-   for (i = 0; i < sizeof(digest); i++)
-   printf("%02x", digest[i]);
+   for (i = 0; i < sizeof(priv->digest); i++)
+   printf("%02x", priv->digest[i]);
printf("  %s\n", f);
 }
diff --git a/sha1sum.c b/sha1sum.c
index e38346a..558be90 100644
--- a/sha1sum.c
+++ b/sha1sum.c
@@ -1,15 +1,28 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#in

[dev] [sbase] [patch v2] Add crypt.[ch] and update md5sum and sha1sum

2013-07-08 Thread sin
Hi,

An updated version of the previous patch based on the feedback.

Thanks,
stateless
>From b3ca3b5ca35f368ab81ae5f6ce3cfbfd7089ef49 Mon Sep 17 00:00:00 2001
From: sin 
Date: Sun, 7 Jul 2013 15:29:45 +0100
Subject: [PATCH v2] Add crypt.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
---
 Makefile |  1 +
 crypt.h  | 10 ++
 md5.h|  6 +++---
 md5sum.c | 49 +
 sha1.h   |  6 +++---
 sha1sum.c| 49 +
 util/crypt.c | 25 +
 util/md5.c   |  9 ++---
 util/sha1.c  | 10 +++---
 9 files changed, 97 insertions(+), 68 deletions(-)
 create mode 100644 crypt.h
 create mode 100644 util/crypt.c

diff --git a/Makefile b/Makefile
index f825ec4..c58fb86 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ LIB = \
util/apathmax.o  \
util/concat.o\
util/cp.o\
+   util/crypt.o \
util/enmasse.o   \
util/eprintf.o   \
util/estrtol.o   \
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 000..e69dad5
--- /dev/null
+++ b/crypt.h
@@ -0,0 +1,10 @@
+struct crypt_ops {
+   void (*init)(void *);
+   void (*update)(void *, const void *, unsigned long);
+   void (*sum)(void *, uint8_t *);
+   void (*print)(void *, const char *, uint8_t *);
+   void *s;
+};
+
+int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f,
+uint8_t *md);
diff --git a/md5.h b/md5.h
index 0895d3e..0b5005e 100644
--- a/md5.h
+++ b/md5.h
@@ -9,10 +9,10 @@ struct md5 {
 enum { MD5_DIGEST_LENGTH = 16 };
 
 /* reset state */
-void md5_init(struct md5 *s);
+void md5_init(void *ctx);
 /* process message */
-void md5_update(struct md5 *s, const void *m, unsigned long len);
+void md5_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is ruined after sum, keep a copy if multiple sum is needed */
 /* part of the message might be left in s, zero it if secrecy is needed */
-void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
+void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH]);
diff --git a/md5sum.c b/md5sum.c
index 722416f..8705b4b 100644
--- a/md5sum.c
+++ b/md5sum.c
@@ -1,15 +1,21 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 #include "util.h"
+#include "crypt.h"
 #include "md5.h"
 
-static void md5sum(int fd, const char *f);
+static void md5_print(void *, const char *, uint8_t *);
+
+struct md5 s;
+struct crypt_ops md5_ops = {
+   md5_init,
+   md5_update,
+   md5_sum,
+   md5_print,
+   &s,
+};
 
 static void
 usage(void)
@@ -20,7 +26,8 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   int fd;
+   FILE *fp;
+   uint8_t md[MD5_DIGEST_LENGTH];
 
ARGBEGIN {
default:
@@ -28,13 +35,13 @@ main(int argc, char *argv[])
} ARGEND;
 
if (argc == 0) {
-   md5sum(STDIN_FILENO, "");
+   cryptsum(&md5_ops, stdin, "", md);
} else {
for (; argc > 0; argc--) {
-   if ((fd = open(*argv, O_RDONLY)) < 0)
-   eprintf("open %s:", *argv);
-   md5sum(fd, *argv);
-   close(fd);
+   if ((fp = fopen(*argv, "r"))  == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&md5_ops, fp, *argv, md);
+   fclose(fp);
argv++;
}
}
@@ -43,25 +50,11 @@ main(int argc, char *argv[])
 }
 
 static void
-md5sum(int fd, const char *f)
+md5_print(void *s, const char *f, uint8_t *md)
 {
-   unsigned char buf[BUFSIZ];
-   unsigned char digest[MD5_DIGEST_LENGTH];
-   struct md5 s;
-   ssize_t n;
int i;
 
-   md5_init(&s);
-   while ((n = read(fd, buf, sizeof buf)) > 0)
-   md5_update(&s, buf, n);
-   if (n < 0) {
-   eprintf("%s: read error:", f);
-   return;
-   }
-
-   md5_sum(&s, digest);
-
-   for (i = 0; i < sizeof(digest); i++)
-   printf("%02x", digest[i]);
+   for (i = 0; i < MD5_DIGEST_LENGTH; i++)
+   printf("%02x", md[i]);
printf("  %s\n", f);
 }
diff --git a/sha1.h b/sha1.h
index e11f49e..8631777 100644
--- a/sha1.h
+++ b/sha1.h
@@ -9,10 +9,10 @@ struct sha1 {
 enum { SHA1_DIGEST_LENGTH = 20 };
 
 /* reset state */
-void sha1_init(struct sha1 *s);
+void sha1_init(void *ctx);
 /* process m

Re: [dev][sbase] Readlink doesn't null-terminate buf

2013-07-09 Thread sin
On Tue, Jul 09, 2013 at 05:48:47PM -0400, Galos, David wrote:
> > The patch introduces buffer overflow. sizeof(buf)-1 should be passed
> > to readlink().
> 
> Furthermore, buf should be made (PATH_MAX + 1) bytes in size, so that
> valid paths don't get truncated.

{PATH_MAX}
Maximum number of bytes in a pathname, including the
terminating null character.[1]

[1] http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html

Thanks,
stateless



[dev] [sbase] [patch v3] Add sum.[ch] and update md5sum and sha1sum

2013-07-12 Thread sin
Hi,

Renamed from crypt to sum.

Thanks,
stateless
>From be2fbf8c8cb6ec434ad2d65ac3eeaf8882c60d77 Mon Sep 17 00:00:00 2001
From: sin 
Date: Sun, 7 Jul 2013 15:29:45 +0100
Subject: [PATCH] Add sum.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
---
 Makefile|  3 ++-
 md5.h   |  6 +++---
 md5sum.c| 49 +
 sha1.h  |  6 +++---
 sha1sum.c   | 49 +
 sum.h   | 10 ++
 util/md5.c  |  9 ++---
 util/sha1.c | 10 +++---
 util/sum.c  | 25 +
 9 files changed, 98 insertions(+), 69 deletions(-)
 create mode 100644 sum.h
 create mode 100644 util/sum.c

diff --git a/Makefile b/Makefile
index f825ec4..a5cd1e4 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,8 @@ LIB = \
util/putword.o   \
util/recurse.o   \
util/rm.o\
-   util/sha1.o
+   util/sha1.o  \
+   util/sum.o
 
 SRC = \
basename.c \
diff --git a/md5.h b/md5.h
index 0895d3e..0b5005e 100644
--- a/md5.h
+++ b/md5.h
@@ -9,10 +9,10 @@ struct md5 {
 enum { MD5_DIGEST_LENGTH = 16 };
 
 /* reset state */
-void md5_init(struct md5 *s);
+void md5_init(void *ctx);
 /* process message */
-void md5_update(struct md5 *s, const void *m, unsigned long len);
+void md5_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is ruined after sum, keep a copy if multiple sum is needed */
 /* part of the message might be left in s, zero it if secrecy is needed */
-void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
+void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH]);
diff --git a/md5sum.c b/md5sum.c
index 722416f..4077bc8 100644
--- a/md5sum.c
+++ b/md5sum.c
@@ -1,15 +1,21 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 #include "util.h"
+#include "sum.h"
 #include "md5.h"
 
-static void md5sum(int fd, const char *f);
+static void md5_print(void *, const char *, uint8_t *);
+
+struct md5 s;
+struct sum_ops md5_ops = {
+   md5_init,
+   md5_update,
+   md5_sum,
+   md5_print,
+   &s,
+};
 
 static void
 usage(void)
@@ -20,7 +26,8 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   int fd;
+   FILE *fp;
+   uint8_t md[MD5_DIGEST_LENGTH];
 
ARGBEGIN {
default:
@@ -28,13 +35,13 @@ main(int argc, char *argv[])
} ARGEND;
 
if (argc == 0) {
-   md5sum(STDIN_FILENO, "");
+   sum(&md5_ops, stdin, "", md);
} else {
for (; argc > 0; argc--) {
-   if ((fd = open(*argv, O_RDONLY)) < 0)
-   eprintf("open %s:", *argv);
-   md5sum(fd, *argv);
-   close(fd);
+   if ((fp = fopen(*argv, "r"))  == NULL)
+   eprintf("fopen %s:", *argv);
+   sum(&md5_ops, fp, *argv, md);
+   fclose(fp);
argv++;
}
}
@@ -43,25 +50,11 @@ main(int argc, char *argv[])
 }
 
 static void
-md5sum(int fd, const char *f)
+md5_print(void *s, const char *f, uint8_t *md)
 {
-   unsigned char buf[BUFSIZ];
-   unsigned char digest[MD5_DIGEST_LENGTH];
-   struct md5 s;
-   ssize_t n;
int i;
 
-   md5_init(&s);
-   while ((n = read(fd, buf, sizeof buf)) > 0)
-   md5_update(&s, buf, n);
-   if (n < 0) {
-   eprintf("%s: read error:", f);
-   return;
-   }
-
-   md5_sum(&s, digest);
-
-   for (i = 0; i < sizeof(digest); i++)
-   printf("%02x", digest[i]);
+   for (i = 0; i < MD5_DIGEST_LENGTH; i++)
+   printf("%02x", md[i]);
printf("  %s\n", f);
 }
diff --git a/sha1.h b/sha1.h
index e11f49e..8631777 100644
--- a/sha1.h
+++ b/sha1.h
@@ -9,10 +9,10 @@ struct sha1 {
 enum { SHA1_DIGEST_LENGTH = 20 };
 
 /* reset state */
-void sha1_init(struct sha1 *s);
+void sha1_init(void *ctx);
 /* process message */
-void sha1_update(struct sha1 *s, const void *m, unsigned long len);
+void sha1_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is ruined after sum, keep a copy if multiple sum is needed */
 /* part of the message might be left in s, zero it if secrecy is needed */
-void sha1_sum(struct sha1 *s, uint8_t md[SHA1_DIGEST_LENGTH]);
+void sha1_sum(void *ctx, uint8_t md[SHA1_DIGEST_LENGTH]);
diff --git a/sha1sum.c b/sha1sum.c
index e38346a..d948966 100644
--- a/sh

Re: [dev] [sbase] [patch v3] Add sum.[ch] and update md5sum and sha1sum

2013-07-12 Thread sin
On Fri, Jul 12, 2013 at 09:56:21AM -0500, Galos, David wrote:
> > Renamed from crypt to sum.
> I think crypt was a good name. cryptsum made sense as a function,
> I think that printing should be removed from cryptsum, though.
> Another function, cryptprint (or mdprint) could take a digest, and
> a length. I  believe this is a better separation of concerns.
> 
> v2 was good, and with those changes, I think it is definitely
> getting close to the point where I will apply it.

Ok, will keep it crypt.  Will implement the suggested changes
over the weekend and send another patch.



[dev] [sbase] [patch v4] Add crypt.[ch] and update md5sum and sha1sum

2013-07-13 Thread sin
Hi,

Added mdprint().

Thanks,
stateless
>From 09a63f4f38ed84914dfde1d00889714e45ae39f7 Mon Sep 17 00:00:00 2001
From: sin 
Date: Sun, 7 Jul 2013 15:29:45 +0100
Subject: [PATCH] Add crypt.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
---
 Makefile |  1 +
 crypt.h  | 10 ++
 md5.h|  6 +++---
 md5sum.c | 52 +---
 sha1.h   |  6 +++---
 sha1sum.c| 52 +---
 util/crypt.c | 33 +
 util/md5.c   |  9 ++---
 util/sha1.c  | 10 +++---
 9 files changed, 97 insertions(+), 82 deletions(-)
 create mode 100644 crypt.h
 create mode 100644 util/crypt.c

diff --git a/Makefile b/Makefile
index f825ec4..c58fb86 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ LIB = \
util/apathmax.o  \
util/concat.o\
util/cp.o\
+   util/crypt.o \
util/enmasse.o   \
util/eprintf.o   \
util/estrtol.o   \
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 000..9eedabf
--- /dev/null
+++ b/crypt.h
@@ -0,0 +1,10 @@
+struct crypt_ops {
+   void (*init)(void *);
+   void (*update)(void *, const void *, unsigned long);
+   void (*sum)(void *, uint8_t *);
+   void *s;
+};
+
+int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f,
+uint8_t *md);
+void mdprint(const uint8_t *md, const char *f, size_t len);
diff --git a/md5.h b/md5.h
index 0895d3e..0b5005e 100644
--- a/md5.h
+++ b/md5.h
@@ -9,10 +9,10 @@ struct md5 {
 enum { MD5_DIGEST_LENGTH = 16 };
 
 /* reset state */
-void md5_init(struct md5 *s);
+void md5_init(void *ctx);
 /* process message */
-void md5_update(struct md5 *s, const void *m, unsigned long len);
+void md5_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is ruined after sum, keep a copy if multiple sum is needed */
 /* part of the message might be left in s, zero it if secrecy is needed */
-void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
+void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH]);
diff --git a/md5sum.c b/md5sum.c
index 722416f..4fd449f 100644
--- a/md5sum.c
+++ b/md5sum.c
@@ -1,15 +1,18 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 #include "util.h"
+#include "crypt.h"
 #include "md5.h"
 
-static void md5sum(int fd, const char *f);
+struct md5 s;
+struct crypt_ops md5_ops = {
+   md5_init,
+   md5_update,
+   md5_sum,
+   &s,
+};
 
 static void
 usage(void)
@@ -20,7 +23,8 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   int fd;
+   FILE *fp;
+   uint8_t md[MD5_DIGEST_LENGTH];
 
ARGBEGIN {
default:
@@ -28,40 +32,18 @@ main(int argc, char *argv[])
} ARGEND;
 
if (argc == 0) {
-   md5sum(STDIN_FILENO, "");
+   cryptsum(&md5_ops, stdin, "", md);
+   mdprint(md, "", sizeof(md));
} else {
for (; argc > 0; argc--) {
-   if ((fd = open(*argv, O_RDONLY)) < 0)
-   eprintf("open %s:", *argv);
-   md5sum(fd, *argv);
-   close(fd);
+   if ((fp = fopen(*argv, "r"))  == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&md5_ops, fp, *argv, md);
+   mdprint(md, *argv, sizeof(md));
+   fclose(fp);
argv++;
}
}
 
return 0;
 }
-
-static void
-md5sum(int fd, const char *f)
-{
-   unsigned char buf[BUFSIZ];
-   unsigned char digest[MD5_DIGEST_LENGTH];
-   struct md5 s;
-   ssize_t n;
-   int i;
-
-   md5_init(&s);
-   while ((n = read(fd, buf, sizeof buf)) > 0)
-   md5_update(&s, buf, n);
-   if (n < 0) {
-   eprintf("%s: read error:", f);
-   return;
-   }
-
-   md5_sum(&s, digest);
-
-   for (i = 0; i < sizeof(digest); i++)
-   printf("%02x", digest[i]);
-   printf("  %s\n", f);
-}
diff --git a/sha1.h b/sha1.h
index e11f49e..8631777 100644
--- a/sha1.h
+++ b/sha1.h
@@ -9,10 +9,10 @@ struct sha1 {
 enum { SHA1_DIGEST_LENGTH = 20 };
 
 /* reset state */
-void sha1_init(struct sha1 *s);
+void sha1_init(void *ctx);
 /* process message */
-void sha1_update(struct sha1 *s, const void *m, unsigned long len);
+void sha1_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is

[dev] [sbase] [patch] Change contact info

2013-07-13 Thread sin

>From 12842899344f5582c6f9ca50c6a2fd87c995d6ce Mon Sep 17 00:00:00 2001
From: sin 
Date: Fri, 12 Jul 2013 14:24:55 +0100
Subject: [PATCH] Change contact info

---
 LICENSE | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/LICENSE b/LICENSE
index 8f1caaf..87351f6 100644
--- a/LICENSE
+++ b/LICENSE
@@ -2,7 +2,7 @@ MIT/X Consortium License
 
 © 2011 Connor Lane Smith 
 © 2011 Kamil Cholewiński 
-© 2011 stateless 
+© 2011 sin 
 © 2011 Rob Pilling 
 © 2011 Hiltjo Posthuma 
 © 2011 pancake 
-- 
1.8.3.2



[dev] [sbase] [patch v5] Add crypt.[ch] and update md5sum and sha1sum

2013-07-13 Thread sin
Update $(HDR) in Makefile to avoid build errors
with sbase-box target.
>From f3be4ec7d8c325bd9b844ce77b2dc7737291383d Mon Sep 17 00:00:00 2001
From: sin 
Date: Sun, 7 Jul 2013 15:29:45 +0100
Subject: [PATCH] Add crypt.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
---
 Makefile |  3 ++-
 crypt.h  | 10 ++
 md5.h|  6 +++---
 md5sum.c | 52 +---
 sha1.h   |  6 +++---
 sha1sum.c| 52 +---
 util/crypt.c | 33 +
 util/md5.c   |  9 ++---
 util/sha1.c  | 10 +++---
 9 files changed, 98 insertions(+), 83 deletions(-)
 create mode 100644 crypt.h
 create mode 100644 util/crypt.c

diff --git a/Makefile b/Makefile
index f825ec4..909f1ce 100644
--- a/Makefile
+++ b/Makefile
@@ -3,13 +3,14 @@ include config.mk
 .POSIX:
 .SUFFIXES: .c .o
 
-HDR = fs.h text.h util.h arg.h
+HDR = crypt.h fs.h text.h md5.h sha1.h util.h arg.h
 LIB = \
util/afgets.o\
util/agetcwd.o   \
util/apathmax.o  \
util/concat.o\
util/cp.o\
+   util/crypt.o \
util/enmasse.o   \
util/eprintf.o   \
util/estrtol.o   \
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 000..9eedabf
--- /dev/null
+++ b/crypt.h
@@ -0,0 +1,10 @@
+struct crypt_ops {
+   void (*init)(void *);
+   void (*update)(void *, const void *, unsigned long);
+   void (*sum)(void *, uint8_t *);
+   void *s;
+};
+
+int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f,
+uint8_t *md);
+void mdprint(const uint8_t *md, const char *f, size_t len);
diff --git a/md5.h b/md5.h
index 0895d3e..0b5005e 100644
--- a/md5.h
+++ b/md5.h
@@ -9,10 +9,10 @@ struct md5 {
 enum { MD5_DIGEST_LENGTH = 16 };
 
 /* reset state */
-void md5_init(struct md5 *s);
+void md5_init(void *ctx);
 /* process message */
-void md5_update(struct md5 *s, const void *m, unsigned long len);
+void md5_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is ruined after sum, keep a copy if multiple sum is needed */
 /* part of the message might be left in s, zero it if secrecy is needed */
-void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
+void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH]);
diff --git a/md5sum.c b/md5sum.c
index 722416f..4fd449f 100644
--- a/md5sum.c
+++ b/md5sum.c
@@ -1,15 +1,18 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 #include "util.h"
+#include "crypt.h"
 #include "md5.h"
 
-static void md5sum(int fd, const char *f);
+struct md5 s;
+struct crypt_ops md5_ops = {
+   md5_init,
+   md5_update,
+   md5_sum,
+   &s,
+};
 
 static void
 usage(void)
@@ -20,7 +23,8 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   int fd;
+   FILE *fp;
+   uint8_t md[MD5_DIGEST_LENGTH];
 
ARGBEGIN {
default:
@@ -28,40 +32,18 @@ main(int argc, char *argv[])
} ARGEND;
 
if (argc == 0) {
-   md5sum(STDIN_FILENO, "");
+   cryptsum(&md5_ops, stdin, "", md);
+   mdprint(md, "", sizeof(md));
} else {
for (; argc > 0; argc--) {
-   if ((fd = open(*argv, O_RDONLY)) < 0)
-   eprintf("open %s:", *argv);
-   md5sum(fd, *argv);
-   close(fd);
+   if ((fp = fopen(*argv, "r"))  == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&md5_ops, fp, *argv, md);
+   mdprint(md, *argv, sizeof(md));
+   fclose(fp);
argv++;
}
}
 
return 0;
 }
-
-static void
-md5sum(int fd, const char *f)
-{
-   unsigned char buf[BUFSIZ];
-   unsigned char digest[MD5_DIGEST_LENGTH];
-   struct md5 s;
-   ssize_t n;
-   int i;
-
-   md5_init(&s);
-   while ((n = read(fd, buf, sizeof buf)) > 0)
-   md5_update(&s, buf, n);
-   if (n < 0) {
-   eprintf("%s: read error:", f);
-   return;
-   }
-
-   md5_sum(&s, digest);
-
-   for (i = 0; i < sizeof(digest); i++)
-   printf("%02x", digest[i]);
-   printf("  %s\n", f);
-}
diff --git a/sha1.h b/sha1.h
index e11f49e..8631777 100644
--- a/sha1.h
+++ b/sha1.h
@@ -9,10 +9,10 @@ struct sha1 {
 enum { SHA1_DIGEST_LENGTH = 20 };
 
 /* reset state */
-void sha1_init(struct sha1 *s);

Re: [dev] [sbase] Patch to make md5 and sha1 more similar

2013-07-15 Thread sin
On Mon, Jul 15, 2013 at 02:00:35AM +0200, Jens Nyberg wrote:
> Hi,
> 
> I've included a patch that makes minor changes to md5 and sha1. Both
> hashing functions are pretty similar but the code differed in a few places
> so I cleaned them both up a bit.
> 
> Also I made some minor changes like moving the rol function to below the
> definitions and also instead of *= 8 I did a <<= 3 which is functionally
> equivalent but should save a tiny bit of speed (unless the compiler is
> smart enough to catch that and replaces it automatically).

I'd break this patch into multiple patches.  The change from *= 8 to <<= 3
doesn't make sense.  Maybe it did in the 80s but not anymore.



Re: [dev] [sbase] [patch v5] Add crypt.[ch] and update md5sum and sha1sum

2013-07-18 Thread sin
On Thu, Jul 18, 2013 at 09:59:30AM -0400, Galos, David wrote:
> >Added mdprint().
> 
> > Update $(HDR) in Makefile to avoid build errors
> > with sbase-box target.
> 
> I have decided to apply this; the net change in code went from
> +83 in the first version to a completely bearable +15. If sha512sum,
> et c. show up, this is definitely be net win.

I'm glad this was useful :)



[dev] [sbase] [patch] Add sha256sum(1)

2013-07-19 Thread sin
Hi,

Added sha256sum(1).

No support for -c.

Thanks,
sin
>From 2f8b277fbef7f7d40051cf2ceb751ea6b1b9b7bd Mon Sep 17 00:00:00 2001
From: sin 
Date: Fri, 19 Jul 2013 14:29:43 +0100
Subject: [PATCH 1/2] Add sha256sum(1)

---
 Makefile  |   4 +-
 sha256.h  |  18 +++
 sha256sum.1   |   8 
 sha256sum.c   |  49 +++
 util/sha256.c | 152 ++
 5 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 sha256.h
 create mode 100644 sha256sum.1
 create mode 100644 sha256sum.c
 create mode 100644 util/sha256.c

diff --git a/Makefile b/Makefile
index bd0c38c..da4d0d0 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,8 @@ LIB = \
util/putword.o   \
util/recurse.o   \
util/rm.o\
-   util/sha1.o
+   util/sha1.o  \
+   util/sha256.o
 
 SRC = \
basename.c \
@@ -80,6 +81,7 @@ SRC = \
unlink.c   \
seq.c  \
sha1sum.c  \
+   sha256sum.c\
wc.c   \
who.c  \
yes.c
diff --git a/sha256.h b/sha256.h
new file mode 100644
index 000..5968b8e
--- /dev/null
+++ b/sha256.h
@@ -0,0 +1,18 @@
+/* public domain sha256 implementation based on fips180-3 */
+
+struct sha256 {
+   uint64_t len;/* processed message length */
+   uint32_t h[8];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { SHA256_DIGEST_LENGTH = 32 };
+
+/* reset state */
+void sha256_init(void *ctx);
+/* process message */
+void sha256_update(void *ctx, const void *m, unsigned long len);
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha256_sum(void *ctx, uint8_t md[SHA256_DIGEST_LENGTH]);
diff --git a/sha256sum.1 b/sha256sum.1
new file mode 100644
index 000..494b9df
--- /dev/null
+++ b/sha256sum.1
@@ -0,0 +1,8 @@
+.TH SHA256SUM 1 sbase\-VERSION
+.SH NAME
+sha256sum \- compute SHA256 message digest
+.SH SYNOPSIS
+.B sha256sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SHA256 (256-bit) checksums. With no file, read standard input.
diff --git a/sha256sum.c b/sha256sum.c
new file mode 100644
index 000..ce97435
--- /dev/null
+++ b/sha256sum.c
@@ -0,0 +1,49 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include "util.h"
+#include "crypt.h"
+#include "sha256.h"
+
+struct sha256 s;
+struct crypt_ops sha256_ops = {
+   sha256_init,
+   sha256_update,
+   sha256_sum,
+   &s,
+};
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   FILE *fp;
+   uint8_t md[SHA256_DIGEST_LENGTH];
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   cryptsum(&sha256_ops, stdin, "", md);
+   mdprint(md, "", sizeof(md));
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fp = fopen(*argv, "r")) == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&sha256_ops, fp, *argv, md);
+   mdprint(md, *argv, sizeof(md));
+   fclose(fp);
+   argv++;
+   }
+   }
+
+   return 0;
+}
diff --git a/util/sha256.c b/util/sha256.c
new file mode 100644
index 000..94db29b
--- /dev/null
+++ b/util/sha256.c
@@ -0,0 +1,152 @@
+/*
+ * public domain sha256 crypt implementation
+ *
+ * original sha crypt design: http://people.redhat.com/drepper/SHA-crypt.txt
+ * in this implementation at least 32bit int is assumed,
+ * key length is limited, the $5$ prefix is mandatory, '\n' and ':' is rejected
+ * in the salt and rounds= setting must contain a valid iteration count,
+ * on error "*" is returned.
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "../sha256.h"
+
+/* public domain sha256 implementation based on fips180-3 */
+
+static uint32_t ror(uint32_t n, int k) { return (n >> k) | (n << (32-k)); }
+#define Ch(x,y,z)  (z ^ (x & (y ^ z)))
+#define Maj(x,y,z) ((x & y) | (z & (x | y)))
+#define S0(x)  (ror(x,2) ^ ror(x,13) ^ ror(x,22))
+#define S1(x)  (ror(x,6) ^ ror(x,11) ^ ror(x,25))
+#define R0(x)  (ror(x,7) ^ ror(x,18) ^ (x>>3))
+#define R1(x)  (ror(x,17) ^ ror(x,19) ^ (x>>10))
+
+static const uint32_t K[64] = {
+0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 
0x923f82a4, 0xab1c5ed5,
+0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 
0x9bdc06a7, 0xc19bf174,
+0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 
0x5cb0a9dc, 0x76f988da,
+0x983e5152, 0xa831c66d, 0xb00327

[dev] [sbase] [patch] Do not hardcode the size of md

2013-07-19 Thread sin
Just to be more consistent.
>From 9ed3cde216a7aeca0af1371b3890a50b852f4d51 Mon Sep 17 00:00:00 2001
From: sin 
Date: Fri, 19 Jul 2013 14:31:42 +0100
Subject: [PATCH 2/2] Do not hardcode the size of md

---
 util/md5.c  | 2 +-
 util/sha1.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/util/md5.c b/util/md5.c
index b68f729..dc2ca8e 100644
--- a/util/md5.c
+++ b/util/md5.c
@@ -105,7 +105,7 @@ void md5_init(void *ctx)
s->h[3] = 0x10325476;
 }
 
-void md5_sum(void *ctx, uint8_t md[16])
+void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH])
 {
struct md5 *s = ctx;
int i;
diff --git a/util/sha1.c b/util/sha1.c
index a298f9f..4003bae 100644
--- a/util/sha1.c
+++ b/util/sha1.c
@@ -101,7 +101,7 @@ void sha1_init(void *ctx)
s->h[4] = 0xC3D2E1F0;
 }
 
-void sha1_sum(void *ctx, uint8_t md[20])
+void sha1_sum(void *ctx, uint8_t md[SHA1_DIGEST_LENGTH])
 {
struct sha1 *s = ctx;
int i;
-- 
1.8.3.3



[dev] [sbase] [patch v2] Add sha256sum(1)

2013-07-19 Thread sin
Forgot to update $(HDR) in Makefile.
>From 9794df52d6af13e67815d796f05e53c6b0b1402a Mon Sep 17 00:00:00 2001
From: sin 
Date: Fri, 19 Jul 2013 14:29:43 +0100
Subject: [PATCH] Add sha256sum(1)

---
 Makefile  |   6 ++-
 sha256.h  |  18 +++
 sha256sum.1   |   8 
 sha256sum.c   |  49 +++
 util/sha256.c | 152 ++
 5 files changed, 231 insertions(+), 2 deletions(-)
 create mode 100644 sha256.h
 create mode 100644 sha256sum.1
 create mode 100644 sha256sum.c
 create mode 100644 util/sha256.c

diff --git a/Makefile b/Makefile
index bd0c38c..a904c47 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ include config.mk
 .POSIX:
 .SUFFIXES: .c .o
 
-HDR = crypt.h fs.h text.h md5.h sha1.h util.h arg.h
+HDR = crypt.h fs.h text.h md5.h sha1.h sha256.h util.h arg.h
 LIB = \
util/afgets.o\
util/agetcwd.o   \
@@ -20,7 +20,8 @@ LIB = \
util/putword.o   \
util/recurse.o   \
util/rm.o\
-   util/sha1.o
+   util/sha1.o  \
+   util/sha256.o
 
 SRC = \
basename.c \
@@ -80,6 +81,7 @@ SRC = \
unlink.c   \
seq.c  \
sha1sum.c  \
+   sha256sum.c\
wc.c   \
who.c  \
yes.c
diff --git a/sha256.h b/sha256.h
new file mode 100644
index 000..5968b8e
--- /dev/null
+++ b/sha256.h
@@ -0,0 +1,18 @@
+/* public domain sha256 implementation based on fips180-3 */
+
+struct sha256 {
+   uint64_t len;/* processed message length */
+   uint32_t h[8];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { SHA256_DIGEST_LENGTH = 32 };
+
+/* reset state */
+void sha256_init(void *ctx);
+/* process message */
+void sha256_update(void *ctx, const void *m, unsigned long len);
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha256_sum(void *ctx, uint8_t md[SHA256_DIGEST_LENGTH]);
diff --git a/sha256sum.1 b/sha256sum.1
new file mode 100644
index 000..494b9df
--- /dev/null
+++ b/sha256sum.1
@@ -0,0 +1,8 @@
+.TH SHA256SUM 1 sbase\-VERSION
+.SH NAME
+sha256sum \- compute SHA256 message digest
+.SH SYNOPSIS
+.B sha256sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SHA256 (256-bit) checksums. With no file, read standard input.
diff --git a/sha256sum.c b/sha256sum.c
new file mode 100644
index 000..ce97435
--- /dev/null
+++ b/sha256sum.c
@@ -0,0 +1,49 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include "util.h"
+#include "crypt.h"
+#include "sha256.h"
+
+struct sha256 s;
+struct crypt_ops sha256_ops = {
+   sha256_init,
+   sha256_update,
+   sha256_sum,
+   &s,
+};
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   FILE *fp;
+   uint8_t md[SHA256_DIGEST_LENGTH];
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   cryptsum(&sha256_ops, stdin, "", md);
+   mdprint(md, "", sizeof(md));
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fp = fopen(*argv, "r")) == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&sha256_ops, fp, *argv, md);
+   mdprint(md, *argv, sizeof(md));
+   fclose(fp);
+   argv++;
+   }
+   }
+
+   return 0;
+}
diff --git a/util/sha256.c b/util/sha256.c
new file mode 100644
index 000..94db29b
--- /dev/null
+++ b/util/sha256.c
@@ -0,0 +1,152 @@
+/*
+ * public domain sha256 crypt implementation
+ *
+ * original sha crypt design: http://people.redhat.com/drepper/SHA-crypt.txt
+ * in this implementation at least 32bit int is assumed,
+ * key length is limited, the $5$ prefix is mandatory, '\n' and ':' is rejected
+ * in the salt and rounds= setting must contain a valid iteration count,
+ * on error "*" is returned.
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "../sha256.h"
+
+/* public domain sha256 implementation based on fips180-3 */
+
+static uint32_t ror(uint32_t n, int k) { return (n >> k) | (n << (32-k)); }
+#define Ch(x,y,z)  (z ^ (x & (y ^ z)))
+#define Maj(x,y,z) ((x & y) | (z & (x | y)))
+#define S0(x)  (ror(x,2) ^ ror(x,13) ^ ror(x,22))
+#define S1(x)  (ror(x,6) ^ ror(x,11) ^ ror(x,25))
+#define R0(x)  (ror(x,7) ^ ror(x,18) ^ (x>>3))
+#define R1(x)  (ror(x,17) ^ ror(x,19) ^ (x>>10))
+
+static const uint32_t K[64] = {
+0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 
0x923f82a4, 0xab1c5ed5,
+0xd807a

[dev] [sbase] [patch] Remove trailing whitespace

2013-07-19 Thread sin

>From 32020667bde97a75741340ff926a804c135c29f5 Mon Sep 17 00:00:00 2001
From: sin 
Date: Fri, 19 Jul 2013 17:05:28 +0100
Subject: [PATCH] Remove trailing whitespace

---
 tar.c | 20 ++--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/tar.c b/tar.c
index 3481942..3de49fb 100644
--- a/tar.c
+++ b/tar.c
@@ -35,8 +35,8 @@ enum {
 };
 
 enum Type {
-   REG = '0', HARDLINK = '1', SYMLINK = '2', CHARDEV = '3', 
-   BLOCKDEV = '4', DIRECTORY = '5', FIFO = '6' 
+   REG = '0', HARDLINK = '1', SYMLINK = '2', CHARDEV = '3',
+   BLOCKDEV = '4', DIRECTORY = '5', FIFO = '6'
 };
 
 static void putoctal(char *, unsigned, int);
@@ -49,7 +49,7 @@ static void xt(int (*)(char*, int, char[Blksiz]));
 
 static FILE *tarfile;
 
-static void 
+static void
 usage(void)
 {
eprintf("usage: tar [-f tarfile] [-C dir] [-]x|t\n"
@@ -58,7 +58,7 @@ usage(void)
"   tar [-C dir] x|tf tarfile\n");
 }
 
-int 
+int
 main(int argc, char *argv[])
 {
char *file = NULL, *dir = ".", *ap;
@@ -142,13 +142,13 @@ putoctal(char *dst, unsigned num, int n)
snprintf(dst, n, "%.*o", n-1, num);
 }
 
-int 
+int
 strlcpy(char *dst, const char *src, int n)
 {
return snprintf(dst, n, "%s", src);
 }
 
-int 
+int
 archive(const char* path, const struct stat* sta, int type)
 {
unsigned char b[Blksiz];
@@ -209,10 +209,10 @@ archive(const char* path, const struct stat* sta, int 
type)
fwrite(b, Blksiz, 1, tarfile);
}
fclose(f);
-   return 0;   
+   return 0;
 }
 
-int 
+int
 unarchive(char *fname, int l, char b[Blksiz])
 {
char lname[101];
@@ -255,7 +255,7 @@ unarchive(char *fname, int l, char b[Blksiz])
default:
fprintf(stderr, "usupported tarfiletype %c\n", h->type);
}
-   if(getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8), 
+   if(getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8),
 strtoul(h->gid, 0, 8)))
perror(fname);
 
@@ -269,7 +269,7 @@ unarchive(char *fname, int l, char b[Blksiz])
return 0;
 }
 
-int 
+int
 print(char * fname, int l, char b[Blksiz])
 {
puts(fname);
-- 
1.8.3.3



Re: [dev] [PATCH] Add compatibility for OpenBSD in tar

2013-07-19 Thread sin
On Fri, Jul 19, 2013 at 04:29:23PM +0200, Roberto E. Vargas Caballero wrote:
> On Fri, Jul 19, 2013 at 09:19:41AM -0400, Galos, David wrote:
> > > OpenBSD defines strlcpy function, and the declaration is a bit different
> > > of the static declartion found in tar.c. The duplication of symbol name
> > > with different type (one extern and other static, one returning int and
> > > the other returning size_t) caused tar couldn't compile in OpenBSD.
> > 
> > Since nothing else in sbase makes use of strlcpy I don't think that it's
> > worth librarizing like this. I did it for consistency in `archive`, as I
> > populated the Header. It might be better for now to just call it strlmov,
> > or define a macro STRLCPY.
> 
> If we are going to use only in this file then maybe is better use the snprintf
> directly.

+1 - that makes more sense.

Thanks,
sin



[dev] [sbase] [patch] Change sprintf to snprintf

2013-07-19 Thread sin

>From 7b1ebb6295aade41f4a0269ac473fff1dbc19bf3 Mon Sep 17 00:00:00 2001
From: sin 
Date: Fri, 19 Jul 2013 17:08:15 +0100
Subject: [PATCH] Change sprintf to snprintf

cal.c:40: warning: sprintf() is often misused, please use snprintf()
---
 cal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cal.c b/cal.c
index f1ba10d..2c42ccd 100644
--- a/cal.c
+++ b/cal.c
@@ -37,7 +37,7 @@ drawcal(int year, int month, int day, int ncols, int nmons, 
int fday)
cur = moff % 12;
yoff = year + moff / 12;
 
-   sprintf(str, "%s %d", smon[cur], yoff);
+   snprintf(str, sizeof(str), "%s %d", smon[cur], yoff);
printf("%-20s   ", str);
count[i] = 1;
}
-- 
1.8.3.3



[dev] [sbase] [patch] Use int instead of char for getc()

2013-07-20 Thread sin

>From 1f2c098b079f2cdb275178215d27325a270adccd Mon Sep 17 00:00:00 2001
From: sin 
Date: Sat, 20 Jul 2013 12:09:42 +0100
Subject: [PATCH] Use int instead of char for getc()

---
 wc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/wc.c b/wc.c
index 45d6dda..4226a44 100644
--- a/wc.c
+++ b/wc.c
@@ -72,7 +72,7 @@ void
 wc(FILE *fp, const char *str)
 {
bool word = false;
-   char c;
+   int c;
long nc = 0, nl = 0, nw = 0;
 
while((c = getc(fp)) != EOF) {
-- 
1.8.3.3



Re: [dev] [sbase] [patch] Add sha256sum(1)

2013-07-20 Thread sin
On Sat, Jul 20, 2013 at 01:25:09PM -0400, Galos, David wrote:
> > /* public domain sha256 implementation based on fips180-3 */
> >
> > you may add that it's from the crypt code of musl libc
> > in case somebody wants to check upstream changes
> 
> I have applied the patch, then moved that line to the top of the
> file, and removed the notice about the nonexistent crypt code.

Cool!

Question, what are we to do with sha512sum?  It requires some
constants to be long long and we are compiling with C89/C90.

Any ideas?

Thanks,
sin



Re: [dev] [sbase] [patch] Add sha256sum(1)

2013-07-21 Thread sin
On Sun, Jul 21, 2013 at 09:10:31AM -0400, Galos, David wrote:
> >> Question, what are we to do with sha512sum?  It requires some
> >> constants to be long long and we are compiling with C89/C90.
> >>
> >> Any ideas?
> > So far, the solution to this has been to use int64_t in stdint.h
> > Although that header is only guaranteed in c99, the header tends
> > to be available in c89 mode, and the compiler does not warn
> > about it.
> 
> Actually, you said constants. I think the real solution to this problem
> is to add '-Wno-long-long' to CFLAGS. Writing 64bit emulation code
> would be idiotic, and if the underlying compiler doesn't have 64 bit
> support, it's not like anything short of such emulation would help
> anyway.

Cool, will have a look at it and send in a patch in the next few
days.



[dev] [sbase] [patch] Add sha512sum(1)

2013-07-22 Thread sin
Hi,

Added sha512sum(1).  No support for -c at the moment.
I've also updated CFLAGS with -Wno-long-long.

Added sha256.h and sha512.h in HDR as well.

At this point we could have a helper function in util/
to parse the checksums in files so we can easily support
the -c flag in md5sum, sha1sum and sha{256,512}sum.

Thanks,
sin
>From 6ca41790d20be6318a5c19d6209aab8df8941016 Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 22 Jul 2013 09:34:07 +0100
Subject: [PATCH] Add sha512sum(1)

---
 Makefile  |   6 ++-
 config.mk |   2 +-
 sha512.h  |  18 +++
 sha512sum.1   |   8 +++
 sha512sum.c   |  49 ++
 util/sha512.c | 163 ++
 6 files changed, 243 insertions(+), 3 deletions(-)
 create mode 100644 sha512.h
 create mode 100644 sha512sum.1
 create mode 100644 sha512sum.c
 create mode 100644 util/sha512.c

diff --git a/Makefile b/Makefile
index da4d0d0..2e31f33 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ include config.mk
 .POSIX:
 .SUFFIXES: .c .o
 
-HDR = crypt.h fs.h text.h md5.h sha1.h util.h arg.h
+HDR = crypt.h fs.h text.h md5.h sha1.h sha256.h sha512.h util.h arg.h
 LIB = \
util/afgets.o\
util/agetcwd.o   \
@@ -21,7 +21,8 @@ LIB = \
util/recurse.o   \
util/rm.o\
util/sha1.o  \
-   util/sha256.o
+   util/sha256.o\
+   util/sha512.o
 
 SRC = \
basename.c \
@@ -82,6 +83,7 @@ SRC = \
seq.c  \
sha1sum.c  \
sha256sum.c\
+   sha512sum.c\
wc.c   \
who.c  \
yes.c
diff --git a/config.mk b/config.mk
index 5054e8d..a3ffc4d 100644
--- a/config.mk
+++ b/config.mk
@@ -9,7 +9,7 @@ MANPREFIX = $(PREFIX)/share/man
 #CC = musl-gcc
 LD = $(CC)
 CPPFLAGS = -D_BSD_SOURCE -D_GNU_SOURCE
-CFLAGS   = -g -ansi -Wall -pedantic $(CPPFLAGS)
+CFLAGS   = -g -ansi -Wall -Wno-long-long -pedantic $(CPPFLAGS)
 LDFLAGS  = -g
 
 #CC = tcc
diff --git a/sha512.h b/sha512.h
new file mode 100644
index 000..c761712
--- /dev/null
+++ b/sha512.h
@@ -0,0 +1,18 @@
+/* public domain sha512 implementation based on fips180-3 */
+
+struct sha512 {
+   uint64_t len; /* processed message length */
+   uint64_t h[8];/* hash state */
+   uint8_t buf[128]; /* message block buffer */
+};
+
+enum { SHA512_DIGEST_LENGTH = 64 };
+
+/* reset state */
+void sha512_init(void *ctx);
+/* process message */
+void sha512_update(void *ctx, const void *m, unsigned long len);
+/* get message digest */
+/* state is ruined after sum, keep a copy if multiple sum is needed */
+/* part of the message might be left in s, zero it if secrecy is needed */
+void sha512_sum(void *ctx, uint8_t md[SHA512_DIGEST_LENGTH]);
diff --git a/sha512sum.1 b/sha512sum.1
new file mode 100644
index 000..50cdd7e
--- /dev/null
+++ b/sha512sum.1
@@ -0,0 +1,8 @@
+.TH SHA512SUM 1 sbase\-VERSION
+.SH NAME
+sha512sum \- compute SHA512 message digest
+.SH SYNOPSIS
+.B sha512sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SHA512 (512-bit) checksums. With no file, read standard input.
diff --git a/sha512sum.c b/sha512sum.c
new file mode 100644
index 000..972f2c7
--- /dev/null
+++ b/sha512sum.c
@@ -0,0 +1,49 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include "util.h"
+#include "crypt.h"
+#include "sha512.h"
+
+struct sha512 s;
+struct crypt_ops sha512_ops = {
+   sha512_init,
+   sha512_update,
+   sha512_sum,
+   &s,
+};
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   FILE *fp;
+   uint8_t md[SHA512_DIGEST_LENGTH];
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   cryptsum(&sha512_ops, stdin, "", md);
+   mdprint(md, "", sizeof(md));
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fp = fopen(*argv, "r")) == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&sha512_ops, fp, *argv, md);
+   mdprint(md, *argv, sizeof(md));
+   fclose(fp);
+   argv++;
+   }
+   }
+
+   return 0;
+}
diff --git a/util/sha512.c b/util/sha512.c
new file mode 100644
index 000..dac3118
--- /dev/null
+++ b/util/sha512.c
@@ -0,0 +1,163 @@
+/* public domain sha256 implementation based on fips180-3 */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "../sha512.h"
+
+static uint64_t ror(uint64_t n, int k) { return (n >> k) | (n << (64-k)); }
+#define Ch(x,y,z)  (z ^ (x & (y ^ z)))
+#define Maj(x,y,z) ((x & y) | (z & (x | y)))
+#define S0(x)  (ror(x,28) ^ ror(x,34) ^ ror(x,39))
+#define S1(x)  (ror(x,14) ^ ror(x

[dev] [sbase] [patch] Add df(1)

2013-07-22 Thread sin
Hi all,

This is an initial bare bones df(1) implementation.
I'm looking for some feedback, I have not put too much thought
into the code yet (too busy atm).

No manpage yet.

Thanks,
sin
>From a82f46681f68edafec898cd82b2ca64941440c11 Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 22 Jul 2013 13:17:32 +0100
Subject: [PATCH] Add df(1)

---
 Makefile |   1 +
 df.c | 120 +++
 2 files changed, 121 insertions(+)
 create mode 100644 df.c

diff --git a/Makefile b/Makefile
index 2e31f33..bb981ce 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,7 @@ SRC = \
comm.c \
cp.c   \
date.c \
+   df.c   \
dirname.c  \
echo.c \
env.c  \
diff --git a/df.c b/df.c
new file mode 100644
index 000..86c58f3
--- /dev/null
+++ b/df.c
@@ -0,0 +1,120 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+
+struct mnt_entry {
+   char *fsname;
+   char *dir;
+   char *type;
+   struct stat stat;
+   struct statvfs statvfs;
+};
+
+struct mnt_tbl {
+   struct mnt_entry *entry;
+   size_t siz;
+};
+
+static void mnt_show(struct mnt_entry *entry);
+
+enum {
+   BLKSIZ = 1024
+};
+
+struct mnt_tbl *
+init_mnt_tbl(void)
+{
+   struct mnt_tbl *mnt_tbl;
+   struct mnt_entry *entry;
+   FILE *fp;
+   struct mntent *me;
+   size_t siz = 0;
+
+   fp = setmntent("/proc/mounts", "r");
+   if (!fp)
+   eprintf("fopen /proc/mounts:");
+
+   mnt_tbl = malloc(sizeof(*mnt_tbl));
+   if (!mnt_tbl)
+   eprintf("malloc:");
+   mnt_tbl->entry = NULL;
+   mnt_tbl->siz = 0;
+
+   while ((me = getmntent(fp))) {
+   mnt_tbl->entry = realloc(mnt_tbl->entry,
+sizeof(*entry) * (siz + 1));
+   if (!mnt_tbl->entry)
+   eprintf("realloc");
+   entry = &mnt_tbl->entry[siz];
+   entry->fsname = strdup(me->mnt_fsname);
+   entry->dir = strdup(me->mnt_dir);
+   entry->type = strdup(me->mnt_type);
+   stat(me->mnt_dir, &entry->stat);
+   statvfs(me->mnt_dir, &entry->statvfs);
+   siz++;
+   }
+   mnt_tbl->siz = siz;
+
+   endmntent(fp);
+   return mnt_tbl;
+}
+
+int
+main(void)
+{
+   struct mnt_entry *entry;
+   struct mnt_tbl *mnt_tbl;
+   size_t i;
+
+   mnt_tbl = init_mnt_tbl();
+   printf("%-20s %-14s %s %s %s %s\n", "Filesystem", "1K-blocks",
+  "Used", "Available", "Use%", "Mounted on");
+   for (i = 0; i < mnt_tbl->siz; i++) {
+   entry = &mnt_tbl->entry[i];
+   if (!entry->stat.st_dev)
+   continue;
+   mnt_show(entry);
+   }
+   return 0;
+}
+
+static void
+mnt_show(struct mnt_entry *entry)
+{
+   long long size, used, avail;
+   long long percent = 0, fsbsize;
+
+   if (!entry->statvfs.f_blocks)
+   return;
+
+   if (entry->statvfs.f_bsize)
+   fsbsize = entry->statvfs.f_bsize;
+   else
+   fsbsize = 1;
+
+   size = (fsbsize * entry->statvfs.f_blocks) / BLKSIZ;
+   used = (fsbsize * (entry->statvfs.f_blocks - entry->statvfs.f_bfree));
+   used /= BLKSIZ;
+   avail = fsbsize;
+   if (getuid())
+   avail *= entry->statvfs.f_bavail;
+   else
+   avail *= entry->statvfs.f_bfree;
+   avail /= BLKSIZ;
+
+   if (used + avail) {
+   percent = (used * 100) / (used + avail);
+   if (used * 100 != percent * (used + avail))
+   percent++;
+   }
+
+   printf("%-20s %9lld ", entry->fsname, size);
+   printf("%9lld ", used);
+   printf("%9lld %3lld%% %s\n",
+  avail, percent, entry->dir);
+}
-- 
1.8.3.3



Re: [dev] [sbase] [patch] Add df(1)

2013-07-22 Thread sin
On Mon, Jul 22, 2013 at 03:26:09PM +0300, sin wrote:
> Hi all,
> 
> This is an initial bare bones df(1) implementation.
> I'm looking for some feedback, I have not put too much thought
> into the code yet (too busy atm).
> 
> No manpage yet.

I should also mention, that this naturally breaks on *BSD.
If we do care about that, we should think of a proper way to
handle this.

Thanks,
sin



[dev] [sbase] [patch v2] Add df(1)

2013-07-23 Thread sin
Cut it down to half the number of lines.
Defaults to 512 byte blocks.
>From 422efb6c391acc9daeb3500cbc5cb1811999a154 Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 22 Jul 2013 13:17:32 +0100
Subject: [PATCH] Add df(1)

---
 Makefile |  1 +
 df.c | 65 
 2 files changed, 66 insertions(+)
 create mode 100644 df.c

diff --git a/Makefile b/Makefile
index 2e31f33..bb981ce 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,7 @@ SRC = \
comm.c \
cp.c   \
date.c \
+   df.c   \
dirname.c  \
echo.c \
env.c  \
diff --git a/df.c b/df.c
new file mode 100644
index 000..d78df72
--- /dev/null
+++ b/df.c
@@ -0,0 +1,65 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+
+static void mnt_show(const char *fsname, const char *dir,
+const char *type);
+
+int
+main(void)
+{
+   FILE *fp;
+   struct mntent *me;
+
+   fp = setmntent("/proc/mounts", "r");
+   if (!fp)
+   eprintf("fopen /proc/mounts:");
+
+   printf("Filesystem  512-blocks  Used Avail Capacity  Mounted 
on\n");
+   while ((me = getmntent(fp)))
+   mnt_show(me->mnt_fsname, me->mnt_dir,
+me->mnt_type);
+   endmntent(fp);
+   return 0;
+}
+
+static void
+mnt_show(const char *fsname, const char *dir,
+const char *type)
+{
+   struct statvfs svfs;
+   struct stat s;
+   unsigned long long total, used, avail;
+   int capacity;
+   int bs;
+
+   stat(dir, &s);
+   statvfs(dir, &svfs);
+
+   if (!s.st_dev || !svfs.f_blocks)
+   return;
+
+   if (svfs.f_bsize)
+   bs = svfs.f_bsize;
+   else
+   bs = 1;
+
+   total = svfs.f_blocks * bs / 512;
+   avail = svfs.f_bfree * bs / 512;
+   used = total - avail;
+
+   if (used + avail) {
+   capacity = (used * 100) / (used + avail);
+   if (used * 100 != capacity * (used + avail))
+   capacity++;
+   }
+
+   printf("%-12s %9llu %9llu %9llu %5d%%%s\n",
+  fsname, total, used, avail, capacity,
+  dir);
+}
-- 
1.8.3.3



[dev] [sbase] [patch v3] Add df(1)

2013-07-23 Thread sin
Clean up the code a bit.  Behave like -a by default.
>From 01cbe6a93df9d71e0fe184405895763d4498ac7f Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 22 Jul 2013 13:17:32 +0100
Subject: [PATCH] Add df(1)

---
 Makefile |  1 +
 df.c | 53 +
 2 files changed, 54 insertions(+)
 create mode 100644 df.c

diff --git a/Makefile b/Makefile
index 2e31f33..bb981ce 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,7 @@ SRC = \
comm.c \
cp.c   \
date.c \
+   df.c   \
dirname.c  \
echo.c \
env.c  \
diff --git a/df.c b/df.c
new file mode 100644
index 000..2b4cebf
--- /dev/null
+++ b/df.c
@@ -0,0 +1,53 @@
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+
+static void mnt_show(const char *fsname, const char *dir,
+const char *type);
+
+int
+main(void)
+{
+   FILE *fp;
+   struct mntent *me;
+
+   fp = setmntent("/proc/mounts", "r");
+   if (!fp)
+   eprintf("fopen /proc/mounts:");
+
+   printf("Filesystem  512-blocks  Used Avail Capacity  Mounted 
on\n");
+   while ((me = getmntent(fp)))
+   mnt_show(me->mnt_fsname, me->mnt_dir,
+me->mnt_type);
+   endmntent(fp);
+   return 0;
+}
+
+static void
+mnt_show(const char *fsname, const char *dir,
+const char *type)
+{
+   struct statvfs s;
+   unsigned long long total, used, avail;
+   int capacity = 0;
+   int bs;
+
+   statvfs(dir, &s);
+
+   bs = s.f_bsize ? s.f_bsize : 1;
+   total = s.f_blocks * bs / 512;
+   avail = s.f_bfree * bs / 512;
+   used = total - avail;
+
+   if (used + avail) {
+   capacity = (used * 100) / (used + avail);
+   if (used * 100 != capacity * (used + avail))
+   capacity++;
+   }
+
+   printf("%-12s %9llu %9llu %9llu %7d%%  %s\n",
+  fsname, total, used, avail, capacity,
+  dir);
+}
-- 
1.8.3.3



Re: [dev] [sbase] [patch v3] Add df(1)

2013-07-23 Thread sin
On Tue, Jul 23, 2013 at 10:18:01AM -0400, Steve Dee wrote:
> Nice.
> 
> BSD doesn't have mntent or /proc, but does have getmntinfo. I think we
> can create a common interface that requires a minimal amount of
> conditional compilation (though obviously no conditional compilation
> is better than any conditional compilation), but I don't have time to
> do this for the next few days.

Yeah I chose to do df(1) exactly because it will be incompatible
with BSD.  I want to find a sensible solution so we can have it
working on BSD as well.

David, the df(1) patch depends on my sha512sum(1) patch.  The latter
introduces -Wno-long-long.  I should have probably sent a separate
patch for that.  Let me know if that is the case and I will send one.

Thanks,
sin



[dev] [sbase] [patch v4] Add df(1)

2013-07-26 Thread sin
Hi,

Incorporated Steve's changes as well to make it compile/work
on OpenBSD.  I realize #ifdef's are terrible but for now it should
do the trick until we come up with a proper solution to this.

Thanks,
sin
>From 34e65ec1b392a90f7d46c90c2d67a25dd6219a4d Mon Sep 17 00:00:00 2001
From: sin 
Date: Mon, 22 Jul 2013 13:17:32 +0100
Subject: [PATCH] Add df(1)

---
 Makefile |  1 +
 df.c | 90 
 2 files changed, 91 insertions(+)
 create mode 100644 df.c

diff --git a/Makefile b/Makefile
index 2e31f33..bb981ce 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,7 @@ SRC = \
comm.c \
cp.c   \
date.c \
+   df.c   \
dirname.c  \
echo.c \
env.c  \
diff --git a/df.c b/df.c
new file mode 100644
index 000..75be614
--- /dev/null
+++ b/df.c
@@ -0,0 +1,90 @@
+#ifdef __OpenBSD__
+#include 
+#include 
+#else
+#include 
+#endif
+
+#include 
+#include 
+#include 
+#include "util.h"
+
+static void mnt_show(const char *fsname, const char *dir);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+#ifdef __OpenBSD__
+   struct statfs *mntbuf;
+   int len, i;
+#else
+   FILE *fp;
+   struct mntent *me;
+#endif
+
+   ARGBEGIN {
+   case 'a':
+   break;
+   case 's':
+   case 'h':
+   case 'i':
+   eprintf("not implemented\n");
+   default:
+   usage();
+   } ARGEND;
+
+   printf("Filesystem  512-blocks  Used Avail Capacity  Mounted 
on\n");
+
+#ifdef __OpenBSD__
+   len = getmntinfo(&mntbuf, MNT_WAIT);
+   if (len == 0)
+   eprintf("gemntinfo:");
+
+   for (i = 0; i < len; i++)
+   mnt_show(mntbuf[i].f_mntfromname,
+mntbuf[i].f_mntonname);
+#else
+   fp = setmntent("/proc/mounts", "r");
+   if (!fp)
+   eprintf("fopen /proc/mounts:");
+
+   while ((me = getmntent(fp)))
+   mnt_show(me->mnt_fsname, me->mnt_dir);
+
+   endmntent(fp);
+#endif
+   return 0;
+}
+
+static void
+mnt_show(const char *fsname, const char *dir)
+{
+   struct statvfs s;
+   unsigned long long total, used, avail;
+   int capacity = 0;
+   int bs;
+
+   statvfs(dir, &s);
+
+   bs = s.f_frsize / 512;
+   total = s.f_blocks * bs;
+   avail = s.f_bfree * bs;
+   used = total - avail;
+
+   if (used + avail) {
+   capacity = (used * 100) / (used + avail);
+   if (used * 100 != capacity * (used + avail))
+   capacity++;
+   }
+
+   printf("%-12s %9llu %9llu %9llu %7d%%  %s\n",
+  fsname, total, used, avail, capacity,
+  dir);
+}
-- 
1.8.3.4



Re: [dev] [sbase] [patch v4] Add df(1)

2013-07-26 Thread sin
On Fri, Jul 26, 2013 at 12:14:08PM +0100, Nick wrote:
> On Fri, Jul 26, 2013 at 01:51:01PM +0300, sin wrote:
> > Incorporated Steve's changes as well to make it compile/work
> > on OpenBSD.  I realize #ifdef's are terrible but for now it should
> > do the trick until we come up with a proper solution to this.
> 
> Is there really no sensible way to get the required information for
> most POSIXish systems? How does coreutils do it (I expect tons of
> way more awful ifdefs than you've done here, but it's worth
> checking)?

No idea, I have not looked at the coreutils code.  Will check it out.

> I wonder what the right approach for utilities that can't be made
> portable should be? Are there any others that are likely to have
> these problems?

If the build system can switch between between say Linux ops and
OpenBSD ops then we could have something like os/openbsd.c and
os/linux.c (for things that are not portable between them).

Then the df.c code can just call a function that will be implemented
by both OSes (with the same signature etc.).

The number of tools that might require this glue code is less than
2% of the overall number of tools.  So maybe leaving the #ifdef's for now
is enough until we encounter other situations as well?

Thanks,
sin



Re: [dev] [sbase] [patch v4] Add df(1)

2013-07-27 Thread sin
On Sat, Jul 27, 2013 at 11:59:02AM +0200, Markus Wichmann wrote:
> On Fri, Jul 26, 2013 at 10:54:43PM +0300, sin wrote:
> > On Fri, Jul 26, 2013 at 12:14:08PM +0100, Nick wrote:
> > > On Fri, Jul 26, 2013 at 01:51:01PM +0300, sin wrote:
> > > > Incorporated Steve's changes as well to make it compile/work
> > > > on OpenBSD.  I realize #ifdef's are terrible but for now it should
> > > > do the trick until we come up with a proper solution to this.
> > > 
> > > Is there really no sensible way to get the required information for
> > > most POSIXish systems? How does coreutils do it (I expect tons of
> > > way more awful ifdefs than you've done here, but it's worth
> > > checking)?
> > 
> > No idea, I have not looked at the coreutils code.  Will check it out.
> > 
> 
> I just did. At least in the current version in Debian Squeeze it
> contains a whole lot of algorithms for different system interfaces,
> including listmntent(), getmntent() (for Linux, 4.3BSD, SunOS, HP-UX,
> Dynix and Irix), getmntinfo() (in two different versions, one for
> 4.4BSD, one for NetBSD 3.0), getmnt() (for Ultrix) and
> next_dev()/fs_stat_dev() (for BeOS). All of these are checked for
> availability, then executed and abstracted into GNU's own
> datastructures. Which, in this case, is a linked list.
> 
> Apparently this stuff can't be done portably.

We should probably make a decision on how portable we want to be.

> > > I wonder what the right approach for utilities that can't be made
> > > portable should be? Are there any others that are likely to have
> > > these problems?
> > 
> > If the build system can switch between between say Linux ops and
> > OpenBSD ops then we could have something like os/openbsd.c and
> > os/linux.c (for things that are not portable between them).
> > 
> > Then the df.c code can just call a function that will be implemented
> > by both OSes (with the same signature etc.).
> > 
> 
> That would be a good choice, but I see problems on the horizon. Like,
> what happens if stuff is actually portable between Linux and OpenBSD,
> but some third option, say, Solaris, behaves differently? File
> descriptor readiness notification comes to mind. Not that I think we'd
> ever need that.

If not all three behave the same then we should still abstract it.

> What if some stuff is portable between operating systems, but unportable
> between architectures? What if the combination of both matters? Will we
> have a sysdeps tree like glibc?

I would not worry much about that at the moment.  I can't think of anything.
Although if that really matters then we extend the above solution to include
something like an arch/ folder.

> > The number of tools that might require this glue code is less than
> > 2% of the overall number of tools.  So maybe leaving the #ifdef's for now
> > is enough until we encounter other situations as well?
> > 
> 
> I think that would be preferable. Unless we get a leaning tower of
> hostnames at some point, I would go with ifdefs in those tools as well.

David, what do you think?

Thanks,
sin



Re: [dev] [sbase] [patch v4] Add df(1)

2013-08-02 Thread sin
On Fri, Aug 02, 2013 at 12:09:10AM -0400, Galos, David wrote:
> > David, what do you think?
> Ideally, sbase should achieve portability through a lack of
> system-dependent code. I think it might soon be important to
> create a ubase for ugly, unportable stuff. ifconfig, mount
> insmod, could live there. Perhaps that would be a better
> home for df? Everything else on the TODO looks like it should
> be completely portable.

That would probably make more sense.

Thanks,
sin



Re: [dev] [sbase] [patch] Add sha512sum(1)

2013-08-02 Thread sin
On Mon, Jul 22, 2013 at 11:36:01AM +0300, sin wrote:
> Hi,
> 
> Added sha512sum(1).  No support for -c at the moment.
> I've also updated CFLAGS with -Wno-long-long.
> 
> Added sha256.h and sha512.h in HDR as well.
> 
> At this point we could have a helper function in util/
> to parse the checksums in files so we can easily support
> the -c flag in md5sum, sha1sum and sha{256,512}sum.

David, let me know if this is ok.

Thanks,
sin



  1   2   3   >