Skip to content

Instantly share code, notes, and snippets.

@badescunicu
Last active August 29, 2015 14:16
Show Gist options
  • Save badescunicu/b4c7eee4ba18524ab903 to your computer and use it in GitHub Desktop.
Save badescunicu/b4c7eee4ba18524ab903 to your computer and use it in GitHub Desktop.
so_lab2_linux
/**
* SO, 2011
* Lab #2, Procese
*
* Task #2, Linux
*
* cat/cp applications
*/
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "utils.h"
#define BUFSIZE 32
int main(int argc, char **argv)
{
int fd_src, fd_dst, rc, bytesRead;
char buffer[BUFSIZE];
if (argc < 2 || argc > 3){
printf("Usage:\n\t%s source_file [destination_file]\n", argv[0]);
return 0;
}
/* TODO 1 - open source file for reading */
fd_src = open(argv[1], O_RDONLY);
if (fd_src < 0) {
perror("error at opening file!");
}
if (argc == 3) {
/* TODO 2 - redirect stdout to destination file */
fd_dst = open(argv[2], O_RDWR | O_CREAT, 0644);
if (fd_dst < 0) {
perror("error at opening dst file!");
}
close(1);
dup(fd_dst);
}
/* TODO 1 - read from file and print to stdout
use _only_ read and write functions */
char debugBuf[30];
bytesRead = read(fd_src, buffer, BUFSIZE);
printf("eroarea este %d \n", errno);
sprintf(debugBuf, "result of read %d\n", bytesRead);
write(1, debugBuf, strlen(debugBuf));
rc = write(1, buffer, bytesRead);
sprintf(debugBuf, "result of write %d\n", rc);
write(1, debugBuf, strlen(debugBuf));
while ((bytesRead = read(fd_src, buffer, BUFSIZE)) > 0) {
sprintf(debugBuf, "result of read %d\n", bytesRead);
write(1, debugBuf, strlen(debugBuf));
rc = write(1, buffer, bytesRead);
sprintf(debugBuf, "result of write %d\n", rc);
write(1, debugBuf, strlen(debugBuf));
}
/* TODO 1 - close file */
close(fd_dst);
close(fd_src);
return 0;
}
/**
* SO, 2011
* Lab #2, Operatii I/O simple
*
* Task #2, Windows
*
* Implementing simple crc method
*/
/* do not use UNICODE */
#undef _UNICODE
#undef UNICODE
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <utils.h>
#include <crc32.h>
#define BUFSIZE 512
#define CHUNKSIZE 32
static void GenerateCrc(CHAR *sourceFile, CHAR *destFile)
{
HANDLE hRead, hWrite;
CHAR buf[BUFSIZE];
BOOL bRet;
DWORD bytesRead, bytesWritten;
int crc;
/* TODO 1 - Open source file for reading */
hRead = CreateFile(sourceFile,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
DIE(hRead == INVALID_HANDLE_VALUE, "CreateFile failed");
/* TODO 1 - Create destination file for writing */
hWrite = CreateFile(destFile,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
/* read from file */
while (1) {
ZeroMemory(buf, sizeof(buf));
bRet = ReadFile(hRead,
buf,
BUFSIZE,
&bytesRead,
NULL);
DIE(bRet == FALSE, "ReadFile");
if (bytesRead == 0)
break;
/* TODO 1 - Read from into buf BUFSIZE bytes */
/* TODO 1 - Test for end of file */
/* calculate crc for buf */
crc = update_crc(0, (unsigned char*) buf, bytesRead);
/* TODO 1 - Write crc to destination file */
sprintf(buf, "%d", crc);
bRet = WriteFile(hWrite,
buf,
strlen(buf),
&bytesWritten,
NULL);
DIE(bRet == FALSE, "WriteFile");
}
/* TODO 1 - Close files */
bRet = CloseHandle(hRead);
DIE(bRet == FALSE, "CloseHandle");
bRet = CloseHandle(hWrite);
DIE(bRet == FALSE, "CloseHandle");
return;
}
static DWORD GetSize(HANDLE file)
{
DWORD dwSize;
dwSize = SetFilePointer(
file,
0, /* offset 0 */
NULL, /* no 64bytes offset */
FILE_END
);
/*
* TODO 2 - Calculate and return file
* size using SetFilePointer
*/
return dwSize;
}
static BOOL CompareFiles(CHAR *file1, CHAR *file2)
{
DWORD bytesRead;
HANDLE hFile1, hFile2;
CHAR chunk1[CHUNKSIZE], chunk2[CHUNKSIZE];
BOOL result = TRUE, bRet;
int crc1, crc2;
/* TODO 3 - Open file handles */
hFile1 = CreateFile(file1,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
DIE(hFile1 == INVALID_HANDLE_VALUE, "CreateFile failed");
hFile2 = CreateFile(file2,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
DIE(hFile2 == INVALID_HANDLE_VALUE, "CreateFile failed");
/* TODO 3 - Compare file size */
if (GetSize(hFile1) == GetSize(hFile2)) {
result = TRUE;
} else {
while (1) {
ZeroMemory(chunk1, sizeof(chunk1));
ZeroMemory(chunk2, sizeof(chunk2));
bRet = ReadFile(hFile1,
chunk1,
BUFSIZE,
&bytesRead,
NULL);
DIE(bRet == FALSE, "ReadFile");
if (bytesRead == 0) {
result = 0;
goto exit;
}
crc1 = update_crc(0, (unsigned char*) chunk1, bytesRead);
bRet = ReadFile(hFile2,
chunk2,
BUFSIZE,
&bytesRead,
NULL);
DIE(bRet == FALSE, "ReadFile");
if (bytesRead == 0) {
result = 0;
goto exit;
}
crc2 = update_crc(0, (unsigned char*) chunk2, bytesRead);
if (crc1 != crc2) {
result = FALSE;
goto exit;
}
}
}
exit:
/* TODO 3 - Close files */
bRet = CloseHandle(hFile1);
DIE(bRet == FALSE, "CloseHandle");
bRet = CloseHandle(hFile2);
DIE(bRet == FALSE, "CloseHandle");
return result;
}
int main(int argc, char *argv[])
{
BOOL equal;
if (argc != 4) {
fprintf(stderr,"Usage:\n"
"\t crc.exe -g <input_file> <output_file> - generate crc\n"
"\t crc.exe -c <file1> <file2> - compare files\n");
exit(EXIT_FAILURE);
}
if (strcmp(argv[1], "-g") == 0){
GenerateCrc(argv[2], argv[3]);
}
if (strcmp(argv[1], "-c") == 0){
equal = CompareFiles(argv[2], argv[3]);
if (equal)
printf("Files are equal\n");
else
printf("Files differ\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment