Printing a number of lines using low level i/o in C - c

I am new to C and I'm trying to get familiar with low level I/O such as read() and write(), and I'm trying to print lines to standard out from a file using it, but I can't figure out how to do it while still only using low level functions.
Heres what I have so far, any suggestions would be super appreciated
I tried to iterate through the char array I have and checking for '\n' but it doesn't seem to be doing the trick for me.
EDIT: Sorry, here's the code to be copied
int lineCounter = 0;
char* filename = argv[3];
int fd = open(filename, O_RDONLY);
//off_t size = lseek(fd, 0, SEEK_END);
if (fd == -1) perror("open");
int n;
char buffer[BUFFSIZE];
while ((n = read(fd, buffer, 1)) > 0) {
write(STDOUT_FILENO, buffer, 1);
}
for (int i = 0; i < BUFFSIZE; i++) {
if (buffer[i] == '\n') {
lineCounter++;
}
}

corrections
int lineCounter = 0;
char* filename = argv[3];
int fd = open(filename, O_RDONLY);
//off_t size = lseek(fd, 0, SEEK_END);
if (fd == -1) perror("open");
int n;
char buffer[BUFFSIZE];
while ((n = read(fd, buffer, BUFFSIZE)) > 0) { <<<=== read uoto BUFFSIZE
write(STDOUT_FILENO, buffer, n); <<<<=== write out the same number of read bytes
for (int i = 0; i < n; i++) { <<<<=== put line counter inside loop to read file && loop over read number of chars
if (buffer[i] == '\n') {
lineCounter++;
}
}
}

Related

testing the program for various memory allocation errors and memory leaks

The tee utility copies its standard input to both stdout and to a file. This allows the user to view the output of a command on the console while writing a log to a file at the same time.
My program implements the tee command from linux POSIX system calls, with the -a option.
How can I modify the program to test for possible memory allocation errors? Positive memory leaks.
Also, the memory allocation doesn't seem right to me. When creating a new buffer each time I call getline(), should I declare and initialize line outside the loop and reallocate it only after the loop has ended?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "apue.h"
int writeAll(int fd, char *buf, int buflen);
int main(int argc, char *argv[]) {
struct stat status;
int option;
bool append = false;
int errCode = 0;
while ((option = getopt(argc, argv, "a")) != -1) {
switch (option) {
case 'a':
append = true;
break;
}
}
// We need to write in all the files given as parameter AND stdout.
int numFileDescriptors = argc - optind + 1;
int *fileDescriptors = malloc((numFileDescriptors + 1) * sizeof(*fileDescriptors));
char **fileNames = malloc((numFileDescriptors + 1) * sizeof(*fileNames));
int lastFileDescriptor = 0;
fileDescriptors[0] = STDOUT_FILENO;
fileNames[0] = "stdout";
int flags = O_CREAT | O_WRONLY;
if (append) {
flags = flags | O_APPEND;
} else {
flags = flags | O_TRUNC;
}
for (int i = optind; i < argc; i++) {
if (access(argv[i], F_OK) == 0) {
if (access(argv[i], W_OK) < 0) {
err_msg("%s: Permission denied", argv[i]);
errCode = 1;
continue;
}
}
if (lstat(argv[i], &status) < 0) {
status.st_mode = 0;
}
if (S_ISDIR(status.st_mode)) {
err_msg("%s: Is a directory", argv[i]);
errCode = 1;
continue;
}
int fd = open(argv[i], flags, 0644);
if (fd < 0) {
err_msg("%s: Failed to open", argv[i]);
errCode = 1;
continue;
}
lastFileDescriptor = lastFileDescriptor + 1;
fileDescriptors[lastFileDescriptor] = fd;
fileNames[lastFileDescriptor] = argv[i];
}
while (true) {
size_t len = 0;
ssize_t read = 0;
char *line = NULL;
read = getline(&line, &len, stdin);
if (read == -1) {
break;
}
for (int i = 0; i <= lastFileDescriptor; i++) {
int written = writeAll(fileDescriptors[i], line, strlen(line));
if (written < 0) {
err_msg("%s: Failed to write", fileNames[i]);
errCode = 1;
}
}
}
for (int i = 0; i <= lastFileDescriptor; i++) {
close(fileDescriptors[i]);
}
free(fileDescriptors);
free(fileNames);
return errCode;
}
int writeAll(int fd, char *buf, int buflen) {
ssize_t written = 0;
while (written < buflen) {
int writtenThisTime = write(fd, buf + written, buflen - written);
if (writtenThisTime < 0) {
return writtenThisTime;
}
written = written + writtenThisTime;
}
return written;
}
Testing for memory allocation failure is simple: just add tests, report the failure and exit with a non zero exit status.
To avoid memory leaks, you must free the line that was allocated by getline inside the while (true) loop:
while (true) {
size_t len = 0;
char *line = NULL;
ssize_t nread = getline(&line, &len, stdin);
if (nread == -1) {
if (errno == ENOMEM) {
fprintf(stderr, "out of memory\n");
exit(1);
}
free(line);
break;
}
for (int i = 0; i <= lastFileDescriptor; i++) {
int written = writeAll(fileDescriptors[i], line, nread);
if (written < 0) {
err_msg("%s: Failed to write", fileNames[i]);
errCode = 1;
}
}
free(line);
}
Alternately, you can reuse the same line for the next iteration and only free the buffer after the while loop:
size_t len = 0;
char *line = NULL;
while (true) {
ssize_t nread = getline(&line, &len, stdin);
if (nread == -1) {
if (errno == ENOMEM) {
fprintf(stderr, "out of memory\n");
exit(1);
}
break;
}
for (int i = 0; i <= lastFileDescriptor; i++) {
int written = writeAll(fileDescriptors[i], line, nread);
if (written < 0) {
err_msg("%s: Failed to write", fileNames[i]);
errCode = 1;
}
}
}
free(line);
Note that reading a full line at a time is risky as the input might contain very long, possibly unlimited lines (eg: /dev/zero). You might want to use fgets() to read a line with a limited length and dispatch the contents as you read, possibly splitting long lines:
char line[4096];
while (fgets(line, sizeof line, stdin)) {
size_t len = strlen(line);
for (int i = 0; i <= lastFileDescriptor; i++) {
int written = writeAll(fileDescriptors[i], line, len);
if (written < 0) {
err_msg("%s: Failed to write", fileNames[i]);
errCode = 1;
}
}
}
The above code has a limitation: if the input streams contains null bytes, they will cause some data to be lost in translation. A solution is to not use fgets(), but getchar() directly:
for (;;) {
char line[4096];
size_t len = 0;
int c;
while (len < sizeof(line) && (c = getchar()) != EOF)) {
if ((line[len++] = c) == '\n')
break;
}
if (len > 0) {
for (int i = 0; i <= lastFileDescriptor; i++) {
int written = writeAll(fileDescriptors[i], line, len);
if (written < 0) {
err_msg("%s: Failed to write", fileNames[i]);
errCode = 1;
}
}
}
if (c == EOF)
break;
}

