util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Use C99 idioms in kill.c
@ 2012-10-18 15:52 Ayan George
  2012-10-18 17:32 ` Karel Zak
  2012-10-18 18:18 ` Dmitry V. Levin
  0 siblings, 2 replies; 5+ messages in thread
From: Ayan George @ 2012-10-18 15:52 UTC (permalink / raw)
  To: util-linux

* Moved some declarations closer to where the vairable is used.
* Declared variables used only in loops within the loop itself.
---
 misc-utils/kill.c |   45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index 6abfd24..2b88852 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -157,10 +157,7 @@ static union sigval sigdata;
 
 int main (int argc, char *argv[])
 {
-    int errors, numsig, pid;
-    char *ep, *arg, *p;
-    int do_pid, do_kill, check_all;
-    int *pids, *ip;
+    char *arg, *p;
 
     progname = argv[0];
     if ((p = strrchr(progname, '/')) != NULL)
@@ -171,10 +168,10 @@ int main (int argc, char *argv[])
     textdomain(PACKAGE);
     atexit(close_stdout);
 
-    numsig = SIGTERM;
-    do_pid = (! strcmp (progname, "pid")); 	/* Yecch */
-    do_kill = 0;
-    check_all = 0;
+    int numsig = SIGTERM,
+    	do_pid = (! strcmp (progname, "pid")), 	/* Yecch */
+    	do_kill = 0;
+    	check_all = 0;
 
     /*  loop through the arguments.
 	actually, -a is the only option can be used with other options.
@@ -274,19 +271,21 @@ int main (int argc, char *argv[])
     /*  we're done with the options.
 	the rest of the arguments should be process ids and names.
 	kill them.  */
+    int errors;
     for (errors = 0; (arg = *argv) != NULL; argv++) {
-	pid = strtol (arg, &ep, 10);
+      	char *ep = NULL;
+	int pid = strtol (arg, &ep, 10);
 	if (! *ep)
 	    errors += kill_verbose (arg, pid, numsig);
 	else {
-	    pids = get_pids (arg, check_all);
+	    int *pids = get_pids (arg, check_all);
 	    if (! pids) {
 		errors++;
 		fprintf (stderr, _("%s: can't find process \"%s\"\n"),
 			 progname, arg);
 		continue;
 	    }
-	    for (ip = pids; *ip >= 0; ip++)
+	    for (int *ip = pids; *ip >= 0; ip++)
 		errors += kill_verbose (arg, *ip, numsig);
 	    free (pids);
 	}
@@ -297,8 +296,7 @@ int main (int argc, char *argv[])
 #ifdef SIGRTMIN
 static int rtsig_to_signum(char *sig)
 {
-	int num, maxi = 0;
-	char *ep = NULL;
+	int maxi = 0;
 
 	if (strncasecmp(sig, "min+", 4) == 0)
 		sig += 4;
@@ -311,7 +309,8 @@ static int rtsig_to_signum(char *sig)
 		return -1;
 
 	errno = 0;
-	num = strtol(sig, &ep, 10);
+	char *ep = NULL;
+	int num = strtol(sig, &ep, 10);
 	if (!ep || sig == ep || errno || num < 0)
 		return -1;
 
@@ -326,8 +325,6 @@ static int rtsig_to_signum(char *sig)
 
 static int signame_to_signum (char *sig)
 {
-    size_t n;
-
     if (! strncasecmp (sig, "sig", 3))
 	sig += 3;
 
@@ -337,7 +334,7 @@ static int signame_to_signum (char *sig)
 	return rtsig_to_signum(sig + 2);
 #endif
     /* Normal sugnals */
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+    for (size_t n = 0; n < ARRAY_SIZE(sys_signame); n++) {
 	if (! strcasecmp (sys_signame[n].name, sig))
 	    return sys_signame[n].val;
     }
@@ -368,9 +365,7 @@ static void nosig (char *name)
 
 static void printsig (int sig)
 {
-    size_t n;
-
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+    for (size_t n = 0; n < ARRAY_SIZE(sys_signame); n++) {
 	if (sys_signame[n].val == sig) {
 	    printf ("%s\n", sys_signame[n].name);
 	    return;
@@ -387,10 +382,10 @@ static void printsig (int sig)
 
 static void printsignals (FILE *fp)
 {
-    size_t n, lth, lpos = 0;
+    size_t lpos = 0;
 
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
-	lth = 1+strlen(sys_signame[n].name);
+    for (size_t n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+	size_t lth = 1+strlen(sys_signame[n].name);
 	if (lpos+lth > 72) {
 	    fputc ('\n', fp);
 	    lpos = 0;
@@ -407,9 +402,7 @@ static void printsignals (FILE *fp)
 
 static int usage (int status)
 {
-    FILE *fp;
-
-    fp = (status == 0 ? stdout : stderr);
+    FILE *fp = (status == 0 ? stdout : stderr);
     fprintf (fp, _("usage: %s [ -s signal | -p ] [ -a ] pid ...\n"), progname);
     fprintf (fp, _("       %s -l [ signal ]\n"), progname);
     return status;
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] Use C99 idioms in kill.c
@ 2012-10-18 16:00 Ayan George
  0 siblings, 0 replies; 5+ messages in thread
From: Ayan George @ 2012-10-18 16:00 UTC (permalink / raw)
  To: util-linux

* Moved some declarations closer to where the vairable is used.
* Declared variables used only in loops within the loop itself.
---
 misc-utils/kill.c |   45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index 6abfd24..c0c87f2 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -157,10 +157,7 @@ static union sigval sigdata;
 
 int main (int argc, char *argv[])
 {
-    int errors, numsig, pid;
-    char *ep, *arg, *p;
-    int do_pid, do_kill, check_all;
-    int *pids, *ip;
+    char *arg, *p;
 
     progname = argv[0];
     if ((p = strrchr(progname, '/')) != NULL)
@@ -171,10 +168,10 @@ int main (int argc, char *argv[])
     textdomain(PACKAGE);
     atexit(close_stdout);
 
-    numsig = SIGTERM;
-    do_pid = (! strcmp (progname, "pid")); 	/* Yecch */
-    do_kill = 0;
-    check_all = 0;
+    int numsig = SIGTERM,
+    	do_pid = (! strcmp (progname, "pid")), 	/* Yecch */
+    	do_kill = 0,
+    	check_all = 0;
 
     /*  loop through the arguments.
 	actually, -a is the only option can be used with other options.
@@ -274,19 +271,21 @@ int main (int argc, char *argv[])
     /*  we're done with the options.
 	the rest of the arguments should be process ids and names.
 	kill them.  */
+    int errors;
     for (errors = 0; (arg = *argv) != NULL; argv++) {
-	pid = strtol (arg, &ep, 10);
+      	char *ep = NULL;
+	int pid = strtol (arg, &ep, 10);
 	if (! *ep)
 	    errors += kill_verbose (arg, pid, numsig);
 	else {
-	    pids = get_pids (arg, check_all);
+	    int *pids = get_pids (arg, check_all);
 	    if (! pids) {
 		errors++;
 		fprintf (stderr, _("%s: can't find process \"%s\"\n"),
 			 progname, arg);
 		continue;
 	    }
-	    for (ip = pids; *ip >= 0; ip++)
+	    for (int *ip = pids; *ip >= 0; ip++)
 		errors += kill_verbose (arg, *ip, numsig);
 	    free (pids);
 	}
@@ -297,8 +296,7 @@ int main (int argc, char *argv[])
 #ifdef SIGRTMIN
 static int rtsig_to_signum(char *sig)
 {
-	int num, maxi = 0;
-	char *ep = NULL;
+	int maxi = 0;
 
 	if (strncasecmp(sig, "min+", 4) == 0)
 		sig += 4;
@@ -311,7 +309,8 @@ static int rtsig_to_signum(char *sig)
 		return -1;
 
 	errno = 0;
-	num = strtol(sig, &ep, 10);
+	char *ep = NULL;
+	int num = strtol(sig, &ep, 10);
 	if (!ep || sig == ep || errno || num < 0)
 		return -1;
 
@@ -326,8 +325,6 @@ static int rtsig_to_signum(char *sig)
 
 static int signame_to_signum (char *sig)
 {
-    size_t n;
-
     if (! strncasecmp (sig, "sig", 3))
 	sig += 3;
 
@@ -337,7 +334,7 @@ static int signame_to_signum (char *sig)
 	return rtsig_to_signum(sig + 2);
 #endif
     /* Normal sugnals */
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+    for (size_t n = 0; n < ARRAY_SIZE(sys_signame); n++) {
 	if (! strcasecmp (sys_signame[n].name, sig))
 	    return sys_signame[n].val;
     }
@@ -368,9 +365,7 @@ static void nosig (char *name)
 
 static void printsig (int sig)
 {
-    size_t n;
-
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+    for (size_t n = 0; n < ARRAY_SIZE(sys_signame); n++) {
 	if (sys_signame[n].val == sig) {
 	    printf ("%s\n", sys_signame[n].name);
 	    return;
@@ -387,10 +382,10 @@ static void printsig (int sig)
 
 static void printsignals (FILE *fp)
 {
-    size_t n, lth, lpos = 0;
+    size_t lpos = 0;
 
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
-	lth = 1+strlen(sys_signame[n].name);
+    for (size_t n = 0; n < ARRAY_SIZE(sys_signame); n++) {
+	size_t lth = 1+strlen(sys_signame[n].name);
 	if (lpos+lth > 72) {
 	    fputc ('\n', fp);
 	    lpos = 0;
@@ -407,9 +402,7 @@ static void printsignals (FILE *fp)
 
 static int usage (int status)
 {
-    FILE *fp;
-
-    fp = (status == 0 ? stdout : stderr);
+    FILE *fp = (status == 0 ? stdout : stderr);
     fprintf (fp, _("usage: %s [ -s signal | -p ] [ -a ] pid ...\n"), progname);
     fprintf (fp, _("       %s -l [ signal ]\n"), progname);
     return status;
-- 
1.7.10.4




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use C99 idioms in kill.c
  2012-10-18 15:52 [PATCH] Use C99 idioms in kill.c Ayan George
@ 2012-10-18 17:32 ` Karel Zak
  2012-10-18 18:12   ` Ayan George
  2012-10-18 18:18 ` Dmitry V. Levin
  1 sibling, 1 reply; 5+ messages in thread
From: Karel Zak @ 2012-10-18 17:32 UTC (permalink / raw)
  To: Ayan George; +Cc: util-linux

On Thu, Oct 18, 2012 at 03:52:30PM +0000, Ayan George wrote:
> * Moved some declarations closer to where the vairable is used.

 No problem if the declaration is at the begin of any block, like

  if (x) {
      int y;
      ...
  }

 everything other is MESS.

    Karel


-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use C99 idioms in kill.c
  2012-10-18 17:32 ` Karel Zak
@ 2012-10-18 18:12   ` Ayan George
  0 siblings, 0 replies; 5+ messages in thread
From: Ayan George @ 2012-10-18 18:12 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux

On 10/18/2012 01:32 PM, Karel Zak wrote:
> On Thu, Oct 18, 2012 at 03:52:30PM +0000, Ayan George wrote:
>> * Moved some declarations closer to where the vairable is used.
>
>   No problem if the declaration is at the begin of any block, like
>
>    if (x) {
>        int y;
>        ...
>    }
>
>   everything other is MESS.
>
>      Karel
>
>

A mess?  Why bother declaring a variable within a block with c99?

It always seemed to make sense to declare a variable closest to where it 
is initialized and used.

Either way -- sorry if it is unacceptable.  I'll retreat to my hole. :^)

-ayan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use C99 idioms in kill.c
  2012-10-18 15:52 [PATCH] Use C99 idioms in kill.c Ayan George
  2012-10-18 17:32 ` Karel Zak
@ 2012-10-18 18:18 ` Dmitry V. Levin
  1 sibling, 0 replies; 5+ messages in thread
From: Dmitry V. Levin @ 2012-10-18 18:18 UTC (permalink / raw)
  To: Ayan George; +Cc: util-linux

[-- Attachment #1: Type: text/plain, Size: 887 bytes --]

Hi,

On Thu, Oct 18, 2012 at 03:52:30PM +0000, Ayan George wrote:
> * Moved some declarations closer to where the vairable is used.
> * Declared variables used only in loops within the loop itself.
> ---
>  misc-utils/kill.c |   45 +++++++++++++++++++--------------------------
>  1 file changed, 19 insertions(+), 26 deletions(-)
> 
> diff --git a/misc-utils/kill.c b/misc-utils/kill.c
> index 6abfd24..2b88852 100644
> --- a/misc-utils/kill.c
> +++ b/misc-utils/kill.c
[...]
> -    numsig = SIGTERM;
> -    do_pid = (! strcmp (progname, "pid")); 	/* Yecch */
> -    do_kill = 0;
> -    check_all = 0;
> +    int numsig = SIGTERM,
> +    	do_pid = (! strcmp (progname, "pid")), 	/* Yecch */
> +    	do_kill = 0;
> +    	check_all = 0;

Please enable your .git/hooks/pre-commit to stop these whitespace errors,
no matter whether to use C99 or not.


-- 
ldv

[-- Attachment #2: Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-10-18 18:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-18 15:52 [PATCH] Use C99 idioms in kill.c Ayan George
2012-10-18 17:32 ` Karel Zak
2012-10-18 18:12   ` Ayan George
2012-10-18 18:18 ` Dmitry V. Levin
  -- strict thread matches above, loose matches on Subject: below --
2012-10-18 16:00 Ayan George

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).