language: C++ 4.7.2 (gcc-4.7.2)
date: 472 days 5 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
using namespace std;
 
//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.
 
class AccountInfo
{
private:
        char* _userLoginName; // store the login name of the user
        char* _password; // explained later
        unsigned int _uid; //user identifier
        unsigned int _gid; // identifier of user’s primary group
        char* _gecos; // general info of the user
        char* _home; // home directory of the user
        char* _shell; // shell of the user
        // other private methods necessary for this class
public:
        AccountInfo();
        AccountInfo(char [], char [], int, int, char [], char [], char []);
 
        // constructors(empty and non-empty), destructor
        // ostream operator<<
        //      output to ostream using the following format 
        //
        //      Login: [_userLoginName]
        //      Directory: [_home]
        //      Shell: [_shell]
        //      Gecos: [_gecos]
        //
        // and other public methods (mutators/setters, accessors/getters)
};
 
AccountInfo::AccountInfo(){
        _userLoginName = new char[9];
        _password = new char[17];
        _uid;
        _gid;
        _gecos;
        _home;
        _shell;
}
AccountInfo::AccountInfo(char userLoginName[], char password[], int uid, int gid, char gecos[], char home[], char shell[]){
        _userLoginName = userLoginName;
        _password = password;
        _uid = uid;
        _gid = gid;
        _gecos = gecos;
        _home = home;
        _shell = shell;
}
 
class UserDB
{
private:
        AccountInfo* _accounts[200]; // store up to 200 accounts
        unsigned int _size; // number of account stored
        unsigned int _nextUid; // next user id to be assigned
        unsigned int _defaultGid; // default group id
        // other private methods necessary for this class
public:
        // constructors(empty), destructor
        // Note: since the objects stored in _accounts are dynamically
        // allocated, you need delete the objects stored in it in
        // the destructor
        // ostream operator<< // print users in ‘/etc/passwd’ format
        // for the fields that are missing,
        // don’t print out anything. The only
        // exception is ‘x’ will be shown if
        // password is not set.
        void adduser( AccountInfo* newUser); // add a new user to
        //      _accounts, also increment _size.
        // print out the following message after the
        // user is added:
        // “[userLoginName] with      [uid]   is added.”
        void    showUsers();
        //      print out “List of users:” at the first line, then print out all user login names
        //      (one line each user login name), then print out the following at the end according to the number of users stored
        //      0 => “There’s no users found in the system.”
        //      1 => “1 user found in the system.”
        // k => “k users found in the system.” for k>1
        void    showPasswd(); // call the ostream operator
        void    finger(char* userLoginName);    // call ostream operator of AccountInfo class
        int     size(); // return the number of accounts stored (_size)
        // and other public methods (mutators/setters, accessors/getters)
};
 
void emptyString(char* token, int size) {
        for (int i=0; i < size; i++) token[i] = '\0';
}
 
void setDefaults(char uloginName[], char *home_directory, char *password, char *shell, char *gecos) {
        emptyString(home_directory, sizeof(home_directory) - 1);
        strcpy(home_directory, "home/home/");
        strcat(home_directory, uloginName);
 
        //emptyString(password, sizeof(password) - 1);
        
        //emptyString(shell, sizeof(shell) - 1);
 
        //shell = "/bin/bash";
        //cout << sizeof(shell) << endl;
 
        //emptyString(gecos, sizeof(gecos) - 1);
}
 
int getNextToken(char* buffer, char* token, int startPos, int bufSize, int tokenSize, char delimeter)
{
        int i, j;
 
        emptyString (token, tokenSize);
 
        i = startPos;
        j = 0;
 
        while ((buffer[i] == ' ') && (i < bufSize)) i++; //skipblanks
        if (i < 256) {
                while ((buffer[i] != delimeter) && (i < 256) && (j < tokenSize))
                        token[j++] = buffer[i++];
        }
        return i;
}
 
int main()
{
        char buffer[256];
        AccountInfo *tempAccount;
        UserDB users;
        char uloginName[9];
        char homeDirectory[33];
        int userID;
        int groupID;
        char password[17];
        char shell[17];
        char gecos[65];
        int i, j, k;
        char flag[3];
        char IDString[6];
        char command[11];
        char blank = ' ';
 
        //      Then add it to “users” by calling adduser(...)
        // if it is a “finger” command, print out the account info for // he specified user using the ostream operator of AccountInfo
        // if it is “showusers”, print out all the users using the // ostream operator of UserDB class. (call showUsers() )
        // if it is “showpasswd”, print out all the users in /etc/passwd // format using the print_passwd() method of UserDB class // (call showPasswd())
        // consume all the white spaces ( such as ‘\n’, ‘\r’ ...)
        while (!cin.eof()) {
                cin.getline(buffer,256);
                cout << buffer << endl;
                k = getNextToken(buffer,command,0,256,10,blank);
                switch(command[0]) {
                case 'a': {
                        cout << "Add User" << endl;
                        k = getNextToken(buffer, uloginName,k,256,8, blank);
                        cout << "****** " << uloginName << endl;
                        while (k < 256) {
                                k = getNextToken(buffer,flag,k,256,2, blank);
                                if (k < 256) {
                                        userID = 0;
                                        groupID = 0;
                                        setDefaults(uloginName, homeDirectory, password, shell, gecos);
                                        switch (flag[1]) {
                                        case 'd': k = getNextToken(buffer,homeDirectory,k,256,32, blank);
                                                cout << "****** " << flag << endl;
                                                cout << "****** " << homeDirectory << endl;
                                                break;
                                        case 's': k = getNextToken(buffer,shell,k,256,16, blank);
                                                cout << "****** " << flag << endl;
                                                cout << "****** " << shell << endl;
                                                break;
                                        case 'p': k = getNextToken(buffer,password,k,256,16, blank);
                                                cout << "****** " << flag << endl;
                                                cout << "****** " << password << endl;
                                                break;
                                        case 'c': k = getNextToken(buffer,gecos,k,256,64, '-');
                                                cout << "****** " << flag << endl;
                                                cout << "****** " << gecos << endl;
                                                break;
                                        case 'u': k = getNextToken(buffer,IDString,k,256,5, blank);
                                                userID = atoi(IDString);
                                                cout << "****** " << flag << endl;
                                                cout << "****** " << IDString << "=" << userID << endl;
                                                break;
                                        case 'g': k = getNextToken(buffer,IDString,k,256,5, blank);
                                                groupID = atoi(IDString);
                                                cout << "****** " << flag << endl;
                                                cout << "****** " << IDString << "= " << groupID << endl;
                                                break;
                                        default: k = 256; break;
                                        } //end Switch(flag[1])
                                } // end while (k < 256)
                        }
                        break;
                                  } //end of adduser
                case '#': {cout << "Comment" << endl; break;}
                case 's': {
                        if (command[4] == 'u') {
                                cout << "Show users" << endl;
                        }
                        else if (command[4] = 'p') {
                                cout << "Show password" << endl;
                        }
                        break;}
                case 'f': {cout << "finger" << endl; break;}
                default: cout << "Command not found!!" << endl;
                }
        }
        return 0;
}
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’
  • result: Compilation error (maybe you wish to see an example for C++ 4.7.2)    

    #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
The "input" is a file set in the project parameters so the project runs by itself without user intervention.