Changing STDOUT to file in ncat source code

I managed to compile ncat. I am using -k option to keep server open. Instead of accepting data to STDOUT, my goal is to write to files instead. So far I was able to write to a file instead of STDOUT but my goal is to loop through new files on each new connection. Right now it is appending to the same filename_0 and f++ is not incrementing. Here is what I have so far. The original code will be below. The difference is in the else clause, basically if n is actually greater than 0. On each loop, n is 512 bytes until the last chunk. I just want to be able to have new files from each new connection. filename_0, filename_1, filename_3, etc.
MODIFIED CODE:
/* Read from a client socket and write to stdout. Return the number of bytes
read from the socket, or -1 on error. */
int read_socket(int recv_fd)
{
char buf[DEFAULT_TCP_BUF_LEN];
struct fdinfo *fdn;
int nbytes, pending;
int f = 0;
fdn = get_fdinfo(&client_fdlist, recv_fd);
ncat_assert(fdn != NULL);
nbytes = 0;
do {
int n, s;
n = ncat_recv(fdn, buf, 512, &pending);
if (n <= 0) {
if (o.debug)
logdebug("Closing fd %d.\n", recv_fd);
#ifdef HAVE_OPENSSL
if (o.ssl && fdn->ssl) {
if (nbytes == 0)
SSL_shutdown(fdn->ssl);
SSL_free(fdn->ssl);
}
#endif
close(recv_fd);
checked_fd_clr(recv_fd, &master_readfds);
rm_fd(&client_fdlist, recv_fd);
checked_fd_clr(recv_fd, &master_broadcastfds);
rm_fd(&broadcast_fdlist, recv_fd);
conn_inc--;
if (get_conn_count() == 0)
checked_fd_clr(STDIN_FILENO, &master_readfds);
return n;
}
else {
char filename[20];
snprintf(filename, sizeof(char) * 20, "filename_%i", f);
FILE *fp = fopen(filename, "a");
if (fp == NULL)
{
printf("Could not open file");
return 0;
}
//Write(STDOUT_FILENO, buf, n);
s = fwrite(buf, 1, n, fp);
fclose(fp);
f++;
nbytes += n;
}
} while (pending);
return nbytes;
}
ORIGINAL CODE:
int read_socket(int recv_fd)
{
char buf[DEFAULT_TCP_BUF_LEN];
struct fdinfo *fdn;
int nbytes, pending;
fdn = get_fdinfo(&client_fdlist, recv_fd);
ncat_assert(fdn != NULL);
nbytes = 0;
do {
int n;
n = ncat_recv(fdn, buf, sizeof(buf), &pending);
if (n <= 0) {
if (o.debug)
logdebug("Closing fd %d.\n", recv_fd);
#ifdef HAVE_OPENSSL
if (o.ssl && fdn->ssl) {
if (nbytes == 0)
SSL_shutdown(fdn->ssl);
SSL_free(fdn->ssl);
}
#endif
close(recv_fd);
checked_fd_clr(recv_fd, &master_readfds);
rm_fd(&client_fdlist, recv_fd);
checked_fd_clr(recv_fd, &master_broadcastfds);
rm_fd(&broadcast_fdlist, recv_fd);
conn_inc--;
if (get_conn_count() == 0)
checked_fd_clr(STDIN_FILENO, &master_readfds);
return n;
}
else {
Write(STDOUT_FILENO, buf, n);
nbytes += n;
}
} while (pending);
return nbytes;
}
I was able to figure out using the other functions involved. i passed a pointer into this function to write to it. the handler is a function i added the open() file pointer to.

