FreeBSD/usr.sbin/config/main.c
/*
 * This one is quick hack. Will be probably moved to elf(3) interface.
 * It takes kernel configuration file name, passes it as an argument to
 * elfdump -a, which output is parsed by some UNIX tools...
 */
This one is quick hack. Will be probably moved to elf(3) interface.
It takes kernel configuration file name, passes it as an argument to
elfdump -a, which output is parsed by some UNIX tools
This one is quick hack . Will be probably moved to elf ( 3 ) interface . It takes kernel configuration file name passes it as an argument to elfdump - a which output is parsed by some UNIX tools
static void
kernconfdump(const char *file)
{
	struct stat st;
	FILE *fp, *pp;
	int error, osz, r;
	unsigned int i, off, size, t1, t2, align;
	char *cmd, *o;
	r = open(file, O_RDONLY);
	if (r == -1)
		err(EXIT_FAILURE, "Couldn't open file '%s'", file);
	error = fstat(r, &st);
	if (error == -1)
		err(EXIT_FAILURE, "fstat() failed");
	if (S_ISDIR(st.st_mode))
		errx(EXIT_FAILURE, "'%s' is a directory", file);
	fp = fdopen(r, "r");
	if (fp == NULL)
		err(EXIT_FAILURE, "fdopen() failed");
	osz = 1024;
	o = calloc(1, osz);
	if (o == NULL)
		err(EXIT_FAILURE, "Couldn't allocate memory");
	asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
	    "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
	if (cmd == NULL)
		errx(EXIT_FAILURE, "asprintf() failed");
	pp = popen(cmd, "r");
	if (pp == NULL)
		errx(EXIT_FAILURE, "popen() failed");
	free(cmd);
	(void)fread(o, osz, 1, pp);
	pclose(pp);
	r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
	free(o);
	if (r != 5)
		errx(EXIT_FAILURE, "File %s doesn't contain configuration "
		    "file. Either unsupported, or not compiled with "
		    "INCLUDE_CONFIG_FILE", file);
	r = fseek(fp, off, SEEK_CUR);
	if (r != 0)
		err(EXIT_FAILURE, "fseek() failed");
	for (i = 0; i < size; i++) {
		r = fgetc(fp);
		if (r == EOF)
			break;
		if (r == '\0') {
			assert(i == size - 1 &&
			    ("\\0 found in the middle of a file"));
			break;
		}
		fputc(r, stdout);
	}
	fclose(fp);
}
static void kernconfdump ( const char * file ) { struct stat st ; FILE * fp , * pp ; int error , osz , r ; unsigned int i , off , size , t1 , t2 , align ; char * cmd , * o ; r = open ( file , O_RDONLY ) ; if ( r == -1 ) err ( EXIT_FAILURE , " " , file ) ; error = fstat ( r , & st ) ; if ( error == -1 ) err ( EXIT_FAILURE , " " ) ; if ( S_ISDIR ( st . st_mode ) ) errx ( EXIT_FAILURE , " " , file ) ; fp = fdopen ( r , " " ) ; if ( fp == NULL ) err ( EXIT_FAILURE , " " ) ; osz = 1024 ; o = calloc ( 1 , osz ) ; if ( o == NULL ) err ( EXIT_FAILURE , " " ) ; asprintf ( & cmd , " " " " , file ) ; if ( cmd == NULL ) errx ( EXIT_FAILURE , " " ) ; pp = popen ( cmd , " " ) ; if ( pp == NULL ) errx ( EXIT_FAILURE , " " ) ; free ( cmd ) ; ( void ) fread ( o , osz , 1 , pp ) ; pclose ( pp ) ; r = sscanf ( o , " " , & off , & size , & t1 , & t2 , & align ) ; free ( o ) ; if ( r != 5 ) errx ( EXIT_FAILURE , " " " " " " , file ) ; r = fseek ( fp , off , SEEK_CUR ) ; if ( r != 0 ) err ( EXIT_FAILURE , " " ) ; for ( i = 0 ; i < size ; i ++ ) { r = fgetc ( fp ) ; if ( r == EOF ) break ; if ( r == ' \0 ' ) { assert ( i == size - 1 && ( " \\ " ) ) ; break ; } fputc ( r , stdout ) ; } fclose ( fp ) ; }
This one is quick hack.
This one is quick hack .
/* ELF note section header. */