1521 mipsy ackermann.s
Enter m: 0
Enter n: 0
Ackermann(0, 0) = 1
1521 mipsy ackermann.s
Enter m: 3
Enter n: 1
Ackermann(3, 1) = 13
1521 mipsy ackermann.s
Enter m: 3
Enter n: 5
Ackermann(3, 5) = 253
1521 mipsy ackermann.s
Enter m: 4
Enter n: 0
Ackermann(4, 0) = 13
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest ackermann
Revision Exercise: JSON decoder (PS-CR)
Write a C program json_decoder so that,
given two or more command line arguments —
the first being the path to a source file to be read,
and the rest being keys.
your program should print the value of that key to stdout.
If the key is not found, print nothing.
as a reminder, JSON follows this generic format:
{
	"key1": value1,
	"key2": value2,
	...
}
Run the following command to get an example JSON file to test
your program with
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest json_decoder
Revision Exercise: Print the type of a file (PS-CR)
Write a C program file_type so that,
given one command line argument —
the path to a source file.
print out whether the file is a regular file, a directory, or a symbolic link.
Write a C program extract_permissions so that,
given one command line argument —
the path to a source file.
Print out the equivalent octal number of the permission, it's bitwise representation, and the formatted permissions string.
To get
For example:
# copy and run the following commands to make some example filesmkdir examples; touch examples/000.s examples/777.c examples/sample.txtchmod 000 examples/000.schmod 777 examples/777.cls -l examples
total 0
---------- 1 z5555555 z5555555 0 Apr 29 12:27 000.s
-rwxrwxrwx 1 z5555555 z5555555 0 Apr 29 12:27 777.c
-rw-r--r-- 1 z5555555 z5555555 0 Apr 29 12:27 sample.txt
./extract_permissions ./examples/sample.txt
Octal: 664, Bitwise: 110110100, String: rw-r--r--
./extract_permissions ./examples/000.s
Octal: 0, Bitwise: 000000000, String: ---------
./extract_permissions ./examples/777.c
Octal: 777, Bitwise: 111111111, String: rwxrwxrwx
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest extract_permissions
Revision Exercise: Hackerman (CR)
Write a C program hackerman so that,
given one command line argument —
the directory to be searched, hackerman recursively searches through the directory and it's sub-directories to find passwords.
The password will always be the contents of a file named "password", although the file's extension may vary. (e.g. password.bin, password, password.txt are all valid password files!!)
For security purposes, the admins have limited your ability to access certain files and directories, however passwords may be hidden behind these
security measures and they will need to be found anyways. You should try to change the permissions of files and directories you find to gain access.
To do this you can use chmod(2) to make the file public and group readable.
If the password is found, print the password to stdout along with the filename where it was found.
If the password cannot be found, your program should print "Passwords not found\n" and then exit(3) with status 0
Use the following command to get a directory to test your program with
# copy and run the following commands get a directory to test your program oncp -r /web/cs1521/25T1/activities/hackerman/hackerman_examples .ls -l hackerman_examples
total 16
drwxrwxrwx 2 z5555555 z5555555 4096 Feb 5 22:49 empty
drwxrwxrwx 2 z5555555 z5555555 4096 Feb 5 22:32 inner_folder
-rwxrwxrwx 1 z5555555 z5555555 8 Feb 5 22:16 password.txt
drwxrwxrwx 2 z5555555 z5555555 4096 Feb 5 22:36 security_folder
cat hackerman_examples/inner_folder/password
prettier
./hackerman hackerman_examples/inner_folder
Password heheheha found in 1521/admins/passwords/password.txt
cat hackerman_examples/security_folder/password.bin
cat: hackerman_examples/security_folder/password.bin: Permission denied
./hackerman hackerman_examples/security_folder
Password "hidden behind a firewall!" found in hackerman_examples/security_folder/password.bin
cat hackerman_examples/password.txt
glazeeee
./hackerman hackerman_examples
Password prettier found in hackerman_examples/inner_folder/password
Password glazeeee found in hackerman_examples/password.txt
Password "hidden behind a firewall!" found in hackerman_examples/security_folder/password.bin
ls -l hackerman_examples/empty
total 4
-rwxrwxrwx 1 z5360323 z5360323 9 Feb 5 22:49 pword_in_body
./hackerman empty
Passwords not found
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest hackerman
Revision Exercise: nth utf8 character (CR)
UTF-8 Encoding
#bytes
#bits
Byte 1
Byte 2
Byte 3
Byte 4
1
7
0xxxxxxx
-
-
-
2
11
110xxxxx
10xxxxxx
-
-
3
16
1110xxxx
10xxxxxx
10xxxxxx
-
4
21
11110xxx
10xxxxxx
10xxxxxx
10xxxxxx
You have been given nth_utf8_character.c,
which contains a C function nth_utf8_character,
that takes a string and an integer n and returns NULL.
Add code to the function nth_utf8_character so that,
given a a valid UTF-8 string,
it returns the nth character.
For example:
unzip nth_utf8_character_examples.zip
(several lines of output)
./nth_utf8_character 0 < nth_utf8_character_examples/hello_world.txt
h
./nth_utf8_character 10 < nth_utf8_character_examples/hello_world.txt
d
./nth_utf8_character 11 < nth_utf8_character_examples/hello_world.txt
No such character.
./nth_utf8_character 20 < nth_utf8_character_examples/emoji.txt
👍
./nth_utf8_character 21 < nth_utf8_character_examples/emoji.txt
!
./nth_utf8_character -1 < nth_utf8_character_examples/emoji.txt
No such character.
./nth_utf8_character 1 < nth_utf8_character_examples/more_emoji.txt
🍇
./nth_utf8_character 5 < nth_utf8_character_examples/more_emoji.txt
↧
./nth_utf8_character 20 < nth_utf8_character_examples/more_emoji.txt
ż
Further example inputs are provided in the nth_utf8_character_examples.zip archive.
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest nth_utf8_character
Revision Exercise: Detect C Files (DN)
Run the following commands to get the files required for this
question:
Add code to detect_c_files.c so that
when given a list of pathnames as command line arguments, it
checks each to see if it's a c file or not.
We have provided a c program check_c_file.c
which takes a single pathname as a command line argument,
and returns 1 if given a c file and 0
otherwise.
There are two parts to this question. First in the main
function spawn a process to compile check_c_file.c.
Secondly, complete the spawn_programs function so that given
a list of filenames, and the length of that list, you spawn processes
to run ./check_c_file in parallel on each filename.
You should wait for each child process to return
(see waitpid(3)), then use the return
value to print whether the file is or is not c program.
dcc detect_c_files.c -o detect_c_files./detect_c_files detect_c_files.c check_c_file.c
detect_c_files.c is a C file
check_c_file.c is a C file
./detect_c_files detect_c_files.c detect_c_file_examples/*
detect_c_files.c is a C file
detect_c_file_examples/a.txt is NOT a C file
detect_c_file_examples/b.txt is NOT a C file
detect_c_file_examples/hello.c is a C file
detect_c_file_examples/hello.txt is NOT a C file
detect_c_file_examples/nonsense.c is a C file
detect_c_file_examples/test.s is NOT a C file
detect_c_file_examples/trick.c is a C file
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest detect_c_files
Revision Exercise: Robot Arms: threads and mutex (DN)
Download robot_arms.c,
or copy it to your CSE account using the following command:
In this question we are writing code to automate a robot
manufacturing line. The robot we're making has four arms,
each made using some of three components. To make robots as
quickly as possible we're going to do it in parallel using
threads.
You have been given robot_arms.c, which contains
an empty main function, and four thread functions;
arm1, arm2, arm3, and
arm4. Each "assembles" a robot arm using a mix of
components; component A, component B, and component C.
In main, you should create four threads to do
the arm1, arm2, arm3, and
arm4 in parallel.
You should wait until all four threads have finished before
you return from main.
There are three components, A, B and C, each component can
only be used to make one arm at a time. We use mutexes
component_A_mutex, component_B_mutex, and
component_C_mutex to represent them.
You should add code to lock and unlock each component mutex
when the arm functions are using them. You should use
pthread_mutex_lock(3) to lock the component before
the arm acquires it, and pthread_mutex_unlock(3)
once the arm is finished assembling.
Think about how you can avoid deadlock - a state where all
arms are waiting for components held by others.
When your program is working, it should look like this:
dcc -pthread robot_arms.c -o robot_arms./robot_arms
Arm 1 acquired Component A
Arm 2 acquired Component C
Arm 2 acquired Component B
Arm 2 is assembling
Arm 1 acquired Component C
Arm 1 is assembling
Arm 4 acquired Component A
Arm 4 acquired Component B
Arm 4 is assembling
Arm 3 acquired Component C
Arm 3 acquired Component A
Arm 3 is assembling
When you think your program is working you can use autotest to run some simple automated tests:
1521 autotest robot_arms
Revision Exercise: Append Date to File (DN-HD)
Download date_file.c,
or copy it to your CSE account using the following command:
Your task is to add code to this function in date_file.c:
voidappend_date_to_file(char*filename){// TODO: complete this functionreturn;}
Add code to the function append_date_to_file so that
given a filename, you append the current date to the end
of the file.
For example:
echo "helllooooo" > hi.txtcat hi.txt
helllooooo
./date_file hi.txt
Appended date to hi.txt
cat hi.txt
helllooooo
Mon Apr 28
To do this, you should use posix_spawn to run the
date command, with the following format string:
date '+%a %b %d'
Mon Apr 28
You will need to use pipes with posix spawn to capture the output
and redirect it into the file. The lecture example
spawn_read_pipe.c
is an example of how to do this.
When you think your program is working you can use autotest to run some simple automated tests: