fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //This is a project that allos the user to add, delete, modify or print users in the system and also be able to print out the user database.
  5.  
  6. class AccountInfo
  7. {
  8. private:
  9. char* _userLoginName; // store the login name of the user
  10. char* _password; // explained later
  11. unsigned int _uid; //user identifier
  12. unsigned int _gid; // identifier of user’s primary group
  13. char* _gecos; // general info of the user
  14. char* _home; // home directory of the user
  15. char* _shell; // shell of the user
  16. // other private methods necessary for this class
  17. public:
  18. AccountInfo();
  19. AccountInfo(char [], char [], int, int, char [], char [], char []);
  20.  
  21. // constructors(empty and non-empty), destructor
  22. // ostream operator<<
  23. // output to ostream using the following format
  24. //
  25. // Login: [_userLoginName]
  26. // Directory: [_home]
  27. // Shell: [_shell]
  28. // Gecos: [_gecos]
  29. //
  30. // and other public methods (mutators/setters, accessors/getters)
  31. };
  32.  
  33. AccountInfo::AccountInfo(){
  34. _userLoginName = new char[9];
  35. _password = new char[17];
  36. _uid;
  37. _gid;
  38. _gecos;
  39. _home;
  40. _shell;
  41. }
  42. AccountInfo::AccountInfo(char userLoginName[], char password[], int uid, int gid, char gecos[], char home[], char shell[]){
  43. _userLoginName = userLoginName;
  44. _password = password;
  45. _uid = uid;
  46. _gid = gid;
  47. _gecos = gecos;
  48. _home = home;
  49. _shell = shell;
  50. }
  51.  
  52. class UserDB
  53. {
  54. private:
  55. AccountInfo* _accounts[200]; // store up to 200 accounts
  56. unsigned int _size; // number of account stored
  57. unsigned int _nextUid; // next user id to be assigned
  58. unsigned int _defaultGid; // default group id
  59. // other private methods necessary for this class
  60. public:
  61. // constructors(empty), destructor
  62. // Note: since the objects stored in _accounts are dynamically
  63. // allocated, you need delete the objects stored in it in
  64. // the destructor
  65. // ostream operator<< // print users in ‘/etc/passwd’ format
  66. // for the fields that are missing,
  67. // don’t print out anything. The only
  68. // exception is ‘x’ will be shown if
  69. // password is not set.
  70. void adduser( AccountInfo* newUser); // add a new user to
  71. // _accounts, also increment _size.
  72. // print out the following message after the
  73. // user is added:
  74. // “[userLoginName] with [uid] is added.”
  75. void showUsers();
  76. // print out “List of users:” at the first line, then print out all user login names
  77. // (one line each user login name), then print out the following at the end according to the number of users stored
  78. // 0 => “There’s no users found in the system.”
  79. // 1 => “1 user found in the system.”
  80. // k => “k users found in the system.” for k>1
  81. void showPasswd(); // call the ostream operator
  82. void finger(char* userLoginName); // call ostream operator of AccountInfo class
  83. int size(); // return the number of accounts stored (_size)
  84. // and other public methods (mutators/setters, accessors/getters)
  85. };
  86.  
  87. void emptyString(char* token, int size) {
  88. for (int i=0; i < size; i++) token[i] = '\0';
  89. }
  90.  
  91. void setDefaults(char uloginName[], char *home_directory, char *password, char *shell, char *gecos) {
  92. emptyString(home_directory, sizeof(home_directory) - 1);
  93. strcpy(home_directory, "home/home/");
  94. strcat(home_directory, uloginName);
  95.  
  96. //emptyString(password, sizeof(password) - 1);
  97.  
  98. //emptyString(shell, sizeof(shell) - 1);
  99.  
  100. //shell = "/bin/bash";
  101. //cout << sizeof(shell) << endl;
  102.  
  103. //emptyString(gecos, sizeof(gecos) - 1);
  104. }
  105.  
  106. int getNextToken(char* buffer, char* token, int startPos, int bufSize, int tokenSize, char delimeter)
  107. {
  108. int i, j;
  109.  
  110. emptyString (token, tokenSize);
  111.  
  112. i = startPos;
  113. j = 0;
  114.  
  115. while ((buffer[i] == ' ') && (i < bufSize)) i++; //skipblanks
  116. if (i < 256) {
  117. while ((buffer[i] != delimeter) && (i < 256) && (j < tokenSize))
  118. token[j++] = buffer[i++];
  119. }
  120. return i;
  121. }
  122.  
  123. int main()
  124. {
  125. char buffer[256];
  126. AccountInfo *tempAccount;
  127. UserDB users;
  128. char uloginName[9];
  129. char homeDirectory[33];
  130. int userID;
  131. int groupID;
  132. char password[17];
  133. char shell[17];
  134. char gecos[65];
  135. int i, j, k;
  136. char flag[3];
  137. char IDString[6];
  138. char command[11];
  139. char blank = ' ';
  140.  
  141. // Then add it to “users” by calling adduser(...)
  142. // if it is a “finger” command, print out the account info for // he specified user using the ostream operator of AccountInfo
  143. // if it is “showusers”, print out all the users using the // ostream operator of UserDB class. (call showUsers() )
  144. // if it is “showpasswd”, print out all the users in /etc/passwd // format using the print_passwd() method of UserDB class // (call showPasswd())
  145. // consume all the white spaces ( such as ‘\n’, ‘\r’ ...)
  146. while (!cin.eof()) {
  147. cin.getline(buffer,256);
  148. cout << buffer << endl;
  149. k = getNextToken(buffer,command,0,256,10,blank);
  150. switch(command[0]) {
  151. case 'a': {
  152. cout << "Add User" << endl;
  153. k = getNextToken(buffer, uloginName,k,256,8, blank);
  154. cout << "****** " << uloginName << endl;
  155. while (k < 256) {
  156. k = getNextToken(buffer,flag,k,256,2, blank);
  157. if (k < 256) {
  158. userID = 0;
  159. groupID = 0;
  160. setDefaults(uloginName, homeDirectory, password, shell, gecos);
  161. switch (flag[1]) {
  162. case 'd': k = getNextToken(buffer,homeDirectory,k,256,32, blank);
  163. cout << "****** " << flag << endl;
  164. cout << "****** " << homeDirectory << endl;
  165. break;
  166. case 's': k = getNextToken(buffer,shell,k,256,16, blank);
  167. cout << "****** " << flag << endl;
  168. cout << "****** " << shell << endl;
  169. break;
  170. case 'p': k = getNextToken(buffer,password,k,256,16, blank);
  171. cout << "****** " << flag << endl;
  172. cout << "****** " << password << endl;
  173. break;
  174. case 'c': k = getNextToken(buffer,gecos,k,256,64, '-');
  175. cout << "****** " << flag << endl;
  176. cout << "****** " << gecos << endl;
  177. break;
  178. case 'u': k = getNextToken(buffer,IDString,k,256,5, blank);
  179. userID = atoi(IDString);
  180. cout << "****** " << flag << endl;
  181. cout << "****** " << IDString << "=" << userID << endl;
  182. break;
  183. case 'g': k = getNextToken(buffer,IDString,k,256,5, blank);
  184. groupID = atoi(IDString);
  185. cout << "****** " << flag << endl;
  186. cout << "****** " << IDString << "= " << groupID << endl;
  187. break;
  188. default: k = 256; break;
  189. } //end Switch(flag[1])
  190. } // end while (k < 256)
  191. }
  192. break;
  193. } //end of adduser
  194. case '#': {cout << "Comment" << endl; break;}
  195. case 's': {
  196. if (command[4] == 'u') {
  197. cout << "Show users" << endl;
  198. }
  199. else if (command[4] = 'p') {
  200. cout << "Show password" << endl;
  201. }
  202. break;}
  203. case 'f': {cout << "finger" << endl; break;}
  204. default: cout << "Command not found!!" << endl;
  205. }
  206. }
  207. return 0;
  208. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
#This line is a comment
#Add some users
adduser lisa
adduser hicks -c Master Weaponsmith
adduser randal -c Riding Trainer
adduser john -u 2001 -g 1002 -p john123 -c Project Work
adduser Andy -p Andy123 -g 1002 -c Andy Project Work
adduser nick
adduser steve -p steveK1234 -c steve study and  Work
adduser samantha -d /home/work/samantha -g 1002 -p samR12345
adduser smith -g 1002
adduser styne -p sssstyne6781123 -g 1002
adduser morkel -p sssstyne67morkel -c Study and Research Work
adduser colin -c Study and Research Work
adduser paul -c Study and Research Work
adduser allan -p sssstynepassword
adduser sam -d /home/work/sam -g 1002 -p sampwdR12345 -c sam project work
# show in passwd file format
showpasswd
# show the user login names
showusers
# query the user with the name 'lisa'
finger lisa
# query the user with the name 'matt'
finger matt
# query the user with the name 'samantha'
finger samantha
# query the user with the name 'allan'
finger allan
# query the user with the name 'morkel'
finger morkel
# query the user with the name 'sam'
finger sam
# add more users
adduser matt -d /home/work/matt -g 1002 -c Matt doing administrator job
adduser mark -d /home/student/mark -c computer science student 
adduser broad
adduser adam -d /home/student/adam -p ni123k -c computer science student 
adduser kevin -d /home/student/kevin -p kevin23k -c computer science student
adduser taylor
# query the user with the name 'matt'
finger matt
# query the user with the name 'Broad'
finger Broad
# query the user with the name 'broad'
finger broad
# show the user login names
showusers
# add some more users 
adduser drusilla -d /home/trainers/drusilla -u 2001 -c Warlock Trainer
adduser wilhelm -p mypassword -d /home/trainers/wilhelm -c Paladin Trainer
adduser paxton -c Librarian
adduser matthew -p matthewpassword
adduser marry -d /home/project/marry -g 1002 -p marryjohn123 -c Project and Research Work
# query the user with name 'wilhelm'
finger wilhelm
# query the user with name 'matthew'
finger matthew
# show in passwd file format
showpasswd
compilation info
prog.cpp: In constructor ‘AccountInfo::AccountInfo()’:
prog.cpp:36: warning: statement has no effect
prog.cpp:37: warning: statement has no effect
prog.cpp:38: warning: statement has no effect
prog.cpp:39: warning: statement has no effect
prog.cpp:40: warning: statement has no effect
prog.cpp: In function ‘void setDefaults(char*, char*, char*, char*, char*)’:
prog.cpp:93: error: ‘strcpy’ was not declared in this scope
prog.cpp:94: error: ‘strcat’ was not declared in this scope
prog.cpp: In function ‘int main()’:
prog.cpp:179: error: ‘atoi’ was not declared in this scope
prog.cpp:199: warning: suggest parentheses around assignment used as truth value
prog.cpp:126: warning: unused variable ‘tempAccount’
prog.cpp:127: warning: unused variable ‘users’
prog.cpp:135: warning: unused variable ‘i’
prog.cpp:135: warning: unused variable ‘j’
stdout

Standard output is empty