Skip to content

Commit 432de23

Browse files
committed
Tasks of file_io
1 parent 2a3ad8b commit 432de23

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

0x15-file_io/100-elf_header.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,123 @@
77
#include <unistd.h>
88

99
void check_elf(unsigned char *e_ident);
10+
void print_magic(unsigned char * e_ident);
11+
void print_class(unsigned char * e_ident);
12+
void print_data(unsigned char * e_ident);
13+
void print_version(unsigned char * e_ident);
14+
void print_abi(unsigned char * e_ident);
15+
void print_osabi(unsigned char * e_ident);
16+
void print_entry(unsigned long int e_entry, unsigned char *e_ident);
17+
void close_elf(int elf);
18+
19+
/**
20+
* check_elf - check if its ELF file
21+
* @e_ident: a pointer
22+
* Description: exit 98 if its not ELF file
23+
*/
24+
void check_elf(unsigned char *e_ident)
25+
{
26+
int start;
27+
28+
for (start = 0; start < 4; start++)
29+
{
30+
if (e_ident[start] != 127 &&
31+
e_ident[start] != 'E' &&
32+
e_ident[start] != 'L' &&
33+
e_ident[start] != 'F')
34+
{
35+
dprintf(STDERR_FILENO, "Error: Not an ELF file\n");
36+
exit(98);
37+
}
38+
}
39+
}
40+
41+
/**
42+
* print_magic - prints a magic number of an ELF header
43+
* @e_ident: a pointer
44+
* Description: print magic number
45+
*/
46+
void print_magic(unsigned char *e_ident)
47+
{
48+
int start;
49+
50+
printf(" Magic: ");
51+
52+
for (start = 0; start < EI_NIDENT; start++)
53+
{
54+
printf("%02x", e_ident[start]);
55+
56+
if (start == EI_NIDENT - 1)
57+
printf("\n");
58+
else
59+
printf(" ");
60+
}
61+
}
62+
63+
/**
64+
* print_class - prints the class of ELF header
65+
* @e_ident: a pointer
66+
* Description: print class
67+
*/
68+
void print_class(unsigned char *e_ident)
69+
{
70+
printf(" class: ");
71+
72+
switch (e_ident[EI_CLASS])
73+
{
74+
case ELFCLASSNONE:
75+
printf("none\n");
76+
break;
77+
case ELFCLASS32;
78+
printf("ELF32\n");
79+
break;
80+
case ELFCLASS64;
81+
printf("ELF64\n");
82+
break;
83+
default:
84+
printf("<unknown: %x>\n", e_ident[EL_CLASS]);
85+
}
86+
}
87+
88+
/**
89+
* print_data - print the data of ELF header
90+
* @e_ident: a pointer
91+
* Description: print data
92+
*/
93+
void print_data(unsigned char *e_ident)
94+
{
95+
print(" Data: ");
96+
97+
switch (e_ident[EL_DATA])
98+
{
99+
case ELFDATANONE:
100+
printf("none\n");
101+
break;
102+
case ELFDATA2LSB:
103+
printf("2's complement, little endian\n");
104+
break;
105+
case ELFDATA2MSB:
106+
printf("2's complement, big endian\n");
107+
break;
108+
default:
109+
printf("<unknown: %x>\n", e_ident[EL_CLASS]);
110+
}
111+
}
112+
113+
/**
114+
* print_version - prints the version of the ELF header
115+
* @e_ident: a pointer
116+
* Description: print version
117+
*/
118+
void print_version(unsigned char *e_ident)
119+
{
120+
printf(" Version: %d",
121+
e_ident[EI_VERSION]);
122+
123+
switch (e_ident[EI_VERSION])
124+
{
125+
case EV_CURRENT:
126+
printf(" (current)\n");
127+
break;
128+
default:
129+
printf("\n");

0x15-file_io/2-append_text_to_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* append_text_to_file - append text to the end of a file
55
* @filename: the name of the file
6-
* @text_content: NULL terminated string to the end of the file
6+
* @text_content: NULL terminated string to the end of the file
77
* Return: 1 or -1
88
*/
99
int append_text_to_file(const char *filename, char *text_content)

0 commit comments

Comments
 (0)