/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/


#include	<unistd.h>
#include	"utils.h"
#include	"limits.h"

int
_numofcpu()
{
int p[2];
int id;
char buf[100];
char * ptr;
int er;
int ret;
	pipe(p);
	id = fork();
	if ( id == 0 ) {
		close(1);
		dup(p[1]);
		close(p[0]);
		for ( id = 3 ; id <= sys_param.max_fd ; id ++ )
			close(id);
		system("hostinfo | egrep 'Default processor'");
		exit(0);
	}
	close(p[1]);
	er = read(p[0],buf,100);
	close(p[0]);
	if ( er < 0 )
		return 1;
	buf[er] = 0;
	for ( ptr = buf+strlen(buf) ; ptr != buf ; ptr -- ) {
		if ( '0' <= *ptr && *ptr <= '9' ) {
			for ( ; '0' <= *ptr && *ptr <= '9' ; ptr -- );
			break;
		}
	}
	ret = atoi(ptr+1);
	if ( ret < 1 )
		return 1;
	return ret;
}
