=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/top/commands.c,v retrieving revision 1.28 retrieving revision 1.29 diff -c -r1.28 -r1.29 *** src/usr.bin/top/commands.c 2007/05/29 00:56:56 1.28 --- src/usr.bin/top/commands.c 2010/03/23 16:16:09 1.29 *************** *** 1,4 **** ! /* $OpenBSD: commands.c,v 1.28 2007/05/29 00:56:56 otto Exp $ */ /* * Top users/processes display for Unix --- 1,4 ---- ! /* $OpenBSD: commands.c,v 1.29 2010/03/23 16:16:09 lum Exp $ */ /* * Top users/processes display for Unix *************** *** 37,42 **** --- 37,43 ---- #include #include + #include #include #include #include *************** *** 53,59 **** #include "machine.h" static char *next_field(char *); ! static int scanint(char *, int *); static char *err_string(void); static size_t str_adderr(char *, size_t, int); static size_t str_addarg(char *, size_t, char *, int); --- 54,60 ---- #include "machine.h" static char *next_field(char *); ! static int scan_arg(char *, int *, char *); static char *err_string(void); static size_t str_adderr(char *, size_t, int); static size_t str_addarg(char *, size_t, char *, int); *************** *** 77,101 **** return (*str == '\0' ? NULL : str); } static int ! scanint(char *str, int *intp) { ! int val = 0; char ch; ! /* if there is nothing left of the string, flag it as an error */ ! /* This fix is dedicated to Greg Earle */ if (*str == '\0') return (-1); while ((ch = *str++) != '\0') { ! if (isdigit(ch)) ! val = val * 10 + (ch - '0'); ! else if (isspace(ch)) break; else ! return (-1); } *intp = val; return (0); } --- 78,112 ---- return (*str == '\0' ? NULL : str); } + /* + * Scan the renice or kill interactive arguments for data and/or errors. + */ static int ! scan_arg(char *str, int *intp, char *nptr) { ! int val = 0, bad_flag = 0; char ch; ! *nptr = '\0'; ! if (*str == '\0') return (-1); while ((ch = *str++) != '\0') { ! if (isspace(ch)) break; + else if (!isdigit(ch)) + bad_flag = 1; else ! val = val * 10 + (ch - '0'); ! ! *(nptr++) = ch; } + *nptr = '\0'; + + if (bad_flag == 1) + return(-1); + *intp = val; return (0); } *************** *** 123,129 **** if (errcnt >= ERRMAX) { \ return(err_toomany); \ } else { \ ! errs[errcnt].arg = (p); \ errs[errcnt++].err = (e); \ } --- 134,142 ---- if (errcnt >= ERRMAX) { \ return(err_toomany); \ } else { \ ! free(errs[errcnt].arg); \ ! if ((errs[errcnt].arg = strdup(p)) == NULL) \ ! err(1, "strdup"); \ errs[errcnt++].err = (e); \ } *************** *** 252,257 **** --- 265,271 ---- { int signum = SIGTERM, procnum; uid_t uid, puid; + char tempbuf[TEMPBUFSIZE]; char *nptr; /* reset error array */ *************** *** 270,276 **** return (" kill: no processes specified"); if (isdigit(str[1])) { ! (void) scanint(str + 1, &signum); if (signum <= 0 || signum >= NSIG) return (" invalid signal number"); } else { --- 284,290 ---- return (" kill: no processes specified"); if (isdigit(str[1])) { ! (void) scan_arg(str + 1, &signum, nptr); if (signum <= 0 || signum >= NSIG) return (" invalid signal number"); } else { *************** *** 287,305 **** /* put the new pointer in place */ str = nptr; } /* loop thru the string, killing processes */ do { ! if (scanint(str, &procnum) == -1) { ! ERROR(str, 0); } else { /* check process owner if we're not root */ puid = proc_owner(procnum); if (puid == (uid_t)(-1)) { ! ERROR(str, ESRCH); } else if (uid && (uid != puid)) { ! ERROR(str, EACCES); } else if (kill(procnum, signum) == -1) { ! ERROR(str, errno); } } } while ((str = next_field(str)) != NULL); --- 301,320 ---- /* put the new pointer in place */ str = nptr; } + nptr = tempbuf; /* loop thru the string, killing processes */ do { ! if (scan_arg(str, &procnum, nptr) == -1) { ! ERROR(nptr, 0); } else { /* check process owner if we're not root */ puid = proc_owner(procnum); if (puid == (uid_t)(-1)) { ! ERROR(nptr, ESRCH); } else if (uid && (uid != puid)) { ! ERROR(nptr, EACCES); } else if (kill(procnum, signum) == -1) { ! ERROR(nptr, errno); } } } while ((str = next_field(str)) != NULL); *************** *** 318,334 **** uid_t uid; char negate; int prio, procnum; ERR_RESET; uid = getuid(); /* allow for negative priority values */ if ((negate = (*str == '-')) != 0) { /* move past the minus sign */ str++; } /* use procnum as a temporary holding place and get the number */ ! procnum = scanint(str, &prio); /* negate if necessary */ if (negate) --- 333,357 ---- uid_t uid; char negate; int prio, procnum; + char tempbuf[TEMPBUFSIZE]; + char *nptr; ERR_RESET; uid = getuid(); + /* skip over leading white space */ + while (isspace(*str)) + str++; + /* allow for negative priority values */ if ((negate = (*str == '-')) != 0) { /* move past the minus sign */ str++; } + + nptr = tempbuf; /* use procnum as a temporary holding place and get the number */ ! procnum = scan_arg(str, &prio, nptr); /* negate if necessary */ if (negate) *************** *** 346,359 **** /* loop thru the process numbers, renicing each one */ do { ! if (scanint(str, &procnum) == -1) { ! ERROR(str, 0); } /* check process owner if we're not root */ else if (uid && (uid != proc_owner(procnum))) { ! ERROR(str, EACCES); } else if (setpriority(PRIO_PROCESS, procnum, prio) == -1) { ! ERROR(str, errno); } } while ((str = next_field(str)) != NULL); --- 369,382 ---- /* loop thru the process numbers, renicing each one */ do { ! if (scan_arg(str, &procnum, nptr) == -1) { ! ERROR(nptr, 0); } /* check process owner if we're not root */ else if (uid && (uid != proc_owner(procnum))) { ! ERROR(nptr, EACCES); } else if (setpriority(PRIO_PROCESS, procnum, prio) == -1) { ! ERROR(nptr, errno); } } while ((str = next_field(str)) != NULL);