File: output.c

package info (click to toggle)
lavaps 1.9-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 692 kB
  • ctags: 698
  • sloc: ansic: 2,390; cpp: 2,089; sh: 1,993; tcl: 542; makefile: 229; perl: 182
file content (53 lines) | stat: -rw-r--r-- 1,499 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Some output conversion routines for libproc
 * Copyright (C) 1996, Charles Blake.  See ../COPYING.LIB for
 * distribution conditions.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>

/* output a string, converting unprintables to octal as we go, and stopping after
   processing max chars of output (accounting for expansion due to octal rep).
*/
unsigned print_str(FILE* file, char *s, unsigned max) {
    int i;
    for (i=0; s[i] && i < max; i++)
	if (isprint(s[i]) || s[i] == ' ')
	    fputc(s[i], file);
	else {
	    if (max - i > 3) {
		fprintf(file, "\\%03o", s[i]);
		i += 3; /* 4 printed, but i counts one */
	    } else
		return max - i;
	}
    return max - i;
}

/* output an argv style NULL-terminated string list, converting unprintables
   to octal as we go, separating items of the list by 'sep' and stopping after
   processing max chars of output (accounting for expansion due to octal rep).
*/
unsigned print_strlist(FILE* file, char **strs, char* sep, unsigned max) {
    int i, n, seplen = strlen(sep);
    for (n=0; *strs && n < max; strs++) {
	for (i=0; strs[0][i] && n+i < max; i++)
	    if (isprint(strs[0][i]) || strs[0][i] == ' ')
		fputc(strs[0][i], file);
	    else {
		if (max-(n+i) > 3) {
		    fprintf(file, "\\%03o", strs[0][i]);
		    n += 3; /* 4 printed, but i counts one */
		} else
		    return max - n;
	    }
	n += i;
	if (n + seplen < max) {
	    fputs(sep, file);
	    n += seplen;
	} else
	    return max - n;
    }
    return max - n;
}