Replacing fgets

F

FireHead

Hello All,

My original topic should have been " using/replace fgets".

I think i have finally solved and used fgets in my program.
But I see somewhere bug/memory leak I just am not able pin the memory
leak.
Could anybody assist me in this issue please.

This can be anybody for any reason.
This code will now offically become part of the Public Domain.

The this the code that I have managed to get it working.
//-------------------------------------------------------------------------------------------------------------------------------------------------//
/*
Will BE MULTITHREADED
open a original file (source file)
Look for the existing custom folder in the HOME directory
If the HOME directory does not exist
Then see if the specs exists in the /usr/local/share (Default)
If the /usr/local/share does not exist then create a template
create a temp
*/

#include <cliargs.h>
short readPreLine(FILE* pSource,char **content);
short readPostLine(FILE* pSource,char **content);
short readLPostLine(FILE *pSource,char** content,int default_length);
short readLPreLine(FILE *pSource,char** content,int default_length);


char* get_Copy(char* pStr);

char* get_Copy(char* pStr){
u_int8_t *temp =0;
u_int8_t *final =0;
u_int8_t byte = 0;
u_int8_t dummy = 0;
temp = pStr;
int pos = 0;
final = &dummy;
/*initilize the contents with new address*/
do{
byte = *temp;printf("%c",byte);
final++;
temp++;
}while(byte!='\0');
*final = '\0';

temp = "";
temp = pStr;
/*clean up everything*/

char *results = "";
results = strcpy( final,temp);

return results;
}


/*
short get_Stream(FILE* pSource,char* result){
size_t buflen = 0;
char offset= 0;
int ret=EXIT_SUCCESS;
fpos_t* pCurPos=0 ; //current file position
fpos_t* pOldPos=0 ; //old file position
char *finally = "";
char *buffer = 0;
//get the starting File pointer position
fgetpos(pSource,&pOldPos);
//find the newline = 0x13
do{offset = 0; offset=fgetc(pSource);
} while(offset!=0x0A && offset!=0x0D && offset!=EOF && offset!=NULL &&
offset!=0xFF);

//get the Current File position
fgetpos (pSource,&pCurPos);
printf("%u",pCurPos);
//Total Buffer Length
buflen = (size_t)pCurPos - (size_t)pOldPos;
if(( offset == 0xFF || offset == EOF || offset==NULL) && (buflen ==
EXIT_FAILURE || buflen == EXIT_SUCCESS))
ret= EXIT_FAILURE;
else{
//Reset Everything
fsetpos (pSource,&pOldPos);
//allocate memory for the new address register
buffer = (char *)malloc(buflen);
//read file buffer using fgets
fgets(buffer,buflen,pSource);
//copy the string to a automatic variable register
//finally = get_Copy(buffer);
printf("\nfeof = %d\n",strlen(buffer));

//free the contents
free(buffer);

ret=EXIT_SUCCESS;
}//ELSE

buffer=0;
pCurPos = 0;
pOldPos = 0;
offset = 0;
return ret;
}*/

int malloc_length(char* source_buffer){
int offset=0;
for(offset = 0;source_buffer[offset]!=NULL || source_buffer[offset]!=0
|| source_buffer[offset]!='\0';offset++);
return offset;
}

