-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_fopen.c
More file actions
43 lines (33 loc) · 882 Bytes
/
Copy pathcheck_fopen.c
File metadata and controls
43 lines (33 loc) · 882 Bytes
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
41
42
43
#pragma once
#ifndef CHECK_FOPEN_INCLUDED
#define CHECK_FOPEN_INCLUDED
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
FILE *
check_fopen( const char *path, const char *mode )
{
FILE *fp;
fp = fopen( path, mode );
if( NULL == fp ) {
char *msg;
size_t len;
len = strlen( path ) + strlen( mode ) + 40;
msg = ( char * ) malloc( len * sizeof( char ) );
if( NULL == msg ) {
fprintf( stderr, "ERROR: cannot allocate a string of %zd elements\n", len );
exit( EXIT_FAILURE );
}
snprintf( msg, len, "could not open '%s' (mode %s)", path, mode );
if( 0 == errno ) {
fprintf( stderr, "ERROR: %s\n", msg );
} else {
perror( msg );
}
free( msg );
exit( EXIT_FAILURE );
}
return fp;
}
#endif