title: Beep on Linux in C
body: <p>I want to generate a beep sound with a specific frequency and length (for different sound signals) using the system beeper (and only the speakers if beeper is not available / accessible). I know it is possible to do this by using ioctl, but that requires root access, which I don't want.</p>
  
  <p>I know I could just use the "beep" command, but that would be a dependency, which, if possible, shouldn't be used (no external dependencies at all, just the basic linux libraries and C).</p>
  
  <p>What I currently have is the following code (but this requires superuser privileges to run):</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;linux/kd.h&gt;
  
  int main(int argc, char *argv[])
  {
   int fd = open("/dev/console", O_RDONLY);
   if (fd == -1 || argc != 3) return -1;
   return ioctl(fd, KDMKTONE, (atoi(argv[2])&lt;&lt;16)+(1193180/atoi(argv[1])));
  }
  </code></pre>
  
  <p>If there is no other way to do this, then I will use beep, but I would really like to avoid dependencies and integrate the beep directly into my script, but I'm sure somebody here will know a solution / workaround.</p>
  
  <p>I don't really want external libraries as the program should be as lightweight as possible.</p>