Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / ACE / apps / drwho / File_Manager.cpp
blob2418da2ffc2e523fa07040866838010ce74d351a
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),
9 max_key_length (0),
10 buffer_ptr (0),
11 current_ptr (0),
12 buffer_size (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...
20 int
21 File_Manager::open_file (const char *filename)
23 return filename == 0
24 ? this->open_passwd_file ()
25 : this->open_friends_file (filename);
28 // Returns the next LOGIN_NAME and REAL_NAME from the file.
30 void
31 File_Manager::get_login_and_real_name (const char *&login_name, const char *&real_name)
33 char *buf_ptr = this->current_ptr;
35 login_name = buf_ptr;
37 // Skip to the end of the login name.
39 while (ACE_OS::ace_isalnum (*buf_ptr))
40 buf_ptr++;
42 *buf_ptr++ = '\0';
44 // Now skip over white space to *start* of real name!
46 while (ACE_OS::ace_isspace (*buf_ptr) || *buf_ptr == '\0')
47 buf_ptr++;
49 real_name = buf_ptr;
51 while (*buf_ptr++ != '\n')
52 continue;
54 // Clear the trailing blanks and junk.
56 for (char *tmp_ptr = buf_ptr - 1;
57 ACE_OS::ace_isspace (*tmp_ptr);
58 tmp_ptr--)
59 *tmp_ptr = '\0';
61 // Skip over consecutive blank lines.
63 while (*buf_ptr == '\n')
64 buf_ptr++;
66 this->current_ptr = buf_ptr;
69 // Open up the yp passwd file and slurp all the users in!
71 int
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");
78 if (fp == 0)
79 return -1;
81 passwd *pwent;
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, ',');
89 if (cp != 0)
90 *cp = '\0';
92 ACE_OS::fprintf (fp,
93 "%-8.8s %s\n",
94 pwent->pw_name,
95 pwent->pw_gecos);
96 this->number_of_friends++;
99 ACE_OS::endpwent ();
101 ACE_OS::fclose (fp);
103 if (this->mmap_.map (filename) == -1)
104 return -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
114 // space.
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.
126 pathname = filename;
127 else
129 directory[0] = '\0';
131 const char *home = ACE_OS::getenv ("HOME");
132 if (home != 0)
134 ACE_OS::strcat (directory, home);
135 ACE_OS::strcat (directory, "/");
137 ACE_OS::strcat (directory, filename);
140 // Do the mmap'ing.
142 if (this->mmap_.map (pathname) == -1)
143 return -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
155 if (*--cp == '\n')
157 this->number_of_friends++;
159 // Skip consecutive newlines.
160 while (cp[-1] == '\n')
161 --cp;
164 return this->number_of_friends;
167 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, File_Manager, ACE_Null_Mutex);