-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzzzmain.c
154 lines (131 loc) · 3.25 KB
/
zzzmain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// cat - concatenate files and print on the standard output
// 2022 - Fred Nora.
// See:
// https://man7.org/linux/man-pages/man1/cat.1.html
//#include <rtl/gramado.h>
//#include <types.h>
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define FALSE 0
#define TRUE 1
//4KB
#define __BufferSize (4*1024)
const char *VersionString = "Version 1.0";
const char *HelpString = "options: --help, --version, --number";
//...
//-----------------------------
static void doHelp(void);
static void doVersion(void);
// ...
//-----------------------------
static void doHelp(void)
{
printf("%s\n",HelpString);
}
static void doVersion(void)
{
printf("%s\n",VersionString);
}
int main(int argc, char *argv[])
{
FILE *fp;
static char buffer[__BufferSize]; // tmp
int fd=-1;
register int nreads = 0;
register int nwrites = 0;
size_t size=0;
int Max = 8; //#test
register int i=0;
// Flags
int fNumber=FALSE;
int fShowTabs=FALSE;
int fShowEnds=FALSE;
/*
// #debug
printf("CAT.BIN: argc %d | argv[0] %s | argv[1] %s\n",
argc, // quantos argumentos
argv[0], // CAT.BIN
argv[1] ); // FILE.TXT
printf("\n");
*/
if (argc <= 0){
printf("cat: No args\n");
goto fail;
}
if (argc == 1){
printf("cat: We need more args\n");
//call usage()
goto fail;
}
// Clear the tmp buffer.
// #todo: Actually we're gonna malloc the buffer
// based on the file size. I guess.
for (i=0; i<__BufferSize; i++){
buffer[i] = 0;
};
if (argc > Max){
printf("Too much files in the commnad line\n");
goto fail;
}
// Probe for some flags.
int isFlag=FALSE;
for (i=1; i<argc; i++)
{
isFlag=FALSE;
if ( strncmp( argv[i], "--help", 6) == 0 ){
doHelp();
isFlag=TRUE;
goto done;
}
if ( strncmp( argv[i], "--version", 9) == 0 ){
doVersion();
isFlag=TRUE;
goto done;
}
if ( strncmp( argv[i], "--number", 8) == 0 ){
fNumber = TRUE;
isFlag=TRUE;
}
if ( strncmp( argv[i], "--show-tabs", 11) == 0 ){
fShowTabs = TRUE;
isFlag=TRUE;
}
if ( strncmp( argv[i], "--show-ends", 11) == 0 ){
fShowEnds = TRUE;
isFlag=TRUE;
}
//
// It's not a flag.
//
// Open the file and print the content into the screen.
if (isFlag == FALSE)
{
fd = (int) open((char *) argv[i], 0, "a+");
if (fd < 0){
goto fail;
}
// Read from fd.
nreads = read( fd, buffer, 511 );
if (nreads <= 0){
printf ("cat: read() failed\n");
goto fail;
}
// Write on stdout.
nwrites = write( 1, buffer, sizeof(buffer) );
if (nwrites <= 0){
printf ("cat: File {%d} failed on write()\n", i);
goto fail;
}
}
};
done:
return EXIT_SUCCESS;
fail:
return EXIT_FAILURE;
}