summaryrefslogtreecommitdiff
path: root/README.md
blob: 31c4aeedac0f1e2c641389332a79d0e41e32c2f9 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# COMP-325 Assignment 1

For Assignment 1, you are going to complete the following learning activities taken from your textbook.
Use the script command to save the terminal session wherever necessary. Saving the terminal starts with the script filename command and ends with exit.

Combine all required problems in this set in one file, name it `COMP325_1_YYMM`
(replacing ‘YYMM’ with the current year and month, e.g., 1309),
and upload it here to submit it to your tutor for marking and feedback.

Be sure to complete the final step—click on the Send for Marking button to notify your tutor.

## Solve problems 7–11 and 15–17 at the end of Chapter 7.

7. Give three commands that you can use to list the absolute pathname of your home directory.

```bash
モ echo $HOME
/home/mokha
モ echo ~/
/home/mokha
モ cd
モ pwd
/home/mokha
```

See `exercises/chapter-7.7`.

8.  Write down the line in the /etc/passwd file on your system that contains information about your login. What are your login shell, user ID, home directory, and group ID? Does your system contain the encrypted password in the /etc/passwd or /etc/shadow file?

```text
$ grep mokha /etc/passwd
mokha:x\:1000:1000:mo:/home/mokha:/bin/bash

$ grep mokha /etc/shadow
mokha:password:18036:0:99999:7:::
```

* user id:  1000
* group id: 1000
* `$HOME`: /home/mo
* shell: /bin/bash
* Password is stored in `/etc/shadow`

See `exercises/chapter-7.8`.

9.  What would happen if the last field of the line in the `/etc/passwd` file were replaced with `/usr/bin/date`? Why?

The login shell would become the `date` command. The `date` program prints the date. This would print the date and end.

```bash
も date
Wed May 22 20:07:38 MDT 2019
```

10.  What are the inode numbers of the root and your home directories on your machine? Give the commands that you used to find these inode numbers.

```bash
ls -id /
ls -id $HOME
```

See `exercises/chapter-7.10`.

11.  Create a directory, called `memos`, in your home directory. Go into this directory and create a file `memo.james` by using one of the editors discussed in Chapter 5. Give three pathnames for this file.

```bash
bash-3.2$ cd
bash-3.2$ mkdir -p memos
bash-3.2$ cd memos/
bash-3.2$ vim memo.james
bash-3.2$ ls ./memo.james
./memo.james
bash-3.2$ ls ~/memos/memo.james
/Users/mokha/memos/memo.james
bash-3.2$ ls /Users/mokha/memos/memo.james
/Users/mokha/memos/memo.james
```

See `exercises/chapter-7.11`.

15.  Give the command for deleting the memos directory. How do you know that the directory has been deleted?

```bash
bash-3.2$ cd
bash-3.2$ rm -fr memos/
bash-3.2$ ls -alh memos
```

See `exercises/chapter-7.15`.

16.  Why does a shell process terminate when you press <Ctrl-D> at the beginning of a new line?

```text
The ^D represents the eof or end of file.
```

```bash
モ stty -a
speed 9600 baud; 14 rows; 238 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr -ixon ixoff ixany imaxbel iutf8
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;
```

17.  Give a command line to display the types of all the files in your `∼/unix` directory that start with the word chapter, are followed by a digit 1, 2, 6, 8, or 9, and end with .eps or .prn.

```bash
cd ~/unix && ls . | grep -e 'chapter[12689]\.[eps|prn]' | xargs file
chapter1.eps: empty
chapter1.prn: empty
chapter2.eps: empty
chapter6.eps: empty
chapter8.eps: empty
chapter9.eps: empty
```

See `exercises/chapter-7.17`.

## Solve problems 7–17 at the end of Chapter 8.

7.  How do the read, write, and execute permissions work in UNIX? Illustrate your answer with some examples.

Permission bits are organized as follows:

| t | r | w | x | r | w | x | r | w | x |

* The first bit indicates if the file is a directory or has the `setuid` bit enabled.
* The following 3 bits represent the read, write, execute permissions for the user.
* The following 3 bits represent the read, write, execute permissions for the group.
* The following 3 bits represent the read, write, execute permissions for everyone else.

E.g

