fmt.c revision 130894
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1992, 1993, 1994 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 3090143Smarkm#if 0 311556Srgrimes#ifndef lint 3236049Scharnierstatic char sccsid[] = "@(#)fmt.c 8.4 (Berkeley) 4/15/94"; 3336049Scharnier#endif 3490143Smarkm#endif 35110391Scharnier 3699110Sobrien#include <sys/cdefs.h> 3799110Sobrien__FBSDID("$FreeBSD: head/bin/ps/fmt.c 130894 2004-06-22 01:59:54Z gad $"); 381556Srgrimes 3941580Sbde#include <sys/types.h> 401556Srgrimes#include <sys/time.h> 411556Srgrimes#include <sys/resource.h> 4290143Smarkm 4341580Sbde#include <err.h> 4496053Sjmallett#include <limits.h> 451556Srgrimes#include <stdio.h> 4699457Smike#include <stdint.h> 471556Srgrimes#include <stdlib.h> 481556Srgrimes#include <string.h> 4937027Sjkoshy#include <unistd.h> 501556Srgrimes#include <vis.h> 5190143Smarkm 521556Srgrimes#include "ps.h" 531556Srgrimes 5490110Simpstatic char *cmdpart(char *); 5590110Simpstatic char *shquote(char **); 561556Srgrimes 571556Srgrimesstatic char * 5890110Simpshquote(char **argv) 591556Srgrimes{ 6096053Sjmallett long arg_max; 6196053Sjmallett static size_t buf_size; 6290143Smarkm size_t len; 631556Srgrimes char **p, *dst, *src; 6437027Sjkoshy static char *buf = NULL; 651556Srgrimes 6637027Sjkoshy if (buf == NULL) { 6737027Sjkoshy if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 6837027Sjkoshy errx(1, "sysconf _SC_ARG_MAX failed"); 69114600Smarkm if (arg_max >= LONG_MAX / 4 || arg_max >= (long)(SIZE_MAX / 4)) 7096053Sjmallett errx(1, "sysconf _SC_ARG_MAX preposterously large"); 7196053Sjmallett buf_size = 4 * arg_max + 1; 7296053Sjmallett if ((buf = malloc(buf_size)) == NULL) 7396053Sjmallett errx(1, "malloc failed"); 7437027Sjkoshy } 7537027Sjkoshy 7697845Sjmallett if (*argv == NULL) { 7797845Sjmallett buf[0] = '\0'; 781556Srgrimes return (buf); 791556Srgrimes } 801556Srgrimes dst = buf; 8197845Sjmallett for (p = argv; (src = *p++) != NULL; ) { 8297845Sjmallett if (*src == '\0') 831556Srgrimes continue; 8496053Sjmallett len = (buf_size - 1 - (dst - buf)) / 4; 8589575Smikeh strvisx(dst, src, strlen(src) < len ? strlen(src) : len, 8689575Smikeh VIS_NL | VIS_CSTYLE); 8797845Sjmallett while (*dst != '\0') 881556Srgrimes dst++; 8996053Sjmallett if ((buf_size - 1 - (dst - buf)) / 4 > 0) 9089575Smikeh *dst++ = ' '; 911556Srgrimes } 926971Sdg /* Chop off trailing space */ 9389575Smikeh if (dst != buf && dst[-1] == ' ') 946971Sdg dst--; 951556Srgrimes *dst = '\0'; 961556Srgrimes return (buf); 971556Srgrimes} 981556Srgrimes 991556Srgrimesstatic char * 10090110Simpcmdpart(char *arg0) 1011556Srgrimes{ 1021556Srgrimes char *cp; 1031556Srgrimes 1041556Srgrimes return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0); 1051556Srgrimes} 1061556Srgrimes 10790143Smarkmconst char * 10890143Smarkmfmt_argv(char **argv, char *cmd, size_t maxlen) 1091556Srgrimes{ 11090143Smarkm size_t len; 1111556Srgrimes char *ap, *cp; 1121556Srgrimes 11390143Smarkm if (argv == NULL || argv[0] == NULL) { 1141556Srgrimes if (cmd == NULL) 1151556Srgrimes return (""); 1161556Srgrimes ap = NULL; 1171556Srgrimes len = maxlen + 3; 1181556Srgrimes } else { 1191556Srgrimes ap = shquote(argv); 1201556Srgrimes len = strlen(ap) + maxlen + 4; 1211556Srgrimes } 12290143Smarkm cp = malloc(len); 12390143Smarkm if (cp == NULL) 12497847Sjmallett errx(1, "malloc failed"); 1251556Srgrimes if (ap == NULL) 126130894Sgad sprintf(cp, "[%.*s]", (int)maxlen, cmd); 1271556Srgrimes else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0) 12890143Smarkm sprintf(cp, "%s (%.*s)", ap, (int)maxlen, cmd); 1291556Srgrimes else 1301556Srgrimes (void) strcpy(cp, ap); 1311556Srgrimes return (cp); 1321556Srgrimes} 133