The difference between const&constexpr objects in C++

In C++, const object means once initialized, its value can’t be changed. The initialization can occur in compile-time:

const auto c = 0;

Or in run-time:

#include <cstdlib>
#include <ctime>

auto func()
{
    return std::rand() % 10;
}

int main()
{
    std::srand(std::time(0));
    const auto c = func();

    return 0;
}

For constexpr objects, you can think they are a subset of const objects, and the constexpr object’s value must be determined in compile-time. Change the declaration of c in above code:

constexpr auto c = func();

The code can’t be compiled because the c‘s value couldn’t be generated during compilation phrase.

The anatomy of uptime&w commands on OpenBSD

On OpenBSD, uptime and w are actually the same program:

$ ls -lt /usr/bin/uptime /usr/bin/w
-r-xr-xr-x  2 root  bin  18136 May 30 12:53 /usr/bin/uptime
-r-xr-xr-x  2 root  bin  18136 May 30 12:53 /usr/bin/w

and the source code is usr.bin/w/w.c.

Compare the outputs of uptime and w:

$ uptime
10:59AM  up 7 days,  1:51, 1 user, load averages: 0.00, 0.00, 0.00
$ w
10:59AM  up 7 days,  1:51, 1 user, load averages: 0.00, 0.00, 0.00
USER    TTY FROM              LOGIN@  IDLE WHAT
root     p0 10.217.242.57     9:10AM     0 w

You can see the uptime just displays the first line of w, and w also shows the login users’ information.

w uses clock_gettime to get system up time:

if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) {
    ......
} 

and getloadavg to retrieve system load average int the past 1, 5, and 15 minutes:

int
getloadavg(double loadavg[], int nelem)
{
    ......
    mib[0] = CTL_VM;
    mib[1] = VM_LOADAVG;
    size = sizeof(loadinfo);
    if (sysctl(mib, 2, &loadinfo, &size, NULL, 0) < 0)
        return (-1);
    ......
}

The current user login information is kept in /var/run/utmp, and it is composed of utmp struct:

struct utmp {
    char    ut_line[UT_LINESIZE];
    char    ut_name[UT_NAMESIZE];
    char    ut_host[UT_HOSTSIZE];
    time_t  ut_time;
};

utmp.ut_line is the login terminal (remove “tty” prefix); utmp.ut_name is the login user name; utmp.ut_host is the login address and the utmp.ut_timeis the login time. These are the first 4 columns of every line:

USER    TTY FROM              LOGIN@  IDLE WHAT
root     p0 10.217.242.57     9:10AM     0 w

The IDLE column displays how long has passed since you last operates on terminal:

if ((ep->idle = now - stp->st_atime) < 0)
        ep->idle = 0;

and WHAT shows the current process.

Boolean in newLISP

In newLISP, true represents Boolean true:

> (if true (println "yes") (println "no"))
yes
"yes"

while nil represents Boolean false:

> (if nil (println "yes") (println "no"))
no
"no"

Like lua, 0 is evaluated true:

> (if 0 (println "yes") (println "no"))
yes
"yes"

empty list is also treated as false:

> (if '() (println "yes") (println "no"))
no
"no"

Check the following result:

> (if false (println "yes") (println "no"))
no
"no"

The reason is false is not defined here and its value is nil:

> false
nil

You can assign true to false and do some crazy thing:

> (let (false true) (if false (println "yes") (println "no")))
yes
"yes"

 

Fix “==> ERROR: options array contains unknown option ‘!upx'” on ArchLinux

You may bump into following error when installing packages from AUR:

$ makepkg -si
==> ERROR: options array contains unknown option '!upx'

One workaround is open the PKGBUILD file, and spot the line which contains options:

......
options=('staticlibs' 'libtool' '!upx')
......

Remove the !upx, then you can build it now.

Reference:
dropbox: unrecognized !upx in the options array.

 

Fix “error while loading shared libraries: libalpm.so.10” on ArchLinux

Today, when I tried to use yaourt to install some software, the following error happened:

......
package-query: error while loading shared libraries: libalpm.so.10: cannot open shared object file: No such file or directory

The root cause is the libalpm in system is already libalpm.so.11:

$ ls -lt /usr/lib/libalpm.so*
lrwxrwxrwx 1 root root     17 Jun  1 00:32 /usr/lib/libalpm.so -> libalpm.so.11.0.0
lrwxrwxrwx 1 root root     17 Jun  1 00:32 /usr/lib/libalpm.so.11 -> libalpm.so.11.0.0
-rwxr-xr-x 1 root root 223616 Jun  1 00:32 /usr/lib/libalpm.so.11.0.0

But the package-query is still searching libalpm.so.10:

$ ldd /usr/bin/package-query
        linux-vdso.so.1 (0x00007fffb1d8d000)
        libcurl.so.4 => /usr/lib/libcurl.so.4 (0x00007f4baee32000)
        libyajl.so.2 => /usr/lib/libyajl.so.2 (0x00007f4baec28000)
        libalpm.so.10 => not found
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f4bae86c000)
        libnghttp2.so.14 => /usr/lib/libnghttp2.so.14 (0x00007f4bae647000)
......

The solution is removing yaourt and package-query, and re-intall them:

$ sudo pacman -Rn yaourt package-query
$ sudo pacman -S fakeroot
$ git clone https://aur.archlinux.org/package-query.git
$ cd package-query
$ makepkg -si
$ git clone https://aur.archlinux.org/yaourt.git
$ cd yaourt
$ makepkg -si

That’s it!