Linux--4.linux Environment

Source: Internet
Author: User

/* * Os.cpp:for Linux OS methods */#include <unistd.h> #include <stdlib.h> #include <time.h> #includ E <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 V    AR, ' means var without value static char *getenv (const char *name) {return:: getenv (name);    }//String: "Name=value"//note:the env Only is 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 =::p utenv (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:: Geth    Ostname (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 (): GM        Time (); 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-chars return Asctime (Is_loca L?    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 is 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::p utenv ("Adfan", "best") cout << "put Event failed ";    Value = os::getenv ("Adfan");            if (value) cout << value << Endl;    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, "%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.rele ASE << ", Version:" << uts.version << ", Mac:" << Uts.machine << Endl;} #endif

Linux--4.linux Environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.