Copy data from file X to file Y program in C - c

I tried to write basic program in C which copy data from file to another with given source path, destination path and buffer size as input.
my problem is the destination file filled with junk or something because its way larger than the source (get bigger depending on buffer size) and can't be open.
How do i read and write just the bytes in the source?
i'm working in linux, and this is the actually copying part:
char buffer[buffer_size];
int readable=1;
int writeable;
while(readable != 0){
readable = read(sourcef, buffer, buffer_size);
if(readable == -1){
close(sourcef);
close(destf);
exit_with_usage("Could not read.");
}
writeable = write(destf, buffer, buffer_size);
if(writeable == -1){
close(sourcef);
close(destf);
exit_with_usage("Could not write.");
}
}

writeable = write(destf, buffer, buffer_size);
must be
writeable = write(destf, buffer, readable);
Currently you do not write the number of characters you read but all the buffer, so the output file is too large
You also manage wrongly the end of the input file
The return value of read is :
On success, the number of bytes read is returned (zero indicates end of file)
On error, -1 is returned
A proposal :
/* you already check input and output file was open with success */
char buffer[buffer_size];
for(;;){
ssize_t readable = read(sourcef, buffer, buffer_size);
if(readable <= 0){
close(sourcef);
close(destf);
if (readable != 0)
/* not EOF */
exit_with_usage("Could not read.");
/* EOF */
break;
}
if (write(destf, buffer, n) != n) {
close(sourcef);
close(destf);
exit_with_usage("Could not write.");
}
}
I suppose exit_with_usage calls exit() so does not return
Note in theory write may write less than the expected number of characters without being an error, and the write has to be done in a loop, but in that case it is useless to manage that

read function returns how many bytes were read to buffer(which has buffer_size). Its not always the case actual bytes read has same value as buffer size(consider scenario if there are not enough bytes left in source file to fully fill your buffer). So you should write to destination file not buffer_size(third argument of the write function), but how many bytes have you read - that is readable variable in your code

You should exit when readable returns an error.So
while(readable != 0){
should be
while(readable != -1){
So that loop could be terminataed when an readfile is exhausted.
You see currently after the whole readfile has been read, calling read fails but write is being called repeatedly since execution has no exit path for failure on read. Also write should only write the number of bytes read. So the code would look like this:
char buffer[buffer_size];
int readable=1;
int writeable;
while(readable != -1){
readable = read(sourcef, buffer, buffer_size);
if(readable == -1){
close(sourcef);
close(destf);
exit_with_usage("Could not read.");
}
writeable = write(destf, buffer, readable);
if(writeable == -1){
close(sourcef);
close(destf);
exit_with_usage("Could not write.");
}
}

Simple code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For system calls write, read e close
#include <fcntl.h>
#define BUFFER_SIZE 4096
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage %s Src_file Dest_file\n", argv[0]);
exit(1);
}
unsigned char buffer[BUFFER_SIZE] = {0};
ssize_t ReadByte = 0;
int src_fd, dst_fd;
// open file in read mode
if ((src_fd = open(argv[1], O_RDONLY)) == -1) {
printf("Failed to open input file %s\n", argv[1]);
exit(1);
}
// open file in write mode and already exists to overwrite
if ((dst_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 644)) == -1) {
printf("Failed to create output file %s\n", argv[2]);
exit(1);
}
// loop
while (1) {
// read buffer
ReadByte = read(src_fd, buffer, sizeof(buffer));
// error with reading
if (ReadByte == -1) {
printf("Encountered an error\n");
break;
} else if (ReadByte == 0) {
// file end exit loop
printf("File copying successful.\n");
break;
}
// error with writing
if (write(dst_fd, buffer, ReadByte) == -1) {
printf("Failed to copying file\n");
break;
}
}
// Close file
close(src_fd);
close(dst_fd);
exit(0);
}
Run
./program src_file dest_file

Related

Problems with C XOR executable file encryption / decryption

