⬢ DragonFlyBSD Kernel Audit
DF-4634 / probe.c
← back to finding ↓ download raw
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <sys/time.h>

static long ms(struct timeval *a, struct timeval *b){
  return (b->tv_sec-a->tv_sec)*1000+(b->tv_usec-a->tv_usec)/1000;
}
int main(int argc, char **argv){
  const char *dev = argc>1?argv[1]:"/dev/dsp0";
  int fd=open(dev,O_WRONLY);
  if(fd<0){perror("open");return 1;}
  int fmt=AFMT_U8, speed=8000, ch=1;
  ioctl(fd,SNDCTL_DSP_SETFMT,&fmt);
  ioctl(fd,SNDCTL_DSP_CHANNELS,&ch);
  ioctl(fd,SNDCTL_DSP_SPEED,&speed);
  char buf[4096]; memset(buf,0x80,sizeof buf);
  struct timeval a,b,c;
  long wr=0;
  gettimeofday(&a,NULL);
  while(wr<262144){ ssize_t n=write(fd,buf,sizeof buf); if(n<=0)break; wr+=n; }
  gettimeofday(&b,NULL);
  close(fd);
  gettimeofday(&c,NULL);
  printf("wrote %ld bytes in %ld ms; close() took %ld ms\n",
     wr, ms(&a,&b), ms(&b,&c));
  return 0;
}