short readLPostLine(FILE *pSource,char** content,int default_length){
short ret=EXIT_SUCCESS;
int return_length;return_length = 0;
char* currentline;currentline = 0;
char* returnline;returnline = 0;
char* duplicate;duplicate = 0;
char offset;offset = 0;
int temp_length = 0;
return_length = return_length+default_length;
/* allocate default sized buffer */
currentline = (char*)malloc(default_length);
currentline = strcpy(currentline,"");
/*allocate memory temporarily*/
returnline = (char*)malloc(return_length+default_length);
returnline = strcpy(returnline,"");
do{
/* read the the line first*/
if((currentline=fgets(currentline,default_length+1,pSource))
!=NULL){
offset = (char)(strlen(currentline) > 0 ?
currentline[strlen(currentline)-1]: 0 );
if(offset != 0x0A && offset!=0x0D &&
(offset!=0&&offset!=0xFF&&offset!=EOF) && offset!=NULL){
temp_length = strlen(returnline);
if(temp_length == (return_length+default_length) ||
temp_length == return_length ){
return_length = return_length+default_length;
duplicate =(char*)malloc(return_length+default_length);
duplicate = strcpy(duplicate,returnline);
free(returnline);returnline=0;
return_length = return_length+default_length;
returnline = (char*)malloc(return_length+default_length);
returnline = strcpy(returnline,duplicate);
free(duplicate);duplicate=0;
}
if(temp_length == 0) returnline = strcpy(returnline,currentline);
else returnline = strcat(returnline,currentline);
}else{
returnline = strncat(returnline,currentline,strlen(currentline)-1);
ret = EXIT_SUCCESS;
}
}/* Main IF */
else
ret = EXIT_FAILURE;
}while(offset != 0x0A && offset!=0x0D &&
(offset!=0&&offset!=0xFF&&offset!=EOF)
&& ret==EXIT_SUCCESS);

/* end of while*/
if(offset == 0x0A||offset == 0x0D ){
*content = malloc(strlen(returnline)+1);
*content = strcpy(*content,returnline);
ret = EXIT_SUCCESS;
}else
ret = EXIT_FAILURE;

free(currentline);
free(returnline);

return ret;
}

short readLPreLine(FILE *pSource,char** content,int default_length){
short ret=EXIT_SUCCESS;
int return_length;return_length = 0;
char* currentline;currentline = 0;

char* returnline;returnline = 0;
char* duplicate;duplicate = 0;
char offset;offset = 0;
int temp_length = 0;
return_length = return_length+default_length;
/* allocate default sized buffer */
currentline = (char*)malloc(default_length);
currentline = strcpy(currentline,"");
/*allocate memory temporarily*/
returnline = (char*)malloc(return_length+default_length);
returnline = strcpy(returnline,"");
/* read the the line first*/
currentline = fgets(currentline,default_length+1,pSource);
if(currentline!=NULL){
offset = (strlen(currentline) > 0?
currentline[strlen(currentline)-1]: EOF);
if(offset!=0&&offset!=0xFF&&offset!=EOF){
while(offset != 0x0A && offset!=0x0D &&
(offset!=0&&offset!=0xFF&&offset!=EOF)){
temp_length = strlen(returnline);
if(temp_length == (return_length+default_length) ||
temp_length == return_length ){
return_length = return_length+default_length;
duplicate =(char*)malloc(return_length+default_length);
duplicate = strcpy(duplicate,returnline);
free(returnline);returnline=0;
return_length = return_length+default_length;
returnline = (char*)malloc(return_length+default_length);
returnline = strcpy(returnline,duplicate);
free(duplicate);duplicate=0;
}/* if */
returnline = strcat(returnline,currentline);
/*read the file again */
currentline = fgets(currentline,default_length+1,pSource);
offset = currentline[strlen(currentline)-1];
}/*while*/
if(offset == 0x0A||offset == 0x0D || offset == 0 || offset ==
EOF){

if( strlen(returnline)==0)
returnline = strncpy(returnline,currentline,strlen(currentline)+1);
else
returnline = strncat(returnline,currentline,strlen(currentline)-1);

ret = EXIT_SUCCESS; }
}else
ret = EXIT_FAILURE;
}else ret = EXIT_FAILURE;

if(ret == EXIT_SUCCESS){
*content = malloc(strlen(returnline)+1);
*content = strcpy(*content,returnline);
}
free(currentline);
free(returnline);
return ret;
}

short readPreLine(FILE* pSource,char **content){
return readLPreLine(pSource,content,512); /* default UNIX length */
}

short readPostLine(FILE* pSource,char **content){
return readLPostLine(pSource,content,512); /* default UNIX length
*/
}

int main(int argc,const char* argv[])
{
FILE* pSourceFile;
const char* ATOMIC_NAME;
char* ret = 0;
ATOMIC_NAME = "/etc/samba/smb.conf";
printf("%s\n",ATOMIC_NAME);
pSourceFile = fopen(ATOMIC_NAME,"rb");

if(pSourceFile==NULL){
printf("File not found\n");
exit(EXIT_FAILURE);
}
else
fseek(pSourceFile,SEEK_SET,SEEK_SET);

while(readPostLine(pSourceFile,&ret)==EXIT_SUCCESS){
printf("%s\n",ret);
free(ret);
}


fclose(pSourceFile);

}
//-------------------------------------------------------------------------------------------------------------------------------------------//


In the following code:
while(readPostLine(pSourceFile,&ret)==EXIT_SUCCESS){
printf("%s\n",ret);
free(ret);
}


How can I avoid calling < free(ret) > ?

Thanks guys & girls ( if any).
 
R

Richard Heathfield

[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]

FireHead said:

In the following code:
while(readPostLine(pSourceFile,&ret)==EXIT_SUCCESS){
printf("%s\n",ret);
free(ret);
}


How can I avoid calling < free(ret) > ?

You seem to be asking "how can I get my line-reading routine to re-use an
existing buffer rather than allocate a fresh one from scratch on each
call?"

If that is indeed your question, the answer is very simple. Simply rewrite
the routine to do what you want. In this case, the only problem you have is
one of communicating the existing buffer size to the function. The fix here
is to pass a size_t *. You set up a size_t with value 0, and pass its
address to the function. On return, it has the buffer size. On subsequent
calls, you pass this buffer size in, so that the function knows how much it
can write before it has to start allocating.

http://www.cpax.org.uk/prg/writings/fgetdata.php discusses this problem and
provides simple source code that solves it.
 
C

Christopher Layne

Richard said:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]
You seem to be asking "how can I get my line-reading routine to re-use an
existing buffer rather than allocate a fresh one from scratch on each
call?"

I thought for sure, Richard, that you would just slaughter this guy for
multiple code violations, but I'm pleasantly surprised.

As far as the code goes, it already infers unix-isms (mention of /usr/local),
in which case, if there is no particular reason for using stdio for this
(i.e. the wheel is not being rewritten for a reason), I seriously suggest use
of posix mmap() here. IMO, stdio is just not made for this type of activity -
whereas mmap() was. Other than that, it's very hard to read, is nowhere near
modular enough, and makes plenty of non-portable assumptions and redundant
logic checks (!= 0 vs != '\0').

At the risk of getting beat down in comp.lang.c for a posix recommendation,
please use mmap() or at the minimum: rewrite it all again.
 
F

FireHead

Richard said:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]

FireHead said:

In the following code:
while(readPostLine(pSourceFile,&ret)==EXIT_SUCCESS){
printf("%s\n",ret);
free(ret);
}


How can I avoid calling < free(ret) > ?

You seem to be asking "how can I get my line-reading routine to re-use an
existing buffer rather than allocate a fresh one from scratch on each
call?"

If that is indeed your question, the answer is very simple. Simply rewrite
the routine to do what you want. In this case, the only problem you have is
one of communicating the existing buffer size to the function. The fix here
is to pass a size_t *. You set up a size_t with value 0, and pass its
address to the function. On return, it has the buffer size. On subsequent
calls, you pass this buffer size in, so that the function knows how much it
can write before it has to start allocating.

http://www.cpax.org.uk/prg/writings/fgetdata.php discusses this problem and
provides simple source code that solves it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


The file < config.h > contains the following header file declartion.
#ifndef COMMON_HEADER_FILES
#define COMMON_HEADER_FILES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif


I should have put the standard header files instead .. oops.
 
R

Richard Heathfield

Christopher Layne said:
Richard said:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]
You seem to be asking "how can I get my line-reading routine to re-use an
existing buffer rather than allocate a fresh one from scratch on each
call?"

