Inconsistent strcmp() return value when passing strings as pointers or as literals

TL:DR: Use gcc -fno-builtin-strcmp so strcmp() isn’t treated as equivalent to __builtin_strcmp(). With optimization disabled, GCC will only be able to do constant-propagation within a single statement, not across statements. The actual library version subtracts the differing character; the compile-time eval probably normalizes the result to 1 / 0 / -1, which isn’t required or … Read more

How can I get the source code for the linux utility tail?

The tail utility is part of the coreutils on linux. Source tarball: ftp://ftp.gnu.org/gnu/coreutils/coreutils-7.4.tar.gz Source file: https://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c (original http link) I’ve always found FreeBSD to have far clearer source code than the gnu utilities. So here’s tail.c in the FreeBSD project: http://svnweb.freebsd.org/csrg/usr.bin/tail/tail.c?view=markup

How to get hard disk serial number using Python

Linux As you suggested, fcntl is the way to do this on Linux. The C code you want to translate looks like this: static struct hd_driveid hd; int fd; if ((fd = open(“/dev/hda”, O_RDONLY | O_NONBLOCK)) < 0) { printf(“ERROR opening /dev/hda\n”); exit(1); } if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) { printf(“%.20s\n”, hd.serial_no); } else if (errno … Read more

How do you write a C program to execute another program?

#include <stdio.h> #include <stdlib.h> #include <unistd.h> /* for fork */ #include <sys/types.h> /* for pid_t */ #include <sys/wait.h> /* for wait */ int main() { /*Spawn a child to run the program.*/ pid_t pid=fork(); if (pid==0) { /* child process */ static char *argv[]={“echo”,”Foo is my name.”,NULL}; execv(“/bin/echo”,argv); exit(127); /* only if execv fails */ … Read more