#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ioctl.h> #include <fcntl.h> #include <linux/fs.h> #include <errno.h> #include <string.h>
#define GLOB_SBD_IOCTL_MARK_BLOCK _IO(0, 0x770B) #define GLOB_SBD_IOCTL_MARK_BLOCK_AS_SPARE _IO(0, 0x770C) #define GLOB_SBD_IOCTL_MARK_BLOCK_AS_DATA _IO(0, 0x770E)
#define BASE 100 #define TAG ","
int main(int argc, char **argv){ FILE *fp; char temp, *p; char *bad = "Bad\0"; char *discarded = "Discarded\n"; char *free = "Free\n"; char *data = "Data\n"; int pbt[9000], i = 100, j, fd, n;
if (argc != 2) { printf("Please input filename\n"); return -1; }
char * filename = argv[1]; fp = fopen(filename, "rw"); if(fp==NULL) { printf("Read Error!"); fclose(fp); exit(0); } /* fd = open("/dev/nda", O_RDWR); if (fd < 0) { printf("open error!\n"); return -1; } */ char buf[100]; while(fgets(buf, 100, fp) != NULL){ i = 0; for (p = strtok(buf, TAG); p; p = strtok(NULL, TAG)) { if(i == 0){ /* the first part strtok by tag*/ i++; sscanf(p, "%d", &n); /*tranvent string p to decimal int n*/ if(n < BASE) break; }else{ if(strcmp(p, discarded) == 0){ if(ioctl(fd, GLOB_SBD_IOCTL_MARK_BLOCK, n)) printf("%s\n Mark logical block %d as discared fail.\n ",strerror(errno), n); } if(strcmp(p, free) == 0){ if(ioctl(fd, GLOB_SBD_IOCTL_MARK_BLOCK_AS_SPARE, n)) printf("%s\n Mark logical block %d as spare fail.\n ",strerror(errno), n); } if(strcmp(p, data) == 0){ if(ioctl(fd, GLOB_SBD_IOCTL_MARK_BLOCK_AS_DATA, n)) printf("%s\n Mark logical block %d as used fail.\n ",strerror(errno), n); } } } }
fclose(fp);
}
|