免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 736 | 回复: 0
打印 上一主题 下一主题

BLP-chap-4 Linux环境 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-18 13:27 |只看该作者 |倒序浏览

                                                                程序参数
getopt用法:
#include
#include
#include
int
main(int argc, char *argv[])
{
        int     opt;
        while ((opt=getopt(argc, argv, "if:lr")) != -1)
        {
                switch(opt) {
                case 'i':
                case 'l':
                case 'r':
                        printf("option:%c\n", opt);
                        break;
                case 'f':
                        printf("filename: %s\n", optarg);
                        break;
                case ':':
                        printf("option needs a value\n");
                        break;
                case '?':
                        printf("unknow option: %c\n", optopt);
                        break;
                }
        }
        for (; optind
运行:
[root@localhost chap4]# ./argopt -i -lr 'hi here' -f fred.c -q
option:i
option:l
option:r
filename: fred.c
./argopt: invalid option -- q
unknow option: q
argument: hi here
getopt_long略
环境变量
    环境变量详解:
环境变量
environ:
#include
#include
extern char **environ;
int
main(void)
{
        char **env = environ;
        while (*env)
        {
                printf("%s\n", *env);
                env++;
        }
        exit(0);
}
getenv与putenv:
#include
#include
#include
int
main(int argc, char *argv[])
{
        char    *var;     /* 环境变量名 */
        char    *value;   /* 环境变量值 */
        if (argc ==1 || argc > 3)
        {
                fprintf(stderr, "useage: ./environ var [value]\n");
                exit(1);
        }
        var = argv[1];
        value = getenv(var);    /* 获取环境变量的值 */
        if (value)
                printf("variable %s has value %s \n", var, value);
        else
                printf("variable %s has no value\n");
        /* 在程序中设置环境变量的值 */
        if (argc == 3)
        {
                char    *string;
                value = argv[2];
                if (!(string = malloc(strlen(var) + strlen(value) + 2)))
                {
                        fprintf(stderr, "overflow\n");
                        exit(1);
                }
                strcpy(string, var);
                strcat(string, "=");
                strcat(string, value);
                printf("calling putenv to %s \n", string);
                if (putenv(string) != 0)        /* 设置环境变量 */
                {
                        fprintf(stderr, "putenv faile\n");
                        free(string);
                        exit(1);
                }
                value = getenv(var);  /* 获取新值 */
                if (value)
                        printf("new value of %s is %s\n", var, value);
                else
                        printf("new value of %s is null???\n", var);
        }
        exit(0);
}
运行:
[root@localhost chap4]# ./environ HOME
variable HOME has value /root
[root@localhost chap4]# ./environ NOSUCHENV
variable  has no value
[root@localhost chap4]# ./environ NOSUCHENV hello
variable  has no value
calling putenv to NOSUCHENV=hello
new value of NOSUCHENV is hello
[root@localhost chap4]# ./environ NOSUCHENV
variable  has no value
时间日期
time函数:
#include
#include
#include
int
main(void)
{
        time_t  the_time;
        int     i;
        for (i=0; i
运行:
[root@localhost chap4]# ./envtime
The time is : 1242628048
The time is : 1242628051
The time is : 1242628054
The time is : 1242628057
The time is : 1242628060
localtime函数:
#include
#include
#include
#include
int
main(void)
{
        struct tm *tm_ptr;
        time_t    the_time;
        (void)time(&the_time);
        tm_ptr = localtime(&the_time);
        printf("Raw time is %ld\n", the_time);
        printf("localtime gives:\n");
        printf("date: %02d/%02d/%02d\n",
                tm_ptr->tm_year+1900, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
        printf("time:%02d:%02d:%02d\n",
                tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
        exit(0);
}
运行:
[root@localhost chap4]# ./localtime; date
Raw time is 1242637706
localtime gives:
date: 2009/05/18
time:17:08:26
Mon May 18 17:08:26 HKT 2009
ctime函数:
#include
#include
#include
int
main(void)
{
        time_t  timeval;
        time(&timeval);
        printf("the date is:%s\n", ctime(&timeval));
        exit(0);
}
运行:
[root@localhost chap4]# ./ctime
the date is:Mon May 18 17:12:37 2009
               
                               
               
               
               
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/94445/showart_1932089.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP