2016-11-16 6 views
0

私が現在書いているプログラムの一部は、ユーザが存在するかどうかをチェックする必要があります(これはUNIXシステム用です)。ただし、ユーザーのパスワードファイルを読み取っている間は、EOFには到達せず、無限ループを作成します。私は何が間違っていますか?パスワードファイルを読み取っている間、プログラムは決して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; 
     }` 
+0

?そして、あなたは 'EOF'をチェックしていない内部ファイル読み取りループをいくつか持っています。コードは完全な混乱です。 –

+0

これは何をすべきか: 'read(passFD、passUser、numPass);'?ところで:fgets()はあなたの友人です。 – wildplasser

+0

私はそれをcharとして持っていて、それを変更するのを忘れていたので、NULLがそこにあります。また、すべてのループがEOFをチェックする必要はなく、行の最後まで読み取るループのみが必要です。たとえば、ユーザー名を読み取るとEOFには到達しないので、チェックするのは無意味です。 – SPFort

答えて

1

もう一方のループ中に詰まっている可能性があります。あなたは他のオプションが用意されていない場合は、自分ですべて/etc/passwdファイルを読み込むことで、ユーザーの検索

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

ありがとう!それはうまくいった。私は、ユーザー名がファイルの最初のものなので、そこにEOFをチェックする必要はないと思っただけです。私は、ループの以前の反復でEOFが見つかった場合、外側のループがそれを捕まえるとも考えました。私は間違っていたと思います。 – SPFort

3

は、罰金です:ループは特に、EOFをチェックしている間に、それぞれ確認してください。しかし、より良いアプローチは、あなたのためにできる既存の機能を使用することです:getpwnam()pwd.hのヘッダから。すでに行われていることを再考する必要はありません。

可能なコードは次のようになります。なぜあなたは `int`に` NULL`を割り当てる

#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; 
} 
+0

これは私の将来のために残しておきますが、これは授業の授業のためのものです。私は可能な限り多くのものを自分で書くことになっていました。 – SPFort

+0

実際のプログラムでは、 'getpwnam'はあなたのために解析するだけでなく、システムがそのように設定されていればNIS、LDAPなどからアカウントを取得するので、' getpwnam'を使うべきです。 – zwol

関連する問題