FreeBSD/usr.bin/rctl/rctl.c
/*
 * Replace human-readable number with its expanded form.
 */
Replace human-readable number with its expanded form.
Replace human - readable number with its expanded form .
static char *
expand_amount(const char *rule, const char *unexpanded_rule)
{
	uint64_t num;
	const char *subject, *subject_id, *resource, *action, *amount, *per;
	char *copy, *expanded, *tofree;
	int ret;
	tofree = copy = strdup(rule);
	if (copy == NULL) {
		warn("strdup");
		return (NULL);
	}
	subject = strsep(&copy, ":");
	subject_id = strsep(&copy, ":");
	resource = strsep(&copy, ":");
	action = strsep(&copy, "=/");
	amount = strsep(&copy, "/");
	per = copy;
	if (amount == NULL || strlen(amount) == 0) {
		free(tofree);
		copy = strdup(rule);
		if (copy == NULL) {
			warn("strdup");
			return (NULL);
		}
		return (copy);
	}
	assert(subject != NULL);
	assert(subject_id != NULL);
	assert(resource != NULL);
	assert(action != NULL);
	if (expand_number(amount, &num)) {
		warnx("malformed rule '%s': invalid numeric value '%s'",
		    unexpanded_rule, amount);
		free(tofree);
		return (NULL);
	}
	if (per == NULL) {
		ret = asprintf(&expanded, "%s:%s:%s:%s=%ju",
		    subject, subject_id, resource, action, (uintmax_t)num);
	} else {
		ret = asprintf(&expanded, "%s:%s:%s:%s=%ju/%s",
		    subject, subject_id, resource, action, (uintmax_t)num, per);
	}
	if (ret <= 0) {
		warn("asprintf");
		free(tofree);
		return (NULL);
	}
	free(tofree);
	return (expanded);
}
static char * expand_amount ( const char * rule , const char * unexpanded_rule ) { uint64_t num ; const char * subject , * subject_id , * resource , * action , * amount , * per ; char * copy , * expanded , * tofree ; int ret ; tofree = copy = strdup ( rule ) ; if ( copy == NULL ) { warn ( " " ) ; return ( NULL ) ; } subject = strsep ( & copy , " " ) ; subject_id = strsep ( & copy , " " ) ; resource = strsep ( & copy , " " ) ; action = strsep ( & copy , " " ) ; amount = strsep ( & copy , " " ) ; per = copy ; if ( amount == NULL || strlen ( amount ) == 0 ) { free ( tofree ) ; copy = strdup ( rule ) ; if ( copy == NULL ) { warn ( " " ) ; return ( NULL ) ; } return ( copy ) ; } assert ( subject != NULL ) ; assert ( subject_id != NULL ) ; assert ( resource != NULL ) ; assert ( action != NULL ) ; if ( expand_number ( amount , & num ) ) { warnx ( " " , unexpanded_rule , amount ) ; free ( tofree ) ; return ( NULL ) ; } if ( per == NULL ) { ret = asprintf ( & expanded , " " , subject , subject_id , resource , action , ( uintmax_t ) num ) ; } else { ret = asprintf ( & expanded , " " , subject , subject_id , resource , action , ( uintmax_t ) num , per ) ; } if ( ret <= 0 ) { warn ( " " ) ; free ( tofree ) ; return ( NULL ) ; } free ( tofree ) ; return ( expanded ) ; }
Replace human-readable number with its expanded form.
Replace human - readable number with its expanded form .
/*
		 * The "copy" has already been tinkered with by strsep().
		 */