Skip to content

Instantly share code, notes, and snippets.

@gfx
Created April 6, 2020 02:50
Show Gist options
  • Save gfx/ccc57b72d27280d51294ce8f73564e78 to your computer and use it in GitHub Desktop.
Save gfx/ccc57b72d27280d51294ce8f73564e78 to your computer and use it in GitHub Desktop.
`fread(3)` returns 0 for success. why?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main() {
char cmdline[256];
char proc_file[256];
snprintf(proc_file, sizeof(proc_file), "/proc/%d/cmdline", getpid());
FILE *f = fopen(proc_file, "r");
if (f == NULL) {
fprintf(stderr, "Failed to open %s: %s\n", proc_file, strerror(errno));
exit(EXIT_FAILURE);
}
size_t nread = fread(cmdline, sizeof(cmdline), 1, f);
printf("nread=%lu\n", nread);
printf("cmdline=%s\n", cmdline);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment