Date: Sun, 19 Aug 2001 01:24:21 +0200 From: Arne Dag Fidjestøl <adf@idi.ntnu.no> To: Josef Karthauser <joe@tao.org.uk> Cc: Dag-Erling Smorgrav <des@ofug.org>, freebsd-fs@FreeBSD.ORG, adf@idi.ntnu.no Subject: Re: parsing problem with /proc/N/status Message-ID: <200108182324.BAA10850@zevs.idi.ntnu.no> In-Reply-To: Your message of "Sat, 18 Aug 2001 14:19:40 BST." <20010818141940.B877@tao.org.uk>
next in thread | previous in thread | raw e-mail | index | archive | help
> > newline). If we have to go down that road, I'd rather just encode
> > whitespace and unprintables in octal notation (like ls -B does).
Unprintables vary with locales, so octal encoding should probably be
limited to ascii-whitespace?
> Sounds fair. I don't particularly care, but as it stands it's next
> to useless as it's unparsable.
Provided that there is a known number of space-seperated fields it is
still parsable. See attached program for a lightly-tested example.
-adf
#include <sys/types.h>
#include <sys/uio.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#define PSTATUS "/proc/curproc/status"
/*
* return number of characters in progname, or -1 on error.
*/
int
getprognamelen(const char *status, size_t len)
{
const char *s = status + len;
int nspc = 14; /* number of field-seperating-spaces */
for (; s > status && nspc; s--)
if (*s == ' ')
nspc--;
return nspc ? -1 : s - status + 1;
}
int
main()
{
char buf[1024];
int i,f,sz;
f = open(PSTATUS, O_RDONLY);
if (f != -1) {
sz = read(f, buf, sizeof(buf));
close(f);
}else
err(1, PSTATUS);
i = getprognamelen(buf, sz);
if (i != -1)
printf("getprogname -> '%.*s' [%d]\n", i, buf, i);
else
printf("getprogname failed\n");
return 0;
}
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-fs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200108182324.BAA10850>