```bash
モ ls -alh README.md
-rw-r--r--  1 mokha  staff   7.8K 18 May 16:20 README.md
```

* The `-` indicates a file.
* The `rw-` indicates read and write permissions for the owner of the file.
* The following `r--` indicate read permissions for members of the group.
* The following `r--` indicate read permissions for everyone else.

8. How many user groups exist on your system? How did you get your answer?
What groups are you a member of and what is your default group?
How many groups is root a member of, on your system?
How did you obtain your answer?
If used any commands, show the commands and their outputs.

```bash
モ users
mokha
モ id -Gn
staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin com.apple.sharepoint.group.1 _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh com.apple.sharepoint.group.2
モ groups
staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin com.apple.sharepoint.group.1 _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh com.apple.sharepoint.group.2
モ groups mokha
staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin com.apple.sharepoint.group.1 _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh com.apple.sharepoint.group.2
モ groups root
wheel daemon kmem sys tty operator procview procmod everyone staff certusers localaccounts admin com.apple.sharepoint.group.1 _appstore _lpadmin _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh com.apple.sharepoint.group.2
```

See `exercises/chapter-8.8`.

9. Create a file `test1` in your present working directory and set its access privileges to read and write for yourself,
read for the users in your group, and none to everyone else.
What command did you use to set privileges?
Give another command that would accomplish the same thing.

```bash
touch test1
chmod 640 test1
ls -alh test1
-rw-r-----  1 mokha  staff     0B 18 May 18:16 test1
chmod u=rw,g=r,o= test1
-rw-r-----  1 mokha  staff     0B 18 May 18:17 test1
```

See `exercises/chapter-8.9`.

10.  The user `sarwar` sets access permissions to his home directory by using the command `chmod 700 $HOME`.
If the file `cp.new` in his home directory has read permissions to `777`, can anyone read this file? Why or why not? Explain your answer.

Yes. The final seven in the permissions means read, write, execute
for all other users.

| Read (4) | Write (2) | Execute (1) |
| ------------- | ------------- | ------------- |
| x | x | x |

11.  What is the effect of each command? Explain your answers.

The following will change the permissions for a file named `lab5` in the
`$HOME` directory to:

* read, write, execute for the owner (4 + 2 + 1 = 7)
* read, write, execute for the group (4 + 2 + 1 = 7)
* read and write for everyone (4 + 2 = 6)

```bash
$ chmod 776 ∼/lab5
```

The following will change the permissions for any files that start with
lab and is followed by any optional single character in the `$HOME` directory.

* read, write, execute for the owner (4 + 2 + 1 = 7)
* read, execute for the group (4 + 1 = 5)
* execute for everyone (1 = 1)

```bash
$ chmod 751 ∼/lab?
```

Change the permissions for all files in the current directory that end
with a `.c` file extension.

* read, execute for the owner (4 + 1 = 5)
* execute for the group (1 = 1)
* execute for everyone (1 = 1)

```bash
$ chmod 511 *.c
```

Change permissions for all files in the `$HOME` directory.

* read, write, and execute for the owner (4 + 2 + 1 = 7)
* execute for the group (1 = 1)
* execute for everyone (1 = 1)

```bash
$ chmod 711 ∼/*
```

List files in current directory using the long listing format.

```bash
$ ls -l
```

List the current directory in long list format.

```bash
$ ls -ld
```

List files/directories in `$HOME/personal` directory using the long list
format.

```bash
$ ls -l ∼/personal
```

List the directory `$HOME/personal` in long list format.

```bash
$ ls -ld ∼/personal
```

12.  What does the execute permission mean for a directory, a file type for which the execute operation makes no sense?

The execute permission on a directory allows you to enter it. .ie. `cd directory`.

13. Create a file `dir1` in your home directory and use `cp /etc/passwd dir1/mypasswd` command to copy the `/etc/passwd` file in it. Use the `chmod` command to have only the search permission on for it and execute the following commands. What is the results of executing these commands? Do the results make sense to you? Explain.

Change directory to a directory named `dir1`.

```bash
$ cd dir1
```

List the contents of the current directory.
```bash
$ ls
```

Remove the file named `mypasswd` from the directory named `dir1`.

