So I searched and looked at all the snprintf(), sprintf() and segmentation fault threads but am still nowhere...
Basically, it seems that the mere presence of a snprintf() statement is causing my code to have a segmentation fault. Long story short, yes, this is for homework...basically, I have to fill in missing code and am under strict orders to not touch the code that already exists. Here's what my code looks like:
struct dirent* dirEntryPtr;
struct stat statBuffer;
char *yourFileName;
while((dirEntryPtr=readdir(dirPtr))!=NULL)
{
int yourFileSize;
memset(buffer,'\0',sizeof(buffer));
yourFileName=dirEntryPtr->d_name;
printf("Directory entry read...%s\n",yourFileName);
snprintf(buffer,MAX_LINE,"%s/%s",DIR_NAME,yourFileName);
printf("Printed directory entry to buffer...\n");
stat(buffer,&statBuffer);
yourFileSize=statBuffer.st_size;
printf("File size: %d\n",yourFileSize);
if(S_ISREG(statBuffer.st_mode))
{
printf("REGULAR!\n");
snprintf(buffer,MAX_LINE,"%30s (%5d)\n",yourFileName,yourFileSize);
printf("[%s] %30s (%5d)\n",(char *)statBuffer.st_mode,yourFileName,yourFileSize);
}
else
if(S_ISDIR(statBuffer.st_mode)){
printf("DIRECTORY!\n");
snprintf(buffer,MAX_LINE,"%30s (dir)\n",yourFileName);
printf("[%s] %30s (dir)\n",(char *)statBuffer.st_mode,yourFileName);}
else
{
printf("OTHER!!\n");
snprintf(buffer,MAX_LINE,"%30s (other)\n",yourFileName);
printf("[%s] [%s] %30s (other)\n",(char*) S_ISREG(statBuffer.st_mode),(char *)statBuffer.st_mode,yourFileName);
}
printf("Writing buffer \"%s\" to client...\n",buffer);
write(clientDescriptor,buffer,MAX_LINE);
free(buffer);
}
close(clientDescriptor);
exit(EXIT_SUCCESS);
All the printf()s are there basically for debugging. (Yes, I know, use GDB, but I could never figure out how that thing works despite years of instruction!)
Long story short, files in the current directory are read in, one at a time. The first file is usually one called server.c, which is always correctly identified as a "regular" file. However, right when that snprintf() line comes in is where the segmentation fault comes into play. buffer is a string declared (part of the code I'm not allowed to touch, but the instructions were specifically to write the entry to "buffer" using snprintf() -- in fact, the EXACT snprintf() command I'm using is what was given in the instructions!) and set to MAX_LINE bytes (80), which is #defined in an outside header file that's being included. I had a strnlen in there before for debugging purposes to confirm that DIR_NAME is one character and yourFileName, at least in the case of server.c, was 9 -- nowhere near the 80!
I even tried declaring a new variable and just writing one single byte to it via snprintf() but still got the segmentation fault! I've tried both with and without the malloc() command.
What else should I look for??? I did e-mail my professor about this but truth be told I have to submit this, like, TODAY (it's not due for a few more days but I'm going to be out of town) and just need pointers ASAP...but according to what I understand about C, this should work. In fact, it actually WAS working yesterday afternoon but all of a sudden it started segmentation faulting on me late last night. :(
buffer
is allocated (if indeed it has been allocated at all). – Flexo♦