当前位置: 首页 > news >正文

【Linux系统编程】操作文件和目录的函数

目录

    • 1、stat、fstat、lstat、fstatat 函数
    • 2、chmod、fchmod 函数
    • 3、chown、fchown、lchown、fchownat函数
    • 4、truncate、ftruncate函数
    • 5、utime、utimes 函数
    • 6、mkdir、rmdir函数
      • 递归删除非空目录
    • 7、opendir、fdopendir、readdir函数
    • 8、chdir、fchdir、getcwd 函数
    • 9、getpwnam、getpwuid 函数
    • 10、getpwent、setpwent、endpwent函数
    • 11、getspnam、getspent、setspent、endspent函数
    • 12、crypt 函数

1、stat、fstat、lstat、fstatat 函数

头文件

	   #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>
       ///* Definition of AT_* constants */
       #include <fcntl.h>

struct stat 结构体

struct stat {
    dev_t     st_dev;     // 文件所在设备的 ID
    ino_t     st_ino;     // 文件的 inode 号
    mode_t    st_mode;    // 文件类型和权限
    nlink_t   st_nlink;   // 硬链接数
    uid_t     st_uid;     // 文件所有者的用户 ID
    gid_t     st_gid;     // 文件所有者的组 ID
    dev_t     st_rdev;    // 设备 ID(如果是特殊文件)
    off_t     st_size;    // 文件大小(字节数)
    blksize_t st_blksize; // 文件系统 I/O 块大小
    blkcnt_t  st_blocks;  // 分配的 512B 块数
    time_t    st_atime;   // 最后访问时间
    time_t    st_mtime;   // 最后修改时间
    time_t    st_ctime;   // 最后状态改变时间
};

函数定义

stat函数

int stat(const char *pathname, struct stat *buf);

功能: 获取指定路径文件的状态信息。

参数:

  • pathname: 文件的路径名。
  • buf: 指向 struct stat 结构体的指针,用于存储文件的状态信息。

返回值:

  • 成功时返回 0,失败时返回 -1 并设置 errno。

特点:

  • 如果 pathname 是一个符号链接,则获取的是符号链接指向的文件的状态信息。
#include <sys/stat.h>
#include <stdio.h>

int main() {
    struct stat buf;
    if (stat("example.txt", &buf) == 0) {
        printf("File size: %ld bytes\n", buf.st_size);
    } else {
        perror("stat");
    }
    return 0;
}

fstat函数

int fstat(int fd, struct stat *buf);

功能: 获取与文件描述符 fd 相关联的文件的状态信息。

参数:

  • fd: 文件描述符。
  • buf: 指向 struct stat 结构体的指针,用于存储文件的状态信息。

返回值:

  • 成功时返回 0,失败时返回 -1 并设置 errno。

特点:

  • 适用于已经打开的文件。
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main() {
    int fd = open("example.txt", O_RDONLY);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    struct stat buf;
    if (fstat(fd, &buf) == 0) {
        printf("File size: %ld bytes\n", buf.st_size);
    } else {
        perror("fstat");
    }

    close(fd);
    return 0;
}

lstat 函数

int lstat(const char *pathname, struct stat *buf);

功能: 获取指定路径文件的状态信息。

参数:

  • pathname: 文件的路径名。
  • buf: 指向 struct stat 结构体的指针,用于存储文件的状态信息。

返回值:

  • 成功时返回 0,失败时返回 -1 并设置 errno。

特点:

  • 如果 pathname 是一个符号链接,则获取的是符号链接本身的状态信息,而不是它指向的文件。
#include <sys/stat.h>
#include <stdio.h>

int main() {
    struct stat buf;
    if (lstat("symlink.txt", &buf) == 0) {
        printf("File size: %ld bytes\n", buf.st_size);
    } else {
        perror("lstat");
    }
    return 0;
}

fstatat函数

int fstatat(int dirfd, const char *pathname, struct stat *buf,int flags);

功能: 获取相对于目录文件描述符 dirfd 的指定路径文件的状态信息。

