FreeBSD/usr.bin/mkimg/mkimg.c
/*
 * A partition specification has the following format:
 *	<type> ':' <kind> <contents>
 * where:
 *	type	  the partition type alias
 *	kind	  the interpretation of the contents specification
 *		  ':'   contents holds the size of an empty partition
 *		  '='   contents holds the name of a file to read
 *		  '-'   contents holds a command to run; the output of
 *			which is the contents of the partition.
 *	contents  the specification of a partition's contents
 *
 * A specification that is a single dash indicates an unused partition
 * entry.
 */


A specification that is a single dash indicates an unused partition
entry.
A specification that is a single dash indicates an unused partition entry .
static int
parse_part(const char *spec)
{
	struct part *part;
	char *sep;
	size_t len;
	int error;
	if (strcmp(spec, "-") == 0) {
		nparts++;
		return (0);
	}
	part = calloc(1, sizeof(struct part));
	if (part == NULL)
		return (ENOMEM);
	sep = strchr(spec, ':');
	if (sep == NULL) {
		error = EINVAL;
		goto errout;
	}
	len = sep - spec + 1;
	if (len < 2) {
		error = EINVAL;
		goto errout;
	}
	part->alias = malloc(len);
	if (part->alias == NULL) {
		error = ENOMEM;
		goto errout;
	}
	strlcpy(part->alias, spec, len);
	spec = sep + 1;
	switch (*spec) {
	case ':':
		part->kind = PART_KIND_SIZE;
		break;
	case '=':
		part->kind = PART_KIND_FILE;
		break;
	case '-':
		part->kind = PART_KIND_PIPE;
		break;
	default:
		error = EINVAL;
		goto errout;
	}
	spec++;
	part->contents = strdup(spec);
	if (part->contents == NULL) {
		error = ENOMEM;
		goto errout;
	}
	spec = part->alias;
	sep = strchr(spec, '/');
	if (sep != NULL) {
		*sep++ = '\0';
		if (strlen(part->alias) == 0 || strlen(sep) == 0) {
			error = EINVAL;
			goto errout;
		}
		part->label = strdup(sep);
		if (part->label == NULL) {
			error = ENOMEM;
			goto errout;
		}
	}
	part->index = nparts;
	STAILQ_INSERT_TAIL(&partlist, part, link);
	nparts++;
	return (0);
 errout:
	if (part->alias != NULL)
		free(part->alias);
	free(part);
	return (error);
}
static int parse_part ( const char * spec ) { struct part * part ; char * sep ; size_t len ; int error ; if ( strcmp ( spec , " " ) == 0 ) { nparts ++ ; return ( 0 ) ; } part = calloc ( 1 , sizeof ( struct part ) ) ; if ( part == NULL ) return ( ENOMEM ) ; sep = strchr ( spec , ' ' ) ; if ( sep == NULL ) { error = EINVAL ; goto errout ; } len = sep - spec + 1 ; if ( len < 2 ) { error = EINVAL ; goto errout ; } part -> alias = malloc ( len ) ; if ( part -> alias == NULL ) { error = ENOMEM ; goto errout ; } strlcpy ( part -> alias , spec , len ) ; spec = sep + 1 ; switch ( * spec ) { case ' ' : part -> kind = PART_KIND_SIZE ; break ; case ' ' : part -> kind = PART_KIND_FILE ; break ; case ' ' : part -> kind = PART_KIND_PIPE ; break ; default : error = EINVAL ; goto errout ; } spec ++ ; part -> contents = strdup ( spec ) ; if ( part -> contents == NULL ) { error = ENOMEM ; goto errout ; } spec = part -> alias ; sep = strchr ( spec , ' ' ) ; if ( sep != NULL ) { * sep ++ = ' \0 ' ; if ( strlen ( part -> alias ) == 0 || strlen ( sep ) == 0 ) { error = EINVAL ; goto errout ; } part -> label = strdup ( sep ) ; if ( part -> label == NULL ) { error = ENOMEM ; goto errout ; } } part -> index = nparts ; STAILQ_INSERT_TAIL ( & partlist , part , link ) ; nparts ++ ; return ( 0 ) ; errout : if ( part -> alias != NULL ) free ( part -> alias ) ; free ( part ) ; return ( error ) ; }
A partition specification has the following format:
      <type> ':' <kind> <contents>
 where:
      type      the partition type alias
      kind      the interpretation of the contents specification
                ':'   contents holds the size of an empty partition
                '='   contents holds the name of a file to read
                '-'   contents holds a command to run; the output of
                      which is the contents of the partition.
A partition specification has the following format : <type > ' : ' <kind > <contents > where : type the partition type alias kind the interpretation of the contents specification ' : ' contents holds the size of an empty partition ' = ' contents holds the name of a file to read ' - ' contents holds a command to run ; the output of which is the contents of the partition .