diff --git a/bin/ps/ps.c b/bin/ps/ps.c index d458d46a36cf..d5417a39c884 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -129,8 +129,9 @@ main(int argc, char *argv[]) dev_t ttydev; pid_t *pids; uid_t *uids; - int all, ch, flag, i, _fmt, lineno, nentries, nocludge, dropgid; - int prtheader, wflag, what, xflg, pid, uid, npids, nuids, showthreads; + int all, ch, dropgid, flag, _fmt, i, lineno; + int nentries, nocludge, noutput, npids, nuids, pid; + int prtheader, showthreads, uid, wflag, what, xflg; char *cols; char errbuf[_POSIX2_LINE_MAX]; const char *cp, *nlistf, *memf; @@ -395,6 +396,7 @@ main(int argc, char *argv[]) /* * for each proc, call each variable output function. */ + noutput = 0; for (i = lineno = 0; i < nentries; i++) { if (xflg == 0 && ((&kinfo[i])->ki_p->ki_tdev == NODEV || ((&kinfo[i])->ki_p->ki_flag & P_CONTROLT ) == 0)) @@ -419,13 +421,21 @@ main(int argc, char *argv[]) (void)putchar(' '); } (void)putchar('\n'); + noutput++; if (prtheader && lineno++ == prtheader - 4) { (void)putchar('\n'); printheader(); lineno = 0; } } - free(uids); + if (pids != NULL) + free(pids); + if (uids != NULL) + free(uids); + + /* Check if '-p proclist' or '-u userlist' matched zero processes */ + if (noutput == 0) + eval = 1; exit(eval); }