-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
991c97c
commit 9c0a459
Showing
19 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <windows.h> | ||
#include <stdio.h> | ||
#include <tchar.h> | ||
|
||
#define BUF_SIZE 512 | ||
|
||
static void CatFile(HANDLE hIn, HANDLE hOut); | ||
static void ReportError(LPCTSTR msg, DWORD errCode, BOOL showErrMsg); | ||
|
||
int _tmain(int argc, TCHAR* argv[]) { | ||
HANDLE hIn, hStdIn = GetStdHandle(STD_INPUT_HANDLE); | ||
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); | ||
BOOL dashS = FALSE; | ||
int iFirstFile = 1; | ||
|
||
/* Flow Step 1a: Parse Options */ | ||
for (int i = 1; i < argc; i++) { | ||
if (_tcscmp(argv[i], _T("-s")) == 0) { | ||
dashS = TRUE; | ||
iFirstFile++; | ||
} | ||
else if (argv[i][0] == _T('-')) { | ||
iFirstFile++; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
|
||
/* Flow Step 2: Input Source Decision */ | ||
if (iFirstFile >= argc) { | ||
CatFile(hStdIn, hStdOut); | ||
return 0; | ||
} | ||
|
||
/* Flow Step 3: File Processing Loop */ | ||
for (int i = iFirstFile; i < argc; i++) { | ||
hIn = CreateFile(argv[i], GENERIC_READ, 0, NULL, | ||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | ||
|
||
if (hIn == INVALID_HANDLE_VALUE) { | ||
if (!dashS) ReportError(_T("File open error"), GetLastError(), TRUE); | ||
continue; | ||
} | ||
|
||
CatFile(hIn, hStdOut); | ||
|
||
DWORD err = GetLastError(); | ||
if (err != 0 && !dashS) { | ||
ReportError(_T("Processing error"), err, TRUE); | ||
} | ||
|
||
CloseHandle(hIn); | ||
} | ||
return 0; | ||
} | ||
|
||
static void CatFile(HANDLE hIn, HANDLE hOut) { | ||
BYTE buffer[BUF_SIZE]; | ||
DWORD nRead, nWritten; | ||
|
||
while (ReadFile(hIn, buffer, BUF_SIZE, &nRead, NULL) && nRead > 0) { | ||
if (!WriteFile(hOut, buffer, nRead, &nWritten, NULL) || nWritten != nRead) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static void ReportError(LPCTSTR msg, DWORD errCode, BOOL showErrMsg) { | ||
_ftprintf(stderr, _T("ERROR: %s"), msg); | ||
|
||
if (showErrMsg) { | ||
LPVOID errMsg; | ||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | ||
FORMAT_MESSAGE_FROM_SYSTEM, | ||
NULL, errCode, 0, | ||
(LPTSTR)&errMsg, 0, NULL); | ||
|
||
_ftprintf(stderr, _T(" (%s)"), (LPTSTR)errMsg); | ||
LocalFree(errMsg); | ||
} | ||
_ftprintf(stderr, _T("\n")); | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() { | ||
std::string name; | ||
std::cout << "Enter your name: "; | ||
std::getline(std::cin, name); | ||
std::cout << "Hello, " << name << "!\n"; | ||
return 0; | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
use std::env; | ||
use std::ffi::OsString; | ||
use std::fs::File; | ||
use std::io::{self, Read, Write}; | ||
|
||
const BUF_SIZE: usize = 512; | ||
|
||
fn main() { | ||
let (dash_s, files) = parse_args(); | ||
|
||
if files.is_empty() { | ||
cat_stream(&mut io::stdin(), &mut io::stdout(), dash_s); | ||
return; | ||
} | ||
|
||
for path in files { | ||
match File::open(&path) { | ||
Ok(mut file) => { | ||
if let Err(e) = cat_stream(&mut file, &mut io::stdout(), dash_s) { | ||
if !dash_s { | ||
eprintln!("Error processing file: {}: {}", path.to_string_lossy(), e); | ||
} | ||
} | ||
} | ||
Err(e) => { | ||
if !dash_s { | ||
eprintln!("Error opening file: {}: {}", path.to_string_lossy(), e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn parse_args() -> (bool, Vec<OsString>) { | ||
let mut dash_s = false; | ||
let mut files = vec![]; | ||
|
||
for arg in env::args_os().skip(1) { | ||
if arg == "-s" { | ||
dash_s = true; | ||
} else if arg.to_str().map_or(false, |s| s.starts_with('-')) { | ||
// Ignore other options | ||
continue; | ||
} else { | ||
files.push(arg); | ||
} | ||
} | ||
|
||
(dash_s, files) | ||
} | ||
|
||
fn cat_stream<R: Read, W: Write>( | ||
input: &mut R, | ||
output: &mut W, | ||
suppress_errors: bool, | ||
) -> io::Result<()> { | ||
let mut buffer = [0u8; BUF_SIZE]; | ||
|
||
loop { | ||
let bytes_read = match input.read(&mut buffer) { | ||
Ok(0) => break, // EOF | ||
Ok(n) => n, | ||
Err(e) if suppress_errors => return Ok(()), | ||
Err(e) => return Err(e), | ||
}; | ||
|
||
let mut bytes_written = 0; | ||
while bytes_written < bytes_read { | ||
let write_result = output.write(&buffer[bytes_written..bytes_read]); | ||
|
||
match write_result { | ||
Ok(n) => bytes_written += n, | ||
Err(e) if suppress_errors => return Ok(()), | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
File | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
File | ||
2 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a test file. |