Created
April 6, 2020 02:50
-
-
Save gfx/ccc57b72d27280d51294ce8f73564e78 to your computer and use it in GitHub Desktop.
`fread(3)` returns 0 for success. why?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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