#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include
#include <sys\stat.h>
#include "windows.h"
#include "global.h"
#include "wiper.c"
int totalfiles=0;
int p;
int q;
long a;
char stop_wiping = 0;
void usage()
{
fprintf(stdout, "\n%s - %s\nUsage: %s DRIVE_PATH\n", PROGRAM_NAME, PROGRAM_VER, PROGRAM_NAME);
}
int get_opts(int argc, char *argv[], char drive_path[256])
{
int i, p;
char ch;
if (argc != 2){
usage();
return -1;
}
strcpy(drive_path, argv[1]);
p = strlen(drive_path) - 1;
if (drive_path[p] == '/' || drive_path[p]=='\\')
{
drive_path[p] = '\0';
}
return 0;
}
int can_write(char * drive_path){
unsigned char filepath[256];
int r = 0;
sprintf(filepath, "%s/%s", drive_path, TEST_FILENAME);
FILE * f = fopen(filepath, "w+b");
if (f == NULL){
r = -1;
goto exit;
}
if (fputs(filepath, f) < 0){
r = -1;
goto exit;
}
exit:
if (f != NULL){
fclose(f);
if (remove(filepath) != 0){
r = -1;
}
}
return r;
}
#if (OS == 1)
long abort_listener(){
getchar();
stop_wiping = 1;
return 0;
}
#else
void * abort_listener(void * args){
getchar();
stop_wiping = 1;
return 0;
}
#endif
void clear_console_screen()
{
#if (OS == 1)
system("CLS");
#else
system("clear");
#endif
}
void wipe_status1(long file_num, long gap)
{
q=a/20;
fprintf(stdout, "%s - %s\n\nStatus: Deleting process in progress [Don't Press ENTER to stop]", PROGRAM_NAME, PROGRAM_VER);
printf("\nTotal time will take for delete in sec:%0.2f\n",(float)(q/150));
fflush(stdout);
}
void delete_tmp_files(char * drive_path, long file_num)
{
unsigned char filepath[256];
long i;
time_t now_t1, start_t1;
time(&start_t1);
wipe_status1(0L, 0L);
fprintf(stdout, "\nStatus: Deleting temporary files.\n");
for(i=1; i<=file_num; i++){
sprintf(filepath, "%s/%s%ld", drive_path, FILE_PREFIX, i);
if (remove(filepath) != 0){
fprintf(stdout, "\nError: Unable to remove temporary files.");
break;
}
}
}
void wipe_status(char * drive_path,long file_num, long gap)
{
clear_console_screen();
p=a/20;
fprintf(stdout, "%s - %s\n\nStatus: Wiping free space in progress [Press ENTER to stop]", PROGRAM_NAME, PROGRAM_VER);
fprintf(stdout, "\nNote: Don't stop with CTRL+C otherwise the temporary files will not be deleted");
fprintf(stdout, "\nInfo: %ld Mb written in %5.2f min\n", (long) ((long)file_num)*((long)DEFAULT_FILE_SIZE), (float)gap/60);
fprintf(stdout, "\nProgress ............%ld Percent ", ((100*file_num)/p));
fprintf(stdout, "\nRemaining ...........%ld Percent ", (100-((100*file_num)/p)));
fflush(stdout);
}
int wipe(char * drive_path, long file_size)
{
FILE * f;
int i;
int const buf_size = 4096;
long written;
long file_num = 0;
unsigned char buf[buf_size];
unsigned int iseed;
unsigned char filepath[256];
int write_failed;
time_t now_t, start_t;
file_size = file_size * 1024000L;
time(&start_t);
wipe_status(drive_path,0L, 0L);
while((long) ((long)file_num)*((long)DEFAULT_FILE_SIZE)<=a)
{
written = 0;
iseed = (unsigned int) time(NULL);
srand (iseed);
for (i=0; i<buf_size;> {
buf[i] = (unsigned char)( 255.0 * rand() / ( RAND_MAX + 0.0 ) );
}
file_num++;
write_failed = 0;
sprintf(filepath, "%s/%s%ld", drive_path, FILE_PREFIX, file_num);
f = fopen(filepath, "wb");
while(1){
if (f == NULL){
fprintf(stdout, "\nError: Unable to create file \"%s\"", filepath);
write_failed = 1;
break;
}
if (stop_wiping == 1 || fwrite(buf, 1, buf_size, f) < buf_size){
#if (DEBUG == 1)
fprintf(stdout, "\nError: Unable to write to file \"%s\"", filepath);
#endif
write_failed = 1;
break;
}
written += buf_size;
if (written >= file_size){
break;
}
}
if (f != NULL){
fclose(f);
}
if (write_failed == 1){
break;
}
if((long) ((long)file_num)*((long)DEFAULT_FILE_SIZE)<=a)
{
time(&now_t);
wipe_status(drive_path,file_num, difftime(now_t, start_t));
}
else
{
break;
}
}
delete_tmp_files(drive_path, file_num);
return 0;
}
int wipe_wrap(char * drive_path, long file_size)
{
#if (OS == 1)
printf("Drive to be checked: %s\n", drive_path);
__int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
BOOL test = GetDiskFreeSpaceEx(
drive_path,
(PULARGE_INTEGER)&lpFreeBytesAvailable,
(PULARGE_INTEGER)&lpTotalNumberOfBytes,
(PULARGE_INTEGER)&lpTotalNumberOfFreeBytes);
printf("Drive to be checked: %s\n", drive_path);
printf("\nUsing GetDiskFreeSpaceEx()...\n");
printf("The return value: %d, error code: %d\n", test, GetLastError());
printf("Total number of free bytes available for user-caller: %I64u\n", lpFreeBytesAvailable);
a=lpTotalNumberOfFreeBytes/(1024*1024);
printf("Total number of free bytes on disk: %ld Mb\n", a);
DWORD dwThreadId;
DWORD dwThrdParam = 1;
HANDLE thread;
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)abort_listener, &dwThrdParam, 0, &dwThreadId);
if (thread == NULL){
fprintf(stderr, "\nError: CreateThread function failed.");
return -1;
}
return wipe (drive_path, file_size);
#else
pthread_t* stopThread = (pthread_t*) calloc(1, sizeof(pthread_t));
if (pthread_create(&stopThread[0], NULL, abort_listener, &stop_wiping) != 0){
fprintf(stderr, "\nError: pthread_create function failed.");
return -1;
}
return wipe (drive_path, file_size);
#endif
return 0;
}
int main(int argc, char *argv[])
{
long file_size = (long) DEFAULT_FILE_SIZE;
char drive_path[256];
char *opts[256];
FILE *f;
#if (OS == 0)
fprintf(stderr, "\nError: current OS is not supported.");
goto exit;
#endif
if (get_opts(argc, argv, drive_path) != 0){
goto exit;
}
if (can_write(drive_path) != 0){
fprintf(stderr, "\nError: unable to access/write to drive \"%s\"", drive_path);
goto exit;
}
fprintf(stdout, "\nPress ENTER to start wiping free space of drive \"%s\"", drive_path);
getchar();
wipe_wrap(drive_path, file_size);
exit:
fprintf(stdout, "\n\nWebsite: https://sourceforge.net/projects/simpledrivewipe/");
fprintf(stdout, "\nPress ENTER to quit.");
fflush(stdout);
getchar();
return 0;
}
Headerfiles.........................
global.h
#define DEFAULT_FILE_SIZE 20 //in Mb and at least >= 1 Mb
#define FILE_PREFIX "SDWDUMMYFILE_"
#define TEST_FILENAME "SDWDUMMYFILE4TESTDRIVE"
#define PROGRAM_NAME "SimpleDriveWiper"
#define PROGRAM_VER "0.2"
#define DEBUG 0
#if defined(WIN32) && !defined(UNIX)
#define OS 1
#elif defined(__unix__) && !defined(WIN32)
#define OS 2
#elif defined(__linux__) && !defined(WIN32)
#define OS 3
#else
#define OS 0
#endif
wiper.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "global.h"
#if (OS == 1)
#include <windows.h>
#else
#include <pthread.h>
#include <unistd.h>
#include <malloc.h>
#endif
#include "wiper.h"
wiper.h
long abort_listener();
void clear_console_screen();
void delete_tmp_files(char * drive_path, long file_num);
int wipe(char * drive_path, long file_size);
int wipe_wrap(char * drive_path, long file_size);
This code is running in dev c++.
process for exe run
start->run->cmd->cd exe path->enter
exename space drivename(C,D,E):->enter
i want find remaining time when wiping and deleting time after wiping.Please help me.give any solution.
Thanks