c

Trim Null Bytes from a Sparse File

I was running a server that didn’t do log rotation, so when the log grew too big I had to truncate it. This left me with a sparse file with a bunch of 0s (null bytes) at the beginning.

To trim the null bytes, first find the offset of the first non-null byte. This can be done with some C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#define BUF_SIZE 4 * 1024
int main(int argc, char *argv[]) {
char buf[BUF_SIZE];
unsigned long long count;
int n, i, fd;
if (argc != 2) {
fprintf(stderr, "Usage: %s FILE\n", argv[0]);
exit(1);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Error opening file: %s: %s\n", argv[1], strerror(errno));
exit(1);
}
count = 0;
while ((n = read(fd, buf, BUF_SIZE)) > 0) {
for (i = 0; i < n; i++) if (buf[i] != 0) break;
count += i;
if (i != n) break;
}
if (n < 0) {
fprintf(stderr, "Error reading file: %s: %s\n", argv[1], strerror(errno));
exit(1);
}
printf("%ld\n", count);
return 0;
}

Find the offset of the first non-null byte:

1
2
3
gcc -o nulllength nulllength.c
./nulllength log.bin
7981307584

Trim the first 7981307584 bytes from the file:

1
dd if=log.bin of=trimmed.bin bs=7981307584 skip=1

Specifying the bs as offset and skipping 1 is faster than doing it the other way, because it would copy 1 byte at a time — slow!

Print Header File Includes After Preprocessing

You can use gcc -M to print all the dependencies of a C source file. For example:

1
gcc -I../my/include/path -M my_header.h

To see the resulting file after all those includes, you can cat them together:

1
gcc -I../my/include/path -M my_header.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat

Then, to print all the preprocessor #defines, excluding the predefined ones:

1
gcc -I../my/include/path -M my_header.h | tr -d '\\\n' | cut -d' ' -f2- | xargs cat | cpp -E -fpreprocessed -dM

Volatile Variables in Optimized C

Ah, forgetting the volatile type qualifier when compiling with optimization enabled:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <pthread.h>
// compile with: gcc -O3 -lpthread volatile.c -o volatile
// Fix by using volatile:
// volatile int flag = 0;
int flag = 0;
void *update() {
flag = 1;
}
int main() {
pthread_t update_thread;
pthread_create(&update_thread, NULL, update, NULL);
while (!flag) {} // This will loop forever
pthread_join(update_thread, NULL);
}

Flash Colors on Terminal Window in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
int main()
{
        int i;
        struct winsize w;
        char *spaces;
        /* Get terminal window size: */
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        /* Allocate enough spaces to fill the whole
* whole window (rows * cols):*/
        spaces = (char *)malloc(w.ws_row * w.ws_col);
        memset(spaces, ' ', w.ws_row * w.ws_col);
        /* Change background color, then print
* rows * cols of white spaces: */
        for(i = 0; 1; i++) {
                printf("\e[7;%dm\n", 31 + i%6);
                write(1, spaces, w.ws_row * w.ws_col);
                usleep(1000 * 1000);
        }
        return 0;
}