用C蜂鸣Linux上 [英] Beep on Linux in C

查看:662
本文介绍了用C蜂鸣Linux上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的系统警报器(只有当扬声器蜂鸣器不可/访问)来产生特定频率和长度(对于不同的声音信号)蜂鸣声。我知道这是可能通过使用ioctl要做到这一点,但是这需要root访问权限,这是我不想要的。

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.

我知道我可以只用嘟的命令,但是这将是一个依赖,其中,如果可能的话,不应该使用(没有外部依赖所有,只是基本的Linux库和C)。

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).

我现在有如下code(但这需要超级用户权限来运行):

What I currently have is the following code (but this requires superuser privileges to run):

#include <stdlib.h>
#include <fcntl.h>
#include <linux/kd.h>

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])<<16)+(1193180/atoi(argv[1])));
}

如果没有其他办法可以做到这一点,那么我会用嘟嘟的,但我真的想避免依赖和蜂鸣直接集成到我的剧本,但我敢肯定,在这里有人会知道解决办法/解决方法。

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.

我真的不希望外部库的程序应该尽可能轻便。

I don't really want external libraries as the program should be as lightweight as possible.

推荐答案

我觉得要做到这一点的唯一方法是要么使用SUID给我自己的程序root权限,或者使用蜂鸣声,已经有替代用户标识。我想我会只是增加一个依赖,那么,为蜂鸣声不是太大呢。

I think the only way to do this is to either use suid to give my own program root access, or to use beep, which already has suid. I suppose I will just add one more dependency, then, as beep is not too big anyway.

感谢您所有的答案,我敢肯定,其他库更复杂的信号是伟大的,但我需要一个非常简单的!

Thank you for all the answers, I'm sure other libraries are great for more complex signals, but I need a very simple one!

我认为这个问题可以被标记为解决/关闭,然后。

I think this question can be marked as solved / closed, then.

如果有人发现了一种使用超级用户没有-特权控制台创建蜂鸣声,我仍然有兴趣在此解决方案:)

If anybody finds a way to create a beep using the console without superuser-privileges, I'm still interested in this solution :)

谢谢大家。

这篇关于用C蜂鸣Linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