参数:

  • dirfd: 目录文件描述符,可以是 AT_FDCWD(表示当前工作目录)。
  • pathname: 文件的路径名(相对于 dirfd)。
  • buf: 指向 struct stat 结构体的指针,用于存储文件的状态信息。
  • flags: 控制行为的标志,如 AT_SYMLINK_NOFOLLOW。

返回值:

  • 成功时返回 0,失败时返回 -1 并设置 errno。

特点:

  • 如果 flags 包含 AT_SYMLINK_NOFOLLOW,则不会跟随符号链接。 适用于相对路径操作。
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main() {
    struct stat buf;
    if (fstatat(AT_FDCWD, "example.txt", &buf, 0) == 0) {
        printf("File size: %ld bytes\n", buf.st_size);
    } else {
        perror("fstatat");
    }
    return 0;
}

总结

stat: 获取文件状态,跟随符号链接。
fstat: 获取已打开文件的状态。
lstat: 获取文件状态,不跟随符号链接。
fstatat: 获取相对于目录文件描述符的文件状态,支持标志控制行为。

2、chmod、fchmod 函数

头文件

#include <sys/stat.h>

函数定义

chmod函数

int chmod(const char *pathname, mode_t mode);

功能: 修改指定路径文件的权限。

参数:

  • pathname: 文件的路径名。
  • mode: 新的权限模式,通常是一个八进制数(如 0644)或通过宏(如 S_IRUSR)组合。

返回值:

  • 成功时返回 0,失败时返回 -1 并设置 errno。

特点:

  • 修改的是文件路径对应的文件的权限。
  • 如果 pathname 是一个符号链接,则修改的是符号链接指向的文件的权限。

示例

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>


int main()
{
#if 0
    //直接修改文件权限
    if(chmod("hello.txt",0444) < 0)
    {
        perror("chmod error");
        return -1;
    }
    printf("chomd success\n");
#endif

    struct stat st = {0};

    stat("hello.txt",&st);

    if(!chmod("hello.txt",st.st_mode | 0200))
    {
        printf("chmod 0200 success\n");
    }
    else
    {
        printf("chmod 0200 error\n");
    }

#if 0
    //if(!chmod("hello.txt",st.st_mode&0773))
    if(!chmod("hello.txt",st.st_mode^0004))
    {
        printf("chmod 004 success\0");
    }
    else
    {
        printf("chmod 0004 error\0");
    }
#endif

}

fchmod函数

int fchmod(int fd, mode_t mode);

功能: 修改与文件描述符 fd 相关联的文件的权限。

参数:

  • fd: 文件描述符。
  • mode: 新的权限模式。

返回值:

  • 成功时返回 0,失败时返回 -1 并设置 errno。

特点:

  • 修改的是已经打开的文件的权限。
  • 适用于需要通过文件描述符操作文件的场景。
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main() {
    int fd = open("example.txt", O_RDONLY);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    if (fchmod(fd, 0644) == 0) {
        printf("File permissions changed successfully.\n");
    } else {
        perror("fchmod");
    }

    close(fd);
    return 0;
}

3、chown、fchown、lchown、fchownat函数

头文件

 	   #include <fcntl.h>           /* Definition of AT_* constants */
       #include <unistd.h>

函数定义
chown函数

int chown(const char *pathname, uid_t owner, gid_t group);

功能: 更改指定路径文件或目录的所有者和/或所属组。
参数:

  • pathname: 文件或目录的路径。
  • owner: 新的用户ID(UID),如果为-1,则不更改所有者。
  • group: 新的组ID(GID),如果为-1,则不更改所属组。
    返回值:
  • 成功返回0,失败返回-1并设置errno。
#include <stdio.h>
#include <unistd.h>

int main()
{
    if(chown("hello.txt",1000,1000) < 0)\
    {
        perror("chown error");
        return -1;
    }
    perror("chown success");
}

fchown函数

int fchown(int fd, uid_t owner, gid_t group);

