hostinfo.cc revision 5202
152SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
352SN/A * All rights reserved.
452SN/A *
552SN/A * Redistribution and use in source and binary forms, with or without
652SN/A * modification, are permitted provided that the following conditions are
752SN/A * met: redistributions of source code must retain the above copyright
852SN/A * notice, this list of conditions and the following disclaimer;
952SN/A * redistributions in binary form must reproduce the above copyright
1052SN/A * notice, this list of conditions and the following disclaimer in the
1152SN/A * documentation and/or other materials provided with the distribution;
1252SN/A * neither the name of the copyright holders nor the names of its
1352SN/A * contributors may be used to endorse or promote products derived from
1452SN/A * this software without specific prior written permission.
1552SN/A *
1652SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1752SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1852SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1952SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2052SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2152SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2252SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2452SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2552SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2652SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
2952SN/A */
3052SN/A
3152SN/A#include <ctype.h>
3252SN/A#include <errno.h>
3352SN/A#include <math.h>
34401SN/A#include <unistd.h>
3552SN/A
363918Ssaidi@eecs.umich.edu#include <stdio.h>
37401SN/A#include <cstdlib>
38401SN/A#include <cstring>
39401SN/A#include <string>
40401SN/A
41401SN/A#include "base/misc.hh"
4256SN/A#include "sim/host.hh"
4352SN/A
44401SN/Ausing namespace std;
45401SN/A
46401SN/Astring
47401SN/A__get_hostname()
48401SN/A{
49401SN/A    char host[256];
50401SN/A    if (gethostname(host, sizeof host) == -1)
51401SN/A        warn("could not get host name!");
52401SN/A    return host;
53401SN/A}
54401SN/A
55401SN/Astring &
56401SN/Ahostname()
57401SN/A{
58401SN/A    static string hostname = __get_hostname();
59401SN/A    return hostname;
60401SN/A}
61401SN/A
6252SN/Auint64_t
635202Sstever@gmail.comprocInfo(const char *filename, const char *target)
6452SN/A{
6552SN/A    int  done = 0;
6652SN/A    char line[80];
6752SN/A    char format[80];
6892SN/A    long usage;
6952SN/A
7052SN/A    FILE *fp = fopen(filename, "r");
7152SN/A
7252SN/A    while (fp && !feof(fp) && !done) {
7352SN/A        if (fgets(line, 80, fp)) {
7452SN/A            if (strncmp(line, target, strlen(target)) == 0) {
751875SN/A                snprintf(format, sizeof(format), "%s %%ld", target);
7652SN/A                sscanf(line, format, &usage);
7752SN/A
7852SN/A                fclose(fp);
7952SN/A                return usage ;
8052SN/A            }
8152SN/A        }
8252SN/A    }
8352SN/A
8452SN/A    if (fp)
8552SN/A      fclose(fp);
8652SN/A
8752SN/A    return 0;
8852SN/A}
89