我想用这种形式“/dev/mixer:line”或“/dev/mixer:cd”从gtk_entry 设置混音器设备。
用户必须输入此格式的混音器设备设置:
/dev/mixer:line
or:
/dev/mixer:cd
为此,我编写代码来设置混音器,并且与 strdup() 函数有同样的困境。在调用 strdup() 之前释放具有赋值的字符是错误的吗?
char *mixer_device = "/dev/mixer";
int mixer_channel = SOUND_MIXER_LINE;
int fd = -1;
int get_volume( void )
{
int v, cmd, devs;
int curvol = 0;
if( fd < 0 ) fd = open( mixer_device, O_RDONLY );
if( fd != -1 ) {
ioctl( fd, SOUND_MIXER_READ_DEVMASK, &devs );
if( devs & mixer_dev_mask ) {
cmd = MIXER_READ( mixer_channel );
} else {
return curvol;
}
ioctl( fd, cmd, &v );
curvol = ( v & 0xFF00 ) >> 8;
}
return curvol;
}
char *core_devnames[] = SOUND_DEVICE_NAMES;
int set_device( const char *devname )
{
const char *channame;
int i;
/* if (mixer_device) free (mixer_device) <-- It is wrong ??? */
mixer_device = strdup( devname );
if( !mixer_device ) return -1;
i = strcspn( mixer_device, ":" );
if( i == strlen( mixer_device ) ) {
channame = "line";
} else {
mixer_device[ i ] = 0;
channame = mixer_device + i + 1;
}
fd = open( mixer_device, O_RDONLY );
if( fd == 0 ) {
fprintf( stderr, "mixer: Can't open device %s, "
"mixer volume and mute unavailable.\n", mixer_device );
return -1;
}
return 0;
}
在调用 strdup() 之前释放具有赋值的字符是错误的