[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH] "apt moo moo moo moo" never randomly shows the April's fools cow



(I hope this is the right place.)

So I was recording an asciicast of `apt moo' <https://asciinema.org/a/84373>, when suddenly, I started to wonder if there was more to it. So, naturally, I got myself a copy of the source code and started looking around. Then I came across this code:

     // time is random enough for our purpose
     if (SuperCow > 3)
     {
        if (april.tm_sec == 1)
           SuperCow = 1 + (timenow % 4);
        else
           SuperCow = 1 + (timenow % 3);
     }

`timenow' is the current time. `april' is `timenow' as a timespec. `SuperCow' is the cow that gets selected (initially it is the number of moos on the command line). Cow number 4 is the April's fools cow.

However, if the seconds is one, then the seconds since epoch minus one is a multiple of 60. The modulo 4 of all multiples of 60 is 0, so the modulo 4 of multiples of 60 plus one is 1. Add 1 to that and you get 2. This means that the second cow always gets selected on the ~first~ *second* second of a minute (which would happen anyways, since 60 is divisible by 3 aswell). This means that you could just remove that if block.

OR, you could fix the things by using nanosecond (works on my machine™!) (maybe millisecond?) time in the modulo. That should be quite simple, since you can get a struct containing the seconds since the Epoch and nanoseconds since the last second in 1 system call. `clock_gettime', specifically. I decided to write a patch.

The patch is at <https://gist.githubusercontent.com/randomdude999/ca30378c3d254fb6ed5c61c24e268b5c/raw/apt-moo.patch>. Apply it to `apt-private/private-moo.cc', apt version 1.3~rc4 (and probably any version released after March 2014, that was the last time the file was modified). It didn't throw any extra warnings on the file when I compiled it.

(The patch also fixes `apt-get -qq moo' having 2 newlines, but 1 on the first of April (now it is 1 newline all the time))


Reply to: