用户和组信息获取
(1)getpwuid
函数
struct passwd *getpwuid(uid_t uid);
功能:根据用户 ID 到/etc/passwd
文件下解析获得结构体信息。
参数:uid
表示用户 ID。
返回值:成功返回 ID 对应用户的信息,失败返回NULL
。
#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
。
#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);
功能:打印指定的字符串s
和errno
对应的错误信息。
参数: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)