Skip to content

Instantly share code, notes, and snippets.

@PypeBros
Created February 27, 2020 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PypeBros/a58243437c803183d730c7a3105e9041 to your computer and use it in GitHub Desktop.
Save PypeBros/a58243437c803183d730c7a3105e9041 to your computer and use it in GitHub Desktop.
why ? don't you trust fgets(3) manpage ?
#include <stdio.h>
#include <string.h>
int showChar(char c) {
switch (c) {
case '!'...'~':
fprintf(stderr, "'%c ", c);
break;
case 0:
fprintf(stderr, "\\0 ");
break;
case '\n':
fprintf(stderr, "\\n ");
break;
case -1:
fprintf(stderr, "?? ");
break;
default:
fprintf(stderr, "%02x ", c);
break;
}
}
int main() {
char data[16];
fprintf(stderr, "%p ", data);
for (size_t i = 0; i < sizeof(data); i++) {
fprintf(stderr, "%02lx-", i);
}
fprintf(stderr, "\n");
while (!feof(stdin)) {
memset(data, -1, sizeof(data));
char* ptr = fgets(data, sizeof(data), stdin);
fprintf(stderr, "%p ", ptr);
for (size_t i = 0; i < sizeof(data); i++) {
showChar(data[i]);
}
fprintf(stderr, "\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment