int atexit(void (*function) (void)) { exitCallback_t *newCallback; newCallback = New(exitCallback_t); if(!newCallback) { printOom(); exit(1); } newCallback->function = function; newCallback->next = callback; callback = newCallback; return 0; }
/* Copy a directory to Unix */ static int unix_copydir(direntry_t *entry, MainParam_t *mp) { Arg_t *arg=(Arg_t *) mp->arg; time_t mtime; Stream_t *File=mp->File; int ret; char *unixFile; if (!arg->recursive && mp->basenameHasWildcard) return 0; File->Class->get_data(File, &mtime, 0, 0, 0); if (!arg->preserveTime) mtime = 0L; if(!arg->type && arg->verbose) { fprintf(stderr,"Copying "); fprintPwd(stderr, entry,0); fprintf(stderr, "\n"); } if(got_signal) return ERROR_ONE; unixFile = mpBuildUnixFilename(mp); if(!unixFile) { printOom(); return ERROR_ONE; } if(arg->type || !*mpPickTargetName(mp) || !makeUnixDir(unixFile)) { Arg_t newArg; newArg = *arg; newArg.mp.arg = (void *) &newArg; newArg.mp.unixTarget = unixFile; newArg.mp.targetName = 0; newArg.mp.basenameHasWildcard = 1; ret = mp->loop(File, &newArg.mp, "*"); set_mtime(unixFile, mtime); free(unixFile); return ret | GOT_ONE; } else { perror("mkdir"); fprintf(stderr, "Failure to make directory %s\n", unixFile); free(unixFile); return ERROR_ONE; } }
void mmount(int argc, char **argv, int type) { char drive; int pid; int status; struct device dev; char name[EXPAND_BUF]; int media; struct bootsector boot; Stream_t *Stream; if (argc<2 || !argv[1][0] || argv[1][1] != ':' || argv[1][2]){ fprintf(stderr,"Usage: %s -V drive:\n", argv[0]); exit(1); } drive = toupper(argv[1][0]); Stream = find_device(drive, O_RDONLY, &dev, &boot, name, &media, 0); if(!Stream) exit(1); FREE(&Stream); destroy_privs(); if ( dev.partition ) { char part_name[4]; sprintf(part_name, "%d", dev.partition %1000); strcat(name, part_name); } /* and finally mount it */ switch((pid=fork())){ case -1: fprintf(stderr,"fork failed\n"); exit(1); case 0: close(2); open("/dev/null", O_RDWR); argv[1] = strdup("mount"); if ( argc > 2 ) execvp("mount", argv + 1 ); else execlp("mount", "mount", name, 0); perror("exec mount"); exit(1); default: while ( wait(&status) != pid ); } if ( WEXITSTATUS(status) == 0 ) exit(0); argv[0] = strdup("mount"); argv[1] = strdup("-r"); if(!argv[0] || !argv[1]){ printOom(); exit(1); } if ( argc > 2 ) execvp("mount", argv); else execlp("mount", "mount","-r", name, 0); exit(1); }
void mbadblocks(int argc, char **argv, int type) { unsigned int i; unsigned int startSector=2; unsigned int endSector=0; struct MainParam_t mp; Fs_t *Fs; Stream_t *Dir; int ret; char *filename = NULL; char c; unsigned int badClus; int sectorMode=0; int writeMode=0; while ((c = getopt(argc, argv, "i:s:cwS:E:")) != EOF) { switch(c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'c': checkListTwice(filename); filename = strdup(optarg); break; case 's': checkListTwice(filename); filename = strdup(optarg); sectorMode = 1; break; case 'S': startSector = atol(optarg); break; case 'E': endSector = atol(optarg); break; case 'w': writeMode = 1; break; case 'h': usage(0); default: usage(1); } } if (argc != optind+1 || !argv[optind][0] || argv[optind][1] != ':' || argv[optind][2]) { usage(1); } init_mp(&mp); Dir = open_root_dir(argv[optind][0], O_RDWR, NULL); if (!Dir) { fprintf(stderr,"%s: Cannot initialize drive\n", argv[0]); exit(1); } Fs = (Fs_t *)GetFs(Dir); in_len = Fs->cluster_size * Fs->sector_size; in_buf = malloc(in_len); if(!in_buf) { printOom(); ret = 1; goto exit_0; } if(writeMode) { int i; pat_buf=malloc(in_len * N_PATTERN); if(!pat_buf) { printOom(); ret = 1; goto exit_0; } srandom(time(NULL)); for(i=0; i < in_len * N_PATTERN; i++) { pat_buf[i] = random(); } } for(i=0; i < Fs->clus_start; i++ ){ ret = READS(Fs->Next, in_buf, sectorsToBytes((Stream_t*)Fs, i), Fs->sector_size); if( ret < 0 ){ perror("early error"); goto exit_0; } if(ret < (signed int) Fs->sector_size){ fprintf(stderr,"end of file in file_read\n"); ret = 1; goto exit_0; } } ret = 0; badClus = Fs->last_fat + 1; if(startSector < 2) startSector = 2; if(endSector > Fs->num_clus + 2 || endSector <= 0) endSector = Fs->num_clus + 2; if(filename) { char line[80]; FILE *f = fopen(filename, "r"); if(f == NULL) { fprintf(stderr, "Could not open %s (%s)\n", filename, strerror(errno)); ret = 1; goto exit_0; } while(fgets(line, sizeof(line), f)) { char *ptr = line + strspn(line, " \t"); long offset = strtoul(ptr, 0, 0); if(sectorMode) offset = (offset-Fs->clus_start)/Fs->cluster_size + 2; if(offset < 2) { fprintf(stderr, "Sector before start\n"); } else if(offset >= Fs->num_clus) { fprintf(stderr, "Sector beyond end\n"); } else { mark(Fs, offset, badClus); ret = 1; } } } else { Stream_t *dev; dev = Fs->Next; if(dev->Next) dev = dev->Next; in_len = Fs->cluster_size * Fs->sector_size; if(writeMode) { /* Write pattern */ for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, pat_buf + in_len * (i % N_PATTERN), 1); } /* Flush cache, so that we are sure we read the data back from disk, rather than from the cache */ if(!got_signal) DISCARD(dev); /* Read data back, and compare to pattern */ for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, pat_buf + in_len * (i % N_PATTERN), 0); } } else { for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, NULL, 0); } } } exit_0: FREE(&Dir); exit(ret); }
/* Write the Unix file */ static int unix_write(direntry_t *entry, MainParam_t *mp, int needfilter) { Arg_t *arg=(Arg_t *) mp->arg; time_t mtime; Stream_t *File=mp->File; Stream_t *Target, *Source; struct stat stbuf; int ret; char errmsg[80]; char *unixFile; File->Class->get_data(File, &mtime, 0, 0, 0); if (!arg->preserveTime) mtime = 0L; if(arg->type) unixFile = "-"; else unixFile = mpBuildUnixFilename(mp); if(!unixFile) { printOom(); return ERROR_ONE; } /* if we are creating a file, check whether it already exists */ if(!arg->type) { if (!arg->nowarn && &arg->type && !access(unixFile, 0)){ if( ask_confirmation("File \"%s\" exists, overwrite (y/n) ? ", unixFile,0)) { free(unixFile); return ERROR_ONE; } /* sanity checking */ if (!stat(unixFile, &stbuf) && !S_ISREG(stbuf.st_mode)) { fprintf(stderr,"\"%s\" is not a regular file\n", unixFile); free(unixFile); return ERROR_ONE; } } } if(!arg->type && arg->verbose) { fprintf(stderr,"Copying "); mpPrintFilename(stderr,mp); fprintf(stderr,"\n"); } if(got_signal) { free(unixFile); return ERROR_ONE; } if ((Target = SimpleFileOpen(0, 0, unixFile, O_WRONLY | O_CREAT | O_TRUNC, errmsg, 0, 0, 0))) { ret = 0; if(needfilter && arg->textmode){ Source = open_filter(COPY(File)); if (!Source) ret = -1; } else Source = COPY(File); if (ret == 0 ) ret = copyfile(Source, Target); FREE(&Source); FREE(&Target); if(ret <= -1){ if(!arg->type) { unlink(unixFile); free(unixFile); } return ERROR_ONE; } if(!arg->type) { set_mtime(unixFile, mtime); free(unixFile); } return GOT_ONE; } else { fprintf(stderr,"%s\n", errmsg); if(!arg->type) free(unixFile); return ERROR_ONE; } }