CS 3733 Operating Systems, Spring 2004 Assignment 1 Comments
- The following lint messages should be understood and not ignored:
- implicitly declared to return int
- types used inconsistently
- argument unused in function
- set but not used
- variable unused
- function declared with variable number of arguments
- more than one main
- operation has not effect
- You may not assume a maximum size for a file or for a line in this
assignment.
- Buffer overflows are serious
- Do not assume that a file that is read in is a string
- What can go wrong with the following:
    read a file into buf
    print(buf);
- For part 10, it is not necessary to make a copy of the file after reading it in. (see below)
- You do not need to call free before exit.
- read may not read all that was requested.
The read_file function should use a loop or call readblock.
- sizeof(char) = 1
- What does the following code do:
     *s++
- Learn what NULL means, do not do
     if (buf[i]!=NULL)
when buf is an array of char.
- Understand what is stored at the end of a UNIX file.
- What does the following do:
     while((n=read(fd,buf,bufsize))>0);
- Do not print program source using a proportional font.
- Watch out for memory leak in get_program_number.
- Many students still do not understand the concept of separate compilation.
C programming notes
- A function definition is the actual code that defines what the function does.
- The function definition contains a declaration that describes the data
types of the parameters and the data type of the return value.
- A function prototype declares the data types of the parameters and return
value without including the function definition.
- The C compiler must see the declaration of a function before the function
is called.
- This declaration may be from the definition or from a prototype.
- If the compiler sees a function before it sees a declaration it assumes
that the return value is of type int.
- It should be considered a programming error if the compiler sees a
function before it sees its declaration.
- Declarations for the system functions are usually contained in files
with extension .h and these include files are in a standard location.
- Use #include <filename.h> to include one of these files.
- You can put your own prototypes in a file in the current directory and
include them with #include "filename.h"
- The code (definitions) for many of the system functions is contained
in libraries.
- The linker automatically looks for definitions for some of these libraries.
- It does not automatically look in all of them, e.g. the math library.
- To use functions in the math library there are two steps:
- Tell the compiler the prototypes by including math.h
- Tell the linker to look in the math library by putting
-lm on the compile line.
- For the functions in some of the standard libraries such as the string
functions, you need only
do the first step and the linker will automatically find the function in the
appropriate library.
Outline of Part 10:
- Print your name
- * Check command line arguments
- * filesize = get_filesize(argv[1])
- * buf = (char *)malloc(filesize);
- * read_file(argv[1],buf,filesize);
- * test buf[filesize-1] == '\n'
- Check for valid program lines:
for (i=0;i<filesize;i++) {
    * test valid_program_line(buf+i)
    while (buf[i]!='\n')
        i++;
}
- * Print the file. Note that you cannot use printf.
Use write in a loop or r_write.
- scanf("%d",&val);
- Find a line with program number val:
for (i=0;i<filesize;i++) {
    if(get_program_number(buf+i) == val)) {
- print confirming message
- * Get the size of the file
- * Allocate space for the file
- * Get the size of the filename
- * Allocate space for the filename
- copy the filename
- * open the file
- * read in the file with read_file
- * write out the file in a loop or with r_write
    }
    while (buf[i]!='\n')
        i++;
}
* check for error, print message and exit if an error occurs