2011/12

洱海,2011年12月9日

这次骑车环洱海的时候天空多云,本以为不会看到太漂亮的景色,没想到正好看到阳光透过云层洒向湖面,犹如仙境一般。。。

C 语言新标准——C11

12月8号,ISO 发布了新的 C 语言的新标准——C11,之前被称为C1X,官方名称 ISO/IEC 9899:2011。新的标准可以这里下载。这个标准是基于今年4月发布的名为 N1570 的草稿,但据说并未做任何改动。

根据 wikipedia 记载,相比 C99,C11 做了以下重要的更新:

1. 对齐处理操作符 alignof,函数 aligned_alloc(),以及 头文件 <stdalign.h>。见 7.15 节。

2. Noreturn 函数标记,类似于 gcc 的 _attribute((noreturn))。例子:

    _Noreturn void thrd_exit(int res);

3. _Generic 关键词,有点儿类似于 gcc 的 typeof。例子:

#define cbrt(X) _Generic((X), long double: cbrtl,
default: cbrt,
float: cbrtf)(X)

4. 静态断言( static assertions),_Static_assert(),在解释 #if 和 #error 之后被处理。例子:

    _Static_assert(FOO > 0, “FOO has a wrong value”);

5. 删除了 gets() 函数,C99中已经将此函数被标记为过时,推荐新的替代函数 gets_s()。

6. 新的 fopen() 模式,(“…x”)。类似 POSIX 中的 O_CREAT|O_EXCL,在文件锁中比较常用。

7. 匿名结构体/联合体,这个早已经在 gcc 中了,我们并不陌生,定义在 6.7.2.1 p13。

8. 多线程支持,包括:_Thread_local,头文件 <threads.h>,里面包含线程的创建和管理函数(比如 thrd_create(),thrd_exit()),mutex (比如 mtx_lock(),mtx_unlock())等等,更多内容清参考 7.26 节。

9. _Atomic类型修饰符和 头文件 <stdatomic.h>,见 7.17 节。

10. 带边界检查(Bounds-checking)的函数接口,定义了新的安全的函数,例如 fopen_s(),strcat_s() 等等。更多参考 Annex K。

11. 改进的 Unicode 支持,新的头文件 <uchar.h> 等。

12. 新增 quick_exit() 函数,作为第三种终止程序的方式,当 exit() 失败时可以做最少的清理工作(deinitializition),具体见 7.22.4.7。

13. 创建复数的宏, CMPLX(),见 7.3.9.3。

14. 更多浮点数处理的宏 (More macros for querying the characteristics of floating point types, concerning subnormal floating point numbers and the number of decimal digits the type is able to store)。

15. struct timespec 成为 time.h 的一部分,以及宏 TIME_UTC,函数 timespec_get()。

gcc 4.6 中新增了新的选项 -std=c1x 来支持这一标准,更多支持参考这里。但是 glibc 相关的部分尚未实现,所以你还不能马上在 Linux 上体验最新的 C11 特性。

Polyglot

可能你之前也见过这种程序:它是用两种以上的编程语言写成,可以不经修改作为两种语言编译/解释。今天在 wikipedia 上看到了它的定义

In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it.
之前见过的用2种语言写的 Polyglot 简直弱爆了,这里有用至少6种语言写成的 Polyglot:

http://ideology.com.au/polyglot/ (8种语言)
http://mauke.dyndns.org/stuff/poly.poly (16种语言!原链接不能用,我备份了一下。)

更多的 Polyglot:

http://www.nyx.net/~gthompso/poly/polyglot.htm

在窗口中显示X桌面

最近在鼓捣 awesome 的时候看到这么一个脚本,可以在 gnome (或其它任何桌面) 的窗口中显示 X 桌面,这样以来测试 awesome 的配置就很方便了!Xephyr 真是个好东西啊!

[bash]

!/bin/sh

#

test.sh

Login :

Started on Thu Sep 3 15:29:14 2009 Cedric GESTES

$Id$

#

Author(s):

- Cedric GESTES

#

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; either version 3 of the License, or

(at your option) any later version.

#

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

#

You should have received a copy of the GNU General Public License

along with this program; if not, write to the Free Software

Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

#

Xephyr -ac -br -noreset -screen 800x600 :1 &
sleep 1
DISPLAY=:1.0 awesome -c ~/.config/awesome/rc.lua
[/bash]

再遇 bash 引号问题

最近在工作中再次遇到引号的问题,与以往不同,这次这个更加棘手。

问题是这样的:我要在某个 bash 脚本中调用某个命令,根据配置文件来决定应该传递哪些参数给调用的那个命令。而且,该命令的其中一个参数中带有空格,所以必须使用双引号把这一参数作为整体传递进去。你可以试一下,无论怎么使用引号都无法解决这个问题。

最后,我在 Bash FAQ 中找到了答案!解决方法是,把该命令的每一个参数作为 bash 数组的一个元素,最后用 “${args[@]}” 一起传递给该命令即可!

另,Bash FAQ 质量非常高,强烈推荐认真读一下。

显示 shell 函数定义

我们知道在 bash/zsh 中可以用 “typeset -f” 来显示某个函数的定义。如果不用这个内置命令的话我们应该怎么显示函数定义呢?

下面这个技巧就可以用来显示函数定义:
[bash]

!/bin/bash

def() {
eval “$1() $2”
eval “function_$1=”$1() $2””
}

def foo ‘{
echo bar
}’
[/bash]

其中 foo 是定义的函数,$function_foo 是函数 foo 的定义。不过缺点也很明显,每次定义函数必须使用”def”,而且后面用的单引号也是个问题。