Week 12 Tutorial Questions

Objectives

Code Review

This week the code review is for compile_c_files.c. The reviewees should give a brief description of their code, and the class should ask questions, comment on the quality of the code, and suggest improvements. Each review should take about 10 minutes.

Tutorial Questions

Assignment 2

  1. Dicuss any issues you have with the assignment?

Revision

  1. Write a function called void print_utf8_encoding(uint32_t code_point);

    The function takes a unicode code point and prints out the code_point, the utf 8 encoding for the code point then the unicode character.

    For example, if we have a main program

    int main(void) {
        print_utf8_encoding(0x42);
        print_utf8_encoding(0x00A2);
        print_utf8_encoding(0x10be);
        print_utf8_encoding(0x1F600);
    }
    

    We would end up with output

    U+42  UTF-8: 0x42  B
    U+a2  UTF-8: 0xc2 0xa2  ยข
    U+10be  UTF-8: 0xe1 0x82 0xbe  แ‚พ
    U+1f600  UTF-8: 0xf0 0x9f 0x98 0x80  ๐Ÿ˜€
    
  2. Implement the following function in MIPS

    int everyKth(int *src, int n, int k, int *dest) 
    {   
       int i, j = 0;
       for (i = 0; i < n; i++) {
          if (i%k == 0) {
             dest[j] = src[i];
             j++;
          }
       }
       return j;
    }
    

  3. This is a past exam question from 18s1

    When paper documents need to be destroyed, we would use a shredder to slice up the paper so that the text is no longer able to be reconstructed. With digital documents, simply removing them (e.g. via Unix's rm command), leaves the contents of the document on the disk where it could be recovered by scanning the raw disk (i.e. bypassing the file system). To render digital documents less likely to be available for discovery, it would be better to write random bytes over the top of the file containing the data.

    In this question, you must write a function which takes an open file descriptor, and writes 64-byte blocks of "random" bytes for at least all of the bytes in the input file. Ideally, we would write these blocks over the top of the original file. However, we don't want to destroy each input file the first time you test your program, so we write the random blocks onto an output file with the suffix .out.

    In this question, you must implement the void my_shred(int infd, int outfd); function, which takes two parameters:

    • infd ... a file descriptor open for reading
    • outfd ... a file descriptor open for writing
    and does the following:

    for the whole infd file {
       read a 64-byte block into a buffer
       // may have < 64 bytes in buffer at end of file
       overwrite the buffer with random characters from 'A'..'Z'
       write the buffer to outfd, then write a newline ('\n') to outfd
    }
    

    Hint: to create random byte from 'A'..'Z' use (rand () % 26) + 'A'

  4. Rewrite the above function using the stdio library functions instead. Assume your prototype is void my_shred (FILE * inf, FILE * outf); .
  5. Write a (silly) program that creates 20 nested directories. So by the end you should have a directory called d19 with a relative path of d0/d1/d2/d3/d4/d5/d6/d7/d8/d9/d10/d11/d12/d13/d14/d15/d16/d17/d18/d19
  6. Write a program to list the contents of directories

    You should use opendir(3) in your answer and use perror to print error messages for example if the directory does not exist or if you do not have permission to list its contents.

    For example:

    ./list_directory .
    .
    ..
    dir3
    list_directory.c
    list_directory
    dir2
    dir1
    ./list_directory dir1 dir2 dir3
    .
    ..
    f1
    .
    ..
    f2
    .
    ..
    f3
    f4