Skip to main content

Goals 

  • Further review C programming topics.
  • Practice with Unix system and library calls.
  • Continue to develop your code reading and maintenance skills.
  • Continue to develop skills to read and correctly interpret Unix manual pages.

Credits

This lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this credits note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e-mail to: perrone[at]bucknell[dot]edu

Academic Responsibility

It should go without saying that all the work that you will turn in for this lab will be yours. You should try your best to debug your code on your own, but it’s fine to get help from a colleague as long as that means getting assistance to identify the problem and doesn’t go as far as receiving source code to fix it (in writing or orally).

Set Up

Before you start, do the following on a terminal:

cd ~/csci307/Labs/Lab3
cp ~perrone/csci307/Labs/Lab3/* .

You will need to copy additional material from your Labs 1 and 2, as necessary.

Context

In this assignment, you will continue to build up our flight reservation system. The code you were given before has been a bit refactored and extended. Take the time to get acquainted with the files and the functionality that each module provides. The executable flight_system.c contains functionality to offer the user a menu of six options, as shown in the figure below.

From this menu, the user enters their selection by pressing a key from 1 to 6 followed by return. This input is processed in a switch/case statement that dispatches each key press to the corresponding function. The options execute as follows:

  1. View available flights: traverses flight_list, a singly-linked list that aggregates all the flights on which the system has information, displaying on the screen the data for each flight;
  2. Add a new flight: asks the user to provide information for each flight, creates a new instance of struct flight and inserts it in flight_list;
  3. Save flights to a file: traverses flight_list and adds the information for each flight into a file with name specified by the user;
  4. Load flights from a file: using a file with name given by the user, extracts the information for each flight from the file, creates a new instance of struct flight and adds it to flight_list;
  5. Make a new reservation: creates a new instance of struct reservation, fills it up with information provided by the user, and inserts it into struct slist *reservations (declared in reservations.h);
  6. View existing reservations: traverses the list reservations, a singly-linked list that aggregates all the reservations recorded in the system, displaying on the screen the data for each reservation encountered.
  7. Exit: terminates the program.

Note: from now on, make an effort to have ALL your code compile without any warnings. In this lab, there will be no points deducted when warnings are left unaddressed, but in future labs that will happen.

Problem 1

Get acquainted with the new functionality provided for you in the slist module. You will find functions that will be helpful to you later. In particular, look at slist_iter_init and slist_iter_next, which you may choose to use to traverse a list without reaching directly into the data structure.

Merge the code you developed for the reservation module into the files given to you with this lab. In particular, you will want to copy over your functions reservation_add and reservation_compare. Use your completed program reservationtest.c from last time to verify that everything works as expected.

After you debug your code and get everything working correctly, add, commit and push your files to your gitlab as follows:

git add reservation.h
git add reservation.c
git add reservationtest.c
git commit -m "lab3-1 completed" 
git push

Problem 2

In flight_system.c, you will find the complete code for function sys_save_flights. This function will first open a file whose name is read from the terminal as write-only, requesting the system to create the file if it does not already exist. Take time time to read the code given to you in this function. You will notice that it does the opening and closing of the file for you. Pay close attention to how it uses the system call open(2) and read the man page for the system call to understand the parameters used in this example.

Function sys_save_flights traverses a list of flights and for each node encountered, it saves the flight information encountered into a file whose name is read from the terminal. The real action in saving the system’s flight data to a file is done by function flight_traverse_save from the flights module, which you need to flesh out. Read the function prototype to see that it takes two parameters: the file descriptor for the file to save into and a pointer to the list of flights. Complete the code of function flight_traverse_save and use the flight_system program to verify that your implementation works as expected.

Once you get it to work without crashing, you will be able to have flight data in a file, but you won’t be sure if each flight was saved correctly because the contents of the file will not all be encoded as text. At this point, you can use the Unix program xxd(1) to take a peak into the file. You will only be sure that the data was correctly recorded once you read it back in, but that’s for the next problem. There is not test program for this function: you will need to “exercise” the system to verify that it is working as you expect it.

After you debug your code and get everything working correctly, add, commit and push your files to your gitlab as follows:

git add flight.c
git add flight_system.c
git commit -m "lab3-2 completed" 
git push

Problem 3

In flight_system.c, flesh out the code for function sys_load_flights. This function will first open a file whose name is read from the terminal as read-only. Upon successful opening, the function will enter a loop in which you perform the following actions repeatedly until all the contents of the file have been read: read from the file enough bytes to fill a struct flight and insert it into the list passed to this function as a parameter. Read the man page for system call read(2) to understand how you will detect that the end of the file has been reached. Use this function to read the data you saved into a file with the solution to Problem 2 and verify that your saving functionality was indeed implemented correctly. Of course, if you realize that the file was not save as expected, you will need to go back to fix your implementation of flight_traverse_save. There is not test program for this function: you will need to “exercise” the system to verify that it is working as you expect it.

After you debug your code and get everything working correctly, add, commit and push your files to your gitlab as follows:

git add flight.c
git add flight_system.c
git commit -m "lab3-3 completed" 
git push

Problem 4

For this problem you will flesh out the code for two functions in flight_system.c. First, work on sys_make_reservation to create a new reservation and insert it in the appropriate list. As in Lab 2, you will need to ensure that only non-existing reservations are added to the system, that is, no duplicates should be allowed. If you think for a moment, you already developed code for this, didn’t you? So, just use what you implemented before. Second, work on sys_display_reservations to traverse the list reservations and display all the reservations encountered on the screen. In both functions, do you best to create the same pattern of interactions with the user that you see in other menu options of the system to create a consistent experience. There is not test program for these functions: you will need to “exercise” the system to verify that it is working as you expect it.

After you debug your code and get everything working correctly, add, commit and push your files to your gitlab as follows:
git add flight_system.c
git commit -m “lab3-4 completed”
git push

Grading rubric:

The rubric below shows the number of points that will be earned for each item that compiles and runs correctly. If the item does not compile correctly, 60% of the total points will be deducted. If the item compiles correctly and runs with major errors, 40% of the total points will be deducted. If the item compiles correctly and runs with minor errors, 20% of the total points will be deducted.

  1. Merge debugged code from Lab 2 into this lab’s files. [25 points]
  2. flight_traverse_save [25 points]
  3. sys_load_flights [25 points]
  4. sys_make_reservation [10 points]
  5. sys_display_reservations [15 points]