static int update_header(void) { off_t save_point; int32 tmp; if(already_warning_lseek) return 0; save_point = lseek(dpm.fd, 0, SEEK_CUR); if(save_point == -1 || lseek(dpm.fd, 4, SEEK_SET) == -1) { ctl->cmsg(CMSG_WARNING, VERB_VERBOSE, "Warning: %s: %s: Can't make valid header", dpm.name, strerror(errno)); already_warning_lseek = 1; return 0; } tmp = LE_LONG(bytes_output + 44 - 8); if(write(dpm.fd, &tmp, 4) == -1) { lseek(dpm.fd, save_point, SEEK_SET); return -1; } lseek(dpm.fd, 40, SEEK_SET); tmp = LE_LONG(bytes_output); write(dpm.fd, &tmp, 4); lseek(dpm.fd, save_point, SEEK_SET); ctl->cmsg(CMSG_INFO, VERB_DEBUG, "%s: Update RIFF WAVE header (size=%d)", dpm.name, bytes_output); return 0; }
int coff_syms(FILE *in) { struct coff_header hdr; char *strtab; uint8_t *symtab; int strtab_len; int i; int ret = 0; if (read_header(in, &hdr) < 0) return -1; strtab_len = read_strtab(in, &hdr, &strtab); if (strtab_len < 0) return -1; if (read_symtab(in, &hdr, &symtab) < 0) { if (strtab) free(strtab); return -1; } for (i = 0; i < hdr.stab_count; i++) { uint8_t *entry = symtab + i * STAB_ENTRY_SIZE; uint32_t value = LE_LONG(entry, 8); int storage_class = entry[16]; char namebuf[9]; const char *name = NULL; if (LE_LONG(entry, 0)) { memcpy(namebuf, entry, 8); namebuf[8] = 0; name = namebuf; } else { uint32_t offset = LE_LONG(entry, 4); if (offset >= 4 && offset < strtab_len) name = strtab + offset; } if (name && (storage_class == C_EXT || storage_class == C_LABEL) && stab_set(name, value) < 0) { printc_err("coff: failed to insert symbol\n"); ret = -1; break; } /* Skip auxiliary entries */ i += entry[17]; } if (symtab) free(symtab); if (strtab) free(strtab); return ret; }
static void parse_header(const uint8_t *data, struct coff_header *hdr) { hdr->version = LE_WORD(data, 0); hdr->sec_count = LE_WORD(data, 2); hdr->timestamp = LE_LONG(data, 4); hdr->stab_start = LE_LONG(data, 8); hdr->stab_count = LE_LONG(data, 12); hdr->opt_bytes = LE_WORD(data, 16); hdr->flags = LE_WORD(data, 18); hdr->target_id = LE_WORD(data, 20); }
static int READDW(uint32_t *vp, struct timidity_file *tf) { if (tf_read(vp, 4, 1, tf) != 1) return -1; *vp = LE_LONG(*vp); return 1; }
static int READCHUNK(SFChunk *vp, struct timidity_file *tf) { if (tf_read(vp, 8, 1, tf) != 1) return -1; vp->size = LE_LONG(vp->size); return 1; }
static int parse_sym(Elf32_Sym *s, FILE *in) { uint8_t data[16]; if (fread(data, sizeof(data), 1, in) != 1) return -1; s->st_name = LE_LONG(data, 0); s->st_value = LE_LONG(data, 4); s->st_size = LE_LONG(data, 8); s->st_info = data[12]; s->st_other = data[13]; s->st_shndx = LE_WORD(data, 14); return 0; }
static int acntl(int request, void *arg) { int tmp, tmp1, samples; switch(request) { case PM_REQ_DISCARD: /* Discard stream */ arts_close_stream(stream); stream=NULL; return 0; case PM_REQ_RATE: /* Change sample rate */ arts_close_stream(stream); tmp = (dpm.encoding & PE_16BIT) ? 16 : 8; tmp1 = (dpm.encoding & PE_MONO) ? 1 : 2; stream = arts_play_stream(*(int*)arg, LE_LONG(tmp), tmp1, "timidity"); server_buffer = arts_stream_get(stream, ARTS_P_SERVER_LATENCY) * dpm.rate * (tmp/8) * tmp1 / 1000; return 0; case PM_REQ_GETQSIZ: /* Get maximum queue size */ *(int*)arg = arts_stream_get(stream, ARTS_P_BUFFER_SIZE); return 0; case PM_REQ_SETQSIZ: /* Set queue size */ *(int*)arg = arts_stream_set(stream, ARTS_P_BUFFER_SIZE, *(int*)arg); return 0; case PM_REQ_GETFRAGSIZ: /* Get device fragment size */ *(int*)arg = arts_stream_get(stream, ARTS_P_PACKET_SIZE); return 0; case PM_REQ_GETSAMPLES: /* Get current play sample */ tmp = arts_stream_get(stream, ARTS_P_BUFFER_SIZE) - arts_stream_get(stream, ARTS_P_BUFFER_SPACE) + server_buffer; samples = output_count - tmp; if(samples < 0) samples = 0; if(!(dpm.encoding & PE_MONO)) samples >>= 1; if(dpm.encoding & PE_16BIT) samples >>= 1; *(int*)arg = samples; return 0; case PM_REQ_GETFILLABLE: /* Get fillable device queue size */ *(int*)arg = arts_stream_get(stream, ARTS_P_BUFFER_SPACE); return 0; case PM_REQ_GETFILLED: /* Get filled device queue size */ *(int*)arg = arts_stream_get(stream, ARTS_P_BUFFER_SIZE) - arts_stream_get(stream, ARTS_P_BUFFER_SPACE); return 0; /* The following are not (yet) implemented: */ case PM_REQ_FLUSH: /* Wait until playback is complete */ case PM_REQ_MIDI: /* Send MIDI event */ case PM_REQ_INST_NAME: /* Get instrument name */ return -1; case PM_REQ_OUTPUT_FINISH: /* Sent after last output_data */ case PM_REQ_PLAY_START: /* Called just before playing */ case PM_REQ_PLAY_END: /* Called just after playing */ return 0; } return -1; }
static int parse_shdr(Elf32_Shdr *s, FILE *in) { uint8_t data[40]; if (fread(data, sizeof(data), 1, in) != 1) return -1; s->sh_name = LE_LONG(data, 0); s->sh_type = LE_LONG(data, 4); s->sh_flags = LE_LONG(data, 8); s->sh_addr = LE_LONG(data, 12); s->sh_offset = LE_LONG(data, 16); s->sh_size = LE_LONG(data, 20); s->sh_link = LE_LONG(data, 24); s->sh_info = LE_LONG(data, 28); s->sh_addralign = LE_LONG(data, 32); s->sh_entsize = LE_LONG(data, 36); return 0; }
int coff_extract(FILE *in, binfile_imgcb_t cb, void *user_data) { struct coff_header hdr; uint8_t *shdrs; int ret = 0; int i; if (read_header(in, &hdr) < 0) return -1; if (read_sechdrs(in, &hdr, &shdrs) < 0) return -1; for (i = 0; i < hdr.sec_count; i++) { uint8_t *header = shdrs + SHDR_SIZE * i; uint32_t flags = LE_LONG(header, 40); if (((flags & STYP_TEXT) || (flags & STYP_DATA)) && !(flags & STYP_NOLOAD)) { uint32_t addr = LE_LONG(header, 8); uint32_t offset = LE_LONG(header, 20); uint32_t size = LE_LONG(header, 16); if (load_section(in, addr, offset, size, cb, user_data) < 0) { printc_err("coff: error while loading " "section %d\n", i); ret = -1; break; } } } if (shdrs) free(shdrs); return ret; }
static int parse_ehdr(Elf32_Ehdr *e, FILE *in) { uint8_t data[52]; if (fread(data, sizeof(data), 1, in) != 1) return -1; memcpy(e->e_ident, data, EI_NIDENT); e->e_type = LE_WORD(data, 16); e->e_machine = LE_WORD(data, 18); e->e_version = LE_LONG(data, 20); e->e_entry = LE_LONG(data, 24); e->e_phoff = LE_LONG(data, 28); e->e_shoff = LE_LONG(data, 32); e->e_flags = LE_LONG(data, 36); e->e_ehsize = LE_WORD(data, 40); e->e_phentsize = LE_WORD(data, 42); e->e_phnum = LE_WORD(data, 44); e->e_shentsize = LE_WORD(data, 46); e->e_shnum = LE_WORD(data, 48); e->e_shstrndx = LE_WORD(data, 50); return 0; }
static int parse_phdr(Elf32_Phdr *p, FILE *in) { uint8_t data[32]; if (fread(data, sizeof(data), 1, in) != 1) return -1; p->p_type = LE_LONG(data, 0); p->p_offset = LE_LONG(data, 4); p->p_vaddr = LE_LONG(data, 8); p->p_paddr = LE_LONG(data, 12); p->p_filesz = LE_LONG(data, 16); p->p_memsz = LE_LONG(data, 20); p->p_flags = LE_LONG(data, 24); p->p_align = LE_LONG(data, 28); return 0; }
int CWAVInputSource::AnalyzeSource() { unsigned char *p = FULL_HEADER, *priff = NULL; // seek to the beginning (just in case) m_spIO->Seek(0, FILE_BEGIN); // get the file size m_nFileBytes = m_spIO->GetSize(); // get the RIFF header RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(RIFF_HEADER))) // make sure the RIFF header is valid if (!(p[0] == 'R' && p[1] == 'I' && p[2] == 'F' && p[3] == 'F')) return ERROR_INVALID_INPUT_FILE; p += sizeof(RIFF_HEADER); // read the data type header RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(DATA_TYPE_ID_HEADER))) // make sure it's the right data type if (!(p[0] == 'W' && p[1] == 'A' && p[2] == 'V' && p[3] == 'E')) return ERROR_INVALID_INPUT_FILE; p += sizeof(DATA_TYPE_ID_HEADER); // find the 'fmt ' chunk RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(RIFF_CHUNK_HEADER))) while (!(p[0] == 'f' && p[1] == 'm' && p[2] == 't' && p[3] == ' ')) { p += sizeof(RIFF_CHUNK_HEADER); // move the file pointer to the end of this chunk RETURN_ON_ERROR(ReadSafe(m_spIO, p, LE_LONG(p+4))) p += LE_LONG(p+4); // check again for the data chunk RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(RIFF_CHUNK_HEADER))) } priff = p+4; p += sizeof(RIFF_CHUNK_HEADER); // read the format info RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(WAV_FORMAT_HEADER))) // error check the header to see if we support it if (LE_SHORT(p) != 1) return ERROR_INVALID_INPUT_FILE; // copy the format information to the WAVEFORMATEX passed in FillWaveFormatEx(&m_wfeSource, LE_LONG(p+4), LE_SHORT(p+14), LE_SHORT(p+2)); p += sizeof(WAV_FORMAT_HEADER); // skip over any extra data in the header int nWAVFormatHeaderExtra = LE_LONG(priff) - sizeof(WAV_FORMAT_HEADER); if (nWAVFormatHeaderExtra < 0) return ERROR_INVALID_INPUT_FILE; else { RETURN_ON_ERROR(ReadSafe(m_spIO, p, nWAVFormatHeaderExtra)) p += nWAVFormatHeaderExtra; } // find the data chunk RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(RIFF_CHUNK_HEADER))) while (!(p[0] == 'd' && p[1] == 'a' && p[2] == 't' && p[3] == 'a')) { p += sizeof(RIFF_CHUNK_HEADER); // move the file pointer to the end of this chunk RETURN_ON_ERROR(ReadSafe(m_spIO, p, LE_LONG(p+4))) p += LE_LONG(p+4); // check again for the data chunk RETURN_ON_ERROR(ReadSafe(m_spIO, p, sizeof(RIFF_CHUNK_HEADER))) } // we're at the data block m_nDataBytes = LE_LONG(p+4); if (m_nDataBytes < 0) m_nDataBytes = m_nFileBytes - m_nHeaderBytes; p += sizeof(RIFF_CHUNK_HEADER); m_nHeaderBytes = p - FULL_HEADER; // make sure the data bytes is a whole number of blocks if ((m_nDataBytes % m_wfeSource.nBlockAlign) != 0) return ERROR_INVALID_INPUT_FILE; // calculate the terminating byts m_nTerminatingBytes = 0; // we made it this far, everything must be cool return ERROR_SUCCESS; }
static int open_output(void) { int i, include_enc, exclude_enc; int sample_width, channels; include_enc = 0; exclude_enc = PE_ULAW|PE_ALAW|PE_BYTESWAP; /* They can't mean these */ if(dpm.encoding & PE_16BIT) include_enc |= PE_SIGNED; else exclude_enc |= PE_SIGNED; dpm.encoding = validate_encoding(dpm.encoding, include_enc, exclude_enc); sample_width = (dpm.encoding & PE_16BIT) ? 16 : 8; channels = (dpm.encoding & PE_MONO) ? 1 : 2; /* Open the audio device */ switch (arts_init_state) { case 0: if((i = arts_init()) != 0) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", dpm.name, arts_error_text(i)); return -1; } arts_init_state = 1; break; case 2: ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "TiMidity aRts bug: open_output() after close_output() not supported"); return -1; } stream = arts_play_stream(dpm.rate, LE_LONG(sample_width), channels, "timidity"); if(stream == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", dpm.name, strerror(errno)); return -1; } arts_stream_set(stream, ARTS_P_BLOCKING, 1); server_buffer = arts_stream_get(stream, ARTS_P_SERVER_LATENCY) * dpm.rate * (sample_width/8) * channels / 1000; output_count = 0; return 0; /* "this aRts function isnot yet implemented" * if (dpm.extra_param[0]) { i = arts_stream_set(stream, ARTS_P_PACKET_COUNT, dpm.extra_param[0]); if (i < 0) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", dpm.name, arts_error_text(i)); return 1; } } return 0; * */ }
int main(int argc, char **argv) { int32 note=DEFAULT_NOTE, root=DEFAULT_ROOT, verbosity=0, samplerate=-1, datasize=-1, junklength, c; int infd=STDIN_FILENO; unsigned char thing[4096], *point, modes=0; #ifdef __MACOS__ mac_main_wav2pat(&root, ¬e, &infd); #else while ((c=getopt(argc, argv, "r:n:vh"))>0) switch(c) { case 'r': root=atol(optarg); break; case 'n': root=freq_table[atoi(optarg) & 127]; break; case 'u': note=atol(optarg); break; case 'v': verbosity++; break; case 'h': usage(); return 0; default: return 1; } if (optind==argc-1) { if ((infd=open(argv[optind], O_RDONLY))<0) { perror("wav2pat: I can't open your WAVE"); return 3; } } else if (optind!=argc) { usage(); return 1; } #endif /* check out the putative RIFF WAVE on stdin */ if (read(infd, thing, 12)<=0) { perror("wav2pat: I can't read your WAVE"); return 3; } if (memcmp(thing, "RIFF", 4) || memcmp(thing+8, "WAVE", 4)) { fprintf(stderr, "wav2pat: I want a RIFF WAVE on stdin!" NLS); return 2; } while (datasize==-1) { if (read(infd, thing, 8)!=8) { perror("wav2pat: " "Your WAVE ran out before I got to the interesting bits"); return 3; } junklength=LE_LONG(*((int32 *)(thing+4))); /* This Microsoft file format is designed to be impossible to parse correctly if one doesn't have the full specification. If you have a wave with an INFO "chunk", you lose. Thank you for playing. */ if (!memcmp(thing, "fmt ", 4)) { if (junklength > 4096) { fprintf(stderr, "wav2pat: " "WAVEs with %ld-byte format blocks make me throw up!" NLS, junklength); return 2; } if (read(infd, thing, junklength) != junklength) { perror("wav2pat: Your WAVE is mangled"); return 3; } if (LE_SHORT(*((int16 *)(thing))) != 1) { fprintf(stderr, "wav2pat: I don't understand your WAVE. " "It has a type %d format chunk!" NLS, LE_SHORT(*((int16 *)(thing)))); return 2; } if (LE_SHORT(*((int16 *)(thing + 2))) != 1) { fprintf(stderr, "wav2pat: This WAVE has %d channels! " "There can be only one!" NLS, LE_SHORT(*((int16 *)(thing + 2)))); return 2; } samplerate=LE_LONG(*((int32 *)(thing + 4))); switch(LE_SHORT(*((int16 *)(thing + 14)))) { case 8: modes |= MODES_UNSIGNED; break; case 16: modes |= MODES_16BIT; break; default: fprintf(stderr, "wav2pat: Ack! Ppthbth! %d-bit samples!", LE_SHORT(*((int16 *)(thing + 14)))); return 2; } if (verbosity) fprintf(stderr, "wav2pat: This is a %d-bit, %ld Hz WAVE" NLS, (LE_SHORT(*((int16 *)(thing + 14)))), samplerate); } else if (!memcmp(thing, "data", 4)) { if (samplerate==-1) { fprintf(stderr, "wav2pat: Your WAVE has no format information before data!" NLS); return 2; } if (verbosity) fprintf(stderr, "wav2pat: It has %ld bytes of data" NLS, junklength); datasize=junklength; } else { if (verbosity) fprintf(stderr, "wav2pat: " "Your WAVE has a %ld-byte chunk called `%4.4s'" NLS, junklength, thing); /* It's cool to pad chunks with NULs to align them on half-word boundaries. */ if (junklength & 1) junklength++; while (junklength>0) { if ((c=read(infd, thing, (junklength>4096) ? 4096 : junklength)) <= 0) { perror("wav2pat: Now your WAVE has run out of data"); return 3; } junklength -= c; } } } /* hammer together something that looks like a GUS patch header */ #define pound(a) *point++=(a); #define pounds(a) { int16 x = (int16)LE_SHORT(a); memcpy(point, &x, 2); point+=2; } #define poundl(a) { int32 x = LE_LONG(a); memcpy(point, &x, 4); point+=4; } #define bounce(a) point += a; memset(thing, 0, 335); point=thing; /* header */ memcpy(point, "GF1PATCH110", 12); point += 12; /* Gravis ID */ memcpy(point, "ID#000002", 10); point += 10; /* description */ strcpy((char *)point, "Copyleft 1995 EWE&U Conductions and one Retreated Gravi\032"); point += 60; pound(1); /* instruments */ pound(14); /* voices */ pound(0); /* channels */ pounds(1); /* waveforms */ pounds(127); /* master volume */ poundl(datasize); /* data size */ bounce(36); /* reserved */ pounds(1); /* instrument # */ strcpy((char *)point, "Bleahnoise"); /* instrument name */ point += 16; poundl(datasize); /* instrument size */ pound(1); /* layers */ bounce(40); /* reserved */ pound(0); /* layer duplicate */ pound(0); /* layer */ poundl(datasize); /* layer size */ pound(1); /* samples */ bounce(40); /* reserved */ strcpy((char *)point, "bleah"); /* wave name */ point += 7; pound(0); /* fractions */ poundl(datasize); /* wave size */ poundl(0); /* loop start */ poundl(datasize); /* loop end */ pounds(samplerate); /* sample rate */ poundl(8176); /* low freq */ poundl(12543854); /* high freq */ poundl(root); /* root freq */ pounds(512); /* tune */ pound(7); /* balance */ pound(63); /* envelope rates */ pound(63); pound(63); pound(63); pound(63); pound(63); pound(240); /* envelope offsets */ pound(240); pound(240); pound(240); pound(240); pound(240); pound(0); /* tremolo sweep */ pound(0); /* tremolo rate */ pound(0); /* tremolo depth */ pound(0); /* vibrato sweep */ pound(0); /* vibrato rate */ pound(0); /* vibrato depth */ pound(modes); /* modes */ pounds(note); /* scale freq */ pounds(1024); /* scale factor */ bounce(36); /* reserved */ write(STDOUT_FILENO, thing, 335); /* wave data */ while (datasize>0) { if ((c=read(infd, thing, (datasize>4096) ? 4096 : datasize)) <= 0) { perror("wav2pat: I can't read data"); return 3; } write(STDOUT_FILENO, thing, c); datasize -= c; } /* be courteous */ if (infd != STDIN_FILENO) close(infd); return 0; }
static int wav_output_open(const char *fname) { int t; char RIFFheader[44]; int fd; if(strcmp(fname, "-") == 0) fd = 1; /* data to stdout */ else { /* Open the audio file */ fd = open(fname, FILE_OUTPUT_MODE); if(fd < 0) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", fname, strerror(errno)); return -1; } } /* Generate a (rather non-standard) RIFF header. We don't know yet what the block lengths will be. We'll fix that at close if this is a seekable file. */ memcpy(RIFFheader, orig_RIFFheader, 44); if(dpm.encoding & PE_ALAW) RIFFheader[20] = WAVE_FORMAT_ALAW; else if(dpm.encoding & PE_ULAW) RIFFheader[20] = WAVE_FORMAT_MULAW; else RIFFheader[20] = WAVE_FORMAT_PCM; if(dpm.encoding & PE_MONO) RIFFheader[22] = 1; else RIFFheader[22] = 2; *((int *)(RIFFheader+24)) = LE_LONG(dpm.rate); t = dpm.rate; if(!(dpm.encoding & PE_MONO)) t *= 2; if(dpm.encoding & PE_16BIT) t *= 2; *((int *)(RIFFheader+28)) = LE_LONG(t); /* Bug fixed from Masaaki Koyanagi <*****@*****.**> */ if((dpm.encoding & (PE_MONO | PE_16BIT)) == PE_MONO) RIFFheader[32]='\001'; else if (!(dpm.encoding & PE_MONO) && (dpm.encoding & PE_16BIT)) RIFFheader[32]='\004'; else RIFFheader[32]='\002'; if(dpm.encoding & PE_16BIT) RIFFheader[34] = 16; else RIFFheader[34] = 8; if(write(fd, RIFFheader, 44) == -1) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: write: %s", dpm.name, strerror(errno)); close_output(); return -1; } return fd; }
int main() { char str[128]; char compiler[128]; char fpath[MAX_PATH+1]; char* path = "."; char* glob_pattern = "*wrap*"; int i; int ch; uint u; time_t t; glob_t g; DIR* dir; DIRENT* dirent; thread_data_t thread_data; int fd; int fd2; int canrelock=0; clock_t ticks; /* Show platform details */ DESCRIBE_COMPILER(compiler); printf("%-15s: %s\n","Platform",PLATFORM_DESC); printf("%-15s: %s\n","Version",os_version(str)); printf("%-15s: %s\n","Compiler" ,compiler); printf("%-15s: %d\n","Random Number",xp_random(1000)); for(i=0;i<3;i++) { if(_beginthread( sopen_child_thread /* entry point */ ,0 /* stack size (0=auto) */ ,(void*)i /* data */ )==(unsigned long)-1) printf("_beginthread failed\n"); else SLEEP(1); } printf("Waiting for all sopen_child_threads to close...\n"); SLEEP(5000); /* wait for all threads to quit */ /* Exclusive sopen test */ printf("\nsopen() test\n"); if((fd=sopen(LOCK_FNAME,O_RDWR|O_CREAT,SH_DENYRW,S_IREAD|S_IWRITE))==-1) { perror(LOCK_FNAME); return(errno); } printf("%s is opened with an exclusive (read/write) lock\n",LOCK_FNAME); getkey(); if(_beginthread( sopen_test_thread /* entry point */ ,0 /* stack size (0=auto) */ ,NULL /* data */ )==(unsigned long)-1) printf("_beginthread failed\n"); else SLEEP(1000); close(fd); /* sopen()/lock test */ printf("\nlock() test\n"); if((fd=sopen(LOCK_FNAME,O_RDWR|O_CREAT,SH_DENYNO,S_IREAD|S_IWRITE))==-1) { perror(LOCK_FNAME); return(errno); } write(fd,"lock testing\n",LOCK_LEN); if(lock(fd,LOCK_OFFSET,LOCK_LEN)==0) printf("lock() succeeds\n"); else printf("!FAILURE: lock() non-functional (or file already locked)\n"); if(lock(fd,LOCK_OFFSET,LOCK_LEN)==0) { printf("!FAILURE: Subsequent lock of region was allowed (will skip some tests)\n"); canrelock=1; } if(_beginthread( lock_test_thread /* entry point */ ,0 /* stack size (0=auto) */ ,NULL /* data */ )==(unsigned long)-1) printf("_beginthread failed\n"); else SLEEP(1000); if(canrelock) printf("?? Skipping some tests due to inability to detect own locks\n"); else { if(lock(fd,LOCK_OFFSET,LOCK_LEN)) printf("Locks in first thread survive open()/close() in other thread\n"); else printf("!FAILURE: lock() in first thread lost by open()/close() in other thread\n"); if(lock(fd,LOCK_OFFSET+LOCK_LEN+1,LOCK_LEN)) printf("!FAILURE: file locking\n"); else printf("Record locking\n"); } if((fd2=sopen(LOCK_FNAME,O_RDWR,SH_DENYRW))==-1) { printf("Cannot reopen SH_DENYRW while lock is held\n"); close(fd2); } else { printf("!FAILURE: can reopen SH_DENYRW while lock is held\n"); } if(unlock(fd,LOCK_OFFSET,LOCK_LEN)) printf("!FAILURE: unlock() non-functional\n"); if(lock(fd,LOCK_OFFSET+LOCK_LEN+1,LOCK_LEN)) printf("Cannot re-lock after non-overlapping unlock()\n"); else printf("!FAILURE: can re-lock after non-overlappping unlock()\n"); if(lock(fd,LOCK_OFFSET,LOCK_LEN)) printf("!FAILURE: cannot re-lock unlocked area\n"); close(fd); /* getch test */ printf("\ngetch() test (ESC to continue)\n"); do { ch=getch(); printf("getch() returned %d\n",ch); } while(ch!=ESC); /* kbhit test */ printf("\nkbhit() test (any key to continue)\n"); while(!kbhit()) { printf("."); fflush(stdout); SLEEP(500); } getch(); /* remove character from keyboard buffer */ /* BEEP test */ printf("\nBEEP() test\n"); getkey(); for(i=750;i>250;i-=5) BEEP(i,15); for(;i<1000;i+=5) BEEP(i,15); /* SLEEP test */ printf("\nSLEEP(5 second) test\n"); getkey(); t=time(NULL); printf("sleeping... "); fflush(stdout); ticks=msclock(); SLEEP(5000); printf("slept %ld seconds (%ld according to msclock)\n",time(NULL)-t,(msclock()-ticks)/MSCLOCKS_PER_SEC); /* Thread SLEEP test */ printf("\nThread SLEEP(5 second) test\n"); getkey(); i=0; if(_beginthread( sleep_test_thread /* entry point */ ,0 /* stack size (0=auto) */ ,&i /* data */ )==(unsigned long)-1) printf("_beginthread failed\n"); else { SLEEP(1); /* yield to child thread */ while(i==0) { printf("."); fflush(stdout); SLEEP(1000); } } /* glob test */ printf("\nglob(%s) test\n",glob_pattern); getkey(); i=glob(glob_pattern,GLOB_MARK,NULL,&g); if(i==0) { for(u=0;u<g.gl_pathc;u++) printf("%s\n",g.gl_pathv[u]); globfree(&g); } else printf("glob(%s) returned %d\n",glob_pattern,i); /* opendir (and other directory functions) test */ printf("\nopendir(%s) test\n",path); getkey(); printf("\nDirectory of %s\n\n",FULLPATH(fpath,path,sizeof(fpath))); dir=opendir(path); while(dir!=NULL && (dirent=readdir(dir))!=NULL) { t=fdate(dirent->d_name); printf("%.24s %10lu %06o %s%c\n" ,ctime(&t) ,flength(dirent->d_name) ,getfattr(dirent->d_name) ,dirent->d_name ,isdir(dirent->d_name) ? '/':0 ); } if(dir!=NULL) closedir(dir); printf("\nFree disk space: %lu kbytes\n",getfreediskspace(path,1024)); /* Thread (and inter-process communication) test */ printf("\nSemaphore test\n"); getkey(); if(sem_init(&thread_data.parent_sem ,0 /* shared between processes */ ,0 /* initial count */ )) { printf("sem_init failed\n"); } if(sem_init(&thread_data.child_sem ,0 /* shared between processes */ ,0 /* initial count */ )) { printf("sem_init failed\n"); } if(_beginthread( sem_test_thread /* entry point */ ,0 /* stack size (0=auto) */ ,&thread_data /* data */ )==(unsigned long)-1) printf("_beginthread failed\n"); else { sem_wait(&thread_data.child_sem); /* wait for thread to begin */ for(i=0;i<10;i++) { printf("<parent>"); sem_post(&thread_data.parent_sem); sem_wait(&thread_data.child_sem); } sem_wait(&thread_data.child_sem); /* wait for thread to end */ } sem_destroy(&thread_data.parent_sem); sem_destroy(&thread_data.child_sem); printf("\nSemaphore blocking test\n"); getkey(); sem_init(&thread_data.parent_sem ,0 /* shared between processes */ ,0 /* initial count */ ); sem_init(&thread_data.child_sem ,0 /* shared between processes */ ,0 /* initial count */ ); if(_beginthread( sem_test_thread_block /* entry point */ ,0 /* stack size (0=auto) */ ,&thread_data /* data */ )==(unsigned long)-1) printf("_beginthread failed\n"); else { sem_wait(&thread_data.child_sem); /* wait for thread to begin */ for(i=0;i<10;i++) { printf("<parent>"); SLEEP(5000); sem_post(&thread_data.parent_sem); sem_wait(&thread_data.child_sem); } sem_wait(&thread_data.child_sem); /* wait for thread to end */ } printf("\nsem_trywait_block test..."); t=time(NULL); sem_trywait_block(&thread_data.parent_sem,5000); printf("\ntimed-out after %ld seconds (should be 5 seconds)\n",time(NULL)-t); sem_destroy(&thread_data.parent_sem); sem_destroy(&thread_data.child_sem); printf("\nendian check..."); memcpy(&i,"\x01\x02\x03\x04",4); if(LE_LONG(i)==67305985) { printf("OK!\n"); } else { printf("FAILED!\n"); } return 0; }