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