kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

14_errno_fopen.c (508B)


      1 /* errno + strerror via a deliberately failing fopen. errno's numeric
      2  * value for ENOENT differs across platforms but strerror's text contains
      3  * the canonical phrase, so we substring-match the human message. */
      4 
      5 #include <errno.h>
      6 #include <stdio.h>
      7 #include <string.h>
      8 
      9 int main(void) {
     10   FILE* f = fopen("/kit/no/such/path/at/all", "r");
     11   if (f) return 1;
     12   if (errno == 0) return 2;
     13   const char* msg = strerror(errno);
     14   if (!msg || !*msg) return 3;
     15   printf("fopen failed: %s\n", msg);
     16   return 0;
     17 }