int main(int argc,char **argv)
{
struct passwd *pwdline;
if(argc < 2)
{
fprintf(stderr,"");
exit(1);
}
pwdline=getpwuid(atoi(argv[1]));
puts(pwdline->pwname);
exit(0);
}
int main(int argc,char **argv)
{
char *input_pass;
struct spwd *shadowline;
char *crypted_pass;
if(argc<2)
{
fprintf(stderr,"USage...\n");
exit(1);
}
//输入口令 去掉终端回显
input_pass=getpass("Password: ");
shadowline=getspname(argv[1]);
crypted_pass=crypt(input_pass,shadowline->sp_pwdp);
if(strcmp(shadowline->sp_pwdp,crypted_pass)==0){
puts("ok!");
}
else
puts("fail")
exit(0);
}查找手册 在makefile文件中加入选项LDFLAGS+==lctypt.要用根用户 权限问题
time_t stamp; time()的使用 time(&stamp ) stamp=time(NULL) struct_tm 夏令时调整字段 tm_isdst daylight saving time mktime参数没有const修饰 会调整时间
返回类型为指针 存到静态区域,第二次使用会被冲掉 待查 从一个结构体当中挑选字段放到buf空间里
time_t stamp;
struct tm *tm;
time(&stamp);
stamp = time(NULL);
tm = localtime(&stamp);
strftime(buf,BUFSIZE,"%Y-%m-%d",tm);
puts(buf);
实现效果: 1 2014-5-16 10:34:20 2 2014-5-16 10:34:21 3 2014-5-16 10:34:22 (假设过了十分钟来续写) 4 2014-5-16 10:44:22 5 2014-5-16 10:44:23
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
//首先指定文件
#define FNAME "tmp/out"
int main()
{
FILE *fp;
char buf[BUFSIZE];
int count =0;
time_t stamp;
struct tm *tm;
fp=fopen(FNAME,"a+");
if(fp == NULL){
perror("fopen()");
exit(1);
}
while(fgets(buf,BUFSIZE,fp)!=NULL)
count++;
while(1)
{
//获取时戳
time(&stamp);
tm=localtime(&stamp);
fprintf(fp,"%-4d%d-%d-%d %d:%d:%d\n",++count,\
tm->tm_year+1990,tm->tm_mon+1,tm->tm_day,\
tm->hour,tm->tm_min,tm->tm_second);
//反斜杠续写 格式好看 往文件中写
fflush(fp);
sleep(1);
}
//执行不到
fclose(fp);
exit(0);
}首先,文件的打开和关闭。fopen,fclose.
读 一次读一行,是为了数行数
io全缓冲 写文件,每一秒往缓冲区中放,塞满后才刷新。最后用刷新函数 fflush(fp);
tail -f /tmp/out动态查看
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#defint TIMESTRSIZE 1024
int main()
{
time_t stamp;
struct tm *tm;
char timestr[TIMESTRSIZE];
stamp=time(NULL);
//每一句其实都应该校验 出错时
localtime(&stamp);
strftime(timestr,TIMESTRSIZE,"Now:%Y-%m-%d",tm);
puts(timestr);
tm->tm_mday +=100;
mktime(tm);//使用它的副作用
strftime(timestr,TIMESTRSIZE,"100 days later: %Y-$m-%d",tm);
puts(timestr);
exit(0);
}