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