Related
I am trying to write a program in C (for xv6) that returns the last "n" number of lines of input text or a file (essentially tail) with the exception that it should not print blank lines("\n"). At the moment, my code is able to correctly ignore blank lines, but it will still print a blank line if the first line to be printed is a blank line.
For example, this is my output if only 2 lines are to be printed:
\n (a blank space)
the quick brown fox jumped over the lazy dog
But the output should look like:
the quick brown fox jumped over the lazy dog
Things I have tried:
Checking if the current index, AND the next index in buf[] contains \n
Checking if the current index AND the previous index in buf[] does not contain \n
I feel like the solution is simple, but I can't figure it out. Does anybody have any ideas?
edit - provided full code
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
char buf [1024];
void tail (int fd, char* name, int lines){
int chunk_index; //keeps track of the chunk index
int chunk_size; //keeps track of the size of the chunk
int lines_in_doc = 0; //keeps track of the total number of lines
int current_line_num = 0; //keeps track of the character count in each chunk
int temp_line = open("temporary_file", O_CREATE | O_RDWR);
while ((chunk_size = read(fd, buf, sizeof(buf))) > 0){
write(temp_line, buf, chunk_size);
for (chunk_index = 0; chunk_index <= chunk_size; chunk_index++){
if (buf[chunk_index] != '\n'){
continue;
}else{
lines_in_doc++;
}
}
}
close(temp_line);
if (chunk_size < 0){
printf(1, "tail - read error \n");
exit();
}
//int total_chunks_read = 0;
temp_line = open("temporary_file", 0);
while((chunk_size = read(temp_line, buf, sizeof(buf))) > 0){
for (chunk_index = 0; chunk_index < chunk_size; chunk_index++){
if (current_line_num >= (lines_in_doc - lines)){
if ((buf[chunk_index] == '\n') && (buf[chunk_index+1] == '\n') && (buf[chunk_index-1] == '\n')){
printf(1,"haha!");
}
else{
printf(1, "%c", buf[chunk_index]);
}
}
else if (buf[chunk_index] == '\n'){
current_line_num++;
}
}
}
close(temp_line);
unlink("temporary_file");
}
//main function
int
main(int argc, char *argv[]){
int i;
int fd = 0;
int x = 10;
char *file;
char a;
file = "";
if (argc <= 1){
tail(0, "", 10);
exit();
} else{
for (i = 1; i < argc; i++){
a = *argv[i];
if (a == '-'){
argv[i]++;
x = atoi(argv[i]++);
}else{
if ((fd = open(argv[i], 0)) < 0){
printf(1, "tail: cannot open %s \n", argv[i]);
exit();
}
}
}
tail(fd, file, x);
close(fd);
exit();
}
}
The requirements for my course's tail assignment was to count, but not print blank lines. So, it would have taken some tinkering of the code of writing to the temporary file which I didn't want to do, since this is not what official tail.c does.
I realized that my logic was missing a component: a blank line between two lines of text would appear as a blankspace, a blankspace, and a non-blankspace. So, I needed to update my if statement to be the following:
if ((buf[chunk_index] == '\n') && (buf[chunk_index + 1] != '\n') &&
(buf[chunk_index - 1] == '\n')) {
printf(1, "");
}
I'm trying to count the number of lines of a file that I'm reading trough a File Descriptor but I don't know what I'm doing wrong because it does not worlk.
This is the code:
fd_openedFile = open(filename, O_RDONLY)
char *miniBuffer[1];
int lineCounter = 0;
while( read(fd_openedFile, miniBuffer, 1) >0) {
if (*miniBuffer[0] == '\n')
lineCounter++;
}
The software never enters the "if" and I've tested a lot of variants that I thought that could work but none of them worked (this is just the one that makes more sense to me).
Any help would be highly apreciated.
Thank you very much!
I have added the full code below:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void err_sys(const char* cadena, int continueExecuting) {
perror(cadena);
if (continueExecuting == 0)
exit(1);
}
int main()
{
//vars
char filename[200];
int fd_output = 1;
int fd_openedFile = -1;
int fd_newFile = -1;
//Ask for the file and open it
while (fd_openedFile < 0){
write(fd_output, "Write the filename: ", 20);
scanf("%s", filename);
if ((fd_openedFile = open(filename, O_RDONLY)) < 0)
err_sys("Error opening the original file", 1);
}
//Construct the new file's name
char *partOfOldFilename = strtok(filename, ".");
char newFileName[208], finalPart[8];
strcpy(newFileName, partOfOldFilename);
strcpy(finalPart, "OUT.txt");
strcat(newFileName, finalPart);
//Create the new file
if ((fd_newFile = open(newFileName, O_WRONLY | O_CREAT)) < 0)
err_sys("Error opening the new file", 1);
//Count the number of lines
char miniBuffer[1];
int lineCounter = 0;
while( read(fd_openedFile, &miniBuffer[0], 1) >0) {
write(fd_output, "R", 1); //To debug
if (miniBuffer[0] == '\n') {
lineCounter++;
write(fd_output, "1", 1); //To debug
} else {
write(fd_output, "0", 1); //To debug
write(fd_output, miniBuffer, 1); //To debug
}
}
lseek(fd_openedFile,0,SEEK_SET);
write(fd_output, "=========\n", 10); //To debug
//Count the number of chars per line
char* charsPerLine[lineCounter];
lineCounter = 0;
int charCounter = 0;
while( read(fd_openedFile, miniBuffer, 1) >0){
write(fd_output, "C", 1); //To debug
if (miniBuffer[0] == '\n') {
*(charsPerLine[lineCounter]) = charCounter +'0';
lineCounter++;
charCounter = 0;
write(fd_output, "1", 1); //To debug
} else {
write(fd_output, "0", 1); //To debug
write(fd_output, miniBuffer, 1); //To debug
charCounter ++;
}
}
lseek(fd_openedFile,0,SEEK_SET);
write(fd_output, "END", 4); //To debug
//Write a copy of the original file starting each line with the number of chars in it
lineCounter = 0;
int bufSize = 1;
char buffer[bufSize];
//First number write
if (write(fd_newFile,charsPerLine[lineCounter], bufSize)!=bufSize)
err_sys("write_error", 0);
lineCounter++;
while( read(fd_openedFile, buffer, bufSize) >0){
if (write(fd_newFile,buffer, bufSize)!=bufSize)
err_sys("write_error", 0);
if (buffer[0] == '\n') {
if (write(fd_newFile,charsPerLine[lineCounter], bufSize)!=bufSize)
err_sys("write_error", 0);
lineCounter++;
}
}
//Finish program
if (close(fd_openedFile)!=0) err_sys("error closing original file's file descriptor", 0);
if (close(fd_newFile)!=0) err_sys("error closing new file's file descriptor", 0);
return 0;
}
This codes assumes that the file is a .txt and that at the end of each line there is a "break line" and it is currently in development.
Thanks again.
You're not allocating any memory for miniBuffer which is an array of char pointers. Which isn't really the problem - the problem is that it shouldn't be an array of char pointers in the first place. You only need it to be an array of char like the following.
char miniBuffer[1];
And the other change then is to check that single element of the array for it being a \n character.
if (miniBuffer[0] == '\n')
You might find it would be more efficient to read in larger chunks by increasing the size of the array and use functions like strchr to find any \n in the string. You would need to store the amount read returns so you could properly NUL terminate the string though.
I am writing a program that finds the number of occurrences of input substrings from the command line inside a text file (also read from the command line) which is written into a buffer.
When I run the code in bash, I get the error: Segmentation fault (core dumped).
I am still learning how to code with C in this environment and have some sort of idea as to why the segmentation fault occurred (misuse of dynamic memory allocation?), but I could not find the problem with it. All I could conclude was that the problem is coming from within the for loop (I labeled where the potential error is being caused in the code).
EDIT: I managed to fix the segmentation fault error by changing argv[j] to argv[i], however when I run the code now, count1 always returns 0 even if the substring occurs multiple times in the text file and I am not sure what is wrong even though I have gone through the code multiple times.
$ more foo.txt
aabbccc
$ ./main foo.txt a
0
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[]) {
FILE *fp;
long lsize;
char *buf;
int count = 0, count1 = 0;
int i, j, k, l1, l2;
if (argc < 3) { printf("Error: insufficient arguments.\n"); return(1); };
fp = fopen(argv[1], "r");
if (!fp) {
perror(argv[1]);
exit(1);
}
//get size of file
fseek(fp, 0L, SEEK_END);
lsize = ftell(fp);
rewind(fp);
//allocate memory for entire content
buf = calloc(1, lsize+1);
if (!buf) {
fclose(fp);
fputs("Memory alloc fails.\n", stderr);
exit(1);
}
//copy the file into the buffer
if (1 != fread(buf, lsize, 1, fp)) {
fclose(fp);
free(buf);
fputs("Entire read fails.\n", stderr);
exit(1);
}
l1 = strlen(buf);
//error is somewhere here
for (i = 2; i < argc; i++) {
for (j = 0; j < l1;) {
k = 0;
count = 0;
while ((&buf[j] == argv[k])) {
count++;
j++;
k++;
}
if (count == strlen(argv[j])) {
count1++;
count = 0;
}
else
j++;
}
printf("%d\n", count1);
}
fclose(fp);
return 0;
}
fread(buf, lsize, 1, fp) will read 1 block of lsize bytes, however fread
doesn't care about the contents and won't add a '\0'-terminating byte for the
string, so l1 = strlen(buf); yields undefined behaviour, the rest of the
result can be ignored as a result of this (and your counting has errors as well).
Note that files usually don't have a 0-terminating byte at the end,
that applies even for files containing text, they usually end with a
newline.
You have to set the 0-terminating byte yourself:
if (1 != fread(buf, lsize, 1, fp)) {
fclose(fp);
free(buf);
fputs("Entire read fails.\n", stderr);
exit(1);
}
buf[lsize] = '0';
And you can use strstr to get the location of the substring, like this:
for(i = 2; i < argc; ++i)
{
char *content = buf;
int count = 0;
while((content = strstr(content, argv[i])))
{
count++;
content++; // point to the next char in the substring
}
printf("The substring '%s' appears %d time(s)\n", argv[i], count);
}
Your counting is wrong, there are some errors. This comparison
&buf[j] == argv[k]
is wrong, you are comparing pointers, not the contents. You have to use strcmp
to compare strings. In this case you would have to use strncmp because you
only want to match the substring:
while(strncmp(&buf[j], argv[k], strlen(argv[k])) == 0)
{
// substring matched
}
but this is also wrong, because you are incrementing k as well, which will
give you the next argument, at the end you might read beyond the limits of
argv if the substring is longer than the number of arguments. Based on your
code, you would have to compare characters:
while(buf[j] == argv[i][k])
{
j++;
k++;
}
You would have to increment the counter only when a substring is matched, like
this:
l1 = strlen(buf);
for (i = 2; i < argc; i++) {
int count = 0;
int k = 0; // running index for inspecting argv[i]
for (j = 0; j < l1; ++j) {
while(buf[j + k] == argv[i][k])
k++;
// if all characters of argv[i]
// matched, argv[i][k] will be the
// 0-terminating byte
if(argv[i][k] == 0)
count++;
// reset running index for argv[i]
// go to next char if buf
k = 0;
}
printf("The substring '%s' appears %d time(s)\n", argv[i], count);
}
I am trying to write a *nix program that copies itself and replaces a string inside the binary. The copy process doesn't seem to work though.
Here's the code:
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#define BUFSIZE 10
#define FILENAME "token"
void findstring(const char *exe, const char* str)
{
char buf[BUFSIZE];
int line_num = 1;
int i = 0, find_result = 0;
FILE *fp = fopen(exe, "rb");
if(fp == NULL)
exit(-1);
FILE *out = fopen("out", "wb");
if(out == NULL)
exit(-1);
while(fgets(buf, BUFSIZE, fp) != NULL) {
if((strstr(buf, str)))
{
printf("A match found on line: %d\n", line_num);
printf("\n%s\n", buf);
find_result++;
// reverse "token" string in the output
for(i = 0; i< BUFSIZE; i++)
{
if(strstr(&buf[i], "t") != NULL)
buf[i] = 'n';
else if(strstr(&buf[i], "o") != NULL)
buf[i] = 'e';
else if(strstr(&buf[i], "k") != NULL)
buf[i] = 'k';
else if(strstr(&buf[i], "e") != NULL)
buf[i] = 'o';
else if(strstr(&buf[i], "n") != NULL)
buf[i] = 't';
}
}
line_num++;
fputs(buf, out);
}
if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
}
fclose(fp);
fclose(out);
}
int main(int argc, char **argv, char **envp)
{
// argv[1] = FILENAME;
char buf[1024];
int fd, rc;
findstring(argv[0], "token");
if(argc == 1) {
printf("\n\n%s [file to read]\n\n", argv[0]);
exit(1);
}
printf("FILENAME macro = %s", FILENAME);
if(strstr(argv[1], "token") != NULL) {
printf("\n\nYou may not access '%s'\n\n", argv[1]);
exit(2);
}
fd = open(argv[1], O_RDONLY);
if(fd == -1) {
printf("\n\nUnable to open %s\n\n", argv[1]);
exit(3);
}
rc = read(fd, buf, sizeof(buf));
if(rc == -1) {
printf("\n\nUnable to read fd %d\n\n", fd);
exit(4);
}
write(1, buf, rc);
return 0;
}
"Token" string should be reversed in the output binary ("nekot"), with the findstring function responsible of performing this task.
It is also worth noting that the number of matches found strictly depends on the BUFSIZE constant.
What is this code missing?
Thanks
consider what this does:
if(strstr(&buf[i], "t") != NULL)
buf[i] = 'n';
This will search the buffer starting at index i, and if the string "t" appears anywhere in the buffer, it will replace the first character with n. So if your buffer has
a string with token inside.
the first iteration of the for loop will change it to
n string with token inside.
as the loop proceeds you'll get
nnnnnnnnnnith token inside.
after 10 iterations and ultimately
nnnnnnnnnnnnnnnekooooooooo.
Other issues:
fgets reads a string up to a newline or up to BUFSIZE-1 characters. There may well be bytes that are equivalent to newline chars.
You're scanning through BUFSIZE bytes regardless of how many bytes you read.
fputs will write up to the first NUL byte. If there are NUL bytes anywhere in your input binary, stuff after the NUL in the buffer will be lost.
The above means you probably want to use fread/fwrite instead of fgets/fputs, and you want to carefully check return values for shot read or writes.
1.
All C style string functions break at first '\0' . So if buf contains null character before Your goal, will be never found.
if((strstr(buf, str))) { ... }
I suggest loop with step one character (byte) coded by hand, or functions from family memXXXXcmp etc
If Your token is over boundaries of two buffers (from two loo[ iterations), no comparison can fount is
I've got a C program that reproduces a server using FIFOs. The program reads two lines from an input FIFO — a number n and a string str— and writes on an output FIFO n lines, each of which is a single occurrence of str. I wrote the following code.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MAX_SIZE 256
char *readline(int fd, char *buffer) {
char c;
int i = 0;
while (read(fd, &c, 1) != 0) {
if (c == '\n')
break;
buffer[i++] = c;
}
return buffer;
}
int main(int argc, char** argv) {
mkfifo(argv[1], 0666);
mkfifo(argv[2], 0666);
int in = open(argv[1], O_RDONLY);
int out = open(argv[2] ,O_WRONLY);
char line[MAX_SIZE];
memset(line, 0, MAX_SIZE);
int n, i;
while (1) {
strcpy(line, readline(in, line));
sscanf(line, "%d", &n);
strcpy(line, readline(in, line));
for (i = 0; i < n; i++) {
write(out, line, strlen(line));
write(out, "\n", 1);
}
}
close(in);
close(out);
return 0;
}
This program compiles and runs with no errors, but it outputs a different number of occurrences of the string at each execution. For example, if the two input lines in the input FIFO are 5\nhello, it then prints out from 1 to 25 occurrences of hello at each run (frequency appears to be completely random).
I've been stuck on this for two days. Please give me some help.
I make no claims or warrants that I even know what your program does, as it has been 20 years since I had any pressing need to work with FIFO's at the system level. But one thing is clear. Two days is a long time to work on something without ever running it in a debugger, of which doing so would have exposed a number of problems.
First, readline() never terminates the string it is passed. This isn't as important the first time around as the second and beyond, since shorter data may be present in the input lines. Furthermore, read() can fail, and in doing so does not return 0, the only condition on your loop which will break. That failure should break the loop and be reflected in the return result. Because you return the buffer pointer, a reasonable failure-result could be NULL:
Consider something like this:
char *readline(int fd, char *buffer)
{
ssize_t res = 0;
char c = 0;
int i = 0;
for(;;)
{
res = read(fd, &c, 1);
if (res < 0)
return NULL;
else if (res == 0 || c == '\n')
break;
buffer[i++] = c;
};
buffer[i] = 0;
return buffer;
}
One could argue that it should return NULL if the buffer is empty, since you can't put a 0-length packet on a FIFO. I leave that for you to decide, but it is a potential hole in your algorithm, to be sure.
Next the strcpy() function has undefined behavior if the buffers submitted overlap. Since readline() returns the very buffer that was passed in, and since said-same buffer is also the target of strcpy() is the same buffer, your program is executing UB. From all that I see, strcpy() is useless in this program in the first place, and shouldn't even be there at all.
This is clearly wrong:
strcpy(line, readline(in, line));
sscanf(line, "%d", &n);
strcpy(line, readline(in, line));
for (i = 0; i < n; i++) {
write(out, line, strlen(line));
write(out, "\n", 1);
}
The above should be this:
if (readline(in, line))
{
if (sscanf(line, "%d", &n) == 1)
{
if (readline(in, line))
{
for (i = 0; i < n; i++)
{
write(out, line, strlen(line));
write(out, "\n", 1);
}
}
}
}
assuming the changes to readline() as prescribed were made. These could be combined into a single three-expression if-clause, but as written above it is at-least debuggable. In other words, via short-circuit eval there should be no problems doing this:
if (readline(in, line) &&
sscanf(line, "%d", &n) == 1 &&
readline(in, line))
{
for (i = 0; i < n; i++)
{
write(out, line, strlen(line));
write(out, "\n", 1);
}
}
but I would advise you keep the former until you have thoroughly debugged this.
Finally, note that readline() is still a buffer overflow just waiting to happen. You really should pass a max-len to that function and limit that potential possibility, or dynamically manage the buffer.
The code does not initalises line for each iteration, so if readline() does not read anything it leaves line's content untouched.
And you do not test whether sscanf() fails, the code does not recognize als n is left unchanged and the last value of line get printed out n times and everything starts over again ...
Also readline() misses to check whether read() failed.
To learn from this exercise it to always test the result of a (system) call whether it failed or not.
int readline(int fd, char *buf, int nbytes) {
int numread = 0;
int value; /* read fonksiyonu sonunda okunan sayı degerini gore islem yapar */
/* Controls */
while (numread < nbytes - 1) {
value = read(fd, buf + numread, 1);
if ((value == -1) && (errno == EINTR))
continue;
if ( (value == 0) && (numread == 0) )
return 0;
if (value == 0)
break;
if (value == -1)
return -1;
numread++;
/* realocating for expand the buffer ...*/
if( numread == allocSize-2 ){
allocSize*=2; /* allocSize yeterli olmadıgı zaman buf ı genisletmemizi saglayarak
memory leak i onler */
buf=realloc(buf,allocSize);
if( buf == NULL ){
fprintf(stderr,"Failed to reallocate!\n");
return -1;
}
}
/* Eger gelen karakter \n ise return okudugu karakter sayısı */
if (buf[numread-1] == '\n') {
buf[numread] = '\0';
return numread;
}
}
errno = EINVAL;
return -1;
}