用户和组信息、系统时间获取,错误处理和Makefile

用户和组信息获取

(1)getpwuid函数

struct passwd *getpwuid(uid_t uid);

        功能:根据用户 ID 到/etc/passwd文件下解析获得结构体信息。

        参数:uid表示用户 ID。

        返回值:成功返回 ID 对应用户的信息,失败返回NULL

9b1851a87e6040ba9b0838c296d02188.png

#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
int main(int argc, char *argv[])
{
    uid_t uid = 1000;
    struct passwd * pw = getpwuid(uid);

    printf("name:%s gid:%d info:%s wd:%s shell:%s\n",pw->pw_name,pw->pw_gid
           ,pw->pw_gecos,pw->pw_dir,pw->pw_shell);

    return 0;
}

(2)getpwnam函数

struct passwd *getpwnam(const char *name);

        功能:根据用户名到/etc/passwd文件下解析获得结构体信息。

        参数:name指定要查找的用户名。

        返回值:成功返回passwd的结构体指针,失败返回NULL

(3)getgrgid函数

struct group *getgrgid(gid_t gid);

        功能:根据gid/etc/group文件中解析组信息。

        参数:gid表示组 ID。

        返回值:成功返回组信息,失败返回NULL

f4148696cfed40b0be7559817ac5e348.png

#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
    gid_t gid = 1000;
    struct group * gr= getgrgid(gid);

    printf("name:%s gid:%d\n",gr->gr_name,gr->gr_gid);

    return 0;
}

(4)getgrnam函数

struct group *getgrnam(const char *name);

        功能:根据组名到/etc/group文件中解析组信息。

        参数:name指定组名。

        返回值:成功返回group的指针,失败返回NULL

系统时间的获取

(1)time函数

 time_t time(time_t *tloc);

        功能:获得1970年到现在的秒数。

        参数t是存放秒数的空间首地址。

        返回值:成功返回1970年到现在的秒数,失败返回 - 1。

​#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
    time_t tm;
    time(&tm);
    printf("%lu",tm);
    return 0;
}

​

(2)localtime函数

struct tm *localtime(const time_t *timep);

        功能:将一个秒数转化成日历时间。

        参数timep是保存秒数空间的地址。

        返回值:成功返回保存日历时间结构体的指针,失败返回NULL。

#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
    time_t tm;
    time(&tm);

    struct tm *tm_info =  localtime(&tm);
    if(NULL == tm_info)
    {
        printf("localtime error\n");
        return 1;
    }
    printf("%d-%d-%d %d:%d:%d\n",tm_info->tm_year+1900,tm_info->tm_mon+1,tm_info->tm_mday
           ,tm_info->tm_hour,tm_info->tm_min,tm_info->tm_sec);
    return 0;
}

(3)ctime函数

char *ctime(const time_t *timep);

        功能:将时间秒数转化成字符串。

        参数timep是保存时间空间的地址。

        返回值:成功返回获得时间字符串的首地址,失败返回NULL。

#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
    time_t tm;
    time(&tm);
    printf("%s",ctime(&tm));
    return 0;
}

链接文件操作

ln -s  123 softlink  快捷方式

(1)symlink函数

int  symlink(const  char  *oldpath, const char *newpath);

        功能:创建一个链接向oldpath文件的新符号链接文件。

        参数:oldpath为被链接向的文件的路径,newpath为新符号链接文件。

        返回值:成功返回 0,失败返回 -1。

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    
    int ret = symlink("./15localtime.c","softlink");
    if(-1 == ret)
    {
        printf("symlink error\n");
        return 1;
    }

    return 0;
}

(2)remove函数

int remove(const char *pathname);

        功能:删除一个文件。

        参数:pathname为删除文件的路径。

        返回值:成功返回 0,失败返回 -1。

#include <stdio.h>
int main(int argc, char *argv[])
{
    
    int ret = remove("./softlink");
    if(-1 == ret)
    {
        printf("remove error\n");
        return 1;
    }
    return 0;
}

(3)rename函数

        等同于mv命令中的重命名功能

int  rename(const  char  *oldpath,  const char *newpath);

        功能:将一个老的路径名改为新的路径。

        参数:oldpath为老路径名,newpath为新路径名。

        返回值:成功返回 0,失败返回 -1。

#include <stdio.h>
int main(int argc, char *argv[])
{
    int ret = rename("aaa","bbb");
    if(-1 == ret)
    {
        printf("rename error\n");
        return 1;
    }
    return 0;
}

(4)link函数

int  link(const char *oldpath, const char *newpath);

        功能:创建一个硬链接文件。

        参数:oldpath为要链接向的文件,newpath为创建的新硬链接文件。

        返回值:成功返回 0,失败返回 -1。

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    
    int ret = link("./01ls.c","hardlink");
    if(-1 == ret)
    {
        printf("link error\n");
        return 1;
    }
    return 0;
}

(5)truncate函数

        出错相关函数接口

int truncate(const char *path, off_t length);

        开辟特殊长度内存空间

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{

    fopen("aaa","w");
    int ret = truncate("aaa",1024*1024*1024);
    if(-1 == ret)
    {
        printf("truncate error\n");
        return 1;
    }
    return 0;
}

错误处理

(1)perror函数

void perror(const char *s);

        功能:打印指定的字符串serrno对应的错误信息。

        参数:s为要打印在终端上的出错信息。

        返回值:无。

#include <stdio.h>
int main(int argc, char *argv[])
{
    
    FILE* fp = fopen("12312","r");
    if(NULL == fp)
    {
    
        perror("fopen");
        return 1;
    }

    char buf[256]={0};
    if(NULL == fgets(buf,sizeof(buf),fp))
    {
        perror("fgets");
        return 1;
    }
    else 
    {
        printf("buf is %s\n",buf);
    }

    return 0;
}

(2)strerror函数

char *strerror(int errnum);

        功能:打印errnum出错码对应的出错信息。

        参数:errnum为出错的errno号。

        返回值:成功返回对应的错误信息。

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    
    int i = 0 ;
    for(i = 0 ;i<256;i++)
    {
        printf("%d %s\n",i,strerror(i));
    }
    return 0;
}

(3)error函数

void error(int status, int errnum, const char *format, ...);

        功能:程序出错时打印对应出错原因和用户输入字符串并退出。

        参数:status为程序退出的状态,errnum为错误码,format类似printf的打印格式。

        返回值:

__FILE__ 表示是哪个文件
__LINE__表示第几行
__func__表示在哪个函数
#include <stdio.h>
#include <error.h>
#include <errno.h>
// errno 
int main(int argc, char *argv[])
{
    
    FILE* fp = fopen("12312","r");
    if(NULL == fp)
    {
        error(1,errno,"fopen,filename:%s func:%s linenum:%d",__FILE__,__func__
              ,__LINE__);
        printf("aaa\n");
        return 1;
    }

    return 0;
}

工程管理工具

vim编辑器
gcc编译器
gdb调试器(调试逻辑错误)多个.c文件同时编译时使用
make-f指定文件名

指定编译多个Makefile中的一
个文件

(1)Makefile

a.out:main.c add.c mul.c
    gcc main.c add.c mul.c -o a.out

clean:
    rm a.out

(2)Makefile2

a.out:main.c add.c mul.c
    gcc $^ -o $@

clean:
    rm a.out

(3)Makefile3

SRC=main.c
SRC+=add.c mul.c
OBJ=all

FLAG = -g -02 -Wall
$(OBJ):$(SRC)
    gcc $(SRC) -o $(OBJ) $(FLAG)

clean
    rm $(OBJ)

 

 

  • 43
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值