I'm trying to create a simple XOR crypter / decrypter in C for .exe files. I'm still pretty new in C and don't understand everything yet, especially memory stuff. So I've been following an online tutorial on how to make a simple XOR string crypter which worked fine. Now I wanted to modify it so I can en/decrypt executable files and decided to utilize the fwrite() and fread() functions. This is what I've come up with:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // execve function
#define XOR_KEY 0xAA // key
#define JOB_CRYPT 1 // alter flow depending on the job
#define JOB_DECRYPT 2
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
void xorFile (char *infile, char *outfile) {
FILE *nFile, *eFile;
long nFileSize; // store file size of the file we want to read
char *buffer; // buffer for reading
char *eBuffer; // buffer for storing encrypted data
size_t rResult;
size_t wResult;
///// READ FILE /////
nFile = fopen(infile, "rb");
if(nFile == NULL) {
fputs("Error opening file...", stderr);
exit(EXIT_FAILURE);
}
fseek(nFile, 0, SEEK_END);
nFileSize = ftell(nFile);
rewind(nFile);
buffer = (char *) malloc(sizeof(char) * nFileSize);
if(buffer == NULL) {
fputs("Error allocating memory...", stderr);
exit(EXIT_FAILURE);
}
rResult = fread(buffer, 1, nFileSize, nFile);
if(rResult != nFileSize) {
fputs("Error reading file...", stderr);
exit(EXIT_FAILURE);
}
fclose(nFile);
printf("File size is: %ld\n", nFileSize);
printf("Buffer size is (pointer): %u\n", sizeof(buffer));
printf("Reading result: %lu\n", rResult);
////// WRITE TO FILE //////
eFile = fopen(outfile, "wb");
if(eFile == NULL) {
fputs("Error creating file...", stderr);
exit(EXIT_FAILURE);
}
eBuffer = (char *) malloc(sizeof(char) * nFileSize);
if(eBuffer == NULL) {
fputs("Error allocating memory (2)...", stderr);
exit(EXIT_FAILURE);
}
// encrypt byte by byte and save to buffer
printf("Proceeding with encryption!\n");
for(int i = 0; buffer[i] != EOF; i++) {
eBuffer[i] = buffer[i] ^ XOR_KEY;
}
printf("Proceeding with fwrite()!\n");
wResult = fwrite(eBuffer, 1, nFileSize, eFile);
fclose(eFile);
printf("eBuffer size is (pointer)%u\n", sizeof(eBuffer));
printf("Writing result: %lu\n", wResult);
free(buffer); // free buffers in heap
free(eBuffer);
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
// checking if all parameters were given
if(argc < 4) {
fprintf(stderr, "Usage: %s [CRYPT | DECRYPT] [IN-FILE] [OUT-FILE]\n", argv[0]);
exit(EXIT_FAILURE);
}
int job;
// DOLOCIMO JOB
if(strcmp(argv[1], "CRYPT") == 0) {
job = JOB_CRYPT;
} else if (strcmp(argv[1], "DECRYPT") == 0) {
job = JOB_DECRYPT;
} else {
fprintf(stderr, "Please select [CRYPT | DECRYPT]!");
exit(EXIT_FAILURE);
}
// CRYPT/DECRYPT OUR FILE
xorFile(argv[2], argv[3]);
if(job == JOB_DECRYPT) {
char *args[] = {argv[3], NULL};
int errExec = execve(args[0], args, NULL);
if(errExec == -1) {
perror("Error executing file...");
exit(EXIT_FAILURE);
}
}
return 0;
}
I'm sorry for the ugly looking code but I first wanted to make it work and I'll refine it later.
Anyways, when I run it in command prompt, the encryption works fine, it generates an encrypted file, but when I run the decrpytion job, the program
crashes during the decryption process. Here's a picture of what happens so you can imagine it better.
Since I have less than 10 reputation, I'm not allowed to embedd pictures.
Here is a link to Imgur.
What's going wrong here? Am I creating a buffer overflow when I'm decrypting it?
Thank you!
Here's the problem:
for(int i = 0; buffer[i] != EOF; i++) {
eBuffer[i] = buffer[i] ^ XOR_KEY;
}
Binary files can contain bytes with any value. So the EOF value is valid and does not designate the end of the file. This means that if the file contains a byte with this value, the loop will quit early and you won't XOR all the bytes. If the file does not contain this byte, the loop will run past the end of the allocated memory which invokes undefined behavior which in this case manifests in a crash.
You know how many bytes you need to processes, so use that as your loop control:
for(int i = 0; i < nFileSize; i++) {
A few other minor corrections:
buffer = (char *) malloc(sizeof(char) * nFileSize);
if(buffer == NULL) {
fputs("Error allocating memory...", stderr);
exit(EXIT_FAILURE);
}
Don't cast the return value of malloc. Also, sizeof(char) is 1 by definition, so you can leave that out.
Also, if a system or library function fails, you should use perror to print the error message. This will print additional information regarding why the function failed.
buffer = malloc(nFileSize);
if(buffer == NULL) {
perror("Error allocating memory...");
exit(EXIT_FAILURE);
}

C can't open file using a variable

I need to open a file located on Desktop(Linux). If i write the location as a string inside the fopen() function it works, but if i pass it as a variable, it doesn't work. Here is my code :
fp = fopen(readPathToFile, "r");
if (!fp){
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
If i write it like this, it shows me the content of file :
fp = fopen("home/user/Desktop/test.txt", "r");
if (!fp){
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
The child process opens the file. Here is my full code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define READ 0
#define WRITE 1
int main ()
{
pid_t pid;
int mypipefd[2];
id_t child_pid;
char line[100];
char *pathToFile[100];
FILE *fp;
char buff[255];
/* create the pipe */
if (pipe(mypipefd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
child_pid = fork () ;
if (child_pid > 0) {
printf("Introduceti locatia catre fisier:");
fgets(pathToFile, 100, stdin);
close(mypipefd[READ]);
write(mypipefd[WRITE], &pathToFile, sizeof(pathToFile));
close(mypipefd[WRITE]);
printf("parent: write value : %s",pathToFile);
}
else if (child_pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
else{
char *readPathToFile[100];
close(mypipefd[WRITE]);
read(mypipefd[READ], &readPathToFile, sizeof(readPathToFile));
close(mypipefd[READ]);
printf("child: read value : %s",readPathToFile);
fp = fopen(readPathToFile, "r");
if (!fp)
{
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
}
return 0;
}
Your compiler did not warn you about the type mismatch in
char *pathToFile[100];
fgets(pathToFile, 100, stdin);
(array of 100 pointers-to-char versus array of 100 chars)? Did you turn warnings off?
Also note that fgets retains the newline. Your file name probably does not end with a newline. You should replace it with a NUL (zero) byte.
Typically you don't need a debugger to track these down. A little bit of printf debugging can do wonders. :-)
Okay, so this is the root of your problem:
char *pathToFile[100];
This declares pathToFile as a 100-element array of pointers to char, not a 100-element array of char. The first thing you need to do is change that declaration to
char pathToFile[100];
Secondly, fgets will save the trailing newline from your input to the target buffer if there's room, so you'll need to remove that newline from the input:
char *newline = strchr( pathToFile, '\n' );
if ( newline )
*newline = 0;

Read a file a number of bytes per time in c

I am trying to write a program on how to read a file 10 bytes per time using read, however, I do not know how to go about it. How should I modify this code to read 10bytes per time. Thanks!!!!
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
int main (int argc, char *argv[])
{
printf("I am here1\n");
int fd, readd = 0;
char* buf[1024];
printf("I am here2\n");
fd =open("text.txt", O_RDWR);
if (fd == -1)
{
perror("open failed");
exit(1);
}
else
{
printf("I am here3\n");
if(("text.txt",buf, 1024)<0)
printf("read error\n");
else
{
printf("I am here3\n");
/*******************************
* I suspect this should be the place I make the modification
*******************************/
if(read("text.txt",buf, 1024)<0)
printf("read error\n");
else
{
printf("I am here4\n");
printf("\nN: %c",buf);
if(write(fd,buf,readd) != readd)
printf("write error\n");
}
}
return 0;
}
The final parameter of read() is the maximum size of the data you wish to read so, to try and read ten bytes at a time, you would need:
read (fd, buf, 10)
You'll notice I've also changed the first parameter to the file descriptor rather than the file name string.
Now, you'll probably want that in a loop since you'll want to do something with the data, and you also need to check the return value since it can give you less than what you asked for.
A good example for doing this would be:
int copyTenAtATime (char *infile, char *outfile) {
// Buffer details (size and data).
int sz;
char buff[10];
// Try open input and output.
int ifd = open (infile, O_RDWR);
int ofd = open (outfile, O_WRONLY|O_CREAT);
// Do nothing unless both opened okay.
if ((ifd >= 0) && (ofd >= 0)) {
// Read chunk, stopping on error or end of file.
while ((sz = read (ifd, buff, sizeof (buff))) > 0) {
// Write chunk, flagging error if not all written.
if (write (ofd, buff, sz) != sz) {
sz = -1;
break;
}
}
}
// Finished or errored here, close files that were opened.
if (ifd >= 0) close (ifd);
if (ofd >= 0) close (ofd);
// Return zero if all okay, otherwise error indicator.
return (sz == 0) ? 0 : -1;
}
change the value in read,
read(fd,buf,10);
From man of read
ssize_t read(int fd, void *buf, size_t count);
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
if(read("text.txt",buf, 1024)<0)// this will give you the error.
First argument must be an file descriptor.

Read all characters written in FIFO using open() system call

I have a FIFO pipe, which is opened at both ends using open() in O_RDWR mode. At the reading end, read() is not reading all the characters, but lesser than that specified in the call. Is there a way to ensure that all characters are read using open()?
Thanks in advance
if (p != NULL){
printf("Inside p not null!\n");
if((fd = open(p, O_RDWR)) < 0){
perror("File could not be opened!");
exit(EXIT_FAILURE);
}
//FILE *rdptr = fopen(p,"r");
memset(buf,0,file_len);
rc = read(fd, buf, file_len);
printf("Number of bytes read: %d\n", rc);
printf("Data detected on FIFO\n");
buf[rc] = '\0';
char base[20] = "output.txt";
char name[20];
sprintf(name, "%d%s", suffix, base);
FILE *fptr = fopen(name,"ab+");
fd_wr = open(name,O_WRONLY);
charnum = write(fd_wr,buf,rc);
kill(id_A, SIGKILL);
//printf("No. of characters written: %d\n",charnum);
//FD_CLR(fd, &rdfs);
}
First minor comment: you should use O_RDONLY to open the file: don't use more permissions than necessary.
Second issue: if file_len is very large, it's possible that the writer has blocked trying to write the entire chunk of data (since a FIFO can only hold a certain amount of unread data). If that's the case, then read will only read the data that has been stored in the FIFO, and will immediately return with whatever it could read. This will allow the writer to write more bytes, which will then be read in the next read.
You should loop reads, adjusting an offset into the buffer, until the entire file_len bytes are read. Something like this:
size_t offset = 0;
while(offset < file_len) {
rc = read(fd, buf+offset, file_len-offset);
if(rc < 0) {
/* handle I/O error or something... */
} else {
offset += rc;
}
}

File reading as a resource

I have a "file" as a resource. I can only use read(), write() and fstat() it. This file is a text file which I would like to parse.
Normally I use fgets() to read the text file line by line and parse it. How can I do this in this case?
FILE *fp;
char buffer[128];
fp = fopen( "/home/txtfile", "r" );
if (fp == NULL){
perror("file missing");
}
while (fgets (buffer, sizeof(buffer), fp) != NULL) {
//some code
}
How can I do the same with read() ?
Is this right?
int fd = open("/dev/file",O_RDONLY);
if (fd==-1) {
printf("Failed to open file!!!\n");
}
while (fgets (buffer, sizeof (buffer), fd) != NULL) {
//some code
}
Unless your file is huge, if you're using read(), it would be easier to read in the entire file, then operate on the memory buffer, rather than in discrete chunks. That is, unless each line is of a fixed length.
I'd do something like this:
int rc;
int fd = open("data", O_RDONLY); // open the file for reading
if (fd == -1) {
// error
}
// to be thorough, do a stat() here to find how big to make the buffer
struct stat sb;
rc = fstat(fd, &sb);
if (rc == -1) {
// error
}
char *buffer = calloc(1, sb.st_size);
int bytes_read = 0;
// read in entire file; each read() can be incomplete, hence why it's in a loop,
// and reading from/writing to increasing sections of the memory and file
while ((rc = read(fd, (buffer + bytes_read), (sb.st_size - bytes_read))) > 0) {
if (rc == -1) {
// error
}
bytes_read += rc;
}
close(fd);
// Now, to read it line-by-line...
char line[128]; // 128 is arbitrary
int pos = 0;
while ((rc = sscanf(buffer + pos, "%127[^\n]\n", line)) > 0) {
pos += strlen(line) + 1;
// do stuff with line
}
return 0;
Then you can operate on your memory buffer line-by-line by scanning for newlines, or using sscanf(). Also make sure to free() your buffer!
Edit: I've added some example code for using sscanf() to handle your buffer. If you know the format of the lines (you say you're parsing them) you might be able to make better use of sscanf() by using the format specifiers. All of this is untested, by the way.
Something like this :
int fd = open("/dev/file",O_RDONLY);
ssize_t res = 0;
while((res = read(fd, buffer, sizeof(buffer))) > 0) {
//some code
}
if (res < 0) {
//handle error
} else{
//close fd
}
Is this right?
No.
read() is a system call that operates on a Unix file descriptor, not a stdio FILE*. Other than that, it works by reading data from the file and putting it in the buffer you supply.
int fd = open("/dev/file",O_RDONLY);
if (fd==-1)
{
printf("Failed to open file!!!\n");
}
else
{
char buffer[BUF_SIZE];
ssize_t bytesRead = read(fd, buffer, BUF_SIZE);
while (bytesRead > 0)
{
// do something with the buffer
bytesRead = read(fd, buffer, BUF_SIZE);
}
if (bytesRead == -1)
{
// error
}
// bytesRead == 0 => end of file
}

Resources