I thought for sure, Richard, that you would just slaughter this guy for
multiple code violations, but I'm pleasantly surprised.

Yeah, I just slaughtered him for *one*, because the one was a showstopper.
:)

Seriously, the word "slaughter" indicates that you think a code critique is
an attack. It isn't. It's an attempt to educate the author. If he takes it
in that vein, he will learn a lot. That's certainly my hope.
As far as the code goes, it already infers unix-isms (mention of
/usr/local), in which case, if there is no particular reason for using
stdio for this

There's no particular reason why he shouldn't use a generally portable
technique for doing something as general-purpose and non-platform-specific
as reading a complete line from a stream, rather than nail his routines to
a particular implementation.
 
R

Richard Heathfield

FireHead said:
Richard said:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]

The file < config.h >

....er, I didn't even see a <config.h> - the only inclusion I saw was of a
contains the following header file declartion.
#ifndef COMMON_HEADER_FILES
#define COMMON_HEADER_FILES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif

(Four of which are not standard C headers.)
I should have put the standard header files instead .. oops.

Yeah, especially when posting in comp.lang.c it's a good idea to reduce your
problem to one that can be expressed using only standard C constructs - or,
at the very least, to special-plead "I know this has got Unixy stuff in it,
but please just ignore all that stuff and focus on the C bit", or even "I'm
not sure whether this is a C problem or a Unix problem, here's the problem,
here's the code, what do you reckon?"
 
F

FireHead

Richard said:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]

The file < config.h >

....er, I didn't even see a <config.h> - the only inclusion I saw was of
a
contains the following header file declartion.
#ifndef COMMON_HEADER_FILES
#define COMMON_HEADER_FILES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif

(Four of which are not standard C headers.)
I should have put the standard header files instead .. oops.
Yeah, especially when posting in comp.lang.c it's a good idea to reduce your
problem to one that can be expressed using only standard C constructs - or,
at the very least, to special-plead "I know this has got Unixy stuff in it,
but please just ignore all that stuff and focus on the C bit", or even "I'm
not sure whether this is a C problem or a Unix problem, here's the problem,
here's the code, what do you reckon?"

//---------------------------------------------------------------------------------------------------------//
Just a pointer the config.h supposed to cliargs.h header file.

In terms of portablilty anybody should be able these codes.

When the fopen is called rather that /usr/local/some_file_name it can
be also be used for any platform as long as you give valid folder/file
path.
For windows it can be < fopen( "Window File Name");

then code that I have currently will still work for any platform.

One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.
But I just keep gets blasted Segmentation Error/ Memory out of range.

By using get_Copy I might be able simply code even further.

You all might have noticed that I have 3 version of the same
functionality.

2 function will be portable for any platform.

The below function is clean and understandable but its not functionable
on SCO UNIX and on windows.
Now I will not include windows because Windows is just @##$@#$.

/*
short get_Stream(FILE* pSource,char* result){
size_t buflen = 0;
char offset= 0;
int ret=EXIT_SUCCESS;
fpos_t* pCurPos=0 ; //current file position
fpos_t* pOldPos=0 ; //old file position
char *finally = "";
char *buffer = 0;
//get the starting File pointer position
fgetpos(pSource,&pOldPos);
//find the newline = 0x13
do{offset = 0; offset=fgetc(pSource);
} while(offset!=0x0A && offset!=0x0D && offset!=EOF && offset!=NULL &&

offset!=0xFF);

//get the Current File position
fgetpos (pSource,&pCurPos);
printf("%u",pCurPos);
//Total Buffer Length
buflen = (size_t)pCurPos - (size_t)pOldPos;
if(( offset == 0xFF || offset == EOF || offset==NULL) && (buflen ==
EXIT_FAILURE || buflen == EXIT_SUCCESS))
ret= EXIT_FAILURE;
else{
//Reset Everything
fsetpos (pSource,&pOldPos);
//allocate memory for the new address register
buffer = (char *)malloc(buflen);
//read file buffer using fgets
fgets(buffer,buflen,pSource);
//copy the string to a automatic variable register
//finally = get_Copy(buffer);
printf("\nfeof = %d\n",strlen(buffer));

//free the contents
free(buffer);

ret=EXIT_SUCCESS;
}//ELSE

buffer=0;
pCurPos = 0;
pOldPos = 0;
offset = 0;
return ret;

}*/


For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.

So to me this proverb equated to where < EXIT_SUCCESS = meant that the
function worked fine and have had no bugs > and likewise EXIT_FAILURE
is the opposite of EXIT_SUCCESS.
 
F

FireHead

Richard said:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]

The file < config.h >

....er, I didn't even see a <config.h> - the only inclusion I saw was of
a
contains the following header file declartion.
#ifndef COMMON_HEADER_FILES
#define COMMON_HEADER_FILES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif

(Four of which are not standard C headers.)
I should have put the standard header files instead .. oops.
Yeah, especially when posting in comp.lang.c it's a good idea to reduce your
problem to one that can be expressed using only standard C constructs - or,
at the very least, to special-plead "I know this has got Unixy stuff in it,
but please just ignore all that stuff and focus on the C bit", or even "I'm
not sure whether this is a C problem or a Unix problem, here's the problem,
here's the code, what do you reckon?"

//---------------------------------------------------------------------------------------------------------//
Just a pointer the config.h supposed to cliargs.h header file.

In terms of portablilty anybody should be able these codes.

When the fopen is called rather that /usr/local/some_file_name it can
be also be used for any platform as long as you give valid folder/file
path.
For windows it can be < fopen( "Window File Name");

then code that I have currently will still work for any platform.

One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.
But I just keep gets blasted Segmentation Error/ Memory out of range.

By using get_Copy I might be able simply code even further.

You all might have noticed that I have 3 version of the same
functionality.

2 function will be portable for any platform.

The below function is clean and understandable but its not functionable
on SCO UNIX and on windows.
Now I will not include windows because Windows is just @##$@#$.

/*
short get_Stream(FILE* pSource,char* result){
size_t buflen = 0;
char offset= 0;
int ret=EXIT_SUCCESS;
fpos_t* pCurPos=0 ; //current file position
fpos_t* pOldPos=0 ; //old file position
char *finally = "";
char *buffer = 0;
//get the starting File pointer position
fgetpos(pSource,&pOldPos);
//find the newline = 0x13
do{offset = 0; offset=fgetc(pSource);
} while(offset!=0x0A && offset!=0x0D && offset!=EOF && offset!=NULL &&

offset!=0xFF);

//get the Current File position
fgetpos (pSource,&pCurPos);
printf("%u",pCurPos);
//Total Buffer Length
buflen = (size_t)pCurPos - (size_t)pOldPos;
if(( offset == 0xFF || offset == EOF || offset==NULL) && (buflen ==
EXIT_FAILURE || buflen == EXIT_SUCCESS))
ret= EXIT_FAILURE;
else{
//Reset Everything
fsetpos (pSource,&pOldPos);
//allocate memory for the new address register
buffer = (char *)malloc(buflen);
//read file buffer using fgets
fgets(buffer,buflen,pSource);
//copy the string to a automatic variable register
//finally = get_Copy(buffer);
printf("\nfeof = %d\n",strlen(buffer));

//free the contents
free(buffer);

ret=EXIT_SUCCESS;
}//ELSE

buffer=0;
pCurPos = 0;
pOldPos = 0;
offset = 0;
return ret;

}*/


For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.

So to me this proverb equated to where < EXIT_SUCCESS = meant that the
function worked fine and have had no bugs > and likewise EXIT_FAILURE
is the opposite of EXIT_SUCCESS.


Thanks for the help so far you have give me.
 
C

Christopher Layne

FireHead said:
In terms of portablilty anybody should be able these codes.

Not when it's a minotaur maze.
then code that I have currently will still work for any platform.

I don't know about that one, but I'm not going to argue it.
One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.

