xref: /original-bsd/bin/ps/fmt.c (revision c73f6197)
1 /*-
2  * Copyright (c) 1992, 1993
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.1 (Berkeley) 05/31/93";
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 "ps.h"
20 
21 static char *cmdpart __P((char *));
22 static char *shquote __P((char **));
23 
24 /*
25  * XXX
26  * This is a stub until marc does the real one.
27  */
28 static char *
29 shquote(argv)
30 	register char **argv;
31 {
32 	register char **p, *dst, *src;
33 	static char buf[1024];		/* XXX */
34 
35 	if (*argv == 0) {
36 		buf[0] = 0;
37 		return (buf);
38 	}
39 	dst = buf;
40 	for (p = argv; (src = *p++) != 0; ) {
41 		if (*src == 0)
42 			continue;
43 		while ((*dst++ = *src++) != 0)
44 			continue;
45 		dst[-1] = ' ';
46 	}
47 	dst[-1] = 0;
48 	return (buf);
49 }
50 
51 static char *
52 cmdpart(arg0)
53 	char *arg0;
54 {
55 	register char *cp;
56 
57 	return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
58 }
59 
60 char *
61 fmt_argv(argv, cmd, maxlen)
62 	register char **argv;
63 	register char *cmd;
64 	int maxlen;
65 {
66 	register int len;
67 	register char *ap, *cp;
68 
69 	if (argv == 0 || argv[0] == 0) {
70 		if (cmd == NULL)
71 			return ("");
72 		ap = NULL;
73 		len = maxlen + 3;
74 	} else {
75 		ap = shquote(argv);
76 		len = strlen(ap) + maxlen + 4;
77 	}
78 	if ((cp = malloc(len)) == NULL)
79 		return (NULL);
80 	if (ap == NULL)
81 		sprintf(cp, "(%.*s)", maxlen, cmd);
82 	else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0)
83 		sprintf(cp, "%s (%.*s)", ap, maxlen, cmd);
84 	else
85 		(void) strcpy(cp, ap);
86 	return (cp);
87 }
88