---
# System prepended metadata

title: Linux Kernel開發學習日誌2020.1.29
tags: [C, linux2020]

---

# Linux Kernel開發學習日誌2020.1.29

###### tags: `linux2020` `C`

## 加解密程式

### Understanding the file Permissions
http://linuxcommand.org/lc3_lts0090.php

### Understanding four FILE type in ```<stdio.h>```
* how to read and write a simple txt file.
    * Function:
        * fwrite
        * nread
        * fopen
        * fclose
    

```cpp=1
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    uint32_t reverse_bit(uint32_t x) {
    	x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
    	x = ((x & 0xff00ff00) >>  8) | ((x & 0x00ff00ff) <<  8);
    	x = ((x & 0xf0f0f0f0) >>  4) | ((x & 0x0f0f0f0f) <<  4);
    	x = ((x & 0xcccccccc) >>  2) | ((x & 0x33333333) <<  2);
    	x = ((x & 0xaaaaaaaa) >>  1) | ((x & 0x55555555) <<  1);
    	return x;
    }
    
    int main(int argc, char *argv[])
    {
    	if (argc < 3) {
    		printf( "\nUsage:"
    			"\t %s input.txt output.txt\n\n"
    			"This is a simple encrypt/decrypt app "
    			"based on reverse_bit algorithm.\n\n"
    			, argv[0]);
    		return 0;
    	}
    
    	const char *input_path = argv[1];
    	FILE *fin = fopen(input_path, "rb");
    	if (!fin) {
    		fprintf(stderr,
    			"ERROR: failed to open input file %s\n", input_path);
    		exit(-1);
    	}
    
    	const char *output_path = argv[2];
    	FILE *fout = fopen(output_path, "wb");
    	if (!fout) {
    		fclose(fin);
    		fprintf(stderr,
    			"ERROR: failed to open output file %s\n", output_path);
    		exit(-1);
    	}
    
    	uint32_t ch;
    	int nread;
    	while((nread = fread(&ch, 1, sizeof(uint32_t), fin)) > 0) {
    		/* if read size less than sizeof(uint32_t), just write it */
    		if (nread < sizeof(uint32_t)) {
    			fwrite(&ch, 1, nread, fout);
    		}
    		else {
    			uint32_t chn = reverse_bit(ch);
    			fwrite(&chn, 1, nread, fout);
    		}
    	}
    
    	fclose(fin);
    	fclose(fout);
    	return 0;
    }
```

## Linux file Permissions:
* Keywords:
    * chmod: change file permissions
    * chown: change ownership
    * chgrp: change group
   

### Ownership of Linux files:

Every file and directory on your Unix/Linux system is assigned 3 tyeps of owner.

**User:**

* A user is the owner of the file. 
* By default, the person who created a file becomes its owner. 
* Hence, a user is also sometimes called an owner.

**Group:**

* A user-group can contain multiple users.
* All users belonging to a group will have the same access permissions to the file. 

* Suppose you have a project where a number of people require access to a file. Instead of manually assigning permissions to each user, you could add all users to a group, and assign group permission to file such that only this group members and no one else can read or modify the files.


**Other:**

* Any other user who has access to a file. 
* This person has neither created the file, nor he belongs to a usergroup who could own the file. 
* It means everyone else.









### 參考資料
[課前測驗參考解答: Q1](/@sysprog/ByzoiggIb?type=view)

[File Permissions][1]



[1]:https://www.guru99.com/file-permissions.html