Well, the equiv is VirtualAlloc() on windows. Anyways, away from mmap()
and equivalents - it looks like your primary purpose is doing a bunch of
length/size management and appropriately returning a line without initial
buffer size having to be known - yet you force it all with:

return readLPreLine(pSource,content,512); /* default UNIX length */

Why bother with all the other crap if you're maxing out at 512??

size_t blah(char *d, size_t sz, FILE *f)
{
char buf[512];

if (fgets(buf, sizeof(buf), f) == NULL) {
/*
* this requires you to check " == (size_t)-1"
* the alternative is 0, which may not be accurate for your case
* or ssize_t (which is not ANSI, but POSIX) or just an int.
*/
return -1;
}

return (strlen(buf) - 1);
}

Or perhaps I'm misunderstanding your purpose - as it was fairly hard to
understand from looking at it.

Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.

The point of mmap() is to providely memory mapped files and to not have to
play around with copies of things unless you need the copies.
For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.

There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).
 
F

FireHead

Christopher said:
FireHead said:
In terms of portablilty anybody should be able these codes.

Not when it's a minotaur maze.
then code that I have currently will still work for any platform.

I don't know about that one, but I'm not going to argue it.
One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.

Well, the equiv is VirtualAlloc() on windows. Anyways, away from mmap()
and equivalents - it looks like your primary purpose is doing a bunch of
length/size management and appropriately returning a line without initial
buffer size having to be known - yet you force it all with:

return readLPreLine(pSource,content,512); /* default UNIX length */

Why bother with all the other crap if you're maxing out at 512??

size_t blah(char *d, size_t sz, FILE *f)
{
char buf[512];

if (fgets(buf, sizeof(buf), f) == NULL) {
/*
* this requires you to check " == (size_t)-1"
* the alternative is 0, which may not be accurate for your case
* or ssize_t (which is not ANSI, but POSIX) or just an int.
*/
return -1;
}

return (strlen(buf) - 1);
}

Or perhaps I'm misunderstanding your purpose - as it was fairly hard to
understand from looking at it.

Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.

The point of mmap() is to providely memory mapped files and to not have to
play around with copies of things unless you need the copies.
For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.

There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).


I had to give 512 as the default size; but if/when there an need for a
bigger memory then function will exactly handle that.
A example is reading TCP raw streams which have more than 512 bytes in
stream length. By using this function, the function automatically the
adjusts the size if need more than 512 bytes.
Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
Since I am a boolean gates which have a size of 1 bit and in terms of
type declaration short type had fitted the target.

Could you explain how does integer more efficient? I would be
interested about this.

There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).

When you are about use a function your proverb is correct.
And when you look at the driver module you will see that the KISS
methology is implemented.
From my POV driver module looked straight simple to me.

By making into a challenging environment I will create a jealousy and
moral aggrevation between individual programmer that are hovering about.
 
F

FireHead

Christopher said:
FireHead said:
In terms of portablilty anybody should be able these codes.

Not when it's a minotaur maze.
then code that I have currently will still work for any platform.

I don't know about that one, but I'm not going to argue it.
One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.

Well, the equiv is VirtualAlloc() on windows. Anyways, away from mmap()
and equivalents - it looks like your primary purpose is doing a bunch of
length/size management and appropriately returning a line without initial
buffer size having to be known - yet you force it all with:

return readLPreLine(pSource,content,512); /* default UNIX length */

Why bother with all the other crap if you're maxing out at 512??

size_t blah(char *d, size_t sz, FILE *f)
{
char buf[512];

if (fgets(buf, sizeof(buf), f) == NULL) {
/*
* this requires you to check " == (size_t)-1"
* the alternative is 0, which may not be accurate for your case
* or ssize_t (which is not ANSI, but POSIX) or just an int.
*/
return -1;
}

return (strlen(buf) - 1);
}

Or perhaps I'm misunderstanding your purpose - as it was fairly hard to
understand from looking at it.

Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.

