I've got this homework, which is to make an implementation of the "tail" command in Linux and this is what i have so far:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
char *resize(char *data, int size)
{
char *newData = malloc((size + 1) * sizeof(char));
int counter;
for(counter = 0; counter < size; counter++)
newData[counter] = data[counter];
return newData;
}
char *readStdIn(char *data)
{
char buff, end = '\n';
int rState, size = 1;
data = malloc(size * sizeof(char));
printf("%ld\n", sizeof(data));
while ((rState = read(STDIN_FILENO, &buff, 1)) > 0)
{
if(rState < 0)
{
if(errno == EINTR) rState = 0;
else
{
perror("read()");
return 0;
}
}
data = resize(data, size);
data[size - 1] = buff;
size++;
}
printf("%ld\n", sizeof(data));
if(rState == 0) write(STDOUT_FILENO, &end, 1);
return data;
}
int printLines(char *data)
{
int lines = 0, position;// counter;
for(position = sizeof(data) - 1; position > -1; position--);
if (data[position] == '\n') lines++;
return 0;
}
int main(int argc, char *argv[])
{
char *data = 0;
if(argc == 1)
{
readStdIn(data);
if(data == 0) return 1;
if(printLines(data) != 0) return 1;
}
return 0;
}
In readStdIn() I'm supposed to save what i have read in the standard input and put it in char* data, so that i can find the last 10 lines(or find that there are less than 10 lines) of the input. The problem is, that when i call resize(), data doesn't resize, and I can't find out why. It's most likely a problem in the resize() itself, but i can't figure it out. The idea is that resize() increases the size of data by 1 char.
I hope I was thorough enough in my explanation.
Your printf is incorrect. You are printing the size of a pointer, which will be a fixed number depending on your architecture. You should instead print out the size directly.
printf("%d\n", size);
Your resize is correct except that you failed to free the previous memory, so you have a memory leak. You can fix this by adding a free() before you return.
char *resize(char *data, int size)
{
char *newData = malloc((size + 1) * sizeof(char));
int counter;
for(counter = 0; counter < size; counter++)
newData[counter] = data[counter];
free(data);
return newData;
}
Related
I am currently developing a function in C that parses a file into a double dimensional array of characters (char **), the problem is that I get an extra line at the end, and I don't see how to fix that.
Can you help me?
Ps: My school requires me to use getline() and fopen().
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void my_free_word_array(char **word_array)
{
size_t i = 0;
if (!word_array) {
return;
}
while (word_array[i] != NULL) {
free(word_array[i]);
++i;
}
free(word_array);
}
ssize_t my_put_str_arr(char **arr, int fd)
{
char cr = '\n';
ssize_t count = 0;
size_t i = 0;
if (!arr)
return -1;
while (arr[i]) {
count += write(fd, arr[i], strlen(arr[i]));
count += write(fd, &cr, 1);
i++;
}
return count;
}
void append_word_array(char ***array, char *line)
{
size_t array_len = 0;
while ((*array)[array_len] != NULL) {
array_len++;
}
size_t len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
(*array)[array_len] = strdup(line);
(*array) = realloc((*array), (array_len + 2) * sizeof(char *));
(*array)[array_len + 1] = NULL;
}
void fill_from_file(char ***array, FILE *file)
{
char *line_buff = NULL;
size_t line_buff_size = 0;
ssize_t line_size = getline(&line_buff, &line_buff_size, file);
while (line_size >= 0) {
append_word_array(array, line_buff);
free(line_buff);
line_buff = NULL;
line_size = getline(&line_buff, &line_buff_size, file);
}
free(line_buff);
}
char **my_load_file_to_word_array(const char *filepath)
{
char **word_array = NULL;
FILE *file = fopen(filepath, "r");
if (!file) {
return NULL;
}
word_array = malloc(sizeof(char *));
if (!word_array) {
return NULL;
}
word_array[0] = NULL;
fill_from_file(&word_array, file);
fclose(file);
return word_array;
}
int main (int argc, char **argv)
{
char **file = my_load_file_to_word_array(argv[1]);
my_put_str_arr(file, 1);
my_free_word_array(file);
return 0;
}
Here is the content of the tested file (I added the \n \0 to make it easier for you to see):
My name is Saul.\n
I am Saul Goodman.\n
Better call Saul.\0
And this is the result I get :
My name is Saul.
I am Saul Goodman.
Better call Saul.
The "problem" with your code is that the function my_put_str_arr() prints the stored lines eached followed by a single \n character. If you don't want to print the last \n you would need to test if a next line exists. You could change your loop as follows:
while (arr[i]) {
count += write(fd, arr[i], strlen(arr[i]));
i++;
if (arr[i]) {
count += write(fd, &cr, 1);
}
}
So I am trying to make a function print_file_rows() that prints the first ten rows of a file. If the file has more than 10 rows it works perfectly fine but if there's 10 or less it starts printing garbage. Any ideas on how I can fix this? (MUST ONLY USE THE SYSTEM FUNCTIONS OPEN/READ/WRITE/CLOSE)
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
void print_file_rows(char *path)
{
int fd = open(path, O_RDONLY);
if (fd < 0)
{
return NULL;
}
size_t size = 100;
size_t offset = 0;
size_t res;
char *buff = malloc(size);
while((res = read(fd, buff + offset, 100)) != 0)
{
offset += res;
if (offset + 100 > size)
{
size *= 2;
buff = realloc(buff, size);
}
}
close(fd);
int j = 0;
for(int i = 0;buff[i] != '\0'; i++)
{
if(j == 10)
{
break;
}
if(buff[i] == '\n')
{
j++;
}
printf("%c", buff[i]);
}
free(buff);
}
int main()
{
print_file_rows("a.txt");
return 0;
}
You do not need any buffers. It is most likely buffered on the OS level so you may print char by char.
int print_file_rows(char *path, size_t nrows)
{
int result = -1;
int fd = open(path, O_RDONLY);
char c;
if (fd > 0)
{
while(nrows && read(fd, &c, 1) == 1)
{
write(STDOUT_FILENO, &c, 1);
if(c == `\n`) nrows--;
}
result = nrows;
}
close(fd);
return result;
}
int main()
{
if(print_file_rows("a.txt", 10) == -1)
printf("Something has gone wrong\n");
return 0;
}
From man 2 read:
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
DESCRIPTION
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
read is for reading raw bytes, and as such has no notion of strings. It does not place a NUL terminating byte ('\0') at the end of the buffer. If you are going to treat the data you read as a string, you must terminate the buffer yourself.
To make room for this NUL terminating byte you should always allocate one extra byte in your buffer (i.e., read one less byte that your maximum).
We can see the return value is actually of type ssize_t, rather than size_t, which allows for
On error, -1 is returned, and errno is set to indicate the error.
This means we will need to check that the return value is greater than zero, rather than not zero which would cause the offset to be decremented on error.
With all that said, note that this answer from a similar question posted just yesterday shows how to achieve this without the use of a dynamic buffer. You can simply read the file one byte at a time and stop reading when you've encountered 10 newline characters.
If you do want to understand how to read a file into a dynamic buffer, then here is an example using the calculated offset to NUL terminate the buffer as it grows. Note that reading the entire file this way is inefficient for this task (especially for a large file).
(Note: the call to write, instead of printf)
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
void print_file_rows(const char *path)
{
int fd = open(path, O_RDONLY);
const size_t read_size = 100;
size_t size = read_size;
size_t offset = 0;
ssize_t res;
char *buff = malloc(size + 1);
while ((res = read(fd, buff + offset, read_size)) > 0) {
offset += res;
buff[offset] = '\0';
if (offset + read_size > size) {
size *= 2;
buff = realloc(buff, size + 1);
}
}
close(fd);
int lines = 0;
for (size_t i = 0; lines < 10 && buff[i] != '\0'; i++) {
write(STDOUT_FILENO, &buff[i], 1);
if (buff[i] == '\n')
lines++;
}
free(buff);
}
int main(void)
{
print_file_rows("a.txt");
}
(Error handling omitted for code brevity. malloc, realloc, and open can all fail, and should normally be handled.)
I'm writing a function that should read a string of unknown length. I can use read, malloc, open, close and my own library that behaves like a normal one. It includes functions from the standard library and a few additional.
The problem is that fill_append() copies string from gnl->buf to *line incorrectly. In addition to copying the string itself, it adds other characters. Functions with ft_ prefix work accurately.
int fill_append(t_gnl* gnl, char** line)
{
char* append;
int index;
char* sub;
char* tmp;
append = ft_strchr(gnl->buf, '\n');
if (append == NULL)
{
*line = ft_strdup(gnl->buf);
return (0);
}
index = (int)(append - gnl->buf);
sub = ft_strsub(gnl->buf, 0, index);
tmp = ft_strnew(ft_strlen(*line) + ft_strlen(sub) + 1);
ft_strcpy(tmp, *line);
ft_strcat(tmp, sub);
*line = tmp;
// tmp = (char *)malloc(ft_strlen(*line) + ft_strlen(sub) + 1);
// memcpy(tmp, *line, ft_strlen(*line));
// memcpy(tmp + ft_strlen(*line), sub, ft_strlen(sub) + 1);
// *line = tmp;
gnl->buf = ft_strdup(&gnl->buf[index + 1]);
return (1);
}
int read_fd(t_gnl* gnl, char** line)
{
int bsize;
char* tmp;
while ((bsize = read(gnl->fd, gnl->buf, BUFF_SIZE)))
{
gnl->buf[bsize] = '\0';
if (ft_strchr(gnl->buf, '\n') == NULL)
{
tmp = *line;
*line = ft_strjoin(*line, gnl->buf);
free(tmp);
free(gnl->buf);
gnl->buf = ft_strnew(BUFF_SIZE);
}
else
return (fill_append(gnl, line));
}
if (bsize == 0 && *line[0] == 0)
{
free(gnl->buf);
return (0);
}
return (1);
}
int get_next_line(const int fd, char** line)
{
static t_gnl* gnl;
if (fd < 0 || line == NULL)
return (-1);
*line = ft_strnew(0);
if (gnl)
if (gnl->buf)
if (fill_append(gnl, line))
return (1);
if (!gnl)
gnl = (t_gnl*)malloc(sizeof(t_gnl));
gnl->buf = ft_strnew(BUFF_SIZE);
gnl->fd = fd;
return (read_fd(gnl, line));
}
For example, with input is sfesefsefsefwefsefsefsef
sfesefsefsefwefsefsefsef function returns sfesefsefsefwefsefsefsef1
sfesefsefsefwefsefsefsef!
Header:
#ifndef GET_NEXT_LINE_H
#define GET_NEXT_LINE_H
#define BUFF_SIZE 150
#include <unistd.h>
#include <stdlib.h>
#include "libft/libft.h"
typedef struct s_gnl
{
char* buf;
int fd;
} t_gnl;
int get_next_line(const int fd, char** line);
#endif
Main:
#include <stdlib.h>
#include <fcntl.h>
#include "get_next_line.h"
#include "libft/libft.h"
int main(void)
{
int fd;
char* line;
fd = open("gnl.txt", O_RDONLY);
while (get_next_line(fd, &line))
{
ft_putendl(line);
ft_strdel(&line);
}
close(fd);
}
I am trying to create a thread and read from stdin inside the thread. In main() have dynamically allocated memory to a 2d array based on the size given as user input. In the thread I am reading from stdin and splitting it using strtok and adding it into the 2d array. I am not sure why there is a segmentation fault, searched SO and I seem to have handled all the cases related to strtok.
This is the program -
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int dimB;
int dimA;
char** buffer;
char* temp;
void *thread(void *threadid){
char *buf;//[30] = {};
size_t len = 0;
ssize_t read;
char *line = NULL;
char *each;
printf("Hello World!.\n");
while ((read = getline(&line, &len, stdin)) != -1) {
printf("%s || \n", line);
each = strtok(line," ,()");
printf("************************%s ", each);
while(each != NULL){
buf = each;
strcpy(buffer[0][0], buf);
printf("%s", buf);
each = strtok(NULL," ,()");
}
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t tidMpr;
long r;
int i;
dimB = atoi(argv[1]);
dimA = atoi(argv[2]);
pthread_t tidRdr[dimA];
buffer = malloc(dimA * sizeof(char*));
temp = malloc(dimA * dimB * sizeof(char));
for (i = 0; i < dimA; i++) {
buffer[i] = temp + (i * dimB);
}
//Create thread Thread
pthread_create(&tidMpr, NULL, thread, NULL);
free(temp);
free(buffer);
pthread_exit(NULL);
}
The 2d array memory allocation is from this question - How do I work with dynamic multi-dimensional arrays in C?.
I know I am writing everything to buffer[0][0], but that is so that I can store each in a buffer array buffer[0][1], buffer[0][2] later on based on some logic. But that shouldn't be a problem now right?
Also line is printing the correct value, whatever it is reading from stdin. So, probably strtok is the problem.
Another very similar program produces the desired output. This -
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
typedef struct { char action; int score; } Rules;
Rules rules[5] = {{'P',50}, {'L',20}, {'D',-10}, {'C',30}, {'S',40}};
int findScore(Rules* rules, char action){
int i;
for(i=0; i<5; i++){
if(rules[i].action == action)
return rules[i].score;
}
fprintf(stderr, "Action not present! Exiting...\n");
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]){
FILE *fp;
char inputTuple[30] = {};
char buf[30] = {};
char* oB;
oB = "(";
char* cB;
char* co = ",";
cB = ")";
fp = fopen(argv[1], "r");
int score;
char *each;
size_t len = 0;
ssize_t read;
char * line = NULL;
char *eacharray;
int u = 0;
char *outputTuple;
int pad = 0;
int g;
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
each = strtok(line," ,()");
while(each != NULL){
if(u%3 == 0){
outputTuple = (char *) malloc(1 + strlen(each)+ strlen(oB) );
strcpy(outputTuple, oB);
strcat(outputTuple, each);
} else if(u%3 == 1){
char q = *each;
score = findScore(rules, q);
} else if(u%3 == 2){
char * str3 = (char *) malloc(1 + strlen(outputTuple)+ strlen(co) );
strcpy(str3, outputTuple);
strcat(str3, co);
char *str4 = (char *) malloc(1 + strlen(str3)+ strlen(each) );
strcpy(str4, str3);
strcat(str4, each);
for(pad = strlen(each); pad<15; pad++)
strcat(str4," ");
sprintf(buf, "%s,%d)\n", str4, score);
printf("%s", buf);
free(outputTuple);
free(str3);
free(str4);
}
each = strtok(NULL," ,()");
u++;
}
u = 0;
}
fclose(fp);
return 0;
}
Update :
strcpy(buffer[0][0], buf); seems to be the problem. When I comment it, it is producing the output. I dont understand why is that causing a problem.
I'm working on an implementation of the tail Unix command, and this is my code so far:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
char *resize(char *data, int size)
{
char *newData = (char*) malloc((size + 1) * sizeof(char));
int counter;
for(counter = 0; counter < size; counter++)
newData[counter] = data[counter];
free(data);
return newData;
}
int printLines(char *data, int size)
{
int lines = 0, position, counter;
for(position = size - 1; position > -1; position--)
{
if (data[position] == '\n') lines++;
if (lines == 10) break;
}
if (lines == 10)
for(counter = position; counter < size; counter++)
{
write(STDOUT_FILENO, &data[counter], 1);
}
else write(STDOUT_FILENO, data, size);
return 0;
}
int stdIn(char *data, int size)
{
char buff, end = '\n';
int rState = 0;
while ((rState = read(STDIN_FILENO, &buff, 1)) > 0)
{
if(rState < 0)
{
if(errno == EINTR) rState = 0;
else
{
perror("read()");
return 1;
}
}
data = resize(data, size);
data[size - 1] = buff;
size++;
}
if(rState == 0) write(STDOUT_FILENO, &end, 1);
return 0;
}
int tailRead(char *data, char *fileName)
{
int size = 1;
data = (char*)malloc(size * sizeof(char));
if(fileName == 0 || fileName == "-")
{
if(stdIn(data, size) > 0) return 1;
}
else
{
}
printLines(data, size);
return 0;
}
int main(int argc, char *argv[])
{
char *data = 0;
int counter;
if(argc == 1)
{
tailRead(data, 0);
if(data > 0) return 1;
}
else for (counter = 1; counter < argc; counter++)
{
tailRead(data, argv[counter]);
if(data > 0) return 1;
}
return 0;
}
The problem is that somewhere in the resize() function i get a Segmentation Fault, and when i ran the program in GDB, i got Program received signal SIGSEGV Segmentation fault. 0x00000000004006f7 in resize (). This tells me that I have some kind of memory allocation problem in resize(), but so far I have been unable to find the bug. What should I do?
int tailRead(char *data, char *fileName)
/* ... */
int main(int argc, char *argv[])
{
char *data = 0;
/* ... */
tailRead(data, 0);
}
You seem to expect, that in main() data will point to the memory allocated in tailRead(). That is not the case. In tailRead() data is a copy of the pointer data from main() you only change the copy, not the original pointer. The original pointer still points to 0.
Then you call resize() with a null pointer, which of course will lead to a segmentation violation.
Solution
use a pointer to a pointer instead, to modify the original pointer.
int tailRead(char **data, char *fileName)
{
int size = 1;
*data = (char*)malloc(size * sizeof(char));
/* ... */
}
int main(int argc, char *argv[])
{
char *data = 0;
/* ... */
tailRead(&data, 0);
/* ... */
}
You have the same issue with stdIn(). It changes data without reflecting the change to the data-pointer at the call site in tailRead(). You leave stdIn() leaking memory and continue to work with a dangling pointer.
I think your problem is here...
for(counter = 0; counter < size; counter++)
newData[counter] = data[counter];
You are trying to access data[counter] when counter is greater than what you allocated for the previous malloc(). By this I mean, you are reading beyond the (current) legitimate end of data. Make sense?
EDIT: Now that I think about it, this may not cause the segfault, but it is a problem.