功能: 更改通过文件描述符指定的文件或目录的所有者和/或所属组。
参数:

  • fd: 文件描述符。
  • owner: 新的用户ID(UID),如果为-1,则不更改所有者。
  • group: 新的组ID(GID),如果为-1,则不更改所属组。
    返回值:
  • 成功返回0,失败返回-1并设置errno。

lchown函数

  int lchown(const char *pathname, uid_t owner, gid_t group);

功能: 类似于chown,但不跟随符号链接,即只更改符号链接本身的所有者和/或所属组,而不是链接指向的文件。
参数:

  • pathname: 符号链接的路径。
  • owner: 新的用户ID(UID),如果为-1,则不更改所有者。
  • group: 新的组ID(GID),如果为-1,则不更改所属组。
    返回值:
  • 成功返回0,失败返回-1并设置errno。

fchownat函数

 int fchownat(int dirfd, const char *pathname,
                    uid_t owner, gid_t group, int flags);

功能: 类似于chown,但可以通过dirfd指定相对路径的基准目录,并且可以通过flags控制行为。
参数:

  • dirfd: 基准目录的文件描述符,如果pathname是绝对路径,则忽略此参数。
  • pathname: 文件或目录的路径。
  • owner: 新的用户ID(UID),如果为-1,则不更改所有者。
  • group: 新的组ID(GID),如果为-1,则不更改所属组。
  • flags: 控制行为的标志,例如AT_SYMLINK_NOFOLLOW表示不跟随符号链接。

返回值:

  • 成功返回0,失败返回-1并设置errno。

4、truncate、ftruncate函数

头文件

	   #include <unistd.h>
       #include <sys/types.h>

truncate函数

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

功能:将指定路径的文件截断到指定长度,也可以扩容制造空洞文件

参数:

  • path:文件的路径名。
  • length:文件截断后的长度(以字节为单位)。

返回值:

  • 成功时返回 0。失败时返回 -1
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    //截断
    if(truncate("hello.txt",5) < 0)
    {
        perror("truncate error");
        return -1;
    }
    printf("truncate success\n");

    //扩充
    if(truncate("hello.txt",20) < 0)
    {
        perror("truncate error");
        return -1;
    }
    printf("truncate success\n");
}

ftruncate函数

 int ftruncate(int fd, off_t length);

功能:将已打开的文件描述符对应的文件截断到指定长度,也可扩容制造空洞文件
在这里插入图片描述

参数:

  • fd:文件描述符。
  • length:文件截断后的长度(以字节为单位)。

返回值:

  • 成功时返回 0。失败时返回 -1

5、utime、utimes 函数

头文件

	   #include <sys/types.h>
       #include <utime.h>

	   #include <sys/time.h>

utime函数

 int utime(const char *filename, const struct utimbuf *times);

功能

修改指定文件的访问时间和修改时间。

参数

  • filename:要修改时间的文件的路径名。
  • times:指向 struct utimbuf 结构体的指针,包含新的访问时间和修改时间。如果为
    NULL,则将文件的访问时间和修改时间设置为当前时间。

struct utimbuf 结构体

struct utimbuf {
    time_t actime;  // 访问时间(Access Time)
    time_t modtime; // 修改时间(Modification Time)
};

返回值

  • 成功时返回 0。
  • 失败时返回 -1,并设置 errno。
#include <stdio.h>
#include <sys/types.h>
#include <utime.h>
#include <time.h>



int main()
{

    struct tm time = {0};
    time.tm_year = 2025 - 1990;
    time.tm_mon = 3 -1;
    time.tm_mday = 3;
    time.tm_hour = 14;
    time.tm_min = 57;
    time.tm_sec = 0;

    time_t sec = mktime(&time);  //获取对应时间的秒数
    
    struct utimbuf buf = {0};
    buf.actime = sec;
    buf.modtime = sec;

    if(utime("hello.txt",&buf) < 0)
    {
        perror("utime error");
        return -1;
    }
    printf("utime success\n");
    return 0;
}

utimes函数

