Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1664475
  • 博文数量: 607
  • 博客积分: 10031
  • 博客等级: 上将
  • 技术积分: 6633
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-30 17:41
文章分类

全部博文(607)

文章存档

2011年(2)

2010年(15)

2009年(58)

2008年(172)

2007年(211)

2006年(149)

我的朋友

分类: LINUX

2008-08-29 22:37:41

1.程序参数
getopt

#include
int getopt(int argc, char* const argv[], const char* optstring);
extern char* optarg;
extern int optind, opterr, optopt;

如果没有更多的参数可以读取,返回-1;
如果参数带个值,保存在optarg;
如果有一个识别不出的选项,返回?并保存在optopt


#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(“unknown option: %c\n”, optopt);
            break;
        }
    }

    for(; optind < argc; optind++)
        printf(“argument: %s\n”, argv[optind]);
   
    exit(0);
}


2.环境变量
可以用echo显示特定的环境变量的值,也可以使用set命令显示所有的环境变量。

c程序中可以使用getenv()和putenv()来访问环境变量。

#include

char* getenv(const char* name);
int putenv(const char* string); //string的格式:name=value


3.时间和日期
#include
time_t time(time_t* tloc);

time_t 代表从UNIX纪元(1970 1-1 00:00:00)流逝的秒数
Linux系统上代表1个long integer型.永远不要假定它是32位.不同的系统上不同的位数.

#include
double difftime(time_t time1, time_t time2);

#include
struct tm* gmtime(const time_t timeval);

4.临时文件
临时文件用来保存中间数据.
要当心一点:由于LINUX是多任务系统,要确保临时文件的名字是唯一的,否则会导致干扰.
#include
char* tmpnam(char* filename);
FILE* tmpfile(void);


老的UNIX系统还采用下面的2个函数来实现上述功能,基于安全性和稳定性不推荐使用.
#include
char* mktemp(char* template);
int mkstemp(char* template);

5. 用户信息
linux程序(除了init之外),都是被别的程序或者用户启动的。
11章会详细讲述进程,处理器,交互这些概念。

1个程序可以找出使用它的人的信息。
当1个用户登录进linux,就必须有用户名和密码。也即意味着该用户有唯一的UID。
你可以重新装订程序就好像另外一个人在启动它们一样。当su被执行时,就好像程序被superuser启动一样。

UID的类型是uid_t,定义在sys/types.h
100以下的UID是保留给系统的。

#include
#include

uid_t getuid(void);//返回关联的UID,通常是启动该程序的用户
char* getlogin(void);//返回当前用户的登录名


先进的类UNIX系统都会使用shadow password, /etc/shadow这个文件普通用户根本看不到。
所以提供了一个程序接口来读取shadow文件中的用户信息。

#include
#include
struct passwd* getpwuid(uid_t uid); //从UID获取
struct passwd* getpwnam(const char* name);//从用户名获取



int main(int argc, char** argv)
{

    uid_t uid;
    gid_t gid;
    struct passwd *pw;
    uid = getuid();
    gid = getgid();
    printf("User is %s\n", getlogin());
    printf("User IDs: uid=%d, gid=%d\n", uid, gid);
    pw = getpwuid(uid);
    printf("UID[%d] passwd entry:\nname=%s, uid=%d, gid=%d, home=%s, gecos=%s, shell=%s\n",
        uid, pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_gecos, pw->pw_shell);
    pw = getpwnam("root");
    printf("root passwd entry:\n");
    printf("name=%s, uid=%d, gid=%d, home=%s, gecos=%s, shell=%s\n",
        pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_gecos, pw->pw_shell);

    return 0;
}

6. 主机信息

如果1个机器安装了网络组件,可以很容易地获取它的网络名
#include
int gethostname(char* name, int namelen);

你还可以通过uname获得更详细的信息。
#include
int uname(struct utsname* name);

7. 日志
系统有必要记录一下活动
不同的日志文件被定义在/etc/syslog.conf中。
特别地,/var/log/messages记录了所有的系统消息

8.资源和限制

limits.
h定义了各种的资源限制

NAME_MAX: 文件名最大长度。特定于文件系统,更具可移植性的方法是pathconf函数.
INT_MAX: 整型最大值
CHAR_MAX:char最大值
CHAR_BIT:char中最大位数

#include

程序可以改变它们或别人的优先级
which 参数:PRIO_PROCESS, PRIO_PGRP, PRIO_USER
int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int priority);

有许多系统资源被定义,resource可以是:
RLIMIT_CPU:cpu时间限制
RLIMIT_FSIZE:文件的最大尺寸
RLIMIT_NOFILE:打开的文件个数的限制
RLIMIT_STACK:堆栈大小的限制
RLIMIT_CORE:COREDUMP文件的最大尺寸
rlimit成员包括rlim_cur(软件限制),rlim_max(硬件限制)

int getrlimit(int resource, struct rlimit* r_limit);
int setrlimit(int resource, const struct rlimit* r_limit);

获得当前程序和系统已经占用了多少cpu时间
who可以是RUSAGE_SELF, RUSAGE_CHILDREN
int getrusage(int who, struct rusage* r_usage);











阅读(940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~