In this project, I learned about the three standard file descriptors and their
POSIX names as well as the difference between function and system calls. I
practiced using the I/O system calls open, close, read, and write
and the flags O_RDONLY, WR_ONLY, and O_RDWR to create, open, close,
read, write, set permissions of files in C.
- tests: Folder of test files. Provided by Holberton School.
- holberton.h: Header file containing prototypes for all functions written in the project.
| File | Prototype |
|---|---|
0-read_textfile.c |
ssize_t read_textfile(const char *filename, size_t letters); |
1-create_file.c |
int create_file(const char *filename, char *text_content); |
2-append_text_to_file.c |
int append_text_to_file(const char *filename, char *text_content); |
-
0. Tread lightly, she is near
- 0-read_textfile.c: C function that reads a text file and
prints it to the
POSIXstandard output. - The parameter
lettersis the number of letters the function should read and print. - If the file is
NULLor cannot be opened or read - returns0. - If the
writecall fails or does not write the expected number of bytes - returns0. - Otherwise - returns the actual number of bytes the function can read and print.
- 0-read_textfile.c: C function that reads a text file and
prints it to the
-
1. Under the snow
- 1-create_file.c: C function that creates a file.
- The paramter
filenameis the name of the file to create. - The parameter
text_contentis a null-terminated string to write to the file. - If
text_contentisNULL, the function creates an empty file. - The created file has the permissions
rw-------. - If the file already exists, the existing permissions are not changed.
- Existing files are truncated.
- If
filenameisNULLor the funciton fails - returns-1. - Otherwise - returns
1on success.
-
2. Speak gently, she can hear
- 2-append_text_to_file.c: C function that appends text at the end of a file.
- The parameter
filenameis the name of the file. - The parameter
text_contentis a null-terminated string to append to the file. - The function does not create the file if it does not exist.
- If
text_contentisNULL, nothing is added to the file. - If the function fails or
filenameisNULL- returns-1. - If the file does not exist or the user lacks write permissions on the file - returns
-1. - Otherwise - returns
1.
-
3. cp
- 3-cp.c: C program that copies the contents of a file to another file.
- Usage:
cp file_from file_to - If
file_toalready exists, it is truncated. - The created file has the permissions
rw-rw-r--. - If the file already exists, the existing permissions are not changed.
- If the number of arguments is incorrect, the function prints
Usage: cp file_from file_to, followed by a new line on thePOSIXstandard error and exits with code97. - If
file_fromdoes not exist or the user lacks read permissions on it, the function printsError: Can't read from file NAME_OF_THE_FILE, followed by a new line and exits with code98.- Where
NAME_OF_THE_FILEis the first argument passed to the program.
- Where
- If files cannot be created or if
writetofile_tofails, the function printsError: Can't write to NAME_OF_THE_FILE, followed by a new line and exits with code99.- Where
NAME_OF_THE_FILEis the second argument passed to the program.
- Where
- If the user cannot close a file descriptor, the function prints
Error: Can't close fd FD_VALUE, followed by a new line on thePOSIXstandard error and exits with code100.- Where
FD_VALUEis the value of the file descriptor.
- Where
-
4. elf
- 100-elf_header.c: C program that displays the information contained
in the
ELFheader at the start of anELFfile. - Usage
elf_header elf_filename - Displayed information:
- Magic
- Class
- Data
- Version
- OS/ABI
- ABI Version
- Type
- Entry point address
- Format is identical to
readelf -h(version2.26.1). - If an error occurs or the file is not an
ELFfile, the function displays a corresponding error message tostderrand exits with a status value of98.
- 100-elf_header.c: C program that displays the information contained
in the