在读取密码文件时,程序永远不会遇到EOF

时间:2016-11-16 21:11:10

标签: 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;
        }`

2 个答案:

答案 0 :(得分:3)

如果您没有其他选择,通过自己阅读/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;
}

答案 1 :(得分:1)

你可能会陷入其中一个循环中。确保每个while循环都检查EOF,特别是:

while (readPass != ':' && readPass != EOF)
            {
                readPass = fgetc(pass);
                numPass += 1;
            }