linux——4.linux环境

/*
 *       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 class
class 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 class
class 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 == 1
int 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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。