myshell 2.0.0
Loading...
Searching...
No Matches
main.c File Reference

External ulility mycat. More...

Classes

struct  file
 

Enumerations

enum  error_code {
  UNKNOWN_OPT = 1 , NO_FILES = 2 , ALLOC_ERR = 3 , OPEN_ERR = 4 ,
  STAT_ERR = 5 , READ_ERR = 6 , WRITE_ERR = 7
}
 

Functions

void herror (error_code code, int use_errno,...)
 
void help (void)
 
int wrbuf (int fd, const char *buffer, ssize_t size)
 
int rdbuf (int fd, char *buffer, ssize_t size)
 
void chtoh (char c, char *hex_escape)
 
ssize_t buftoh (const char *buffer, ssize_t size, char *fbuffer)
 
void rfiles (int argc, char *argv[], struct file *files)
 
void cat (const struct file *file, int a_flag, char *buffer, char *fbuffer)
 
int main (int argc, char *argv[])
 

Variables

static const char * error_messages []
 

Detailed Description

External ulility mycat.

Enumeration Type Documentation

◆ error_code

enum error_code

Error codes

Function Documentation

◆ buftoh()

ssize_t buftoh ( const char * buffer,
ssize_t size,
char * fbuffer )

Escapes non-printable characters in buffer and writes the result to fbuffer

Parameters
bufferBuffer to filter
sizeSize of the buffer
fbufferBuffer to write the result to
Returns
Number of bytes written to fbuffer

◆ cat()

void cat ( const struct file * file,
int a_flag,
char * buffer,
char * fbuffer )

Reads file file and writes it to stdout

Parameters
fileFile to read
a_flagIf set to 1, non-printable characters will be replaced with their hexadecimal escape sequences
bufferBuffer to read to
fbufferAuxiliary buffer for a_flag
Note
Returns nothing. Exits on error.
Warning
fbuffer may be NULL if a_flag is not set, otherwise behavior is undefined. fbuffer must be at least
4 * BUFFER_SIZE
bytes long if a_flag is set.

◆ chtoh()

void chtoh ( char c,
char * hex_escape )

Converts a character to its hexadecimal escape sequence

Parameters
cCharacter to convert
hex_escapeBuffer to write the escape sequence to

◆ help()

void help ( void )

Prints help message to stdout

◆ herror()

void herror ( error_code code,
int use_errno,
... )

Prints error message to stderr and exits with the specified code

See also
error_code
Parameters
codeError code
use_errnoIf set to 1, the description of errno will be appended to the error message
...Additional arguments for the error message

◆ rdbuf()

int rdbuf ( int fd,
char * buffer,
ssize_t size )

Reads size bytes from fd to buffer

Parameters
fdFile descriptor
bufferBuffer to read to
sizeSize of the buffer
Returns
0 on success, -1 on error
Note
Automatically retries if read() is interrupted

◆ rfiles()

void rfiles ( int argc,
char * argv[],
struct file * files )

Opens files from argv and saves their metadata to files

Parameters
argcNumber of files
argvArray of filenames
filesArray of struct file
Note
Returns nothing. Exits on error.

◆ wrbuf()

int wrbuf ( int fd,
const char * buffer,
ssize_t size )

Writes size bytes from buffer to fd

Parameters
fdFile descriptor
bufferBuffer to write
sizeSize of the buffer
Returns
0 on success, -1 on error
Note
Automatically retries if write() is interrupted

Variable Documentation

◆ error_messages

const char* error_messages[]
static
Initial value:
= {
[UNKNOWN_OPT] = "Unknown option",
[NO_FILES] = "No input files provided",
[ALLOC_ERR] = "Memory allocation error",
[OPEN_ERR] = "Cannot open file %s",
[STAT_ERR] = "Cannot stat file %s",
[READ_ERR] = "Cannot read from file %s",
[WRITE_ERR] = "Cannot write to stdout"
}

Error messages