WEBVTT 00:00.940 --> 00:06.940 Welcome, happy assault, the challenge, if you didn't, please do so after this course, you won't 00:06.940 --> 00:08.290 remember anything about school. 00:08.740 --> 00:10.630 You can only learn it if you work on it. 00:11.020 --> 00:11.390 All right. 00:11.410 --> 00:12.880 Now let's take a look at the solution. 00:17.090 --> 00:21.360 First thing I need to do is to get the user name and the password from the command line. 00:22.220 --> 00:24.380 Normally you shouldn't get this from the command line. 00:24.770 --> 00:28.700 Operating systems can record this kind of information, but for now, it's OK. 00:28.970 --> 00:34.190 OK, let's first store the oil that are extreme slice into a variable because I'm going to use it a 00:34.190 --> 00:34.460 lot. 00:35.360 --> 00:40.910 Like I said, this program expects a username and password, so there should be three arguments, right? 00:41.180 --> 00:44.300 Remember that the first argument is the path to the running program. 00:45.250 --> 00:52.140 So I'm going to check whether there are three arguments or not like this, if land are not equal to 00:52.140 --> 00:52.510 the three. 00:53.260 --> 01:00.040 OK, so if there are less than three arguments that display a usage message like this one, I'm going 01:00.040 --> 01:01.000 to call Princella. 01:03.110 --> 01:06.200 Then I'm going to type usage username. 01:09.190 --> 01:10.040 And password. 01:10.450 --> 01:10.840 OK. 01:11.780 --> 01:17.630 And let's just return from the main function, directly visible, terminate the program because the 01:17.630 --> 01:20.100 program ends, then the main function returns. 01:20.570 --> 01:23.200 It is the only function that executes a program. 01:23.510 --> 01:24.620 That's the reason of that. 01:25.220 --> 01:26.350 OK, let's try it now. 01:27.210 --> 01:32.970 I'm going to type go around mean that go OK, as you can see, the program prints the usage information 01:32.970 --> 01:35.510 because I didn't provide any input to it. 01:36.550 --> 01:39.640 This past Twitter user and a password now like this. 01:40.880 --> 01:48.770 Go around in that go, Richard Pyper OK, now it doesn't say anything good, this is good because since 01:48.770 --> 01:51.470 there are three arguments inside start slice. 01:51.650 --> 01:54.380 So the if statements block is an executed. 01:55.800 --> 02:02.880 All right, let's continue now, let's save the username into these ueber so it's the second element, 02:03.090 --> 02:06.590 that's why I'm going to get it from the first element. 02:07.380 --> 02:09.870 Now I'm going to check for a single user, Jack. 02:10.780 --> 02:16.150 OK, this first check, whether the user is Jack or not like this, if you. 02:19.170 --> 02:20.550 Not equal to Jack. 02:21.210 --> 02:21.590 OK. 02:22.860 --> 02:29.100 And if the user is not Jack, then display an error message like this and will use Prenter for that. 02:32.260 --> 02:35.440 This first type exists tonight for. 02:37.020 --> 02:42.090 Person Q This will wrap the string inside double cuts and new line. 02:43.050 --> 02:47.010 OK, then I'm going to pass the other one to display the user name again. 02:47.610 --> 02:49.190 OK, now let's run the program again. 02:50.860 --> 02:56.600 It says that the user, Richard, isn't allowed to pass good, let's pass Jack instead. 02:59.470 --> 03:03.910 Now, it doesn't say anything, it's because the program has recognized the user. 03:04.600 --> 03:06.340 OK, let's continue now. 03:06.340 --> 03:09.020 Let's save the passwords into the P variable. 03:09.260 --> 03:12.070 OK, as you can see, this is a short declaration. 03:12.790 --> 03:15.850 ARC's one, here is the username and ARC's two. 03:15.850 --> 03:17.140 Here is the password. 03:17.710 --> 03:22.900 So you start the past user name and P stores the past passwords. 03:24.140 --> 03:30.470 OK, now let's check whether the password is equal to eighteen eighty eight or not, just using a not 03:30.470 --> 03:33.110 equal operator like this elusive. 03:35.320 --> 03:37.240 P, not equal to 1888. 03:38.380 --> 03:43.400 OK, this as if Branch will only run if this first condition is false, right? 03:44.080 --> 03:47.710 So this as if will run if the user is correct. 03:49.430 --> 03:54.230 But if the password isn't correct, then let's display an error message again like this. 03:58.140 --> 04:04.810 Invalid passwords for the user new line, then I'm going to pass the user, OK? 04:05.610 --> 04:13.140 All right, that right now, check by bring in it prints, this was a wrong password for the user Jack 04:13.400 --> 04:13.770 crew. 04:14.520 --> 04:20.160 Normally this is a security flaw to tell about this, because you shouldn't tell that there was a user 04:20.160 --> 04:21.700 with the same name or not. 04:22.380 --> 04:24.270 So please don't try this at all. 04:24.430 --> 04:28.230 OK, now let's try it with the correct password like this. 04:28.440 --> 04:29.870 Jack 1888. 04:30.630 --> 04:37.230 The program becomes silent again is because it has accepted the password nahles and asked Branch to 04:37.230 --> 04:47.730 this if statement to tell that access is granted like this as printf access granted to Q OK and the 04:47.730 --> 04:48.090 user. 04:49.030 --> 04:49.430 Right. 04:50.470 --> 04:57.340 So the program first checks whether the username is equal to Jack, and if not, then it drops the first 04:57.340 --> 04:58.170 if statement. 04:58.720 --> 05:02.270 So if it equals to Jack, then it checks the active branch. 05:02.920 --> 05:08.020 So if the password isn't equal to 1888, it runs to Altuve branch. 05:08.950 --> 05:14.860 And lastly, if the password equals to eighteen eighty eight, then it means that both the user and 05:14.860 --> 05:20.260 the password are correct and it runs to as French did, is simply how it works. 05:20.620 --> 05:23.010 OK, so let's run the program again. 05:24.250 --> 05:27.070 Well, the access is granted, Cuke, I mean. 05:28.070 --> 05:30.890 What do you think can improve this program further? 05:31.800 --> 05:36.960 Maybe so this trial factoring this program a little bit by using constants now. 05:38.280 --> 05:43.740 I know that these error messages won't change, so it's better to put them into Constance. 05:44.820 --> 05:51.600 As far as Copiah, the usage message from here like this and this type here usage instead. 05:52.880 --> 05:57.570 Dallas create a constant like this constant usage is posted here. 05:57.600 --> 05:57.990 OK. 05:59.300 --> 06:02.420 OK, now do the same thing for the existing right message. 06:03.320 --> 06:04.580 I'm going to cut it from here. 06:07.050 --> 06:13.110 I'm going to change it to our user, then I'm going to declare a new constant. 06:14.450 --> 06:15.320 Names are you? 06:15.860 --> 06:17.390 I'm going to pass the value here. 06:18.530 --> 06:18.920 OK. 06:20.650 --> 06:23.610 Right now, do the same thing for the incorrect password message. 06:23.650 --> 06:25.000 I'm going to just get it from here. 06:26.980 --> 06:29.930 SPW, the OK. 06:31.360 --> 06:34.000 Let's declare a new constant basis here. 06:34.200 --> 06:40.270 All right, and now let's change the access granted message that constant again like this. 06:40.520 --> 06:45.090 OK, cut it from here and let's change the axis, OK? 06:46.350 --> 06:48.320 And declare success, OK? 06:49.730 --> 06:50.790 This bastard here. 06:51.230 --> 06:51.960 OK, good. 06:52.610 --> 06:57.270 And lastly, let's refactor the user and password into constants. 06:57.320 --> 06:57.680 OK. 06:59.220 --> 06:59.970 User. 07:05.280 --> 07:06.090 And Jack. 07:10.310 --> 07:11.060 Password. 07:15.360 --> 07:16.590 OK, let me right now. 07:21.130 --> 07:23.500 All right, it displays the usage cool. 07:24.770 --> 07:27.760 Let's give it an incorrect user, for example, my name. 07:27.800 --> 07:29.190 One, two, three, four. 07:30.080 --> 07:31.880 It works right now. 07:31.890 --> 07:34.670 Let's give it a correct user, but an incorrect password. 07:35.320 --> 07:35.780 Check. 07:35.810 --> 07:36.980 One, two, three, four. 07:37.880 --> 07:39.000 All right, good. 07:39.470 --> 07:40.090 It works. 07:40.730 --> 07:43.170 Now, let's pass the correct username and password. 07:43.490 --> 07:44.720 Eighteen eighty eight. 07:46.030 --> 07:48.060 Cool, everything works right there. 07:48.100 --> 07:50.050 So for now, see in the next lecture.