Hello,
top returns me a 100% xterm and xorg, because of '\r'.
I tried to use the printf every percent, but I have an issue with float, double and round. Any idea would be highly highly welcome because I do not find any possible solution, after long long trials. Help would be great.
Thank you !!!
top returns me a 100% xterm and xorg, because of '\r'.
I tried to use the printf every percent, but I have an issue with float, double and round. Any idea would be highly highly welcome because I do not find any possible solution, after long long trials. Help would be great.
Thank you !!!
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
//#include <math.h>
//#include <time.h>
long totalfilesize = 0;
///////////////////////////////
///////////////////////////////
///////////////////////////////
/////////////////////////////////
/////////////////////////////////
/////////////////////////////////
int fexist(char *a_option)
{
char dir1[PATH_MAX];
char *dir2;
DIR *dip;
strncpy( dir1 , "", PATH_MAX );
strncpy( dir1 , a_option, PATH_MAX );
struct stat st_buf;
int status;
int fileordir = 0 ;
status = stat ( dir1 , &st_buf);
if (status != 0) {
fileordir = 0;
}
// this is compatible to check if a file exists
FILE *fp2check = fopen( dir1 ,"r");
if( fp2check ) {
// exists
fileordir = 1;
fclose(fp2check);
}
if (S_ISDIR (st_buf.st_mode)) {
fileordir = 2;
}
return fileordir;
/////////////////////////////
}
void fseek_filesize(const char *filename)
{
FILE *fp = NULL;
long off;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("failed to fopen %s\n", filename);
exit(EXIT_FAILURE);
}
if (fseek(fp, 0, SEEK_END) == -1)
{
printf("failed to fseek %s\n", filename);
exit(EXIT_FAILURE);
}
off = ftell(fp);
if (off == (long)-1)
{
printf("failed to ftell %s\n", filename);
exit(EXIT_FAILURE);
}
printf("%ld\n", off);
totalfilesize = off;
if (fclose(fp) != 0)
{
printf("failed to fclose %s\n", filename);
exit(EXIT_FAILURE);
}
}
double f_round(double dval, int n)
{
char l_fmtp[32], l_buf[64];
char *p_str;
sprintf (l_fmtp, "%%.%df", n);
if (dval>=0)
sprintf (l_buf, l_fmtp, dval);
else
sprintf (l_buf, l_fmtp, dval);
return ((double)strtod(l_buf, &p_str));
}
//////////////////
void ncpdisplay( char *filetarget, char *filesource )
{
/// new
int curtime = 100;
int lastime = 100;
// fread
char buffer[1];
size_t n;
size_t m;
FILE *fp;
FILE *fp1;
FILE *fp2;
int counter = 0 ;
int freader = 1 ;
int i , j ,posy, posx ;
long copypos = -1;
if ( fexist( filesource ) == 1 )
{
fp = fopen( filesource, "rb");
fp2 = fopen( filetarget, "wb");
counter = 0;
while( !feof(fp) && ( freader == 1) ) {
if ( feof(fp) )
{
freader = 0 ;
}
n = fread( buffer, sizeof(char), 1 , fp);
m = fwrite( buffer, sizeof(char), 1, fp2);
copypos++;
//////////////////////////////////////////////////////////////
//////////////// this area will destroy the CPU !!!! <----
/// [[[[
double z = (double)copypos / (double)totalfilesize*100;
//if ( (int)f_round( z, 0 ) % 2 == 0 )
{
printf("Copying byte: [%ld/%ld] [%lf \%] [%lf]", copypos, totalfilesize, z, f_round(z, 0) );
printf("\r");
}
/// ]]]]
//////////////////////////////////////////////////////////////
usleep( 1 );
}
fclose(fp2);
fclose(fp);
}
printf("\n");
}
int main(int argc, char *argv[])
{
int i ;
if (argc < 2)
{
printf("%s <file1> <file2>...\n", argv[0]);
exit(0);
}
if (argc >= 2)
if (argc == 3)
{
fseek_filesize(argv[1]);
printf( "Source: %s\n", argv[1]);
printf( "Target: %s\n", argv[2]);
ncpdisplay( argv[2], argv[1] );
}
return 0;
}