Is there a way to make this code concise?

I'm currently learning and practicing c, but the exercise I'm doing wants each functions to have 25 lines limit (without changing { } or using single-line if statements!)
Please help if there's a way to make this even shorter.
void ft_write_file(void)
{
char c;
int fd;
int i;
i = 0;
if ((fd = open("write_exam", O_WRONLY | O_TRUNC | O_CREAT, 00777)) == -1)
{
ft_putstr("map error");
return ;
}
while (read(0, &c, 1))
{
write(fd, &c, 1);
if (c == '\n')
break ;
ft_allocate_g_var(i, c, 0);
i++;
}
int j = 0;
while (j < g_line)
{
while (read(0, &c, 1))
{
write(fd, &c, 1);
if (c == '\n')
break ;
}
j++;
}
close(fd);
}
To start with, don't try to do two things in one function. And try to write your functions with sensible arguments instead of hard-coding their subjects.
For example, your function is really doing two things:
Finding and potentially creating the output file (with a hard-coded name).
Copying the entire contents of one stream (hard-coded to stdin) to another stream.
So you could break that down: (prototypes only)
/* Returns fd or -1 on error */
int open_output(const char* name);
/* Returns number of bytes copied or -1 on error */
ssize_t copy_fd(int fd_dest, int fd_source);
Then your driver could be:
ssize_t copy_stdin_to_file(const char *name)
{
int fd = open_output(name);
if (fd < 0)
{
ft_putstr("Could not open output file");
return -1;
}
ssize_t copied = copy_fd(fd, 0);
if (copied < 0) {
ft_putstr("Could not write data.");
return copied;
}
}
A simple way would be to declare all the variables at the top in one line, for exemple :
char c; int fd; int i; i = 0;
Except from that I dont know, hope it can help a bit a least !

read integer from file with read() c

i have a problem with file read() function. My file is like this:
4boat
5tiger
3end
Where the number is the length of the string that follows. I need to read integer and string from input file and print them out on stdoutput, using low level I/O. This is my code:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
int main(int argc, char *argv[]){
int *len, fd, r_l, r_s;
char *s;
fd=open(argv[1], O_RDONLY);
if(fd>=0){
do{
r_l=read(fd, len, sizeof(int));
r_s=read(fd, s, (*len)*sizeof(char));
if(r_l>=0){
write(1, len, sizeof(int));
write(1, " ",sizeof(char));
}
if(r_s>=0)
write(1, s, (*len)*sizeof(char));
}while(r_l>=0 && r_s>=0);
}
return 0;
}
But it not works =/
You did not allocate space for the poitner len, you need to allocate space for it and you can simply do it by declaring it as int len; so it gets allocated in the stack and you don't need to handle it's allocation manually, so it would be something like this
int main(void) {
int len, fd, r_l, r_s;
char *s;
fd = open(argv[1], O_RDONLY);
if (fd >= 0) {
do {
r_l = read(fd, &len, sizeof(int));
s = malloc(len); /* <--- allocate space for `s' */
r_s = 0;
if (s != NULL)
r_s = read(fd, s, len);
if (r_l >= 0) {
write(1, &len, sizeof(int));
write(1, " ", 1);
}
if ((r_s >= 0) && (s != NULL))
write(1, s, len);
free(s);
} while (r_l >= 0 && r_s >= 0);
close(fd);
}
return 0;
}
you also didn't allocate space for s which is another problem, I did allocate space for s in the corrected code above by using malloc().
And sizeof(char) == 1 by definition, so you don't need that.
Although, the code above will not have the errors your code has, which invoke undefined behavior, it will not do what you expect, because your data cannot be read with this algorithm.
The numbers in your file are not really integers, they are characters, so what you really need is this
int main(void) {
char chr;
int len, fd, r_l, r_s;
char *s;
fd = open(argv[1], O_RDONLY);
if (fd >= 0) {
do {
r_l = read(fd, &chr, 1);
len = chr - '0';
s = malloc(len); /* <--- allocate space for `s' */
r_s = 0;
if (s != NULL)
r_s = read(fd, s, len);
if (r_l >= 0) {
printf("%d ", len);
}
if ((r_s >= 0) && (s != NULL))
write(1, s, len);
free(s);
} while (r_l >= 0 && r_s >= 0);
close(fd);
}
return 0;
}

