fmt.c revision 114600
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 * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes */
331556Srgrimes
3450471Speter#if 0
351556Srgrimes#ifndef lint
361556Srgrimesstatic char sccsid[] = "@(#)fmt.c	8.4 (Berkeley) 4/15/94";
371556Srgrimes#endif
381556Srgrimes#endif
391556Srgrimes
401556Srgrimes#include <sys/cdefs.h>
411556Srgrimes__FBSDID("$FreeBSD: head/bin/ps/fmt.c 114600 2003-05-03 20:52:48Z markm $");
421556Srgrimes
431556Srgrimes#include <sys/types.h>
4446684Skris#include <sys/time.h>
451556Srgrimes#include <sys/resource.h>
461556Srgrimes
471556Srgrimes#include <err.h>
481556Srgrimes#include <limits.h>
491556Srgrimes#include <stdio.h>
5046684Skris#include <stdint.h>
511556Srgrimes#include <stdlib.h>
521556Srgrimes#include <string.h>
531556Srgrimes#include <unistd.h>
541556Srgrimes#include <vis.h>
551556Srgrimes
561556Srgrimes#include "ps.h"
571556Srgrimes
581556Srgrimesstatic char *cmdpart(char *);
591556Srgrimesstatic char *shquote(char **);
601556Srgrimes
611556Srgrimesstatic char *
621556Srgrimesshquote(char **argv)
631556Srgrimes{
641556Srgrimes	long arg_max;
651556Srgrimes	static size_t buf_size;
661556Srgrimes	size_t len;
671556Srgrimes	char **p, *dst, *src;
681556Srgrimes	static char *buf = NULL;
691556Srgrimes
701556Srgrimes	if (buf == NULL) {
711556Srgrimes		if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
721556Srgrimes			errx(1, "sysconf _SC_ARG_MAX failed");
731556Srgrimes		if (arg_max >= LONG_MAX / 4 || arg_max >= (long)(SIZE_MAX / 4))
741556Srgrimes			errx(1, "sysconf _SC_ARG_MAX preposterously large");
751556Srgrimes		buf_size = 4 * arg_max + 1;
761556Srgrimes		if ((buf = malloc(buf_size)) == NULL)
771556Srgrimes			errx(1, "malloc failed");
781556Srgrimes	}
791556Srgrimes
801556Srgrimes	if (*argv == NULL) {
811556Srgrimes		buf[0] = '\0';
821556Srgrimes		return (buf);
831556Srgrimes	}
841556Srgrimes	dst = buf;
851556Srgrimes	for (p = argv; (src = *p++) != NULL; ) {
861556Srgrimes		if (*src == '\0')
871556Srgrimes			continue;
881556Srgrimes		len = (buf_size - 1 - (dst - buf)) / 4;
891556Srgrimes		strvisx(dst, src, strlen(src) < len ? strlen(src) : len,
901556Srgrimes		    VIS_NL | VIS_CSTYLE);
911556Srgrimes		while (*dst != '\0')
921556Srgrimes			dst++;
931556Srgrimes		if ((buf_size - 1 - (dst - buf)) / 4 > 0)
941556Srgrimes			*dst++ = ' ';
951556Srgrimes	}
961556Srgrimes	/* Chop off trailing space */
971556Srgrimes	if (dst != buf && dst[-1] == ' ')
981556Srgrimes		dst--;
99203613Simp	*dst = '\0';
1001556Srgrimes	return (buf);
1011556Srgrimes}
1021556Srgrimes
1031556Srgrimesstatic char *
1041556Srgrimescmdpart(char *arg0)
1051556Srgrimes{
1061556Srgrimes	char *cp;
1071556Srgrimes
1081556Srgrimes	return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
1091556Srgrimes}
1101556Srgrimes
1111556Srgrimesconst char *
1121556Srgrimesfmt_argv(char **argv, char *cmd, size_t maxlen)
1131556Srgrimes{
1141556Srgrimes	size_t len;
1151556Srgrimes	char *ap, *cp;
1161556Srgrimes
1171556Srgrimes	if (argv == NULL || argv[0] == NULL) {
1181556Srgrimes		if (cmd == NULL)
1191556Srgrimes			return ("");
1201556Srgrimes		ap = NULL;
1211556Srgrimes		len = maxlen + 3;
1221556Srgrimes	} else {
1231556Srgrimes		ap = shquote(argv);
1241556Srgrimes		len = strlen(ap) + maxlen + 4;
1251556Srgrimes	}
1261556Srgrimes	cp = malloc(len);
1271556Srgrimes	if (cp == NULL)
1281556Srgrimes		errx(1, "malloc failed");
1291556Srgrimes	if (ap == NULL)
1301556Srgrimes		sprintf(cp, " (%.*s)", (int)maxlen, cmd);
1311556Srgrimes	else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0)
1321556Srgrimes		sprintf(cp, "%s (%.*s)", ap, (int)maxlen, cmd);
1331556Srgrimes	else
1341556Srgrimes		(void) strcpy(cp, ap);
1351556Srgrimes	return (cp);
1361556Srgrimes}
1371556Srgrimes