From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id BDDAC3870892; Thu, 1 Feb 2024 16:20:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDDAC3870892 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1706804435; bh=kZZrlZmCxnQjQdCDnhtHPtGCa1666RFXxVjOSwjggBY=; h=From:To:Subject:Date:From; b=Cl6Cjz6P0ki6gL3VMuw5a54zNIY9ef831t/srzuLe9l/+M0YDFljNCp0xgI1/i35g 993ygXmAwnsxgcWCFkz8NNQzlpeicVTcX9EfdJJE3NFVT6It+jeBgqcKto51qIpGZt E+jXNGgkYhyv05VNfJxR5iPhGbyVZb4XQA2TPpbU= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: ps: avoid long lines on tty X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 6d0769efa9df92696d159e87e761ad899870ba6c X-Git-Newrev: c414e1b97252789d2ab46c1b8fbe502a9ffca8b8 Message-Id: <20240201162035.BDDAC3870892@sourceware.org> Date: Thu, 1 Feb 2024 16:20:35 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dc414e1b9725= 2789d2ab46c1b8fbe502a9ffca8b8 commit c414e1b97252789d2ab46c1b8fbe502a9ffca8b8 Author: Corinna Vinschen AuthorDate: Thu Feb 1 17:20:17 2024 +0100 Commit: Corinna Vinschen CommitDate: Thu Feb 1 17:20:17 2024 +0100 Cygwin: ps: avoid long lines on tty =20 If stdout is a tty, restrict line output to tty width. =20 Also, rename a weird buffer name. Diff: --- winsup/utils/ps.cc | 77 ++++++++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/winsup/utils/ps.cc b/winsup/utils/ps.cc index 2f7a72710a3a..b51608317c18 100644 --- a/winsup/utils/ps.cc +++ b/winsup/utils/ps.cc @@ -18,6 +18,7 @@ details. */ #include #include #include +#include #include #include #include @@ -26,6 +27,8 @@ details. */ for that value. Note that PATH_MAX is only 4K. */ #define NT_MAX_PATH 32767 =20 +#define OUTPUT_BUFSIZ 65536 + static char *prog_name; =20 static struct option longopts[] =3D @@ -146,23 +149,34 @@ struct =20 char pname[NT_MAX_PATH + sizeof (" ") + 1]; =20 +char output_buffer[OUTPUT_BUFSIZ]; + +void +ps_print (const char *string, int width) +{ + printf ("%.*s\n", width, string); +} + int main (int argc, char *argv[]) { external_pinfo *p; - int aflag, lflag, fflag, sflag, proc_id; + int aflag, lflag, fflag, sflag, proc_id, width, col; uid_t uid; bool found_proc_id =3D true; cygwin_getinfo_types query =3D CW_GETPINFO; - const char *dtitle =3D " PID TTY STIME COMMAND\n"; - const char *dfmt =3D "%7d%4s%10s %s\n"; - const char *ftitle =3D " UID PID PPID TTY STIME COMMA= ND\n"; - const char *ffmt =3D "%8.8s%8d%8d%4s%10s %s\n"; - const char *ltitle =3D " PID PPID PGID WINPID TTY = UID STIME COMMAND\n"; - const char *lfmt =3D "%c %7d %7d %7d %10u %4s %8u %8s %s\n"; + const char *stitle =3D " PID TTY STIME COMMAND"; + const char *sfmt =3D "%7d%4s%10s %s"; + const char *ftitle =3D " UID PID PPID TTY STIME COMMA= ND"; + const char *ffmt =3D "%8.8s%8d%8d%4s%10s %s"; + const char *ltitle =3D " PID PPID PGID WINPID TTY = UID STIME COMMAND"; + const char *lfmt =3D "%c %7d %7d %7d %10u %4s %8u %8s %s"; char ch; void *drive_map =3D NULL; time_t boot_time =3D -1; + char *columns, *end; + struct winsize ws; + =20 aflag =3D lflag =3D fflag =3D sflag =3D 0; uid =3D getuid (); @@ -231,13 +245,6 @@ main (int argc, char *argv[]) exit (1); } =20 - if (sflag) - fputs (dtitle, stdout); - else if (fflag) - fputs (ftitle, stdout); - else if (lflag) - fputs (ltitle, stdout); - (void) cygwin_internal (CW_LOCK_PINFO, 1000); =20 if (query =3D=3D CW_GETPINFO_FULL) @@ -275,6 +282,26 @@ main (int argc, char *argv[]) boot_time =3D to_time_t ((FILETIME*)&stodi.BootTime); } =20 + width =3D OUTPUT_BUFSIZ; + if ((columns =3D getenv ("COLUMNS")) && *columns + && (col =3D strtoul (columns, &end, 0)) > 0 && !*end) + width =3D col; + else if (isatty (STDOUT_FILENO)) + { + width =3D 80; + if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) !=3D -1) + width =3D ws.ws_col; + } + if (width > OUTPUT_BUFSIZ) + width =3D OUTPUT_BUFSIZ; + + if (sflag) + ps_print (stitle, width); + else if (fflag) + ps_print (ftitle, width); + else if (lflag) + ps_print (ltitle, width); + for (int pid =3D 0; (p =3D (external_pinfo *) cygwin_internal (query, pid | CW_NEXTPID)= ); pid =3D p->pid) @@ -388,19 +415,25 @@ main (int argc, char *argv[]) } =20 if (sflag) - printf (dfmt, p->pid, ttynam (p->ctty, ttyname), start_time (p), pname); + { + snprintf (output_buffer, sizeof output_buffer, sfmt, + p->pid, ttynam (p->ctty, ttyname), start_time (p), pname); + } else if (fflag) { - printf (ffmt, uname, p->pid, p->ppid, ttynam (p->ctty, ttyname), - start_time (p), cmdline ?: pname); + snprintf (output_buffer, sizeof output_buffer, ffmt, + uname, p->pid, p->ppid, ttynam (p->ctty, ttyname), + start_time (p), cmdline ?: pname); free (cmdline); } else if (lflag) - printf (lfmt, status, p->pid, p->ppid, p->pgid, - p->dwProcessId, ttynam (p->ctty, ttyname), - p->version >=3D EXTERNAL_PINFO_VERSION_32_BIT ? p->uid32 : p->uid, - start_time (p), pname); - + snprintf (output_buffer, sizeof output_buffer, lfmt, + status, p->pid, p->ppid, p->pgid, + p->dwProcessId, ttynam (p->ctty, ttyname), + p->version >=3D EXTERNAL_PINFO_VERSION_32_BIT + ? p->uid32 : p->uid, + start_time (p), pname); + ps_print (output_buffer, width); } if (drive_map) cygwin_internal (CW_FREE_DRIVE_MAP, drive_map);