commit - ffcd65f14f9701781871511ded9489000a49584c
commit + bd974b1ab491a57a6158424b5ade3492af2bb8a1
blob - c3fa0c95669d9d9974a25e3091ec8a5b9af914cf
blob + e04810b9bcb205b6fc3ca39f9e24dceee510a8ee
--- shreddisk.c
+++ shreddisk.c
#include <util.h>
#define BYTES 32768
+#define BUFF 512
int main(void);
void sh_sig(int);
main(void)
{
FILE *dd;
- char *realdev = NULL, *ppasses = NULL, *cont = NULL;
- char *doshred = NULL, *todo = NULL;
- char dev[512];
- int passes, i_pass, dev_fd, si, tdo;
+ char *realdev = NULL;
+ char ppasses[BUFF], cont[BUFF], doshred[BUFF], todo[BUFF], dev[BUFF];
+ unsigned int passes, i_pass, dev_fd, si, tdo;
double percent, total_bytes, time_remaining = 0;
uint8_t rnum[BYTES];
u_int64_t sectors, total;
struct disklabel lab;
size_t written;
time_t sh_time, now_time;
+ const char *errstr;
signal(SIGINT, sh_sig);
signal(SIGTERM, sh_sig);
if ((realdev = malloc(sizeof(char))) == NULL)
err(1, "malloc");
- if ((ppasses = malloc(sizeof(char))) == NULL)
- err(1, "malloc");
- if ((cont = malloc(sizeof(char))) == NULL)
- err(1, "malloc");
- if ((doshred = malloc(sizeof(char))) == NULL)
- err(1, "malloc");
- if ((todo = malloc(sizeof(char))) == NULL)
- err(1, "malloc");
printf("\n\n%s\n%s\n%s\n\n", string0, string1, string0);
do {
printf("%s", string2);
fgets(dev, sizeof(dev), stdin);
- dev[strlen(dev) - 1] = '\0';
- } while (strlen(dev) < 1);
+ } while (strlen(dev) < 4 || strlen(dev) > BUFF);
+ dev[strlen(dev) - 1] = '\0';
printf("\n");
printf("%s", string3);
fgets(ppasses, sizeof(ppasses), stdin);
ppasses[strlen(ppasses) - 1] = '\0';
- passes = atoi(ppasses);
- } while (passes < 1);
+ passes = strtonum(ppasses, 1, INT_MAX, &errstr);
+ if (errstr != NULL)
+ printf("\nInvalid input: %s\n\n", ppasses);
+ } while (errstr != NULL);
+ errstr = NULL;
printf("\n");
printf("%s", string5);
fgets(todo, sizeof(todo), stdin);
todo[strlen(todo) - 1] = '\0';
- tdo = atoi(todo);
- } while (tdo < 1 || tdo > 3);
+ tdo = strtonum(todo, 1, 3, &errstr);
+ if (errstr != NULL)
+ printf("\nInvalid input: %s\n\n", todo);
+ } while (errstr != NULL);
+ errstr = NULL;
printf("\n");
sectors = DL_GETDSIZE(&lab);
total_bytes = sectors * lab.d_secsize;
- close(dev_fd);
+ if (close(dev_fd) == -1)
+ err(1, "close");
- dd = fopen(realdev, "w");
+ if ((dd = fopen(realdev, "w")) == NULL)
+ err(1, "fopen");
};
if (pledge("stdio unveil", NULL) == -1)
for (i_pass = 0; i_pass < passes; i_pass++) {
total = 0;
- fseek(dd, 0, SEEK_END);
+ if (fseek(dd, 0, SEEK_END) != 0)
+ err(1, "fseek");
sh_time = time(NULL);
printf("\n\nPass %d of %d started %s\n", i_pass + 1, passes,
break;
}
written = fwrite(&rnum, BYTES, 1, dd);
+ if (written != 1 && sizeof(rnum) != written * BYTES)
+ err(1, "fwrite");
+
total += (written * BYTES);
now = seconds;
if (now - elapsed >= 1) {
(total / passed_time);
if (time_remaining > (60 * 60)) {
time_remaining /= (60 * 60);
- asprintf(&suffix, "%s", "hours ");
+ if (asprintf(&suffix, "%s", "hours") ==
+ -1)
+ err(1, "asprintf");
} else if (time_remaining < (60 * 60) &&
time_remaining > 60) {
time_remaining /= 60;
- asprintf(&suffix, "%s", "minutes ");
+ if (asprintf(&suffix, "%s", "minutes")
+ == -1)
+ err(1, "asprintf");
} else
- asprintf(&suffix, "%s", "seconds ");
+ if (asprintf(&suffix, "%s", "seconds")
+ == -1)
+ err(1, "asprintf");
percent = total / total_bytes *
100;
"Approximate time remaining: %0.2f %s ",
percent, time_remaining * (passes - i_pass),
suffix);
- fflush(stdout);
+ if (fflush(stdout) != 0)
+ err(1, "fflush");
free(suffix);
printf("\n\nFinished shredding %s\n", ctime(&sh_time));
- fclose(dd);
+ if (fclose(dd) != 0)
+ err(1, "fclose");
/* re-enable cursor on finish */
printf("\e[?25h");