AcessNative - Better error reporting in NativeFS
[tpg/acess2.git] / Usermode / Applications / login_src / main.c
1 /*
2  * Acess 2 Login
3  */
4 #include "header.h"
5
6 // === CONSTANTS ===
7 #define BUFLEN  1024
8
9 // === PROTOTYPES ===
10 char    *GetUsername();
11 char    *GetPassword();
12
13 // === CODE ===
14 int main(int argc, char *argv[])
15 {
16         char    *sUsername, *sPassword;
17          int    pid, uid = 0;
18          int    status = 0;
19         tUserInfo       *uinfo;
20
21         printf("\x1B[?25h");    // Re-enable the cursor 
22 //      printf("\x1B[2J");      // Clear Screen
23
24         for(;;)
25         {
26                 // Validate User
27                 for(;;)
28                 {
29                         sUsername = GetUsername();
30                         sPassword = GetPassword();
31                         if( (uid = ValidateUser(sUsername, sPassword)) == -1 )
32                         {
33                                 printf("\nInvalid username or password\n");
34                                 free(sUsername);
35                                 free(sPassword);
36                         }
37                         else
38                                 break;
39                 }
40                 printf("\n");
41
42                 uinfo = GetUserInfo(uid);
43                 struct s_sys_spawninfo  spawninfo;
44                 spawninfo.flags = 0;
45                 spawninfo.gid = uinfo->GID;
46                 spawninfo.uid = uinfo->UID;
47                 const char      *child_argv[2] = {"-", 0};
48                 const char      **child_envp = NULL;
49                  int    fds[] = {0, 1, 2};
50                 pid = _SysSpawn(uinfo->Shell, child_argv, child_envp, 3, fds, &spawninfo);
51                 // Error check
52                 if(pid == -1) {
53                         fprintf(stderr, "Unable to fork the login process!\n");
54                         return -1;
55                 }
56                 
57                 // Wait for child to terminate
58                 _SysWaitTID(pid, &status);
59         }
60         
61         return 0;
62 }
63
64 /**
65  * \fn char *GetUsername()
66  */
67 char *GetUsername()
68 {
69         char    ret[BUFLEN] = {0};
70          int    pos = 0;
71         char    ch;
72         
73         // Prompt the user
74         printf("Username: ");
75         fflush(stdout);
76         
77         // Read in text
78         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
79         {
80                 if(ch == '\b') {
81                         if( pos <= 0 )  continue;
82                         pos --;
83                         ret[pos] = '\0';
84                 }
85                 else
86                         ret[pos++] = ch;
87                 
88                 // Echo out to the screen
89                 fputc(ch, stdout);
90                 fflush(stdout);
91                 
92                 if(pos == BUFLEN-1)     break;
93         }
94         
95         // Finish String
96         ret[pos] = '\0';
97         
98         printf("\n");
99         return strdup(ret);
100 }
101
102 /**
103  * \fn char *GetPassword()
104  */
105 char *GetPassword()
106 {
107         char    ret[BUFLEN];
108          int    pos = 0;
109         char    ch;
110         
111         // Prompt the user
112         printf("Password: ");
113         fflush(stdout);
114         
115         // Read in text
116         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
117         {
118                 if(ch == '\b') {
119                         if( pos <= 0 )  continue;
120                         pos --;
121                         ret[pos] = '\0';
122                 }
123                 else
124                         ret[pos++] = ch;
125                 
126                 // Don't echo out to the screen
127                 //fputc(stdout, ch);
128                 
129                 if(pos == BUFLEN-1)     break;
130         }
131         
132         ret[pos] = '\0';
133         
134         printf("\n");
135         return strdup(ret);
136 }

UCC git Repository :: git.ucc.asn.au