1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_pwd.h"
3 #include "ace/OS_NS_string.h"
4 #include "ace/OS_NS_ctype.h"
5 #include "File_Manager.h"
7 File_Manager::File_Manager ()
8 : number_of_friends (0),
16 // Either opens the friends file (if FILENAME is not a NULL pointer)
17 // or opens up the password file. In either case, the number of
18 // entries in the file are returned, i.e., number of friends...
21 File_Manager::open_file (const char *filename
)
24 ? this->open_passwd_file ()
25 : this->open_friends_file (filename
);
28 // Returns the next LOGIN_NAME and REAL_NAME from the file.
31 File_Manager::get_login_and_real_name (const char *&login_name
, const char *&real_name
)
33 char *buf_ptr
= this->current_ptr
;
37 // Skip to the end of the login name.
39 while (ACE_OS::ace_isalnum (*buf_ptr
))
44 // Now skip over white space to *start* of real name!
46 while (ACE_OS::ace_isspace (*buf_ptr
) || *buf_ptr
== '\0')
51 while (*buf_ptr
++ != '\n')
54 // Clear the trailing blanks and junk.
56 for (char *tmp_ptr
= buf_ptr
- 1;
57 ACE_OS::ace_isspace (*tmp_ptr
);
61 // Skip over consecutive blank lines.
63 while (*buf_ptr
== '\n')
66 this->current_ptr
= buf_ptr
;
69 // Open up the yp passwd file and slurp all the users in!
72 File_Manager::open_passwd_file ()
74 char *filename
= const_cast<char *> ("passwd-XXXXXX");
75 ACE_HANDLE f
= ACE_OS::mkstemp (filename
);
76 FILE *fp
= ACE_OS::fdopen (f
, "w");
83 for (ACE_OS::setpwent ();
84 (pwent
= ACE_OS::getpwent ()) != 0; )
85 if (*pwent
->pw_gecos
!= '\0')
87 char *cp
= ACE_OS::strchr (pwent
->pw_gecos
, ',');
96 this->number_of_friends
++;
103 if (this->mmap_
.map (filename
) == -1)
106 this->buffer_ptr
= (char *) this->mmap_
.addr ();
108 this->buffer_size
= this->mmap_
.size ();
109 this->current_ptr
= this->buffer_ptr
;
110 return this->number_of_friends
;
113 // This function opens up FILENAME and memory maps it in our address
117 File_Manager::open_friends_file (const char *filename
)
119 char directory
[MAXPATHLEN
];
120 const char *pathname
= directory
;
122 // See if we've got a filename or a pathname (i.e., directory/filename).
124 if (ACE_OS::strrchr (filename
, '/') != 0)
125 // We've got a complete pathname.
131 const char *home
= ACE_OS::getenv ("HOME");
134 ACE_OS::strcat (directory
, home
);
135 ACE_OS::strcat (directory
, "/");
137 ACE_OS::strcat (directory
, filename
);
142 if (this->mmap_
.map (pathname
) == -1)
145 this->buffer_ptr
= (char *) this->mmap_
.addr ();
147 this->buffer_size
= this->mmap_
.size ();
148 this->current_ptr
= this->buffer_ptr
;
150 // Determine how many friends there are by counting the newlines.
152 for (char *cp
= this->buffer_ptr
+ this->buffer_size
;
153 cp
> this->buffer_ptr
157 this->number_of_friends
++;
159 // Skip consecutive newlines.
160 while (cp
[-1] == '\n')
164 return this->number_of_friends
;
167 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton
, File_Manager
, ACE_Null_Mutex
);