c - 读取密码文件时,程序从未遇到 EOF

标签 c linux unix

我目前正在编写的部分程序需要检查用户是否存在(这是针对 unix 系统的)。但是,在为用户读取密码文件时,永远不会到达 EOF,因此会形成无限循环。我可能做错了什么?

        int readPass;

        int userExists = 0; // 0 means user doesn't exist, 1 means they do

        // open password file to read in usernames
        int passFD = open("/etc/passwd", O_RDONLY);

        // open password file to read character by character
        FILE* pass = fopen("/etc/passwd", "r");

        // c-string to store usernames
        char passUser[100];

        // read through the password file
        while (readPass != EOF)
        {
            int numPass = 0;

            // read until reaching a colon, this is the username of the current line
            while (readPass != ':')
            {
                readPass = fgetc(pass);
                numPass += 1;
            }
            // store username is a c-string
            read(passFD, passUser, numPass);
            passUser[numPass - 1] = '\0';

            // if the user exists, stop checking
            if ((strcmp(passUser, argv[user])) == 0)
            {
                userExists = 1;
                break;
            }

            // read junk until next line
            readPass = NULL;
            int junksRead = 0;
            char passJunk[100];
            while (junksRead < 6)
            {
                numPass = 0;
                readPass = NULL;
                while (readPass != ':' && readPass != '\n' && readPass != EOF)
                {
                    readPass = fgetc(pass);
                    numPass += 1;
                    //printf("%c\n", readPass);
                }
                read(passFD, passJunk, numPass);
                junksRead += 1;
            }
        }

        // if the user doesn't exist, end the program
        if (userExists == 0)
        {
            printf("%s does not exist\n", argv[user]);
            return 0;
        }`

最佳答案

如果您没有其他选择,您可以通过自己阅读 /etc/passwd 文件来搜索用户。但是,更好的方法是使用可以为您执行此操作的现有函数:getpwnam()来自标题 pwd.h .无需重新发明已经完成和有效的东西。

可能的代码如下所示:

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

int main()
{
  //reset errno to be able to detect errors
  errno = 0;
  struct passwd * result = getpwnam("username");
  //Return value NULL could either mean error or "user not found".
  if (result == NULL)
  {
    //If errno is not zero, then an error occurred while getpwnam() was called.
    if (errno != 0)
      printf("An error occurred while searching for the user.\n");
    //Otherwise the user just does not exist.
    else
      printf("User does not exist!\n");
  }
  //Not NULL: user was found.
  else
  {
    printf("User exists.\n");
  }

  return 0;
}

关于c - 读取密码文件时,程序从未遇到 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40642390/

相关文章:

python - Linux Apache Django 视频静态文件问题

php - 在 Linux 机器上使用 PHP 上传文件

linux - 你如何在 Unix(或至少 Linux)中安全地读取内存?

c - fread 在c中用随机字符填充字符串

c - scanf 允许单词之间有空格

c - if then语句c编程

python - 在CENTOS上安装Google Cloud SDK : Not finding correct Python version

linux - 在 BASHRC 与命令行中启动后台进程之间的区别

linux - 如何将文件复制到时间戳自动生成的文件夹?

c - 从 C 中的 bin 文件中读取signed int 给出了错误的结果