```bash
$ rm dir1/mypasswd
```

Copy the file from `/etc/passwd` to the directory named `dir1`.
```bash
$ cp /etc/passwd dir1
```

See `exercises/chapter-8.13`.

14.  What `umask` command should be executed to set the permissions bit mask to `037`? With this mask, what default access privileges are associated with any new file that you create on the system? Why? Where would you put this command so that every time you log on to the system this mask is effective?

The `umask` indicates which permission bits to turn off. ie. `777 - mask`.

To change the default umask to `037`.

```bash
$ mkdir -p before
$ umask 037
$ mkdir -p after
$ ls -alh before after
drwxr-----. 2 root root 4.0K May 25 19:31 after
drwxr-xr-x. 2 root root 4.0K May 25 19:31 before
```

The default access privileges are:

```text
777 - 037 = 740

rwx|r--|---
```

User: Read, write, execute.
Group: Read
Everyone: no access

The `umask` can be placed in any startup scripts such as:

* $HOME/.profile (System V UNIX)
* $HOME/.login (BSD UNIX)
* $HOME/.bash\_profile

See `exercises/chapter-8.14`.

15.  Give a command line for setting the default access mode so that you have read, write, and execute privileges, your group has read and execute permissions, and all others have no permission for a newly created executable file or directory. How would you test it to be sure that it works correctly?

I can use the `umask` builtin to set the default access privileges. I
can test this by creating a file before the change and then creating a
file after the change. Then I can list the permissions for both the
before and after files to witness the difference. I can also run `umask`
to see what the current setting is.

To change the default access privileges to `rwx|r-x|---` (750) I can use the
formula:

```text
777 - 750 = 027
```

The desired `umask` value would be `027`.

```bash
$ mkdir before
$ umask 027
$ mkdir after
$ ls -l
drwxr-x---. 2 root root 4096 May 25 19:36 after
drwxr-xr-x. 2 root root 4096 May 25 19:36 before
```

See `exercises/chapter-8.15`.

16.  Give `chmod` command lines that perform the same tasks that the `mesg n` and `mesg y` commands do. (Hint: Every hardware device, including your terminal, has an associated file in the /dev directory.)

`mesg n` blocks write access to the currently logged in users `tty` by
any other users. `mesg y` allows write access to the currentl logged in
users `tty` session.

To accomplish the same tasks without using `mesg` you will need to
identity which device the current tty is attached to. This can be done
by using the `tty` program. Once the device is identified, you can
change the permissions for that file.

```bash
$ tty
/dev/pts/6
$ ls -alh /dev/pts/6
crw--w----. 1 mokha 136, 6 May 25 13:45 /dev/pts/6
$ chmod 600 /dev/pts/6
```

See `exercises/chapter-8.16`.

17.  What are the purposes of the set-user-ID (SUID), set-group-ID (SGID), and sticky bits?

## Do problem 9 (What do the following commands do?) in Chapter 9.

What do the following commands do?

Force copy `sample` to `sample.bak`.

```bash
$ cp -f sample sample.bak
```

Force copy `sample` to `sample.bak` and preserve the mode, ownership and timestamps from the original file.

```bash
$ cp -fp sample sample.bak
```

Remove all files from `$HOME/personal/` that start with `memo` and have
a `.doc` file extension. Prompt for deletion before each file.

```bash
$ rm -i ∼/personal/memo*.doc
```

Remove all files from `$HOME/unixbook/finals/` that start with `ch`, is followed by 2 characters and has
a `.prn` file extension. Prompt for deletion before each file.

```bash
$ rm -i ∼/unixbook/final/ch??.prn
```

Fore delete all files from `$HOME/unixbook/finals/` that ends with a `.o` file extension.

```bash
$ rm -f ∼/unixbook/final/*.o
```

Force delete all files that start with `lab` followed by any # from 1-6
and has a file extension of `.c` or `.C` from the directory
`$HOME/courses/ece446/`.

```bash
$ rm -f ∼/courses/ece446/lab[1-6].[cC]
```

Recursively delete the directory `$HOME/NotNeededDirectory`.

```bash
$ rm -r ∼/NotNeededDirectory
```

Recursively force delete the directory `$HOME/NotNeededDirectory`.