int utimes(const char *filename, const struct timeval times[2]);

功能

修改指定文件的访问时间和修改时间,支持微秒级精度。

参数

  • filename:要修改时间的文件的路径名。
  • times:指向 struct timeval 数组的指针,包含新的访问时间和修改时间。如果为
    NULL,则将文件的访问时间和修改时间设置为当前时间。

struct timeval 结构体

struct timeval {
    time_t tv_sec;  // 秒数
    suseconds_t tv_usec; // 微秒数
};

返回值

  • 成功时返回 0。
  • 失败时返回 -1,并设置 errno。
#include <sys/time.h>
#include <stdio.h>

int main() {
    struct timeval times[2];
    times[0].tv_sec = 1633072800;  // 设置访问时间为 2021-10-01 00:00:00
    times[0].tv_usec = 0;
    times[1].tv_sec = 1633072800;  // 设置修改时间为 2021-10-01 00:00:00
    times[1].tv_usec = 0;

    if (utimes("example.txt", times) == -1) {
        perror("utimes failed");
        return 1;
    }

    printf("File times updated successfully.\n");
    return 0;
}

6、mkdir、rmdir函数

头文件

	   //mkdir函数
	   #include <sys/stat.h>
       #include <sys/types.h>
       //rmdir函数
       #include <unistd.h>

mkdir函数

int mkdir(const char *pathname, mode_t mode);

功能

  • 创建一个新目录

参数

  • pathname:要创建的目录的路径名。
  • mode:目录的权限模式,通常使用八进制表示(如 0755)。权限模式可以通过以下宏进行组合:
    • S_IRWXU:用户(所有者)具有读、写、执行权限。
    • S_IRUSR:用户具有读权限。
    • S_IWUSR:用户具有写权限。
    • S_IXUSR:用户具有执行权限。
    • S_IRWXG:组用户具有读、写、执行权限。
    • S_IRGRP:组用户具有读权限。
    • S_IWGRP:组用户具有写权限。
    • S_IXGRP:组用户具有执行权限。
    • S_IRWXO:其他用户具有读、写、执行权限。
    • S_IROTH:其他用户具有读权限。
    • S_IWOTH:其他用户具有写权限。
    • S_IXOTH:其他用户具有执行权限。

返回值

  • 成功时返回 0。
  • 失败时返回 -1,并设置 errno。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
    // 创建目录
    if(mkdir("dir",0777) < 0)
    {
        perror("mkdir error");
        return -1;
    }

    printf("mkdir success\n");

}

rmdir函数

int rmdir(const char *pathname);

功能

  • 删除一个空目录

参数

  • pathname:要删除的目录的路径名。

返回值

  • 成功时返回 0。
  • 失败时返回 -1,并设置 errno。

递归删除非空目录

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>

void remove_mydir(const char *pathname)
{
    DIR *dp = opendir(pathname);

    if (NULL == dp)
    {
        perror("open dp error");
        return;
    }

    while (1)
    {
        struct dirent *dirp = readdir(dp);

        if (dirp == NULL)
        {
            break;
        }

        if (!(strcmp(dirp->d_name, ".")) || !(strcmp(dirp->d_name, "..")))
            continue;

        char buf[128] = "";
        strcat(buf, pathname);
        strcat(buf, "/");
        strcat(buf, dirp->d_name);

        struct stat st = {0};
        stat(buf, &st);

        if (S_ISREG(st.st_mode))
            unlink(buf);
        if (S_ISDIR(st.st_mode))
            remove_mydir(buf);
    }

    closedir(dp);

    remove(pathname);
}

int main()
{
    remove_mydir("dir");
    return 0;
}

7、opendir、fdopendir、readdir函数

头文件

 	   #include <sys/types.h>
       #include <dirent.h>

opendir函数

DIR *opendir(const char *name);

功能: 打开一个目录,返回指向目录流的指针。

参数: name:要打开的目录的路径名。

返回值

  • 成功时返回指向 DIR 结构的指针。
  • 失败时返回 NULL,并设置 errno。
