On Wednesday 17 June 2009 22:36:57 Rob Landley wrote:
> I needed more functionality from pgrep/pkill, so I decided to add -s and -P 
> and similar, and make a new config option for 'em.
> 
> Adding -s went just fine, but when I added a second option getopt32() 
> wouldn't 
> use it.
> 
> I.E. this does what I expect:
> 
> diff --git a/procps/pgrep.c b/procps/pgrep.c
> index 0e8e529..b63539f 100644
> --- a/procps/pgrep.c
> +++ b/procps/pgrep.c
> @@ -77,7 +77,9 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
>               }
>               first_arg_idx++;
>       }
> -     opt = getopt32(argv, "vlfxon");
> +char *temp = NULL;
> +     opt = getopt32(argv, "vlfxons:", &temp);
> +printf("temp=%s\n",temp);
>       argv[first_arg_idx] = first_arg;
>  
>       argv += optind;
> 
> Although I note it only works if I call "./busybox pgrep -s42" and not with 
> "./busybox pgrep -s 42".
> 
> This does not work at all:
> 
> diff --git a/procps/pgrep.c b/procps/pgrep.c
> index 0e8e529..38ff088 100644
> --- a/procps/pgrep.c
> +++ b/procps/pgrep.c
> @@ -77,7 +77,9 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
>               }
>               first_arg_idx++;
>       }
> -     opt = getopt32(argv, "vlfxon");
> +char *temp = NULL, *temp2 = NULL;
> +     opt = getopt32(argv, "vlfxons:P:", &temp, &temp2);
> +printf("temp=%s temp2=%s\n",temp, temp2);
>       argv[first_arg_idx] = first_arg;
>  
>       argv += optind;
> 
> Using the second patch, -P never sets temp2.
> 
> Can anyone tell me what I'm doing wrong?  I seem to have missed a curve 
> somewhere...
> 
> Rob

Hi Rob,
seems to me that this code confuses the arg parsing

        /* We must avoid interpreting -NUM (signal num) as an option */
        first_arg_idx = 1;
        while (1) {
                first_arg = argv[first_arg_idx];
                if (!first_arg)
                        break;
                /* not "-<small_letter>..."? */
                if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 
'z') {
                        argv[first_arg_idx] = NULL; /* terminate argv here */
                        break;
                }
                first_arg_idx++;
        }

Please try if this snippet does the trick for you, it is just a proof of 
concept and only little tested.

Ciao,
Tito

--- procps/pgrep_orig.c 2009-05-01 21:22:59.000000000 +0200
+++ procps/pgrep.c      2009-06-18 00:45:34.000000000 +0200
@@ -49,10 +49,11 @@
        int signo = SIGTERM;
        unsigned opt;
        int scan_mask = PSSCAN_COMM;
-       char *first_arg;
-       int first_arg_idx;
+       char *first_arg = NULL;
        int matched_pid;
        char *cmd_last;
+       char *tmp = NULL;
+       char *tmp2 =NULL;
        procps_status_t *proc;
        /* These are initialized to 0 */
        struct {
@@ -65,20 +66,14 @@
        memset(&Z, 0, sizeof(Z));

        /* We must avoid interpreting -NUM (signal num) as an option */
-       first_arg_idx = 1;
-       while (1) {
-               first_arg = argv[first_arg_idx];
-               if (!first_arg)
-                       break;
-               /* not "-<small_letter>..."? */
-               if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 
'z') {
-                       argv[first_arg_idx] = NULL; /* terminate argv here */
-                       break;
-               }
-               first_arg_idx++;
+       /* procps pkill allows -NUM only as first arg */
+       /* TODO fix pkill help text accordingly */
+       if (pkill && argv[1] && argv[1][0] == '-' && argv[1][1] && 
isdigit(argv[1][1])) {
+               first_arg = argv[1];
+               /* change '-' to ' ' so that it doesn't interfere with getopt32 
*/
+               first_arg[0] = ' ';
        }
-       opt = getopt32(argv, "vlfxon");
-       argv[first_arg_idx] = first_arg;
+       opt = getopt32(argv, "vlfxons:P:", &tmp, &tmp2);

        argv += optind;
        //argc -= optind; - unused anyway
@@ -90,7 +85,7 @@
                        print_signames();
                        return 0;
                }
-               if (first_arg && first_arg[0] == '-') {
+               if (first_arg) {
                        signo = get_signum(&first_arg[1]);
                        if (signo < 0) /* || signo > MAX_SIGNUM ? */
                                bb_error_msg_and_die("bad signal name '%s'", 
&first_arg[1]);

--- procps/pgrep_orig.c	2009-05-01 21:22:59.000000000 +0200
+++ procps/pgrep.c	2009-06-18 00:45:34.000000000 +0200
@@ -49,10 +49,11 @@
 	int signo = SIGTERM;
 	unsigned opt;
 	int scan_mask = PSSCAN_COMM;
-	char *first_arg;
-	int first_arg_idx;
+	char *first_arg = NULL;
 	int matched_pid;
 	char *cmd_last;
+	char *tmp = NULL;
+	char *tmp2 =NULL;
 	procps_status_t *proc;
 	/* These are initialized to 0 */
 	struct {
@@ -65,20 +66,14 @@
 	memset(&Z, 0, sizeof(Z));
 
 	/* We must avoid interpreting -NUM (signal num) as an option */
-	first_arg_idx = 1;
-	while (1) {
-		first_arg = argv[first_arg_idx];
-		if (!first_arg)
-			break;
-		/* not "-<small_letter>..."? */
-		if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 'z') {
-			argv[first_arg_idx] = NULL; /* terminate argv here */
-			break;
-		}
-		first_arg_idx++;
+	/* procps pkill allows -NUM only as first arg */
+	/* TODO fix pkill help text accordingly */
+	if (pkill && argv[1] && argv[1][0] == '-' && argv[1][1] && isdigit(argv[1][1])) {
+		first_arg = argv[1];
+		/* change '-' to ' ' so that it doesn't interfere with getopt32 */
+		first_arg[0] = ' ';
 	}
-	opt = getopt32(argv, "vlfxon");
-	argv[first_arg_idx] = first_arg;
+	opt = getopt32(argv, "vlfxons:P:", &tmp, &tmp2);
 
 	argv += optind;
 	//argc -= optind; - unused anyway
@@ -90,7 +85,7 @@
 			print_signames();
 			return 0;
 		}
-		if (first_arg && first_arg[0] == '-') {
+		if (first_arg) {
 			signo = get_signum(&first_arg[1]);
 			if (signo < 0) /* || signo > MAX_SIGNUM ? */
 				bb_error_msg_and_die("bad signal name '%s'", &first_arg[1]);
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to