procps/proc/output.c

57 lines
1.6 KiB
C
Raw Normal View History

// Some output conversion routines for libproc
// Copyright (C) 1996, Charles Blake. See COPYING for details.
//
// Copyright 2002, Albert Cahalan
2002-02-02 04:17:29 +05:30
#include <stdio.h>
#include <ctype.h>
#include <string.h>
2002-12-09 12:30:07 +05:30
#include "output.h"
2002-02-02 04:17:29 +05:30
2002-12-09 12:30:07 +05:30
#if 1
2002-02-02 04:17:29 +05:30
/* 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).
*/
2002-11-25 15:46:33 +05:30
unsigned print_str(FILE *restrict file, const char *restrict const s, unsigned max) {
unsigned i;
2002-12-09 12:30:07 +05:30
for (i=0; likely(s[i]) && likely(i<max); i++)
if (likely(isprint(s[i]) || s[i] == ' '))
2002-02-02 04:17:29 +05:30
fputc(s[i], file);
else {
2002-11-25 15:46:33 +05:30
if (max > i+3) {
fprintf(file, "\\%03o", (unsigned char)s[i]);
2002-02-02 04:17:29 +05:30
i += 3; /* 4 printed, but i counts one */
} else
return max - i;
}
return max - i;
}
2002-11-25 15:46:33 +05:30
#endif
2002-02-02 04:17:29 +05:30
/* 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).
*/
2002-11-25 15:46:33 +05:30
unsigned print_strlist(FILE *restrict file, const char *restrict const *restrict strs, unsigned max) {
unsigned i, n;
2002-12-09 12:30:07 +05:30
for (n=0; *strs && n<max; strs++) {
2002-02-02 04:17:29 +05:30
for (i=0; strs[0][i] && n+i < max; i++)
2002-12-09 12:30:07 +05:30
if (likely(isprint(strs[0][i]) || strs[0][i] == ' '))
2002-02-02 04:17:29 +05:30
fputc(strs[0][i], file);
else {
2002-11-25 15:46:33 +05:30
if (max > n+i+3) {
fprintf(file, "\\%03o", (unsigned char)strs[0][i]);
2002-02-02 04:17:29 +05:30
n += 3; /* 4 printed, but i counts one */
} else
return max - n;
}
n += i;
2002-10-12 09:55:57 +05:30
if (n + 1 < max) {
fputc(' ', file);
n++;
2002-02-02 04:17:29 +05:30
} else
return max - n;
}
return max - n;
}