-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtee.cc
97 lines (80 loc) · 2.05 KB
/
tee.cc
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
/** Rohit Sindhu [sindh010]
* Aravind Alagiri Ramkumar [alagi005]
* Aparna Mahadevan [mahad028]
*/
/**
* Reads standard input and writes to standard output
* and the named file.
* A simple tee program for a simulated file system.
* @author Ray Ontko -> Converted to C++ by Kwangsung
*/
#include "Kernel.h"
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 4096
#define OUTPUT_MODE 0700
using namespace std;
int main(int argc, char ** argv)
{
char PROGRAM_NAME[8];
strcpy(PROGRAM_NAME, "tee");
char name[64];
memset(name, '\0', 64);
// initialize the file system simulator kernel
if(Kernel::initialize() == false)
{
cout << "Failed to initialized Kernel" << endl;
Kernel::exit(1);
}
if(argc < 2)
{
cout << PROGRAM_NAME << ": usage: " << PROGRAM_NAME << " output-file" << endl;
Kernel::exit( 1 ) ;
}
// give the command line argument a better name
strcpy(name, argv[1]);
// create the output file
int out_fd = Kernel::creat( name , OUTPUT_MODE ) ;
if( out_fd < 0 )
{
Kernel::perror( PROGRAM_NAME ) ;
cout << PROGRAM_NAME << ": unable to open output file \"" << name << "\"" << endl;
Kernel::exit( 2 ) ;
}
// create a buffer for reading from standard input
// char buffer[BUF_SIZE] ;
// memset(buffer, '\0', BUF_SIZE);
char c;
// while we can, read from standard input
int wr_count =0;
while(1)//cin.eof())
{
// read a buffer full of data from standard input
// cin.get(buffer, BUF_SIZE);
// if we reach the end (-1), quit the loop
//if(cin.eof())
//{
// break ;
//}
c = cin.get();
if(cin.eof())
{
break;
}
// write what we read to the output file; if error, exit
wr_count = Kernel::write(out_fd, &c, 1);//buffer, strlen(buffer));
if( wr_count <= 0 )
{
cout << endl;
Kernel::perror(PROGRAM_NAME);
cout << PROGRAM_NAME << ": error during write to output file"<< endl;
Kernel::exit( 3 ) ;
}
// write what we read to standard output
cout << c;//" -> " << wr_count << "bytes written" << endl;
}
// close the output file
Kernel::close( out_fd ) ;
// exit with success
Kernel::exit( 0 ) ;
}