I read more than I write in file

I have a file, partitioned in fixed sized blocks. I am copying a test_file.txt into the 3rd block of the file. I read and copied 18 bytes.
Then I am trying to copy from the file that very same .txt file I just imported to a newly created .txt, but I am writing 256 bytes to the new file. Moreover, when I try to read it, it is full of garbage.
The first function is used to import the .txt and the second one to export it.
void copy_file(int mfs_desc, char* filename, Superblock* s, MDS mds) {
if(mds.size == 0)
return;
char buffer[s->block_size];
int i = 0;
for (; i < s->block_size; ++i) {
buffer[i] = '\0';
}
int source_desc = open(filename, O_RDONLY);
// error handling
if (source_desc == -1) {
perror("opening file in copy file");
exit(1);
}
ssize_t nread;
int total = 0;
off_t seek = lseek(mfs_desc,
sizeof(Superblock) + mds.datablocks[0] * s->block_size,
SEEK_SET);
printf("offset = %d\n", mds.datablocks[0]);
if (seek < 0) {
perror("seek");
exit(1);
}
total = 0;
while ((nread = read(source_desc, buffer, s->block_size)) > 0) {
total += nread;
write(mfs_desc, buffer, s->block_size);
}
printf("read and copied: %d bytes\n", total);
if (close(source_desc) == -1) {
perror("closing file in copy file");
exit(1);
}
}
int copy_file_export(int mfs_desc, char* filename, Superblock s, MDS mds) {
if(mds.size == 0) {
printf("File is empty, abort\n");
return 0;
}
char buffer[s.block_size];
int i = 0;
for (; i < s.block_size; ++i) {
buffer[i] = '\0';
}
int destination_desc = open(filename, O_CREAT | O_WRONLY);
// error handling
if (destination_desc == -1) {
printf("filename = |%s|\n", filename);
perror("opening file in copy file export");
exit(1);
}
ssize_t nread;
int total = 0;
off_t seek = lseek(mfs_desc,
sizeof(Superblock) + mds.datablocks[0] * s.block_size,
SEEK_SET);
printf("offset = %d\n", mds.datablocks[0]);
if (seek < 0) {
perror("seek");
exit(1);
}
for(i = 0; i < mds.size; ++i) {
nread = read(mfs_desc, buffer, s.block_size);
total += nread;
write(destination_desc, buffer, nread);
}
printf("wrote: %d bytes\n", total);
if (close(destination_desc) == -1) {
perror("closing file in copy file");
exit(1);
}
return 1;
}
Output:
import test_file.txt ... / <-- just a command
offset = 2
read and copied: 18 bytes
export test_file.txt ... ../../ <-- just a command
offset = 2
wrote: 256 bytes
What I am doing wrong?
I would replace
write(mfs_desc, buffer, s->block_size);
with
write(mfs_desc, buffer, nread);
In this chunk of code:
while ((nread = read(source_desc, buffer, s->block_size)) > 0) {
total += nread;
write(mfs_desc, buffer, s->block_size);
}
You're very likely handling the last write() incorrectly. You need to write only the bytes you read.
write(mfs_desc, buffer, nread);
Also, these lines are most likely bogus:
char buffer[s->block_size];
char buffer[s.block_size];
You're trying to use a variable sized allocation for an array on the stack. You Can't Do That™. Those allocations have to be fixed (compile time constant) sized.

Resources