The point of mmap() is to providely memory mapped files and to not have to
play around with copies of things unless you need the copies.
For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.

There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).


I had to give 512 as the default size; but if/when there an need for a
bigger memory then function will exactly handle that.
A example is reading TCP raw streams which have more than 512 bytes in
stream length. By using this function, the function automatically the
adjusts the size if need more than 512 bytes.
Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
Since I am a boolean gates which have a size of 1 bit and in terms of
type declaration short type had fitted the target.

Could you explain how does integer is more efficient? I would be
interested about this.
There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).

When you are about use a function your proverb is correct.
And when you look at the driver module you will see that the KISS
methology is implemented.
From my POV driver module looked straight simple to me.

By making into a challenging environment I will create a jealousy and
moral aggrevation between individual programmer that are hovering about.
 
F

FireHead

Christopher said:
FireHead said:
In terms of portablilty anybody should be able these codes.

Not when it's a minotaur maze.
then code that I have currently will still work for any platform.

I don't know about that one, but I'm not going to argue it.
One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.

Well, the equiv is VirtualAlloc() on windows. Anyways, away from mmap()
and equivalents - it looks like your primary purpose is doing a bunch of
length/size management and appropriately returning a line without initial
buffer size having to be known - yet you force it all with:

return readLPreLine(pSource,content,512); /* default UNIX length */

Why bother with all the other crap if you're maxing out at 512??

size_t blah(char *d, size_t sz, FILE *f)
{
char buf[512];

if (fgets(buf, sizeof(buf), f) == NULL) {
/*
* this requires you to check " == (size_t)-1"
* the alternative is 0, which may not be accurate for your case
* or ssize_t (which is not ANSI, but POSIX) or just an int.
*/
return -1;
}

return (strlen(buf) - 1);
}

Or perhaps I'm misunderstanding your purpose - as it was fairly hard to
understand from looking at it.

Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.

The point of mmap() is to providely memory mapped files and to not have to
play around with copies of things unless you need the copies.
For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.

There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).


I had to give 512 as the default size; but if/when there an need for a
bigger memory then function will exactly handle that.
A example is reading TCP raw streams which have more than 512 bytes in
stream length. By using this function, the function automatically the
adjusts the size if need more than 512 bytes.
Also, what's up with the short return types? It smells of pre-optimization of
some sort, and if that's the case - it will most likely be more efficient to
just return an int or size_t.
Since I am a boolean gates which have a size of 1 bit and in terms of
type declaration short type had fitted the target.
By using 32765 data limit for me it was too much and not needed.
Short type as you is only 0 - 255.
Why use type integer?
Could you explain how does integer is more efficient? I would be
interested about this.
There's a larger more important proverb: Keep It Simple (unless you have a
really good reason not to).

When you are about use a function your proverb is correct.
And when you look at the driver module you will see that the KISS
methology is implemented.
 
B

Barry Schwarz

Hello All,

My original topic should have been " using/replace fgets".

I think i have finally solved and used fgets in my program.
But I see somewhere bug/memory leak I just am not able pin the memory
leak.
Could anybody assist me in this issue please.

Your code does not compile cleanly. It is riddled with constraint
violations, undefined behavior, and obvious logical errors. In the
spirit of learning to walk before running, I suggest you start with
simpler projects.
The this the code that I have managed to get it working.

If it doesn't compile cleanly, working is a forlorn hope.
//-------------------------------------------------------------------------------------------------------------------------------------------------//
/*
Will BE MULTITHREADED
open a original file (source file)
Look for the existing custom folder in the HOME directory
If the HOME directory does not exist
Then see if the specs exists in the /usr/local/share (Default)
If the /usr/local/share does not exist then create a template
create a temp
*/

#include <cliargs.h>

A non-standard header. We need to see it if we are going to help.
short readPreLine(FILE* pSource,char **content);
short readPostLine(FILE* pSource,char **content);
short readLPostLine(FILE *pSource,char** content,int default_length);
short readLPreLine(FILE *pSource,char** content,int default_length);


