xref: /original-bsd/bin/ps/fmt.c (revision 0842ddeb)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)fmt.c	8.5 (Berkeley) 04/27/95";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <vis.h>
21 
22 #include "ps.h"
23 
24 static char *cmdpart __P((char *));
25 static char *shquote __P((char **));
26 
27 /*
28  * XXX
29  * This is a stub until marc does the real one.
30  */
31 static char *
32 shquote(argv)
33 	char **argv;
34 {
35 	char **p, *dst, *src;
36 	static char buf[4096];		/* XXX */
37 
38 	if (*argv == 0) {
39 		buf[0] = 0;
40 		return (buf);
41 	}
42 	dst = buf;
43 	for (p = argv; (src = *p++) != 0; ) {
44 		if (*src == 0)
45 			continue;
46 		strvis(dst, src, VIS_NL | VIS_CSTYLE);
47 		while (*dst)
48 			dst++;
49 		*dst++ = ' ';
50 	}
51 	if (dst != buf)
52 		--dst;
53 	*dst = '\0';
54 	return (buf);
55 }
56 
57 static char *
58 cmdpart(arg0)
59 	char *arg0;
60 {
61 	char *cp;
62 
63 	return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
64 }
65 
66 char *
67 fmt_argv(argv, cmd, maxlen)
68 	char **argv;
69 	char *cmd;
70 	int maxlen;
71 {
72 	int len;
73 	char *ap, *cp;
74 
75 	if (argv == 0 || argv[0] == 0) {
76 		if (cmd == NULL)
77 			return ("");
78 		ap = NULL;
79 		len = maxlen + 3;
80 	} else {
81 		ap = shquote(argv);
82 		len = strlen(ap) + maxlen + 4;
83 	}
84 	if ((cp = malloc(len)) == NULL)
85 		return (NULL);
86 	if (ap == NULL)
87 		sprintf(cp, "(%.*s)", maxlen, cmd);
88 	else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0)
89 		sprintf(cp, "%s (%.*s)", ap, maxlen, cmd);
90 	else
91 		(void) strcpy(cp, ap);
92 	return (cp);
93 }
94