io_error_t copy_file(const char *oldname, const char *newname) { /* make newname a copy of oldname */ int ifd, ofd; mus_long_t bytes, wb, total; char *buf = NULL; total = 0; ifd = OPEN(oldname, O_RDONLY, 0); if (ifd == -1) return(IO_CANT_OPEN_FILE); ofd = CREAT(newname, 0666); if (ofd == -1) { snd_close(ifd, oldname); return(IO_CANT_CREATE_FILE); } buf = (char *)calloc(8192, sizeof(char)); while ((bytes = read(ifd, buf, 8192))) { total += bytes; wb = write(ofd, buf, bytes); if (wb != bytes) { snd_close(ofd, newname); snd_close(ifd, oldname); free(buf); return(IO_WRITE_ERROR); } } snd_close(ifd, oldname); wb = disk_kspace(newname); snd_close(ofd, newname); free(buf); if (wb < 0) return(IO_DISK_FULL); return(IO_NO_ERROR); }
ATF_TC_BODY(seekdir_basic, tc) { DIR *dp; char *wasname; struct dirent *entry; long here; #ifdef __FreeBSD__ #define CREAT(x, m) do { \ int _creat_fd; \ ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))), \ "creat(%s, %x) failed: %s", (x), (m), \ strerror(errno)); \ (void)close(_creat_fd); \ } while(0); ATF_REQUIRE_MSG(mkdir("t", 0755) == 0, "mkdir failed: %s", strerror(errno)); CREAT("t/a", 0600); CREAT("t/b", 0600); CREAT("t/c", 0600); #else mkdir("t", 0755); creat("t/a", 0600); creat("t/b", 0600); creat("t/c", 0600); #endif dp = opendir("t"); if ( dp == NULL) atf_tc_fail("Could not open temp directory."); /* skip two for . and .. */ entry = readdir(dp); entry = readdir(dp); /* get first entry */ entry = readdir(dp); here = telldir(dp); #ifdef __FreeBSD__ ATF_REQUIRE_MSG(here != -1, "telldir failed: %s", strerror(errno)); #endif /* get second entry */ entry = readdir(dp); #ifdef __FreeBSD__ ATF_REQUIRE_MSG(entry != NULL, "readdir failed: %s", strerror(errno)); #endif wasname = strdup(entry->d_name); if (wasname == NULL) atf_tc_fail("cannot allocate memory"); /* get third entry */ entry = readdir(dp); /* try to return to the position after the first entry */ seekdir(dp, here); entry = readdir(dp); if (entry == NULL) atf_tc_fail("entry 1 not found"); if (strcmp(entry->d_name, wasname) != 0) atf_tc_fail("1st seekdir found wrong name"); /* try again, and throw in a telldir() for good measure */ seekdir(dp, here); here = telldir(dp); entry = readdir(dp); if (entry == NULL) atf_tc_fail("entry 2 not found"); if (strcmp(entry->d_name, wasname) != 0) atf_tc_fail("2nd seekdir found wrong name"); /* One more time, to make sure that telldir() doesn't affect result */ seekdir(dp, here); entry = readdir(dp); if (entry == NULL) atf_tc_fail("entry 3 not found"); if (strcmp(entry->d_name, wasname) != 0) atf_tc_fail("3rd seekdir found wrong name"); closedir(dp); #ifdef __FreeBSD__ free(wasname); #endif }
int main(int argc, char **argv) { PHASH freq = NewHash(); int freqfid, codefid; int nrSource, delta; double clk; int count = 0; INDATA srcFile, compFile; /* Must be passed the name of a file to compress */ if(argc < 2) { printf("Must give a file to compress\n"); exit(1); } TIMESTART(clk); initLFIOFile(&srcFile, argv[1]); /* Read the file, 2 bytes at a time and create the frequency table */ nrSource = 0; while(canFetch(&srcFile, 2)) { U16 wrd = fetchWordLE(&srcFile); nrSource += 2; bumpFrequency(freq, wrd); } finishIOFile(&srcFile); printf("Read %d bytes\n", nrSource); /* Open the code and frequency files */ freqfid = CREAT("huffman.freq"); codefid = CREAT("huffman.code"); compressfid = CREAT("huffman.compressed"); if(freqfid < 0 || codefid < 0 || compressfid < 0) { ERROR0(1, "Cannot create frequency and code files\n"); } createCompressTables(freq, freqfid, codefid); close(freqfid); close(codefid); FreeHash(freq); /* Now read again and compress */ initCompressor("huffman.code"); initLFIOFile(&srcFile, argv[1]); outPoint = 0; written = 0; startCompress(&writeByte); /* Read the file, 2 bytes at a time and compress */ while(canFetch(&srcFile, 2)) { U16 wrd = fetchWordLE(&srcFile); writeCompressedSymbol(wrd); } endCompress(); flushOut(); close(compressfid); finishIOFile(&srcFile); /* Now decompress and compare */ for(count=0;count<30;count++) { initLFIOFile(&compFile, "huffman.compressed"); initLFIOFile(&srcFile, argv[1]); startDecompress(); delta = nrSource; while(delta > 0) { int comp = fetchCompressedSymbol(&compFile); int src = fetchWordLE(&srcFile); if(src != comp) { ERROR3(-1, "Src(%04x) != Comp(%04x) (at offset %d)\n", src, comp, nrSource - delta); } delta -= 2; } endDecompress(&compFile); finishIOFile(&srcFile); finishIOFile(&compFile); } finalizeCompressor(); TIMESTOP(clk); // sm: according to my man pages, the 'l' flag doesn't apply // to the 'f' format, which is always a double argument printf("Source %d bytes. Compressed %d bytes. Ratio: %5.2f\n", nrSource, written, (double)nrSource / (double)written); printf("Run hufftest in %8.3fms\n", clk / 1000.0); exit (0); return 0; }
int main () { int retval, i; int fd; int index_node_number; /* Some arbitrary data for our files */ memset (data1, '1', sizeof (data1)); memset (data2, '2', sizeof (data1)); memset (data3, '3', sizeof (data1)); #ifdef TEST1 /* ****TEST 1: MAXIMUM file creation**** */ /* Generate MAXIMUM regular files */ for (i = 0; i < MAX_FILES + 1; i++) { // go beyond the limit sprintf (pathname, "/file%d", i); retval = CREAT (pathname); if (retval < 0) { fprintf (stderr, "creat: File creation error! status: %d\n", retval); if (i != MAX_FILES) exit(EXIT_FAILURE); } memset (pathname, 0, 80); } /* Delete all the files created */ for (i = 0; i < MAX_FILES; i++) { sprintf (pathname, "/file%d", i); retval = UNLINK (pathname); if (retval < 0) { fprintf (stderr, "unlink: File deletion error! status: %d\n", retval); exit(EXIT_FAILURE); } memset (pathname, 0, 80); } #endif // TEST1 #ifdef TEST2 /* ****TEST 2: LARGEST file size**** */ /* Generate one LARGEST file */ retval = CREAT ("/bigfile"); if (retval < 0) { fprintf (stderr, "creat: File creation error! status: %d\n", retval); exit(EXIT_FAILURE); } retval = OPEN ("/bigfile"); /* Open file to write to it */ if (retval < 0) { fprintf (stderr, "open: File open error! status: %d\n", retval); exit(EXIT_FAILURE); } fd = retval; /* Assign valid fd */ /* Try writing to all direct data blocks */ retval = write (fd, data1, sizeof(data1)); if (retval < 0) { fprintf (stderr, "write: File write STAGE1 error! status: %d\n", retval); exit(EXIT_FAILURE); } #ifdef TEST_SINGLE_INDIRECT /* Try writing to all single-indirect data blocks */ retval = write (fd, data2, sizeof(data2)); if (retval < 0) { fprintf (stderr, "write: File write STAGE2 error! status: %d\n", retval); exit(EXIT_FAILURE); } #ifdef TEST_DOUBLE_INDIRECT /* Try writing to all double-indirect data blocks */ retval = write (fd, data3, sizeof(data3)); if (retval < 0) { fprintf (stderr, "write: File write STAGE3 error! status: %d\n", retval); exit(EXIT_FAILURE); } #endif // TEST_DOUBLE_INDIRECT #endif // TEST_SINGLE_INDIRECT #endif // TEST2 #ifdef TEST3 /* ****TEST 3: Seek and Read file test**** */ retval = LSEEK (fd, 0); /* Go back to the beginning of your file */ if (retval < 0) { fprintf (stderr, "lseek: File seek error! status: %d\n", retval); exit(EXIT_FAILURE); } /* Try reading from all direct data blocks */ retval = READ (fd, addr, sizeof(data1)); if (retval < 0) { fprintf (stderr, "read: File read STAGE1 error! status: %d\n", retval); exit(EXIT_FAILURE); } /* Should be all 1s here... */ printf ("Data at addr: %s\n", addr); #ifdef TEST_SINGLE_INDIRECT /* Try reading from all single-indirect data blocks */ retval = READ (fd, addr, sizeof(data2)); if (retval < 0) { fprintf (stderr, "read: File read STAGE2 error! status: %d\n", retval); exit(EXIT_FAILURE); } /* Should be all 2s here... */ printf ("Data at addr: %s\n", addr); #ifdef TEST_DOUBLE_INDIRECT /* Try reading from all double-indirect data blocks */ retval = write (fd, addr, sizeof(data3)); if (retval < 0) { fprintf (stderr, "read: File read STAGE3 error! status: %d\n", retval); exit(EXIT_FAILURE); } /* Should be all 3s here... */ printf ("Data at addr: %s\n", addr); #endif // TEST_DOUBLE_INDIRECT #endif // TEST_SINGLE_INDIRECT /* Close the bigfile */ retval = CLOSE(fd); if (retval < 0) { fprintf (stderr, "close: File close error! status: %d\n", retval); exit(EXIT_FAILURE); } /* Remove the biggest file */ retval = UNLINK ("/bigfile"); if (retval < 0) { fprintf (stderr, "unlink: /bigfile file deletion error! status: %d\n", retval); exit(EXIT_FAILURE); } #endif // TEST3 #ifdef TEST4 /* ****TEST 4: Make directory and read directory entries**** */ retval = MKDIR ("/dir1"); if (retval < 0) { fprintf (stderr, "mkdir: Directory 1 creation error! status: %d\n", retval); exit(EXIT_FAILURE); } retval = MKDIR ("/dir1/dir2"); if (retval < 0) { fprintf (stderr, "mkdir: Directory 2 creation error! status: %d\n", retval); exit(EXIT_FAILURE); } retval = MKDIR ("/dir1/dir3"); if (retval < 0) { fprintf (stderr, "mkdir: Directory 3 creation error! status: %d\n", retval); exit(EXIT_FAILURE); } retval = OPEN ("/dir1"); /* Open directory file to read its entries */ if (retval < 0) { fprintf (stderr, "open: Directory open error! status: %d\n", retval); exit(EXIT_FAILURE); } fd = retval; /* Assign valid fd */ memset (addr, 0, sizeof(addr)); /* Clear scratchpad memory */ while ((retval = READDIR (fd, addr))) { /* 0 indicates end-of-file */ if (retval < 0) { fprintf (stderr, "readdir: Directory read error! status: %d\n", retval); exit(EXIT_FAILURE); } index_node_number = atoi(&addr[14]); printf ("Contents at addr: [%s,%d]\n", addr, index_node_number); } #endif // TEST4 #ifdef TEST5 /* ****TEST 5: 2 process test**** */ if((retval = fork())) { if(retval == -1) { fprintf(stderr, "Failed to fork\n"); exit(EXIT_FAILURE); } /* Generate 300 regular files */ for (i = 0; i < 300; i++) { sprintf (pathname, "/file_p_%d", i); retval = CREAT (pathname); if (retval < 0) { fprintf (stderr, "(Parent) create: File creation error! status: %d\n", retval); exit(EXIT_FAILURE); } memset (pathname, 0, 80); } } else { /* Generate 300 regular files */ for (i = 0; i < 300; i++) { sprintf (pathname, "/file_c_%d", i); retval = CREAT (pathname); if (retval < 0) { fprintf (stderr, "(Child) create: File creation error! status: %d\n", retval); exit(EXIT_FAILURE); } memset (pathname, 0, 80); } } #endif // TEST5 printf("Congratulations, you have passed all tests!!\n"); return 0; }
int DBF::create ( const char *file, DBfield field[], int numfield ) { if ( _fd >= 0 ) close() ; if ( numfield > MAX_FIELDS ) { _errNo = DB_STRUC_ERR ; return DB_FAILURE ; } #if defined(UNIX) int FD = CREAT(strlwr(makeFileExt(file,".dbf"))) ; #else int FD = CREAT(file) ; #endif if ( FD < 0 ) { _errNo = DB_CREATE_ERR ; return DB_FAILURE ; } int i ; int RS = 1 ; for ( i = 0 ; i < numfield ; i++ ) { RS += field[i].length ; } time_t T = time(NULL) ; tm *t = localtime(&T) ; HEADER h ; _updYear = t->tm_year + 1900; _updMonth = t->tm_mon + 1; _updDay = t->tm_mday; memset(&h,0,sizeof(h)) ; h.signature = DBF_SIGNATURE ; h.date[0] = t->tm_year ; // since 1900 h.date[1] = _updMonth; h.date[2] = _updDay; h.headSize = sizeof(HEADER) + numfield * sizeof(FIELD) + 1 ; h.recSize = RS ; if ( WRITE(FD,&h,sizeof(h)) != sizeof(h) ) { CLOSE(FD) ; _errNo = DB_CREATE_ERR ; return DB_FAILURE ; } char *F ; int sz ; F = new char[sz=numfield*sizeof(FIELD)+1] ; if ( F == NULL ) { CLOSE(FD) ; _errNo = DB_NO_MEMORY ; return DB_FAILURE ; } memset(F,0,sz) ; FIELD *f = (FIELD*)F ; for ( i = 0 ; i < numfield ; i++, f++ ) { strcpy(f->name,field[i].name) ; strupr(f->name) ; f->type = field[i].type ; f->length = field[i].length ; f->decimal = field[i].decimal ; } f->name[0] = 0x0d ; i = WRITE(FD,F,sz) ; delete[] F ; if ( i != sz ) { CLOSE(FD) ; _errNo = DB_CREATE_ERR ; return DB_FAILURE ; } FLUSH(FD) ; _fd = FD ; return initial(&h) ; }