```bash
$ rm -rf ∼/NotNeededDirectory
```

Recursively delete the directory `$HOME/NotNeededDirectory`, prompting
before each item to delete.

```bash
$ rm -ri ∼/NotNeededDirectory
```

## Enter a C program that reads a temperature in Celsius entered from a keyboard and displays the corresponding temperature in Fahrenheit and complete the steps below.
(Hint: Look for such a program in the textbook or online.)

Compile this program using the cc compiler, or any other compiler. Fix any errors, and recompile.

Your program must have declaration statements, such as float c, f;
**Delete the semicolon from the end of the statement.**  Recompile and report the kind of error.
The C compiler often provides cryptic error messages.  Interpret this message.

The compiler error is the following:

```bash
も make
cc -Wall -g -std=c99 -Isrc -c src/temperature.c
src/temperature.c: In function ‘main’:
src/temperature.c:5:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘printf’
    5 |   printf("Degrees in Celsius?\n");
      |   ^~~~~~
src/temperature.c:8:3: error: ‘f’ undeclared (first use in this function)
    8 |   f = 9*c/5 + 32;
      |   ^
src/temperature.c:8:3: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile:14: temperature.o] Error 1
```

It states that a `;` colon is missing from before line 5 to terminate
the previous line.

**Change the float c, f; statement to (float c; char f;)**

Do you get any errors during compilation?

No. I do get a compiler warning, but not an error.
```bash
も make
cc -Wall -g -std=c99 -Isrc -c src/temperature.c
src/temperature.c: In function ‘main’:
src/temperature.c:9:35: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
    9 |   printf("Degrees in Fahrenheit: %f\n", f);
      |                                  ~^     ~
      |                                   |     |
      |                                   |     int
      |                                   double
      |                                  %d
cc -Wall -g -std=c99 -Isrc -o ./bin/temperature temperature.o
./bin/temperature
Degrees in Celsius?
100
Degrees in Fahrenheit: 0.000000
```

* What are they and why?

The compiler warning says that the `%f` format specifier was expecting a
type of double, but received a type of `int`. The `char f` is being
interpreted as a memory address rather than a value.

* Do you see any difference between running this program and the earlier version? Why?

Yes, the output is now incorrect because the value for `f` is coming
from the memory address instead of the value.

* If your program uses a `cout` statement, then replace it with a `printf` statement that does the same thing and vice versa.
* If you have used neither `cout` nor `printf` in the first version, then replace what you have with `printf`.

## Explain how you can schedule the executable program to run at 1:00 a.m. and to take the input from a file, rather than the keyboard, without any changes to the source program.

You can use the `at` program to run a script at a specific time.

E.g.

```bash
$ at 1am -f script.sh
```

## Explain how you can find the time it took the computer to execute your program.

You can use the `time` command.

E.g

```bash
も time curl -s -I https://www.mokhan.ca/   
HTTP/2 200                              
content-type: text/html                                                                                                
content-length: 8017                                                                                                   
date: Tue, 21 May 2019 08:49:34 GMT
last-modified: Fri, 19 Apr 2019 18:06:57 GMT
etag: "902a8badcb38234a5d17b0f4ad54406e"
accept-ranges: bytes                                                                                                   
server: AmazonS3                                                                                                       
vary: Accept-Encoding                                                                                                  
age: 7455                                                  
x-cache: Hit from cloudfront
via: 1.1 805232684895bb3db77c2db44011c8d0.cloudfront.net (CloudFront)
x-amz-cf-id: AxXM-x6oQ4RnM_YrQoR0nYtbrVF5mW7pREVHyu5tPD3SNswhLR2h0g==
                                                                                                                       
                                                           
real    0m0.136s
user    0m0.030s
sys     0m0.010s
```

The `time` program will emit 3 times at the end of the run. The `real`
time is the total time from invocation to completion. The `user` time is the time spent in user mode.
The `sys` time is the time spent in kernel mode. The difference between
`user` and `sys` shows the amount of time spent in the program version
the `kernel`.

## Explain how the output of your program can be automatically emailed to your friend upon completion.

You can pipe the output from the first program to the `mail` program.

```bash
$ curl -s -I https://www.mokhan.ca/ | mail -s "My Website" destination@example.org
```