linux——4.linux環境

來源:互聯網
上載者:User

標籤:

/* *       os.cpp: for linux os methods */#include <unistd.h>#include <stdlib.h>#include <time.h>#include <stdio.h>#include <sys/types.h>#include <sys/utsname.h>#include <iostream>#include <string>using namespace std;#define UNIT_TEST   1// os classclass OS{public:    // env var    // return: null means no var, '' means var without value    static char *getenv(const char *name)    {        return ::getenv(name);    }    // string: "name=value"    // Note: the env only be valid with the process, not affect the outer env.    static bool putenv(const char *name, const char *value)    {        char *str = new char[strlen(name) + strlen(value) + 2];        strcpy(str, name);        strcat(str, "=");        strcat(str, value);        int ret = ::putenv(str);        delete []str;        return ret != -1;    }        // temp file    static int tmpnam(char *s)    {        // deprecated in ios        //return ::tmpnam(s);        return ::mkstemp(s);    }    static FILE* tmpfile()    {        return ::tmpfile();    }        // user info    static uid_t getuid()    {        return ::getuid();    }    static char* getlogin()    {        return ::getlogin();    }        // machine    // machine net name    static int gethostname(char *name, size_t namelen)    {        return ::gethostname(name, namelen);    }    // more info    /*    static int uname(struct ustname *name)    {        return ::uname(name);    }*/};// time classclass Time{public:    Time(time_t tm = 0)    {        m_time = tm ? tm : time(NULL);    }    Time(tm &t)    {        m_time = mktime(&t);    }        // diff time in secs    double diff(const Time& time)    {        return difftime(m_time, time.m_time);    }        // get tm struct: GMT world time    tm* gmtime()    {        return ::gmtime(&m_time);    }    // format to string    void format(char *out, bool is_local = true)    {        tm *ptm = is_local ? localtime() : gmtime();        if (ptm)        {            sprintf(out, "%02d/%02d/%02d %02d:%02d:%02d",                    ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,                    ptm->tm_hour, ptm->tm_min, ptm->tm_sec);        }    }        // local time    tm* localtime()    {        return ::localtime(&m_time);    }        // asc time    char* asc(bool is_local = true)    {        // return fixed 26 chars        return asctime(is_local ? localtime() : gmtime());    }    // ctime    char* ctime()    {        // = asctime(localtime(timeval))        return ::ctime(&m_time);    }    // strf    // format: %a %A %b %B %c %d ect..    size_t strf(char *s, size_t maxsize, const char *format)    {        return ::strftime(s, maxsize, format, localtime());    }    // strptime..protected:    time_t      m_time;};#if UNIT_TEST == 1int main(int argc, char *argv[]){    // getopt    int opt;    // : means that option must be followed by a value    while ((opt = getopt(argc, argv, ":if:lr")) != -1)    {        switch (opt) {            case 'i':            case 'l':            case 'r':                cout << "option: " << (char)opt << endl;                break;            case 'f':                cout << "filename: " << optarg << endl;                break;            case ':':                cout << "option needs a value" << endl;                break;            case '?':                cout << "unknown option: " << optopt << endl;                break;            default:                cout << "default:" << (char)opt << endl;                break;        }    }        for (; optind < argc; optind++)        cout << "argument: " << argv[optind] << endl;            // Note: you can test: adfan=123 ./os    // env var    char *value = OS::getenv("adfan");    if (value)        cout << value << endl;    if (!OS::putenv("adfan", "best"))        cout << "put event failed";    value = OS::getenv("adfan");    if (value)        cout << value << endl;            // time    Time time;    Time time1;    cout << time.diff(time1) << endl;        char buf[20] = {0};    time1.format(buf);    cout << buf << endl;    cout << time1.asc() << endl;    time1.strf(buf, 20, "%H:%M:%S %Y");    cout << buf << endl;        // tmp    strcpy(buf, "tmp_XXXXXX");    OS::tmpnam(buf);    cout << "tmp:" << buf << endl;        // user    cout << "uid:" << OS::getuid() << ", login:" << OS::getlogin() << endl;        // machine    OS::gethostname(buf, 20);    cout << "host:" << buf << endl;    struct utsname uts;    uname(&uts);    cout << "sys:" << uts.sysname << ", node:" << uts.nodename << ", release:" << uts.release        << ", version:" << uts.version << ", mac:" << uts.machine << endl;}#endif

linux——4.linux環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.