FreeBSD/sbin/ddb/ddb_script.c
/*
 * Print all scripts (scriptname==NULL) or a specific script.
 */
Print all scripts (scriptname==NULL) or a specific script.
Print all scripts ( scriptname == NULL ) or a specific script .
static void
ddb_list_scripts(const char *scriptname)
{
	char *buffer, *line, *nextline;
	char *line_script, *line_scriptname;
	size_t buflen, len;
	int ret;
repeat:
	if (sysctlbyname(SYSCTL_SCRIPTS, NULL, &buflen, NULL, 0) < 0)
		err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
	if (buflen == 0)
		return;
	buffer = malloc(buflen);
	if (buffer == NULL)
		err(EX_OSERR, "malloc");
	bzero(buffer, buflen);
	len = buflen;
	ret = sysctlbyname(SYSCTL_SCRIPTS, buffer, &len, NULL, 0);
	if (ret < 0 && errno != ENOMEM)
		err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
	if (ret < 0) {
		free(buffer);
		goto repeat;
	}
	if (scriptname == NULL) {
		printf("%s", buffer);
		free(buffer);
		return;
	}
	nextline = buffer;
	while ((line = strsep(&nextline, "\n")) != NULL) {
		line_script = line;
		line_scriptname = strsep(&line_script, "=");
		if (line_script == NULL)
			continue;
		if (strcmp(scriptname, line_scriptname) != 0)
			continue;
		printf("%s\n", line_script);
		break;
	}
	if (line == NULL) {
		errno = ENOENT;
		err(EX_DATAERR, "%s", scriptname);
	}
	free(buffer);
}
static void ddb_list_scripts ( const char * scriptname ) { char * buffer , * line , * nextline ; char * line_script , * line_scriptname ; size_t buflen , len ; int ret ; repeat : if ( sysctlbyname ( SYSCTL_SCRIPTS , NULL , & buflen , NULL , 0 ) < 0 ) err ( EX_OSERR , " " , SYSCTL_SCRIPTS ) ; if ( buflen == 0 ) return ; buffer = malloc ( buflen ) ; if ( buffer == NULL ) err ( EX_OSERR , " " ) ; bzero ( buffer , buflen ) ; len = buflen ; ret = sysctlbyname ( SYSCTL_SCRIPTS , buffer , & len , NULL , 0 ) ; if ( ret < 0 && errno != ENOMEM ) err ( EX_OSERR , " " , SYSCTL_SCRIPTS ) ; if ( ret < 0 ) { free ( buffer ) ; goto repeat ; } if ( scriptname == NULL ) { printf ( " " , buffer ) ; free ( buffer ) ; return ; } nextline = buffer ; while ( ( line = strsep ( & nextline , " \n " ) ) != NULL ) { line_script = line ; line_scriptname = strsep ( & line_script , " " ) ; if ( line_script == NULL ) continue ; if ( strcmp ( scriptname , line_scriptname ) != 0 ) continue ; printf ( " \n " , line_script ) ; break ; } if ( line == NULL ) { errno = ENOENT ; err ( EX_DATAERR , " " , scriptname ) ; } free ( buffer ) ; }
Print all scripts (scriptname==NULL) or a specific script.
Print all scripts ( scriptname == NULL ) or a specific script .
/*
	 * We nul'd the buffer before calling sysctl(), so at worst empty.
	 *
	 * If a specific script hasn't been requested, print it all.
	 */ /*
	 * If a specific script has been requested, we have to parse the
	 * string to find it.
	 */