#include <dirent.h>
#include <stdio.h>

int main() {
    const char *dir_path = "example_dir";

    DIR *dir = opendir(dir_path);
    if (!dir) {
        perror("opendir failed");
        return 1;
    }

    printf("Directory '%s' opened successfully.\n", dir_path);
    closedir(dir);
    return 0;
}

fdopendir函数

DIR *fdopendir(int fd);

功能 :通过文件描述符打开目录,返回指向目录流的指针。
参数 :fd:已经打开的目录的文件描述符。
返回值

  • 成功时返回指向 DIR 结构的指针。
  • 失败时返回 NULL,并设置 errno。
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    const char *dir_path = "example_dir";

    int fd = open(dir_path, O_RDONLY | O_DIRECTORY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    DIR *dir = fdopendir(fd);
    if (!dir) {
        perror("fdopendir failed");
        close(fd);
        return 1;
    }

    printf("Directory '%s' opened successfully via fd.\n", dir_path);
    closedir(dir); // 关闭目录流时会自动关闭文件描述符
    return 0;
}

readdir函数

struct dirent *readdir(DIR *dirp);

功能 :从目录流中读取下一个目录项。

参数

  • dirp:指向 DIR 结构的指针(由 opendir 或 fdopendir 返回)。

返回值

  • 成功时返回指向 struct dirent 的指针。
  • 到达目录末尾时返回 NULL。
  • 失败时返回 NULL,并设置 errno。
    struct dirent 结构
struct dirent {
    ino_t d_ino;       // 文件的 inode 号
    off_t d_off;       // 目录文件中的偏移量
    unsigned short d_reclen; // 记录长度
    unsigned char d_type;   // 文件类型
    char d_name[256];       // 文件名
};

示例-循环读取目录下的文件名

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

int main()
{
    // 打开目录
    DIR *dp = opendir("dir");

    if (NULL == dp)
    {
        perror("opendir error");
        return -1;
    }

    // 读取目录

    while (1)
    {
        struct dirent *dirp = readdir(dp);
        if (NULL == dirp)
            break;
        printf("name = %s\n", dirp->d_name);
    }

    // 关闭目录
    closedir(dp);
}

8、chdir、fchdir、getcwd 函数

头文件

#include <unistd.h>

chdir函数

int chdir(const char *path);

功能:将当前进程的工作目录更改为指定的路径。
参数:path:目标目录的路径名。
返回值

  • 成功时返回 0。
  • 失败时返回 -1,并设置 errno。
#include <unistd.h>
#include <stdio.h>

int main() {
    const char *new_dir = "/tmp";

    if (chdir(new_dir) == -1) {
        perror("chdir failed");
        return 1;
    }

    printf("Changed working directory to '%s'.\n", new_dir);
    return 0;
}

fchdir函数

int fchdir(int fd);

