FreeBSD/contrib/ntp/ntpd/ntp_scanner.c
/*
 * Allocate an info structure and attach it to a file.
 *
 * Note: When 'mode' is NULL, then the INFO block will be set up to
 * contain a NULL file pointer, as suited for remote config command
 * parsing. Otherwise having a NULL file pointer is considered an error,
 * and a NULL info block pointer is returned to indicate failure!
 *
 * Note: We use a variable-sized structure to hold a copy of the file
 * name (or, more proper, the input source description). This is more
 * secure than keeping a reference to some other storage that might go
 * out of scope.
 */
Allocate an info structure and attach it to a file.
Note: When 'mode' is NULL, then the INFO block will be set up to
contain a NULL file pointer, as suited for remote config command
parsing. Otherwise having a NULL file pointer is considered an error,
and a NULL info block pointer is returned to indicate failure!

We use a variable-sized structure to hold a copy of the file
name (or, more proper, the input source description). This is more
secure than keeping a reference to some other storage that might go
out of scope.
Allocate an info structure and attach it to a file . Note : When ' mode ' is NULL then the INFO block will be set up to contain a NULL file pointer as suited for remote config command parsing . Otherwise having a NULL file pointer is considered an error and a NULL info block pointer is returned to indicate failure! We use a variable - sized structure to hold a copy of the file name ( or more proper the input source description ) . This is more secure than keeping a reference to some other storage that might go out of scope .
static struct FILE_INFO *
lex_open(
	const char *path,
	const char *mode
	)
{
	struct FILE_INFO *stream;
	size_t            nnambuf;
	nnambuf = strlen(path);
	stream = emalloc_zero(sizeof(*stream) + nnambuf);
	stream->curpos.nline = 1;
	stream->backch = EOF;
	memcpy(stream->fname, path, nnambuf);
	if (NULL != mode) {
		stream->fpi = fopen(path, mode);
		if (NULL == stream->fpi) {
			free(stream);
			stream = NULL;
		}
	}
	return stream;
}
static struct FILE_INFO * lex_open ( const char * path , const char * mode ) { struct FILE_INFO * stream ; size_t nnambuf ; nnambuf = strlen ( path ) ; stream = emalloc_zero ( sizeof ( * stream ) + nnambuf ) ; stream -> curpos . nline = 1 ; stream -> backch = EOF ; memcpy ( stream -> fname , path , nnambuf ) ; if ( NULL != mode ) { stream -> fpi = fopen ( path , mode ) ; if ( NULL == stream -> fpi ) { free ( stream ) ; stream = NULL ; } } return stream ; }
Allocate an info structure and attach it to a file.
Allocate an info structure and attach it to a file .
/* copy name with memcpy -- trailing NUL already there! */