public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: Karel Zak <kzak@redhat.com>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH 4/6] renice: avoid having same lines of code twice
Date: Tue, 16 Sep 2014 21:13:12 +0100 (BST)	[thread overview]
Message-ID: <alpine.LNX.2.03.1409162106330.3712@kerolasa-home> (raw)
In-Reply-To: <20140912073504.GQ21325@x2.net.home>

On Fri, 12 Sep 2014, Karel Zak wrote:

> On Sat, Sep 06, 2014 at 12:24:51PM +0100, Sami Kerola wrote:
>> +static int getprio(const int which, const int who, const char *idtype)
>> +{
>> +	int ret;
>> +
>> +	errno = 0;
>> +	ret = getpriority(which, who);
>> +	if (ret == -1 && errno) {
>> +		warn(_("failed to get priority for %d (%s)"), who, idtype);
>> +		return PRIO_MAX + 1;
>> +	}
>> +	return ret;
>> +}
>
> This is exactly the situation when you want to use "return" only for 
> function status rather than for any data.
>
> static int getprio(const int which,
>                   const int who,
>                   const char *idtype,
>                   int *prio)                       <--------!
> {
>    errno = 0;
>    *prio = getpriority(which, who);
>    if (*prio == -1 && errno) {
>        warn(_("failed to get priority for %d (%s)"), who, idtype);
>        return -errno;
>    }
>    return 0;
> }
>
>
>> -	errno = 0;
>> -	newprio = getpriority(which, who);
>> -	if (newprio == -1 && errno) {
>> -		warn(_("failed to get priority for %d (%s)"), who, idtype);
>> +	if ((newprio = getprio(which, who, idtype)) == PRIO_MAX + 1)
>>  		return 1;
>> -	}
>
>   if (getprio(which, who, idtype, &newprio) != 0)
>        return 1;
>
> .. so you don't need complicated things like PRIO_MAX + 1.

Hi Karel,

That is a great improvement. Here is updated version of the change.

-->8---
From: Sami Kerola <kerolasa@iki.fi>
Date: Fri, 5 Sep 2014 23:33:16 +0100
Subject: [PATCH 4/7] renice: avoid having same lines of code twice

Add getprio() function to avoid duplication of a simple task.

Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
  sys-utils/renice.c | 32 ++++++++++++++++----------------
  1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/sys-utils/renice.c b/sys-utils/renice.c
index 2075d40..d83fc4a 100644
--- a/sys-utils/renice.c
+++ b/sys-utils/renice.c
@@ -67,8 +67,19 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
  	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
  }

-static int
-donice(int which, int who, int prio) {
+static int getprio(const int which, const int who, const char *idtype, int *prio)
+{
+	errno = 0;
+	*prio = getpriority(which, who);
+	if (*prio == -1 && errno) {
+		warn(_("failed to get priority for %d (%s)"), who, idtype);
+		return -errno;
+	}
+	return 0;
+}
+
+static int donice(int which, int who, int prio)
+{
  	int oldprio, newprio;
  	const char *idtype = _("process ID");

@@ -76,24 +87,14 @@ donice(int which, int who, int prio) {
  		idtype = _("user ID");
  	else if (which == PRIO_PGRP)
  		idtype = _("process group ID");
-
-	errno = 0;
-	oldprio = getpriority(which, who);
-	if (oldprio == -1 && errno) {
-		warn(_("failed to get priority for %d (%s)"), who, idtype);
+	if (getprio(which, who, idtype, &oldprio) != 0)
  		return 1;
-	}
  	if (setpriority(which, who, prio) < 0) {
  		warn(_("failed to set priority for %d (%s)"), who, idtype);
  		return 1;
  	}
-	errno = 0;
-	newprio = getpriority(which, who);
-	if (newprio == -1 && errno) {
-		warn(_("failed to get priority for %d (%s)"), who, idtype);
+	if (getprio(which, who, idtype, &newprio) != 0)
  		return 1;
-	}
-
  	printf(_("%d (%s) old priority %d, new priority %d\n"),
  	       who, idtype, oldprio, newprio);
  	return 0;
@@ -103,8 +104,7 @@ donice(int which, int who, int prio) {
   * Change the priority (the nice value) of processes
   * or groups of processes which are already running.
   */
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
  {
  	int which = PRIO_PROCESS;
  	int who = 0, prio, errs = 0;
-- 
2.1.0

  reply	other threads:[~2014-09-16 20:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-06 11:24 [PATCH 0/6] pull: renice changes Sami Kerola
2014-09-06 11:24 ` [PATCH 1/6] renice: reorder functions to avoid need of function prototype Sami Kerola
2014-09-06 11:24 ` [PATCH 2/6] rename: use usage and version print out macros Sami Kerola
2014-09-06 11:24 ` [PATCH 3/6] renice: disallow --priority <arg> without pid argument Sami Kerola
2014-09-06 11:24 ` [PATCH 4/6] renice: avoid having same lines of code twice Sami Kerola
2014-09-12  7:35   ` Karel Zak
2014-09-16 20:13     ` Sami Kerola [this message]
2014-09-06 11:24 ` [PATCH 5/6] renice: fix numeric uid argument parsing Sami Kerola
2014-09-12  8:03   ` Karel Zak
2014-09-16 20:17     ` Sami Kerola
2014-09-06 11:24 ` [PATCH 6/6] rename: add getpriority() message lookup table Sami Kerola
2014-09-16 20:21 ` [PATCH 0/6] pull: renice changes Sami Kerola
2014-09-17  7:57   ` Benno Schulenberg
2014-09-17 10:06     ` Sami Kerola
2014-09-22 12:48 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.03.1409162106330.3712@kerolasa-home \
    --to=kerolasa@iki.fi \
    --cc=kzak@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox