I was trying to create a simple C program to take out all the ^M's that appear when you view a Windows text file in Unix. (I'm using FreeBSD by the way.) So far, I've just created enough that it reads character by character and stores anything that's not equal to 0x0D in a buffer. I'll worry about saving it after I get this error fixed. The problem is that when it gets to the line "buffer[counter] = getchar(filep);" it gives me a SIGBUS error. Through much experimentation, I've narrowed it down further. Gdb (a debugger) gave me the exact location in memory where it's faulting. I disassembled it and the instruction at that exact point is "mov %al,(%eax)" or something like that. (I'm not keen on AT&T syntax.) So I would say it's during the assignment that it's getting a bus error. But anyway, here's the code so hopefully you can point out my mistake.
Code:
#include <stdio.h>
int main(int argc,char *argv[])
{
char *buffer;
int counter = 0;
FILE *filep;
if(argc != 2)
{
printf("You must specify a file to convert.\n");
exit(0);
}
if(!(filep = fopen(argv[1],"r")))
{
printf("Couldn't open file.\n");
exit(0);
}
while(!feof(filep))
{
buffer[counter] = (char)fgetc(filep);
counter++;
if(buffer[counter-1] = 0x0D)
counter--;
}
}