IMP.: Your programs must use the file system calls, e.g. open(), read(), close()
, not the C library functions for streams, e.g. fopen(), fread(), fclose()
Read either the OSTEP chapter on files and directories or the notes on the UNIX file-system API by Prof. Pimenta Monteiro (in Portuguese). You should also read, or at least browse, the man pages for the different system calls.
cat
utility when invoked with a single argument. Hint: You can use the macro STDOUT_FILENO
, defined in unistd.h
, as the stdandard output file descriptor.
IMP.: To ensure that your program will have to invoke read()
several times, use a buffer with only 64 bytes. You may test your program using the file with its source code.
Hint: Read the open()
man page carefully, specially the different flags.
IMP.: Your program should report an error, if the second argument is the name of an existing file.
split
utility splits a file into multiple files (for more details read its man page: man split
)
Write the merge
program which restores a file from all the pieces generated by split
. Assume that all these pieces are in the directory where you invoke merge
.
merge
shall be invoked as follows:
merge <prefix>
where:
<prefix>
is the prefix used as argument ofsplit
The name of the file created by merge
shall be <prefix>.mrg
Hint: Use the string handling functions of the C library (man 3 string
).
Consider the following declarations:
typedef char name[30]; typedef struct { name st; int grades[3]; } test_t;
test_t
, i.e. the grades of a test with 3 questions. The members of each struct shall be read from the standard input. Your program should take as argument the name of the output file, which may already exist. In that case, you should append the information to that file.Write the sample
program that samples a file, whose name is passed as an argument. I.e. sample
shall be invoked as follows:
sampe <filename> <offset>
where:
<filename>
is the name of the file to sample
<offset>
is the offset of the first sampled byte with respect to the beginning of the file
The size of the sample should be one tenth of the length of the sampled file, unless the specified offset is such that it must be shorter. The sample shall be written to a file with name <filename>.smp
.
Hint: To find out the size of the sampled file you can use one of the stat(),fstat(),lstat()
system calls. Check out their man page.
Hint: To advance the current position of the file you can use the lseek
system call. Check out its man page.
Consider problem 1 above. Write a new version, which shall behave either as in 1.a or as in 1.b, depending on the number of arguments of the command line.
IMP. Your solution must use the dup2()
syscall. Check out its man page.