fmt.c revision 37027
1161651Skan/*- 2161651Skan * Copyright (c) 1992, 1993, 1994 3161651Skan * The Regents of the University of California. All rights reserved. 4161651Skan * 5161651Skan * Redistribution and use in source and binary forms, with or without 6161651Skan * modification, are permitted provided that the following conditions 7161651Skan * are met: 8161651Skan * 1. Redistributions of source code must retain the above copyright 9161651Skan * notice, this list of conditions and the following disclaimer. 10161651Skan * 2. Redistributions in binary form must reproduce the above copyright 11161651Skan * notice, this list of conditions and the following disclaimer in the 12161651Skan * documentation and/or other materials provided with the distribution. 13161651Skan * 3. All advertising materials mentioning features or use of this software 14161651Skan * must display the following acknowledgement: 15161651Skan * This product includes software developed by the University of 16161651Skan * California, Berkeley and its contributors. 17161651Skan * 4. Neither the name of the University nor the names of its contributors 18161651Skan * may be used to endorse or promote products derived from this software 19161651Skan * without specific prior written permission. 20161651Skan * 21161651Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22161651Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23161651Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24161651Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25161651Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26161651Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27161651Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28161651Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29161651Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30161651Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31161651Skan * SUCH DAMAGE. 32161651Skan */ 33161651Skan 34161651Skan#ifndef lint 35161651Skan#if 0 36161651Skanstatic char sccsid[] = "@(#)fmt.c 8.4 (Berkeley) 4/15/94"; 37161651Skan#endif 38161651Skanstatic const char rcsid[] = 39161651Skan "$Id: fmt.c,v 1.11 1998/05/15 06:29:15 charnier Exp $"; 40161651Skan#endif /* not lint */ 41161651Skan 42161651Skan#include <sys/param.h> 43161651Skan#include <sys/syslimits.h> 44161651Skan#include <sys/time.h> 45161651Skan#include <sys/resource.h> 46161651Skan#include <stdio.h> 47161651Skan#include <stdlib.h> 48161651Skan#include <string.h> 49161651Skan#include <unistd.h> 50161651Skan#include <vis.h> 51161651Skan#include "ps.h" 52161651Skan 53161651Skanstatic char *cmdpart __P((char *)); 54161651Skanstatic char *shquote __P((char **)); 55161651Skan 56161651Skan/* 57161651Skan * XXX 58161651Skan * This is a stub until marc does the real one. 59161651Skan */ 60161651Skanstatic char * 61161651Skanshquote(argv) 62161651Skan char **argv; 63161651Skan{ 64161651Skan long arg_max; 65161651Skan char **p, *dst, *src; 66161651Skan static char *buf = NULL; 67161651Skan 68161651Skan if (buf == NULL) { 69161651Skan if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 70161651Skan errx(1, "sysconf _SC_ARG_MAX failed"); 71161651Skan if ((buf = malloc((4 * arg_max) + 1)) == NULL) 72161651Skan errx(1, "malloc failed"); 73161651Skan } 74161651Skan 75161651Skan if (*argv == 0) { 76161651Skan buf[0] = 0; 77161651Skan return (buf); 78161651Skan } 79161651Skan dst = buf; 80 for (p = argv; (src = *p++) != 0; ) { 81 if (*src == 0) 82 continue; 83 strvis(dst, src, VIS_NL | VIS_CSTYLE); 84 while (*dst) 85 dst++; 86 *dst++ = ' '; 87 } 88 /* Chop off trailing space */ 89 if (dst != buf) 90 dst--; 91 *dst = '\0'; 92 return (buf); 93} 94 95static char * 96cmdpart(arg0) 97 char *arg0; 98{ 99 char *cp; 100 101 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0); 102} 103 104char * 105fmt_argv(argv, cmd, maxlen) 106 char **argv; 107 char *cmd; 108 int maxlen; 109{ 110 int len; 111 char *ap, *cp; 112 113 if (argv == 0 || argv[0] == 0) { 114 if (cmd == NULL) 115 return (""); 116 ap = NULL; 117 len = maxlen + 3; 118 } else { 119 ap = shquote(argv); 120 len = strlen(ap) + maxlen + 4; 121 } 122 if ((cp = malloc(len)) == NULL) 123 return (NULL); 124 if (ap == NULL) 125 sprintf(cp, " (%.*s)", maxlen, cmd); 126 else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0) 127 sprintf(cp, "%s (%.*s)", ap, maxlen, cmd); 128 else 129 (void) strcpy(cp, ap); 130 return (cp); 131} 132