A
arnuld
I have created a program which creates and renames files. I have
described everything in comments. All I have is the
cod-duplication. function like fopen, sprint and fwrite are being called
again and again.
I know to remove code-duplication I have to make functions and pass
arguments to them but I am not able to think of a way doing it. Can you
post some example for me, out of this code:
/* The Logging program:
* A programs that will take input from stdin and put that into log files.
* using C99 (GNU-Linux/UNIX specific extensions may be there)
*
* VERSION 1.0
*
Each log file is of fixed size called LOG_SIZE, if input from stdin is more than
LOG_SIZE then we will write the date to 2nd file. file naming convention is %d.log,
e.g. 1.log, 2.log , 3.log etc.
We have 3 scenerios:
1) if we already have 0.log and (new incoming data + sizeof(0.log)) is less than
LOG_SIZE then we will write the data to 0.log. otherwise we wil go to scenerio
number 2.
2) if we have 0.log filled with size LOG_SIZE, then we will rename it to 1.log and
create a new file 0.log and feed the new data into it. Hence largest the number
oldest is the file.
3) Number of log-files are to be kept less than LOG_CNT_MAX, if we hit the LOG_CNT_MAX
then we we will delete the oldest file, rename the files to new numbers and create
a new file to write data. e.g. if LOG_CNT_MAX = 3 and we have 0.log, 1.log, 2.log
filled with data of LOG_SIZE, then we will not create 3.log but we will delete 2.log
and rename 1.log to 2.log and 0.log to 1.log respectively and create a new 0.log to
write data.
thats the whole basic idea of my logging system.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#define LOG_NAME "%d.log"
#define LOG_DIR "."
#define TRUE 1
#define FALSE 0
enum { LOG_NAME_SIZE = 6, LOG_SIZE = 6, LOG_CNT_MAX = 30, LOG_BASE_NUM = 0 };
int find_log_num( const char * );
/* LOG_NAME_SIZE is the size of LOG_NAME in bytes
* LOG_CNT_MAX is the maximum number of log files to be kept on the system
* LOG_SIZE is the size of log file.
* LOG_INPUT_MAX is also the maximum input that a client-log is supposed to send.
* if the log-input is more than LOG_INPUT_MAX, then it means we are not having a log file
* but either a system bug or malicious intentions of some SPAMMER. So refuse the baby to
* access your system
*
* If you insist on using this file as a separate program running in main() then make sure
LOG_CNT_MAX and "i" in main for loop are in COMPLETE HARMONY
*
*
*/
int main( void )
{
// char log_arr[LOG_SIZE];
const char log_recv_arr[] = "Love\n";
char log_name[LOG_NAME_SIZE];
long log_size;
int log_cnt;
FILE *fp;
size_t log_recv_size;
int BACKUP;
/* directory and file manipulation variables */
struct stat statbuf;
/* initialize arrays */
memset( log_name, '\0', LOG_NAME_SIZE);
log_size = 0;
log_recv_size = strlen( log_recv_arr );
// printf("INPUT IS: %s\n\n", log_recv_arr);
log_cnt = 0;
printf("log_cnt = %d\n", log_cnt);
for( int i = 0; i != LOG_CNT_MAX ; ++i )
{
if( sprintf(log_name, LOG_NAME, i) < 0 )
{
perror("SPRINTF ERROR - (BEGIN PROGRAM)");
exit( EXIT_FAILURE );
}
if( ! (stat(log_name, &statbuf)) )
{
log_cnt = i;
}
else if( ENOENT == errno )
{
continue;
}
else
{
perror("STAT ERROR");
exit( EXIT_FAILURE );
}
}
printf("---> log_cnt = %d\n", log_cnt);
if( log_cnt )
{
BACKUP = TRUE;
}
else
{
BACKUP = FALSE;
}
// we have the log_cnt, which means file exists, so we can find its size
if( sprintf(log_name, LOG_NAME, log_cnt) < 0 )
{
perror("SNPRINTF ERROR - after search loop");
exit( EXIT_FAILURE );
}
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
else
{
log_size = 0;
}
printf("%s = %ld\n", log_name, log_size);
/* start Logging */
for(int i = 0 ; i != 8; ++i)
{
printf("\n\nLOOP #%d\n", i);
if( !log_cnt ) // S1 -- begin
{
/* There are 2 sub-cases here:
1) 0.log does not exist, we create a new one.
since we created a fresh file, hence we are sure that size of fresh file
is == LOG_SIZE, so we don't need any check before writing to it. after
creating the file we need to check how much data we have written.
filesize == LOG_SIZE --> create a new filename, probably 1.log
filesize < LOG_SIZE --> go for input once more
2) 0.log exists but size is less than LOG_SIZE
(filesize + received data) <= LOGSIZE --> write data
ELSE --> create anew file
*/
printf("--------------- Into SCENERIO (1) :: log_cnt == 0 -----------------\n\n");
// log_size == 0 means we have no backup files, hence we will do a fresh log
// or else we can use BACKUP flag
if( ! log_size ) // S1 1st case
{
printf("Entered 1st case\n");
printf("File does not exist, Creating a new one\n");
// In future, it will be a function: create_log_name( log_name );
if( sprintf(log_name, LOG_NAME, log_cnt) < 0 )
{
perror("SPRINTF ERROR -- near line 217");
exit( EXIT_FAILURE );
}
// In future, it will be a function: write_data( log_name );
if( NULL == (fp = fopen(log_name, "a")) )
{
perror("FOPEN() EROR");
exit( EXIT_FAILURE );
}
if( (fwrite(log_recv_arr, 1, log_recv_size, fp)) != log_recv_size )
{
perror("FWRITE() ERROR");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 219");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long)statbuf.st_size;
}
printf(" %s written = %ld\n\n", log_name, log_size);
if( log_size > LOG_SIZE )
{
perror("YOU STUPID MORON .... 1st case (END)");
exit( EXIT_FAILURE );
}
} // S1 1st case
else if( (log_size + log_recv_size) <= LOG_SIZE )//S1 --> 2nd case
{
printf("Entered 2nd case\n");
printf("File exists\n");
printf("%s size = %ld\n", log_name, log_size);
/* open file and append the data */
if( ! (fp = fopen(log_name, "a")) )
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
/* update log_size */
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
} // S1 <-- 2nd case
else if( (log_size + log_recv_size) > LOG_SIZE ) // S1 --> 3rd case
{
printf("Entered 3rd case\n");
printf("%s = %ld\n", log_name, log_size);
++log_cnt;
}
} // if( !log_cnt ) S1 <--- END
// SCENERIO (2)
else if( (log_cnt > 0) && (log_cnt < LOG_CNT_MAX) )//log_cnt < LOG_CNT_MAX ) // S2 --> begin
{
/* Entering into this condition means we are definitely sure that 0.log has been
either filled or size of (new input + size of (0.log)) is > LOG_SIZE.
NOTE: 1st of all we will move the log files. we will reach at 1.log with final move.
we will create an empty 0.log file and then proceed as usual.
sinec ewe have fresh file, 0.log, we can fit all the data into it as, because of the ENUM
constants above, we are sure the data will always be <= LOG_SIZE
*/
printf("------------------- Into SCENERIO (2) -------------------\n");
printf("log_cnt = %d\n", log_cnt);
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("Oldest %s exists with size = %ld bytes\n", log_name, log_size);
printf( "BACKUP = %d\n", BACKUP);
/* move_log( log_cnt - 1 )
log_create( 0 );
*/
//====================================================================================
//========================== RENAME MECHANISM ========================================
int backup_cnt;
if( BACKUP )
{
backup_cnt = log_cnt + 1;
}
else
{
backup_cnt = log_cnt;
}
char temp_oldname[LOG_NAME_SIZE];
char temp_newname[LOG_NAME_SIZE];
for( int i = backup_cnt; (i > LOG_BASE_NUM) && (i < LOG_CNT_MAX); --i )
{
if( sprintf(temp_oldname, LOG_NAME, i-1) < 0 )
{
perror("SPRINTF ERROR oldname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
if( sprintf(temp_newname, LOG_NAME, i ) < 0 )
{
perror("SPRINTF ERROR newname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
printf("i = %d\n", i );
printf("temp_oldname = %s\n", temp_oldname);
printf("temp_newname = %s\n", temp_newname);
if( rename(temp_oldname, temp_newname) < 0 )
{
perror("RENAME ERROR :: S2 --> case b");
exit( EXIT_FAILURE );
}
else
{
printf("renamed <%s> to <%s>\n", temp_oldname, temp_newname);
//sleep(2);
}
}
// create 0.log
if( sprintf(log_name, LOG_NAME, LOG_BASE_NUM) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
// open file and append the data
if( ! (fp = fopen(log_name, "a")) ) // S2 -- case a
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
// update the log_cnt
if( (log_size + log_recv_size) >= LOG_SIZE )
{
++log_cnt;
}
//========================== RENAME MECHANISM ========================================
//====================================================================================
} // S2 begin ends
// SCENERIO (3)
else // log_cnt >= LOG_CNT_MAX
{
printf("------------------- Into SCENERIO (3) -------------------\n");
printf("log_cnt = %d, LOG_CNT_MAX = %d\n", log_cnt, LOG_CNT_MAX);
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("Oldest %s exists with size = %ld bytes\n", log_name, log_size);
printf( "BACKUP = %d\n", BACKUP);
/* well here we are log_cnt >= LOG_CNT_MX. We will simply do 3 things:
1) Delete the oldest file, which is (LOG_CNT_MAX - 1).log
2) Rename all files
3) create 0.log name
4) create 0.log file
5) write data
*/
//====================================================================================
//========================== RENAME MECHANISM ========================================
int backup_cnt = log_cnt - 1;
char temp_oldname[LOG_NAME_SIZE];
char temp_newname[LOG_NAME_SIZE];
for( int i = backup_cnt; (i > LOG_BASE_NUM) && (i < LOG_CNT_MAX); --i )
{
if( sprintf(temp_oldname, LOG_NAME, i-1) < 0 )
{
perror("SPRINTF ERROR oldname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
if( sprintf(temp_newname, LOG_NAME, i ) < 0 )
{
perror("SPRINTF ERROR newname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
printf("i = %d\n", i );
printf("temp_oldname = %s\n", temp_oldname);
printf("temp_newname = %s\n", temp_newname);
if( rename(temp_oldname, temp_newname) < 0 )
{
perror("RENAME ERROR :: S2 --> case b");
exit( EXIT_FAILURE );
}
else
{
printf("renamed <%s> to <%s>\n", temp_oldname, temp_newname);
//sleep(2);
}
}
// create 0.log
if( sprintf(log_name, LOG_NAME, LOG_BASE_NUM) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
// open file and append the data
if( ! (fp = fopen(log_name, "a")) ) // S2 -- case a
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
// No need to update the log_cnt as log_cnt == LOG_CNT_MAX
//========================== RENAME MECHANISM ========================================
//====================================================================================
/*
if( ! (stat(log_name_old, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("File %s exists with size = %ld bytes\n", log_name, log_size);
printf("// code yet to be written\n\n");
*/
}
} // for( ... ) loop
return 0;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// R . E. N. A. M. E F. I. L. E. S
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
char temp_oldname[LOG_NAME_SIZE];
char temp_newname[LOG_NAME_SIZE];
for( int i = log_cnt; i <= 0; --i )
{
if( sprintf(temp_oldname, LOG_NAME, i) < 0 )
{
perror("SPRINTF ERROR oldname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
if( sprintf(temp_newname, LOG_NAME, (i + 1) ) < 0 )
{
perror("SPRINTF ERROR newname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
printf("i = %d\n", i );
printf("temp_oldname = %s\n", temp_oldname);
printf("temp_newname = %s\n", temp_newname);
printf("---------------------------------------------------------------------------------\n\n");
system("ls");
if( rename(temp_oldname, temp_newname) < 0 )
{
perror("RENAME ERROR :: S2 --> case b");
exit( EXIT_FAILURE );
}
system("ls");
printf("---------------------------------------------------------------------------------\n\n");
}
// create 0.log
if( sprintf(log_name, LOG_NAME, 0) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
// open file and append the data
if( ! (fp = fopen(log_name, "a")) ) // S2 -- case a
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
// update the log_cnt
if( log_size >= LOG_SIZE )
{
++log_cnt;
printf("log_cnt = %d\n", log_cnt);
if( sprintf(log_name, LOG_NAME, log_cnt) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b (END) \n");
exit( EXIT_FAILURE );
}
}
} // S2 <-- case b
}
*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// R . E. N. A. M. E F. I. L. E. S
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
described everything in comments. All I have is the
cod-duplication. function like fopen, sprint and fwrite are being called
again and again.
I know to remove code-duplication I have to make functions and pass
arguments to them but I am not able to think of a way doing it. Can you
post some example for me, out of this code:
/* The Logging program:
* A programs that will take input from stdin and put that into log files.
* using C99 (GNU-Linux/UNIX specific extensions may be there)
*
* VERSION 1.0
*
Each log file is of fixed size called LOG_SIZE, if input from stdin is more than
LOG_SIZE then we will write the date to 2nd file. file naming convention is %d.log,
e.g. 1.log, 2.log , 3.log etc.
We have 3 scenerios:
1) if we already have 0.log and (new incoming data + sizeof(0.log)) is less than
LOG_SIZE then we will write the data to 0.log. otherwise we wil go to scenerio
number 2.
2) if we have 0.log filled with size LOG_SIZE, then we will rename it to 1.log and
create a new file 0.log and feed the new data into it. Hence largest the number
oldest is the file.
3) Number of log-files are to be kept less than LOG_CNT_MAX, if we hit the LOG_CNT_MAX
then we we will delete the oldest file, rename the files to new numbers and create
a new file to write data. e.g. if LOG_CNT_MAX = 3 and we have 0.log, 1.log, 2.log
filled with data of LOG_SIZE, then we will not create 3.log but we will delete 2.log
and rename 1.log to 2.log and 0.log to 1.log respectively and create a new 0.log to
write data.
thats the whole basic idea of my logging system.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#define LOG_NAME "%d.log"
#define LOG_DIR "."
#define TRUE 1
#define FALSE 0
enum { LOG_NAME_SIZE = 6, LOG_SIZE = 6, LOG_CNT_MAX = 30, LOG_BASE_NUM = 0 };
int find_log_num( const char * );
/* LOG_NAME_SIZE is the size of LOG_NAME in bytes
* LOG_CNT_MAX is the maximum number of log files to be kept on the system
* LOG_SIZE is the size of log file.
* LOG_INPUT_MAX is also the maximum input that a client-log is supposed to send.
* if the log-input is more than LOG_INPUT_MAX, then it means we are not having a log file
* but either a system bug or malicious intentions of some SPAMMER. So refuse the baby to
* access your system
*
* If you insist on using this file as a separate program running in main() then make sure
LOG_CNT_MAX and "i" in main for loop are in COMPLETE HARMONY
*
*
*/
int main( void )
{
// char log_arr[LOG_SIZE];
const char log_recv_arr[] = "Love\n";
char log_name[LOG_NAME_SIZE];
long log_size;
int log_cnt;
FILE *fp;
size_t log_recv_size;
int BACKUP;
/* directory and file manipulation variables */
struct stat statbuf;
/* initialize arrays */
memset( log_name, '\0', LOG_NAME_SIZE);
log_size = 0;
log_recv_size = strlen( log_recv_arr );
// printf("INPUT IS: %s\n\n", log_recv_arr);
log_cnt = 0;
printf("log_cnt = %d\n", log_cnt);
for( int i = 0; i != LOG_CNT_MAX ; ++i )
{
if( sprintf(log_name, LOG_NAME, i) < 0 )
{
perror("SPRINTF ERROR - (BEGIN PROGRAM)");
exit( EXIT_FAILURE );
}
if( ! (stat(log_name, &statbuf)) )
{
log_cnt = i;
}
else if( ENOENT == errno )
{
continue;
}
else
{
perror("STAT ERROR");
exit( EXIT_FAILURE );
}
}
printf("---> log_cnt = %d\n", log_cnt);
if( log_cnt )
{
BACKUP = TRUE;
}
else
{
BACKUP = FALSE;
}
// we have the log_cnt, which means file exists, so we can find its size
if( sprintf(log_name, LOG_NAME, log_cnt) < 0 )
{
perror("SNPRINTF ERROR - after search loop");
exit( EXIT_FAILURE );
}
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
else
{
log_size = 0;
}
printf("%s = %ld\n", log_name, log_size);
/* start Logging */
for(int i = 0 ; i != 8; ++i)
{
printf("\n\nLOOP #%d\n", i);
if( !log_cnt ) // S1 -- begin
{
/* There are 2 sub-cases here:
1) 0.log does not exist, we create a new one.
since we created a fresh file, hence we are sure that size of fresh file
is == LOG_SIZE, so we don't need any check before writing to it. after
creating the file we need to check how much data we have written.
filesize == LOG_SIZE --> create a new filename, probably 1.log
filesize < LOG_SIZE --> go for input once more
2) 0.log exists but size is less than LOG_SIZE
(filesize + received data) <= LOGSIZE --> write data
ELSE --> create anew file
*/
printf("--------------- Into SCENERIO (1) :: log_cnt == 0 -----------------\n\n");
// log_size == 0 means we have no backup files, hence we will do a fresh log
// or else we can use BACKUP flag
if( ! log_size ) // S1 1st case
{
printf("Entered 1st case\n");
printf("File does not exist, Creating a new one\n");
// In future, it will be a function: create_log_name( log_name );
if( sprintf(log_name, LOG_NAME, log_cnt) < 0 )
{
perror("SPRINTF ERROR -- near line 217");
exit( EXIT_FAILURE );
}
// In future, it will be a function: write_data( log_name );
if( NULL == (fp = fopen(log_name, "a")) )
{
perror("FOPEN() EROR");
exit( EXIT_FAILURE );
}
if( (fwrite(log_recv_arr, 1, log_recv_size, fp)) != log_recv_size )
{
perror("FWRITE() ERROR");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 219");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long)statbuf.st_size;
}
printf(" %s written = %ld\n\n", log_name, log_size);
if( log_size > LOG_SIZE )
{
perror("YOU STUPID MORON .... 1st case (END)");
exit( EXIT_FAILURE );
}
} // S1 1st case
else if( (log_size + log_recv_size) <= LOG_SIZE )//S1 --> 2nd case
{
printf("Entered 2nd case\n");
printf("File exists\n");
printf("%s size = %ld\n", log_name, log_size);
/* open file and append the data */
if( ! (fp = fopen(log_name, "a")) )
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
/* update log_size */
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
} // S1 <-- 2nd case
else if( (log_size + log_recv_size) > LOG_SIZE ) // S1 --> 3rd case
{
printf("Entered 3rd case\n");
printf("%s = %ld\n", log_name, log_size);
++log_cnt;
}
} // if( !log_cnt ) S1 <--- END
// SCENERIO (2)
else if( (log_cnt > 0) && (log_cnt < LOG_CNT_MAX) )//log_cnt < LOG_CNT_MAX ) // S2 --> begin
{
/* Entering into this condition means we are definitely sure that 0.log has been
either filled or size of (new input + size of (0.log)) is > LOG_SIZE.
NOTE: 1st of all we will move the log files. we will reach at 1.log with final move.
we will create an empty 0.log file and then proceed as usual.
sinec ewe have fresh file, 0.log, we can fit all the data into it as, because of the ENUM
constants above, we are sure the data will always be <= LOG_SIZE
*/
printf("------------------- Into SCENERIO (2) -------------------\n");
printf("log_cnt = %d\n", log_cnt);
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("Oldest %s exists with size = %ld bytes\n", log_name, log_size);
printf( "BACKUP = %d\n", BACKUP);
/* move_log( log_cnt - 1 )
log_create( 0 );
*/
//====================================================================================
//========================== RENAME MECHANISM ========================================
int backup_cnt;
if( BACKUP )
{
backup_cnt = log_cnt + 1;
}
else
{
backup_cnt = log_cnt;
}
char temp_oldname[LOG_NAME_SIZE];
char temp_newname[LOG_NAME_SIZE];
for( int i = backup_cnt; (i > LOG_BASE_NUM) && (i < LOG_CNT_MAX); --i )
{
if( sprintf(temp_oldname, LOG_NAME, i-1) < 0 )
{
perror("SPRINTF ERROR oldname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
if( sprintf(temp_newname, LOG_NAME, i ) < 0 )
{
perror("SPRINTF ERROR newname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
printf("i = %d\n", i );
printf("temp_oldname = %s\n", temp_oldname);
printf("temp_newname = %s\n", temp_newname);
if( rename(temp_oldname, temp_newname) < 0 )
{
perror("RENAME ERROR :: S2 --> case b");
exit( EXIT_FAILURE );
}
else
{
printf("renamed <%s> to <%s>\n", temp_oldname, temp_newname);
//sleep(2);
}
}
// create 0.log
if( sprintf(log_name, LOG_NAME, LOG_BASE_NUM) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
// open file and append the data
if( ! (fp = fopen(log_name, "a")) ) // S2 -- case a
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
// update the log_cnt
if( (log_size + log_recv_size) >= LOG_SIZE )
{
++log_cnt;
}
//========================== RENAME MECHANISM ========================================
//====================================================================================
} // S2 begin ends
// SCENERIO (3)
else // log_cnt >= LOG_CNT_MAX
{
printf("------------------- Into SCENERIO (3) -------------------\n");
printf("log_cnt = %d, LOG_CNT_MAX = %d\n", log_cnt, LOG_CNT_MAX);
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("Oldest %s exists with size = %ld bytes\n", log_name, log_size);
printf( "BACKUP = %d\n", BACKUP);
/* well here we are log_cnt >= LOG_CNT_MX. We will simply do 3 things:
1) Delete the oldest file, which is (LOG_CNT_MAX - 1).log
2) Rename all files
3) create 0.log name
4) create 0.log file
5) write data
*/
//====================================================================================
//========================== RENAME MECHANISM ========================================
int backup_cnt = log_cnt - 1;
char temp_oldname[LOG_NAME_SIZE];
char temp_newname[LOG_NAME_SIZE];
for( int i = backup_cnt; (i > LOG_BASE_NUM) && (i < LOG_CNT_MAX); --i )
{
if( sprintf(temp_oldname, LOG_NAME, i-1) < 0 )
{
perror("SPRINTF ERROR oldname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
if( sprintf(temp_newname, LOG_NAME, i ) < 0 )
{
perror("SPRINTF ERROR newname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
printf("i = %d\n", i );
printf("temp_oldname = %s\n", temp_oldname);
printf("temp_newname = %s\n", temp_newname);
if( rename(temp_oldname, temp_newname) < 0 )
{
perror("RENAME ERROR :: S2 --> case b");
exit( EXIT_FAILURE );
}
else
{
printf("renamed <%s> to <%s>\n", temp_oldname, temp_newname);
//sleep(2);
}
}
// create 0.log
if( sprintf(log_name, LOG_NAME, LOG_BASE_NUM) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
// open file and append the data
if( ! (fp = fopen(log_name, "a")) ) // S2 -- case a
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
// No need to update the log_cnt as log_cnt == LOG_CNT_MAX
//========================== RENAME MECHANISM ========================================
//====================================================================================
/*
if( ! (stat(log_name_old, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("File %s exists with size = %ld bytes\n", log_name, log_size);
printf("// code yet to be written\n\n");
*/
}
} // for( ... ) loop
return 0;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// R . E. N. A. M. E F. I. L. E. S
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
char temp_oldname[LOG_NAME_SIZE];
char temp_newname[LOG_NAME_SIZE];
for( int i = log_cnt; i <= 0; --i )
{
if( sprintf(temp_oldname, LOG_NAME, i) < 0 )
{
perror("SPRINTF ERROR oldname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
if( sprintf(temp_newname, LOG_NAME, (i + 1) ) < 0 )
{
perror("SPRINTF ERROR newname :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
printf("i = %d\n", i );
printf("temp_oldname = %s\n", temp_oldname);
printf("temp_newname = %s\n", temp_newname);
printf("---------------------------------------------------------------------------------\n\n");
system("ls");
if( rename(temp_oldname, temp_newname) < 0 )
{
perror("RENAME ERROR :: S2 --> case b");
exit( EXIT_FAILURE );
}
system("ls");
printf("---------------------------------------------------------------------------------\n\n");
}
// create 0.log
if( sprintf(log_name, LOG_NAME, 0) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b\n");
exit( EXIT_FAILURE );
}
// open file and append the data
if( ! (fp = fopen(log_name, "a")) ) // S2 -- case a
{
perror("FOPEN() ERROR whne log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fwrite(log_recv_arr, 1, log_recv_size, fp) != log_recv_size )
{
perror("FWRIRE ERROR in log_cnt == 0");
exit( EXIT_FAILURE );
}
if( fclose(fp) )
{
perror("FLOSE() ERROR near line 187");
exit( EXIT_FAILURE );
}
// update log_size
if( ! (stat(log_name, &statbuf)) )
{
log_size = (long) statbuf.st_size;
}
printf("%s written = %ld\n\n", log_name, log_size);
// update the log_cnt
if( log_size >= LOG_SIZE )
{
++log_cnt;
printf("log_cnt = %d\n", log_cnt);
if( sprintf(log_name, LOG_NAME, log_cnt) < 0 )
{
perror("SPRINTF ERROR :: S2 --> case b (END) \n");
exit( EXIT_FAILURE );
}
}
} // S2 <-- case b
}
*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// R . E. N. A. M. E F. I. L. E. S
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~