功能:通过文件描述符更改当前进程的工作目录。
参数:fd:已经打开的目录的文件描述符。
返回值

  • 成功时返回 0。
  • 失败时返回 -1,并设置 errno。
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main() {
    const char *new_dir = "/tmp";

    int fd = open(new_dir, O_RDONLY | O_DIRECTORY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    if (fchdir(fd) == -1) {
        perror("fchdir failed");
        close(fd);
        return 1;
    }

    printf("Changed working directory to '%s' via fd.\n", new_dir);
    close(fd);
    return 0;
}

getcwd函数

char *getcwd(char *buf, size_t size);

功能:获取当前工作目录的绝对路径。
参数

  • buf:存储路径的缓冲区。
  • size:缓冲区的大小。

返回值

  • 成功时返回指向 buf 的指针。
  • 失败时返回 NULL,并设置 errno。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int main()
{
    //获取当前工作目录
    char buf[128] = "";
    getcwd(buf,sizeof(buf)-1);
    printf("buf = %s\n",buf);

    //改变工作路径
    if(chdir("/home")<0)
    {
        perror("chdir error");
        return -1;
    }

    memset(buf,0,sizeof(buf));
    getcwd(buf,sizeof(buf)-1);
    printf("buf = %s\n",buf);

    if(mkdir("lisi",0777)<0)
    {
        perror("mkdir error");
        return -1;
    }
}

9、getpwnam、getpwuid 函数

头文件

 	   #include <sys/types.h>
       #include <pwd.h>

struct passwd 结构体

#include <pwd.h>

struct passwd {
    char   *pw_name;   // 用户名
    uid_t   pw_uid;    // 用户 ID
    gid_t   pw_gid;    // 组 ID
    char   *pw_dir;    // 用户主目录
    char   *pw_shell;  // 用户默认 shell
};

getpwnam函数

struct passwd *getpwnam(const char *name);

功能: 通过用户名查询用户信息。
参数 用户名(字符串)。
返回值

  • 成功时返回指向 struct passwd 结构体的指针。
  • 失败时返回 NULL,并设置 errno。

getpwuid函数

struct passwd *getpwuid(uid_t uid);

功能:通过用户 ID 查询用户信息。
参数:uid:用户 ID。
返回值

  • 成功时返回指向 struct passwd 结构体的指针。
  • 失败时返回 NULL,并设置 errno。
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>

int main()
{
    // struct passwd *pwd = getpwnam("student");
    struct passwd *pwd = getpwuid(1000);
    if(NULL == pwd)
    {
        perror("getpwname error");
        return -1;
    }
    printf("name = %s uid = %d home = %s\n",pwd->pw_name,pwd->pw_uid,pwd->pw_dir);
}

10、getpwent、setpwent、endpwent函数

头文件

	   #include <sys/types.h>
       #include <pwd.h>

函数定义

  struct passwd *getpwent(void);

       void setpwent(void);

       void endpwent(void);

getpwent函数
功能:从用户数据库(就是etc/passwd中的用户信息)中逐条读取用户信息。
返回值

  • 成功时返回指向 struct passwd 结构体的指针。
  • 失败或到达文件末尾时返回 NULL,并设置 errno。
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>

int main()
{
    while (1)
    {
        struct passwd *pwd = getpwent(); //读取用户信息
        if(pwd == NULL)
            break;
        printf("name = %s uid = %d home = %s\n",pwd->pw_name,pwd->pw_uid,pwd->pw_dir);
    }
    
}

setpwent函数
功能

将用户数据库的读取位置重置到文件开头,以便重新开始遍历。

endpwent函数
功能

关闭用户数据库,释放相关资源。

#include <pwd.h>
#include <stdio.h>

int main() {
    struct passwd *pwd;

    // 第一次遍历
    printf("First pass:\n");
    while ((pwd = getpwent()) != NULL) {
        printf("User: %s\n", pwd->pw_name);
    }

    setpwent(); // 重置读取位置

    // 第二次遍历
    printf("\nSecond pass:\n");
    while ((pwd = getpwent()) != NULL) {
        printf("User: %s\n", pwd->pw_name);
    }

    endpwent(); // 关闭用户数据库
    return 0;
}

11、getspnam、getspent、setspent、endspent函数

头文件

 #include <shadow.h>
#include <shadow.h>

struct spwd {
    char *sp_namp;      // 用户名
    char *sp_pwdp;      // 加密后的密码
    long  sp_lstchg;    // 上次密码更改的日期(从1970-01-01开始的天数)
    long  sp_min;       // 密码最短有效天数
    long  sp_max;       // 密码最长有效天数
    long  sp_warn;      // 密码过期前的警告天数
    long  sp_inact;     // 密码过期后账户被锁定的天数
    long  sp_expire;    // 账户过期的日期(从1970-01-01开始的天数)
    unsigned long sp_flag; // 保留字段
};

函数定义

 struct spwd *getspnam(const char *name);

       struct spwd *getspent(void);

       void setspent(void);

       void endspent(void);

getspnam函数
功能

根据用户名从影子密码文件中查找并返回相应用户的密码信息。
参数:name:要查找的用户名。
返回值

  • 成功时返回指向 struct spwd 结构体的指针。
  • 失败时返回 NULL,并设置 errno。

getspent函数
功能
从影子密码文件中逐条读取用户密码信息。
返回值

  • 成功时返回指向 struct spwd 结构体的指针。
  • 失败或到达文件末尾时返回 NULL,并设置 errno。
#include <stdio.h>
#include <shadow.h>

int main()
{
#if 1
    struct spwd *pwd = getspnam("student");
    if(NULL == pwd)
    {
        perror("getspnam error");
        return -1;
    }

    printf("%s\n",pwd->sp_pwdp);
#endif
    // while(1)
    // {
    //     struct spwd *pwd = getspent();
    //     if(NULL == pwd)
    //         break;
    //     printf("%s %s\n",pwd->sp_namp,pwd->sp_pwdp);
    // }
}

setspent函数

将影子密码文件的读取位置重置到文件开头,以便重新开始遍历。

endspent函数

关闭影子密码文件并释放相关资源。

#include <shadow.h>
#include <stdio.h>

int main() {
    struct spwd *spwd;

    // 第一次遍历
    printf("First pass:\n");
    while ((spwd = getspent()) != NULL) {
        printf("Username: %s\n", spwd->sp_namp);
    }

    setspent(); // 重置读取位置

    // 第二次遍历
    printf("\nSecond pass:\n");
    while ((spwd = getspent()) != NULL) {
        printf("Username: %s\n", spwd->sp_namp);
    }

    endspent(); // 关闭影子密码文件
    return 0;
}

12、crypt 函数

头文件

 #include <unistd.h>

函数定义

char *crypt(const char *key, const char *salt);

参数

  • key:需要加密的字符串(通常是用户密码)。
  • salt:用于加密的“盐值”(salt),通常是一个长度为 2 的字符串。盐值用于确保相同的密码在使用不同的盐值时生成的加密结果不同。

返回值

  • 成功时返回指向加密后字符串的指针。
  • 失败时返回 NULL,并设置 errno。
#include <stdio.h>
#include <crypt.h>
#include <unistd.h>

int main()
{
    char *key = "888";   //需要加密的密码
    char *res = crypt(key, "$6$ZqEnV1wT$");

    if (res != NULL)
    {
        printf("res = %s\n", res);
    }
    else
    {
        perror("crypt error");
        return -1;
    }
}

相关文章:

  • 03_NLP常用的文本数据分析处理方法
  • elasticsearch 8.17.3部署文档
  • 『VUE』vue 引入Font Awesome图标库(详细图文注释)
  • 二、docker 存储
  • 文件系统调用(下) ─── linux第18课
  • 【redis】string应用场景:缓存功能和计数功能
  • UVa12303 Composite Transformations
  • c#客户端请求 Server-Sent Events
  • 音视频开发面试准备
  • Python组合数据类型(二)
  • Python字典,集合
  • Linux 网络:skb 数据管理
  • WEB实时推送消息的7种方式
  • 开发常用软件
  • C++设计模式-抽象工厂模式:从原理、适用场景、使用方法,常见问题和解决方案深度解析
  • Python 构建Flask网页端远程控制Windows系统功能
  • 基于Ollama平台部署的Qwen大模型实现聊天机器人
  • 计算机考研C语言
  • Docker搭建Redis哨兵模式【一主两从三哨兵】
  • 《TCP/IP网络编程》学习笔记 | Chapter 17:优于 select 的 epoll
  • 蓝佛安:中方将采取更加积极有为的宏观政策,有信心实现2025年的5%左右增长目标
  • 上海虹桥机场至北京首都机场快线试运行跨航司自愿签转服务
  • 【社论】跑赢12级狂风,敦煌做对了什么
  • 各地各部门贯彻落实习近平总书记重要指示精神坚决防范遏制重特大事故发生
  • 习近平给谢依特小学戍边支教西部计划志愿者服务队队员回信
  • “名额5分钟抢完”,一场花费上万元:越野赛凭什么这么火?