char* get_Copy(char* pStr);

char* get_Copy(char* pStr){
u_int8_t *temp =0;
u_int8_t *final =0;
u_int8_t byte = 0;
u_int8_t dummy = 0;
temp = pStr;

Didn't your compiler complain about incompatible pointer types here?
Even if u_int8_t is a typedef for unsigned char, pStr is not a pointer
to this type.
int pos = 0;
final = &dummy;
/*initilize the contents with new address*/
do{
byte = *temp;printf("%c",byte);
final++;

At each iteration of this loop, the pointer final is incremented to
point some number of bytes further beyond the end of dummy.
temp++;
}while(byte!='\0');
*final = '\0';

Since final no longer points to dummy or to any memory you have the
right to write to, this invokes undefined behavior. Since final
points to an integer type, why are you using character notation?
temp = "";

You went to some trouble in the preceding while loop to have temp
point one beyond the '\0' that terminates pStr. You have now lost
that address and replaced it with the address of a string literal.
temp = pStr;

You obviously don't care about the string literal either. temp now
points to the start of the input string.
/*clean up everything*/

char *results = "";

Those of us with C89 compilers can't help you if you define objects
after executable statements. This is a C99 feature.
results = strcpy( final,temp);

This also invokes undefined behavior. Even if you hadn't altered
final in the while loop, it would still not point to an area large
enough to receive a string of length greater than 0.
return results;
}


/*
short get_Stream(FILE* pSource,char* result){
size_t buflen = 0;
char offset= 0;
int ret=EXIT_SUCCESS;
fpos_t* pCurPos=0 ; //current file position
fpos_t* pOldPos=0 ; //old file position
char *finally = "";
char *buffer = 0;
//get the starting File pointer position
fgetpos(pSource,&pOldPos);

Didn't your compiler complain here. The second argument to fgetpos
must have type fpos_t*. Your code has type fpos_t**.
//find the newline = 0x13
do{offset = 0; offset=fgetc(pSource);

What is the purpose of the first statement? offset is changed
immediately. offset needs to be an int (see below).
} while(offset!=0x0A && offset!=0x0D && offset!=EOF && offset!=NULL &&
offset!=0xFF);

offset is a char. NULL could be defined as (void*)0. If so, this is
another constraint violation. As your code stands now, there is no
guarantee that EOF can be represented in a char.
//get the Current File position
fgetpos (pSource,&pCurPos);
printf("%u",pCurPos);

More undefined behavior. %u is for unsigned int. Your argument has
type fpos_t*.
//Total Buffer Length
buflen = (size_t)pCurPos - (size_t)pOldPos;
if(( offset == 0xFF || offset == EOF || offset==NULL) && (buflen ==
EXIT_FAILURE || buflen == EXIT_SUCCESS))

buflen is a size_t. There is no guarantee that either EXIT_FAILURE or
EXIT_SUCCESS can be represented as a size_t. Furthermore, you seem to
think these values have some significance in relation to length of a
buffer. They don't; though it might appear that way on your system.
ret= EXIT_FAILURE;
else{
//Reset Everything
fsetpos (pSource,&pOldPos);

Another constraint violation.
//allocate memory for the new address register
buffer = (char *)malloc(buflen);

Don't cast the return from malloc. It can't help and could cause the
compiler to suppress a diagnostic you would really want to see.
//read file buffer using fgets
fgets(buffer,buflen,pSource);
//copy the string to a automatic variable register
//finally = get_Copy(buffer);
printf("\nfeof = %d\n",strlen(buffer));

More undefined behavior. strlen returns a size_t which will be
unsigned but not necessarily int. %d requires a signed int.
//free the contents
free(buffer);

ret=EXIT_SUCCESS;
}//ELSE

buffer=0;
pCurPos = 0;
pOldPos = 0;
offset = 0;

Why are you bothering to assign to automatic variables that will
disappear after the next statement.
return ret;
}*/

Sorry, there didn't seem to be any point to going beyond these two
functions.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top