Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Aug 2010 12:50:54 GMT
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 182076 for review
Message-ID:  <201008081250.o78CosSk002531@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@182076?ac=10

Change 182076 by trasz@trasz_victim on 2010/08/08 12:50:33

	Use expand_number(3) in hrl(8).

Affected files ...

.. //depot/projects/soc2009/trasz_limits/TODO#26 edit
.. //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/Makefile#6 edit
.. //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#25 edit

Differences ...

==== //depot/projects/soc2009/trasz_limits/TODO#26 (text+ko) ====

@@ -82,5 +82,3 @@
 
  - Should priv(9) checking be done by HRL, or by the callers?
 
- - Use expand_number(3) in hrl(8).
-

==== //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/Makefile#6 (text+ko) ====

@@ -3,6 +3,9 @@
 PROG=   hrl
 MAN=	hrl.8
 
+DPADD=	${LIBUTIL}
+LDADD=	-lutil
+
 WARNS?=	6
 
 .include <bsd.prog.mk>

==== //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#25 (text+ko) ====

@@ -29,11 +29,13 @@
 
 #include <sys/types.h>
 #include <sys/hrl.h>
+#include <assert.h>
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <getopt.h>
 #include <grp.h>
+#include <libutil.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -129,6 +131,48 @@
 }
 
 /*
+ * This routine replaces "human-readable" number with its expanded form.
+ */
+static char *
+expand_amount(char *rule)
+{
+	int64_t num;
+	const char *subject, *subject_id, *resource, *action, *amount, *per;
+	char *copy, *expanded;
+
+	copy = strdup(rule);
+	if (copy == NULL)
+		err(1, "strdup");
+
+	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(copy);
+		return (rule);
+	}
+
+	assert(subject != NULL);
+	assert(subject_id != NULL);
+	assert(resource != NULL);
+	assert(action != NULL);
+
+	if (expand_number(amount, &num))
+		err(1, "expand_number");
+
+	if (per == NULL)
+		asprintf(&expanded, "%s:%s:%s:%s=%ld", subject, subject_id, resource, action, num);
+	else
+		asprintf(&expanded, "%s:%s:%s:%s=%ld/%s", subject, subject_id, resource, action, num, per);
+
+	return (expanded);
+}
+
+/*
  * Print rules, one per line,  */
 static void
 print_rules(char *rules)
@@ -330,6 +374,7 @@
 		    "at the same time");
 
 	rule = resolve_ids(rule);
+	rule = expand_amount(rule);
 
 	if (aflag) {
 		add_rule(rule);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201008081250.o78CosSk002531>