public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 04/15] cytune: use single loop for setting and getting ioctl() calls
Date: Sun,  4 May 2014 16:49:45 +0100	[thread overview]
Message-ID: <1399218596-21321-5-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1399218596-21321-1-git-send-email-kerolasa@iki.fi>

This commit also fixes functional bug.  Documentation gives an idea one
might set current, and default values in one go but as a matter of fact
that did not work earlier.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 sys-utils/cytune.c | 93 +++++++++++++++++++++++-------------------------------
 1 file changed, 39 insertions(+), 54 deletions(-)

diff --git a/sys-utils/cytune.c b/sys-utils/cytune.c
index 63f6980..995a75f 100644
--- a/sys-utils/cytune.c
+++ b/sys-utils/cytune.c
@@ -386,65 +386,50 @@ int main(int argc, char **argv)
 	/* For signal routine. */
 	global_optind = optind;
 
-	if (set_threshold || set_threshold_def) {
-		for (i = optind; i < argc; i++) {
-			file = open(argv[i], O_RDONLY);
-			if (file == -1)
-				err(EXIT_FAILURE, _("cannot open %s"), argv[i]);
-			if (ioctl(file,
-				  set_threshold ? CYSETTHRESH : CYSETDEFTHRESH,
-				  set_threshold ? threshold_val : threshold_def_val))
-				err(EXIT_FAILURE,
-				    _("cannot set %s to threshold %d"), argv[i],
-				    set_threshold ? threshold_val : threshold_def_val);
-			close(file);
-		}
-	}
-	if (set_timeout || set_timeout_def) {
-		for (i = optind; i < argc; i++) {
-			file = open(argv[i], O_RDONLY);
-			if (file == -1)
-				err(EXIT_FAILURE, _("cannot open %s"), argv[i]);
-			if (ioctl(file,
-				  set_timeout ? CYSETTIMEOUT : CYSETDEFTIMEOUT,
-				  set_timeout ? timeout_val : timeout_def_val))
-				err(EXIT_FAILURE,
-				    _("cannot set %s to time threshold %d"),
-				    argv[i],
-				    set_timeout ? timeout_val : timeout_def_val);
-			close(file);
+	for (i = optind; i < argc; i++) {
+		file = open(argv[i], O_RDONLY);
+		if (file == -1)
+			err(EXIT_FAILURE, _("cannot open %s"), argv[i]);
+
+		if (set_threshold)
+			if (ioctl(file, CYSETTHRESH, threshold_val))
+				err(EXIT_FAILURE, _("cannot set %s to threshold %d"), argv[i], threshold_val);
+
+		if (set_threshold_def)
+			if (ioctl(file, CYSETDEFTHRESH, threshold_def_val))
+				err(EXIT_FAILURE, _("cannot set %s to threshold %d"), argv[i], threshold_def_val);
+
+		if (set_timeout)
+			if (ioctl(file, CYSETTIMEOUT, timeout_val))
+				err(EXIT_FAILURE, _("cannot set %s to time threshold %d"), argv[i], timeout_val);
+
+		if (set_timeout_def)
+			if (ioctl(file, CYSETDEFTIMEOUT, timeout_def_val))
+				err(EXIT_FAILURE, _("cannot set %s to time threshold %d"), argv[i], timeout_def_val);
+
+		if (get_defaults) {
+			if (ioctl(file, CYGETDEFTHRESH, &threshold_value))
+				err(EXIT_FAILURE, _("cannot get threshold for %s"), argv[i]);
+			if (ioctl(file, CYGETDEFTIMEOUT, &timeout_value))
+				err(EXIT_FAILURE, _("cannot get timeout for %s"), argv[i]);
+			printf(_("%s: %ld default threshold and %ld default timeout\n"),
+			       argv[i], threshold_value, timeout_value);
 		}
-	}
 
-	if (get_defaults || get_current) {
-		for (i = optind; i < argc; i++) {
-			file = open(argv[i], O_RDONLY);
-			if (file == -1)
-				err(EXIT_FAILURE, _("cannot open %s"), argv[i]);
-			if (ioctl
-			    (file, get_defaults ? CYGETTHRESH : CYGETDEFTHRESH,
-			     &threshold_value))
-				err(EXIT_FAILURE,
-				    _("cannot get threshold for %s"), argv[i]);
-			if (ioctl
-			    (file, get_current ? CYGETTIMEOUT : CYGETDEFTIMEOUT,
-			     &timeout_value))
-				err(EXIT_FAILURE,
-				    _("cannot get timeout for %s"), argv[i]);
-			close(file);
-			if (get_defaults)
-				printf(_("%s: %ld current threshold and %ld current timeout\n"),
-					argv[i], threshold_value, timeout_value);
-			else
-				printf(_("%s: %ld default threshold and %ld default timeout\n"),
-					argv[i], threshold_value, timeout_value);
+		if (get_current) {
+			if (ioctl(file, CYGETTHRESH, &threshold_value))
+				err(EXIT_FAILURE, _("cannot get threshold for %s"), argv[i]);
+			if (ioctl(file, CYGETTIMEOUT, &timeout_value))
+				err(EXIT_FAILURE, _("cannot get timeout for %s"), argv[i]);
+			printf(_("%s: %ld current threshold and %ld current timeout\n"),
+			       argv[i], threshold_value, timeout_value);
 		}
-	}
 
-	if (!query)
-		return EXIT_SUCCESS;
+		close(file);
+	}
 
-	query_tty_stats(argc, argv, interval, argc - optind, &threshold_value, &timeout_value);
+	if (query)
+		query_tty_stats(argc, argv, interval, argc - optind, &threshold_value, &timeout_value);
 
 	return EXIT_SUCCESS;
 }
-- 
1.9.2


  parent reply	other threads:[~2014-05-04 15:50 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-04 15:49 [PATCH 00/15] cytune modernization Sami Kerola
2014-05-04 15:49 ` [PATCH 01/15] cytune: rename threshold and timeout variables Sami Kerola
2014-05-05  8:34   ` Benno Schulenberg
2014-05-04 15:49 ` [PATCH 02/15] cytune: remove unnecessary variables Sami Kerola
2014-05-05  8:29   ` Benno Schulenberg
2014-05-04 15:49 ` [PATCH 03/15] cytune: be consistent with interval data type Sami Kerola
2014-05-04 15:49 ` Sami Kerola [this message]
2014-05-05  8:24   ` [PATCH 04/15] cytune: use single loop for setting and getting ioctl() calls Benno Schulenberg
2014-05-04 15:49 ` [PATCH 05/15] cytune: kernel still does not have send_count in cyclades_monitor structure Sami Kerola
2014-05-04 15:49 ` [PATCH 06/15] cytune: prefer sigaction(), and remove unnecessary abstractions Sami Kerola
2014-05-04 15:49 ` [PATCH 07/15] cytune: add cyg_get_mon() to avoid duplicate code Sami Kerola
2014-05-04 15:49 ` [PATCH 08/15] cytune: add structure to hold run time configuration Sami Kerola
2014-05-04 15:49 ` [PATCH 09/15] cytune: pull signal handling and statistic priting apart Sami Kerola
2014-05-04 15:49 ` [PATCH 10/15] cytune: remove unnecessary type casts Sami Kerola
2014-05-04 15:49 ` [PATCH 11/15] cytune: deprecate undescriptive options Sami Kerola
2014-05-04 15:49 ` [PATCH 12/15] cytune: add filename to struct cyclades_control Sami Kerola
2014-05-04 15:49 ` [PATCH 13/15] cytune: add noreturn function attributes Sami Kerola
2014-05-04 15:49 ` [PATCH 14/15] cytune: use matching type in struct cyclades_control with kernel Sami Kerola
2014-05-04 15:49 ` [PATCH 15/15] cytune: update copyright Sami Kerola
2014-05-07  7:10 ` [PATCH 00/15] cytune modernization Karel Zak
2014-05-07  8:12   ` Sami Kerola

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=1399218596-21321-5-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --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