还有就是清单2-3和2-4根本没main() 函数,这两个清单跟2-2是一起编译吗?
15 个解决方案
#1
请把完整代码贴出来
我可以帮你看看
我可以帮你看看
#2
这是清单2-1,是AWK程序,不懂!
不过看样子,printf()函数里面的全部打印出来就是一个C程序。
1 BEGIN {
2 printf("#include \"apue.h\"\n")
3 printf("#include <errno.h>\n")
4 printf("#include <limits.h>\n")
5 printf("\n")
6 printf("static void pr_sysconf(char *, int);\n")
7 printf("static void pr_pathconf(char *, char *,int);\n")
8 printf("\n")
9 printf("int\n")
10 printf("main(int argc, char *argv[])\n")
11 printf("{\n")
12 printf("\tif (argc != 2)\n")
13 printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")
14 FS="\t+"
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
28 while (getline <"pathconf.sym" > 0) {
29 printf("#ifdef %s\n", $1)
30 printf("\tprintf(\"%sdefned to be %%d\\n\", %s+0);\n", $1, $1)
31 printf("#else\n")
32 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
33 printf("#endif\n")
34 printf("#ifdef %s\n", $2)
35 printf("\tpr_pathconf(\"%s = \", argv[1], %s);\n", $1, $2)
36 printf("#else\n")
37 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
38 printf("#endif\n")
39 }
40 close("pathconf.sym")
41 exit
42 }
43 END {
44 printf("\texit(0);\n")
45 printf("}\n\n")
46 printf("static void \n")
47 printf("pr_sysconf(char *mesg, int name)\n")
48 printf("{\n")
49 printf("\tlong val;\n\n")
50 printf("\tfputs(mesg, stdout);\n")
51 printf("\terrno = 0;\n")
52 printf("\tif ((val = sysconf(name)) < 0) {\n")
53 printf("\t\tif (errno != 0) {\n")
54 printf("\t\t\tif (errno == EINVAL)\n")
55 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
56 printf("\t\t\telse\n")
57 printf("\t\t\t\terr_sys(\"sysconf error\");\n")
58 printf("\t\t} else {\n")
59 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
60 printf("\t\t}\n")
61 printf("\t} else {\n")
62 printf("\t\tprintf(\" %%ld\\n\", val);\n")
63 printf("\t}\n")
64 printf("}\n\n")
65 printf("static void\n")
66 printf("pr_pathconf(char *mesg, char *path, int name)\n")
67 printf("{\n")
68 printf("\tlong val;\n")
69 printf("\n")
70 printf("\tfputs(mesg, stdout);\n")
71 printf("\terrno = 0;\n")
72 printf("\tif ((val = pathconf(path, name)) < 0) {\n")
73 printf("\t\tif (errno != 0) {\n")
74 printf("\t\t\tif (errno != EINVAL)\n")
75 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
76 printf("\t\t\telse\n")
77 printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")
78 printf("\t\t} else {\n")
79 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
80 printf("\t\t}\n")
81 printf("\t} else {\n")
82 printf("\t\tprintf(\" %%ld\\n\", val);\n")
83 printf("\t}\n")
84 printf("}\n")
85 }
86
不过看样子,printf()函数里面的全部打印出来就是一个C程序。
1 BEGIN {
2 printf("#include \"apue.h\"\n")
3 printf("#include <errno.h>\n")
4 printf("#include <limits.h>\n")
5 printf("\n")
6 printf("static void pr_sysconf(char *, int);\n")
7 printf("static void pr_pathconf(char *, char *,int);\n")
8 printf("\n")
9 printf("int\n")
10 printf("main(int argc, char *argv[])\n")
11 printf("{\n")
12 printf("\tif (argc != 2)\n")
13 printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")
14 FS="\t+"
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
28 while (getline <"pathconf.sym" > 0) {
29 printf("#ifdef %s\n", $1)
30 printf("\tprintf(\"%sdefned to be %%d\\n\", %s+0);\n", $1, $1)
31 printf("#else\n")
32 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
33 printf("#endif\n")
34 printf("#ifdef %s\n", $2)
35 printf("\tpr_pathconf(\"%s = \", argv[1], %s);\n", $1, $2)
36 printf("#else\n")
37 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
38 printf("#endif\n")
39 }
40 close("pathconf.sym")
41 exit
42 }
43 END {
44 printf("\texit(0);\n")
45 printf("}\n\n")
46 printf("static void \n")
47 printf("pr_sysconf(char *mesg, int name)\n")
48 printf("{\n")
49 printf("\tlong val;\n\n")
50 printf("\tfputs(mesg, stdout);\n")
51 printf("\terrno = 0;\n")
52 printf("\tif ((val = sysconf(name)) < 0) {\n")
53 printf("\t\tif (errno != 0) {\n")
54 printf("\t\t\tif (errno == EINVAL)\n")
55 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
56 printf("\t\t\telse\n")
57 printf("\t\t\t\terr_sys(\"sysconf error\");\n")
58 printf("\t\t} else {\n")
59 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
60 printf("\t\t}\n")
61 printf("\t} else {\n")
62 printf("\t\tprintf(\" %%ld\\n\", val);\n")
63 printf("\t}\n")
64 printf("}\n\n")
65 printf("static void\n")
66 printf("pr_pathconf(char *mesg, char *path, int name)\n")
67 printf("{\n")
68 printf("\tlong val;\n")
69 printf("\n")
70 printf("\tfputs(mesg, stdout);\n")
71 printf("\terrno = 0;\n")
72 printf("\tif ((val = pathconf(path, name)) < 0) {\n")
73 printf("\t\tif (errno != 0) {\n")
74 printf("\t\t\tif (errno != EINVAL)\n")
75 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
76 printf("\t\t\telse\n")
77 printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")
78 printf("\t\t} else {\n")
79 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
80 printf("\t\t}\n")
81 printf("\t} else {\n")
82 printf("\t\tprintf(\" %%ld\\n\", val);\n")
83 printf("\t}\n")
84 printf("}\n")
85 }
86
#3
这是清单2-3:
1 #include <apue.h>
2 #include <errno.h>
3 #include <limits.h>
4
5 #ifdef PATH_MAX
6 static int pathmax = PATH_MAX;
7 #else
8 static int pathmax = 0;
9 #endif
10
11 #define SUSV3 200112L
12
13 static long posix_version = 0;
14
15 /* If PATH_MAX is indeterminate, no guarantee this is adequate */
16 #define PATH_MAX_GUESS 1024
17
18 char *path_alloc(int *sizep) /* also return allocated size, if nonnull */
19 {
20 char *ptr = NULL;
21 int size = 0;
22
23 if (posix_version == 0) {
24 posix_version = sysconf(_SC_VERSION);
25 }
26
27 if (pathmax == 0) { /* first time through */
28 errno = 0;
29
30 if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
31 if (errno == 0) {
32 pathmax = PATH_MAX_GUESS; /* it's indeterminate */
33 } else {
34 err_sys("pathconf error for _PC_PATH_MAX");
35 }
36 } else {
37 pathmax++; /* add one since it's relative to root */
38 }
39 }
40 if (posix_version < SUSV3) {
41 size = pathmax + 1;
42 } else {
43 size = pathmax;
44 }
45
46 if ((ptr = malloc(size)) == NULL) {
47 err_sys("malloc error for pathname");
48 }
49 if (sizep != NULL) {
50 *sizep = size;
51 }
52
53 return (ptr);
54 }
1 #include <apue.h>
2 #include <errno.h>
3 #include <limits.h>
4
5 #ifdef PATH_MAX
6 static int pathmax = PATH_MAX;
7 #else
8 static int pathmax = 0;
9 #endif
10
11 #define SUSV3 200112L
12
13 static long posix_version = 0;
14
15 /* If PATH_MAX is indeterminate, no guarantee this is adequate */
16 #define PATH_MAX_GUESS 1024
17
18 char *path_alloc(int *sizep) /* also return allocated size, if nonnull */
19 {
20 char *ptr = NULL;
21 int size = 0;
22
23 if (posix_version == 0) {
24 posix_version = sysconf(_SC_VERSION);
25 }
26
27 if (pathmax == 0) { /* first time through */
28 errno = 0;
29
30 if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
31 if (errno == 0) {
32 pathmax = PATH_MAX_GUESS; /* it's indeterminate */
33 } else {
34 err_sys("pathconf error for _PC_PATH_MAX");
35 }
36 } else {
37 pathmax++; /* add one since it's relative to root */
38 }
39 }
40 if (posix_version < SUSV3) {
41 size = pathmax + 1;
42 } else {
43 size = pathmax;
44 }
45
46 if ((ptr = malloc(size)) == NULL) {
47 err_sys("malloc error for pathname");
48 }
49 if (sizep != NULL) {
50 *sizep = size;
51 }
52
53 return (ptr);
54 }
#4
清单2-2和2-4如果有需要我再贴出来!
#5
把代码保存到文件:
test.awk
执行命令:
awk -f test.awk
如果要把结果写入文件:
awk -f test.awk > test.c
#6
我想知道下面这个while() 循环是个什么意思? 跟awk 命令有关吗?
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
另外书上还有这样一段话:
该AWK程序读取两个输入文件——pathconf.sym和sysconfig.sym, 这两个文件中包含了用制表符分隔的限制名和符号列表。并非每种平台都 会定义所有符号,所以围绕每个pathconf和sysconf调用,AWK程序都使用了必要的#ifdef 语句。
例如,AWK程序将输入文件中类似于下列形式的行
NAME_MAX _PC_NAME_MAX
转换成下列C代码:
#ifdef NAME_MAX
printf("NAME_MAX is defined to be %d\n", NAME_MAX+0);
#else
printf("no symbol for NAME_MAX\n");
#endif
#ifdef _PC_NAME_MAX
pr_pathconf("NAME_MAX =", argv[1], _PC_NAME_MAX);
#else
printf("no symbol for _PC_NAME_MAX\n");
#endif
这一段代码是怎么转换过来的?
请教了!
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
另外书上还有这样一段话:
该AWK程序读取两个输入文件——pathconf.sym和sysconfig.sym, 这两个文件中包含了用制表符分隔的限制名和符号列表。并非每种平台都 会定义所有符号,所以围绕每个pathconf和sysconf调用,AWK程序都使用了必要的#ifdef 语句。
例如,AWK程序将输入文件中类似于下列形式的行
NAME_MAX _PC_NAME_MAX
转换成下列C代码:
#ifdef NAME_MAX
printf("NAME_MAX is defined to be %d\n", NAME_MAX+0);
#else
printf("no symbol for NAME_MAX\n");
#endif
#ifdef _PC_NAME_MAX
pr_pathconf("NAME_MAX =", argv[1], _PC_NAME_MAX);
#else
printf("no symbol for _PC_NAME_MAX\n");
#endif
这一段代码是怎么转换过来的?
请教了!
#7
循环读取sysconf.sym的每一行
#8
sysconf.sym 这个文件是系统里的文件吗?可以给我说说这是个什么文件,在哪个路径下?
#9
还有pathconf.sym 文件
#10
你可以亲自查找一下:
find / -type f -name "sysconf.sym"
#11
清单2-1的上面这几行中的$1 和 $2 指的是什么,是使用AWK 命令的后面两个参数吗?
如果命令是:awk -f test.awk > test.c
$1 = -f, $2 = test.awk吗?
#12
不是
$1表示文件的第一列
$2表示文件的第二列
#13
写入文件时,提示: bash: test.c 权限不够。怎么破?要先建立test.c文件,才能写入?
#14
写入文件时,提示: bash: test.c 权限不够。怎么破?要先建立test.c文件,才能写入?
cd /tmp
awk -f test.awk > test.c
#15
首先 chmod u+x test.awk
再 awk -f test.awk > test.c
再 awk -f test.awk > test.c
#1
请把完整代码贴出来
我可以帮你看看
我可以帮你看看
#2
这是清单2-1,是AWK程序,不懂!
不过看样子,printf()函数里面的全部打印出来就是一个C程序。
1 BEGIN {
2 printf("#include \"apue.h\"\n")
3 printf("#include <errno.h>\n")
4 printf("#include <limits.h>\n")
5 printf("\n")
6 printf("static void pr_sysconf(char *, int);\n")
7 printf("static void pr_pathconf(char *, char *,int);\n")
8 printf("\n")
9 printf("int\n")
10 printf("main(int argc, char *argv[])\n")
11 printf("{\n")
12 printf("\tif (argc != 2)\n")
13 printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")
14 FS="\t+"
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
28 while (getline <"pathconf.sym" > 0) {
29 printf("#ifdef %s\n", $1)
30 printf("\tprintf(\"%sdefned to be %%d\\n\", %s+0);\n", $1, $1)
31 printf("#else\n")
32 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
33 printf("#endif\n")
34 printf("#ifdef %s\n", $2)
35 printf("\tpr_pathconf(\"%s = \", argv[1], %s);\n", $1, $2)
36 printf("#else\n")
37 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
38 printf("#endif\n")
39 }
40 close("pathconf.sym")
41 exit
42 }
43 END {
44 printf("\texit(0);\n")
45 printf("}\n\n")
46 printf("static void \n")
47 printf("pr_sysconf(char *mesg, int name)\n")
48 printf("{\n")
49 printf("\tlong val;\n\n")
50 printf("\tfputs(mesg, stdout);\n")
51 printf("\terrno = 0;\n")
52 printf("\tif ((val = sysconf(name)) < 0) {\n")
53 printf("\t\tif (errno != 0) {\n")
54 printf("\t\t\tif (errno == EINVAL)\n")
55 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
56 printf("\t\t\telse\n")
57 printf("\t\t\t\terr_sys(\"sysconf error\");\n")
58 printf("\t\t} else {\n")
59 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
60 printf("\t\t}\n")
61 printf("\t} else {\n")
62 printf("\t\tprintf(\" %%ld\\n\", val);\n")
63 printf("\t}\n")
64 printf("}\n\n")
65 printf("static void\n")
66 printf("pr_pathconf(char *mesg, char *path, int name)\n")
67 printf("{\n")
68 printf("\tlong val;\n")
69 printf("\n")
70 printf("\tfputs(mesg, stdout);\n")
71 printf("\terrno = 0;\n")
72 printf("\tif ((val = pathconf(path, name)) < 0) {\n")
73 printf("\t\tif (errno != 0) {\n")
74 printf("\t\t\tif (errno != EINVAL)\n")
75 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
76 printf("\t\t\telse\n")
77 printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")
78 printf("\t\t} else {\n")
79 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
80 printf("\t\t}\n")
81 printf("\t} else {\n")
82 printf("\t\tprintf(\" %%ld\\n\", val);\n")
83 printf("\t}\n")
84 printf("}\n")
85 }
86
不过看样子,printf()函数里面的全部打印出来就是一个C程序。
1 BEGIN {
2 printf("#include \"apue.h\"\n")
3 printf("#include <errno.h>\n")
4 printf("#include <limits.h>\n")
5 printf("\n")
6 printf("static void pr_sysconf(char *, int);\n")
7 printf("static void pr_pathconf(char *, char *,int);\n")
8 printf("\n")
9 printf("int\n")
10 printf("main(int argc, char *argv[])\n")
11 printf("{\n")
12 printf("\tif (argc != 2)\n")
13 printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")
14 FS="\t+"
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
28 while (getline <"pathconf.sym" > 0) {
29 printf("#ifdef %s\n", $1)
30 printf("\tprintf(\"%sdefned to be %%d\\n\", %s+0);\n", $1, $1)
31 printf("#else\n")
32 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
33 printf("#endif\n")
34 printf("#ifdef %s\n", $2)
35 printf("\tpr_pathconf(\"%s = \", argv[1], %s);\n", $1, $2)
36 printf("#else\n")
37 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
38 printf("#endif\n")
39 }
40 close("pathconf.sym")
41 exit
42 }
43 END {
44 printf("\texit(0);\n")
45 printf("}\n\n")
46 printf("static void \n")
47 printf("pr_sysconf(char *mesg, int name)\n")
48 printf("{\n")
49 printf("\tlong val;\n\n")
50 printf("\tfputs(mesg, stdout);\n")
51 printf("\terrno = 0;\n")
52 printf("\tif ((val = sysconf(name)) < 0) {\n")
53 printf("\t\tif (errno != 0) {\n")
54 printf("\t\t\tif (errno == EINVAL)\n")
55 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
56 printf("\t\t\telse\n")
57 printf("\t\t\t\terr_sys(\"sysconf error\");\n")
58 printf("\t\t} else {\n")
59 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
60 printf("\t\t}\n")
61 printf("\t} else {\n")
62 printf("\t\tprintf(\" %%ld\\n\", val);\n")
63 printf("\t}\n")
64 printf("}\n\n")
65 printf("static void\n")
66 printf("pr_pathconf(char *mesg, char *path, int name)\n")
67 printf("{\n")
68 printf("\tlong val;\n")
69 printf("\n")
70 printf("\tfputs(mesg, stdout);\n")
71 printf("\terrno = 0;\n")
72 printf("\tif ((val = pathconf(path, name)) < 0) {\n")
73 printf("\t\tif (errno != 0) {\n")
74 printf("\t\t\tif (errno != EINVAL)\n")
75 printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
76 printf("\t\t\telse\n")
77 printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")
78 printf("\t\t} else {\n")
79 printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
80 printf("\t\t}\n")
81 printf("\t} else {\n")
82 printf("\t\tprintf(\" %%ld\\n\", val);\n")
83 printf("\t}\n")
84 printf("}\n")
85 }
86
#3
这是清单2-3:
1 #include <apue.h>
2 #include <errno.h>
3 #include <limits.h>
4
5 #ifdef PATH_MAX
6 static int pathmax = PATH_MAX;
7 #else
8 static int pathmax = 0;
9 #endif
10
11 #define SUSV3 200112L
12
13 static long posix_version = 0;
14
15 /* If PATH_MAX is indeterminate, no guarantee this is adequate */
16 #define PATH_MAX_GUESS 1024
17
18 char *path_alloc(int *sizep) /* also return allocated size, if nonnull */
19 {
20 char *ptr = NULL;
21 int size = 0;
22
23 if (posix_version == 0) {
24 posix_version = sysconf(_SC_VERSION);
25 }
26
27 if (pathmax == 0) { /* first time through */
28 errno = 0;
29
30 if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
31 if (errno == 0) {
32 pathmax = PATH_MAX_GUESS; /* it's indeterminate */
33 } else {
34 err_sys("pathconf error for _PC_PATH_MAX");
35 }
36 } else {
37 pathmax++; /* add one since it's relative to root */
38 }
39 }
40 if (posix_version < SUSV3) {
41 size = pathmax + 1;
42 } else {
43 size = pathmax;
44 }
45
46 if ((ptr = malloc(size)) == NULL) {
47 err_sys("malloc error for pathname");
48 }
49 if (sizep != NULL) {
50 *sizep = size;
51 }
52
53 return (ptr);
54 }
1 #include <apue.h>
2 #include <errno.h>
3 #include <limits.h>
4
5 #ifdef PATH_MAX
6 static int pathmax = PATH_MAX;
7 #else
8 static int pathmax = 0;
9 #endif
10
11 #define SUSV3 200112L
12
13 static long posix_version = 0;
14
15 /* If PATH_MAX is indeterminate, no guarantee this is adequate */
16 #define PATH_MAX_GUESS 1024
17
18 char *path_alloc(int *sizep) /* also return allocated size, if nonnull */
19 {
20 char *ptr = NULL;
21 int size = 0;
22
23 if (posix_version == 0) {
24 posix_version = sysconf(_SC_VERSION);
25 }
26
27 if (pathmax == 0) { /* first time through */
28 errno = 0;
29
30 if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
31 if (errno == 0) {
32 pathmax = PATH_MAX_GUESS; /* it's indeterminate */
33 } else {
34 err_sys("pathconf error for _PC_PATH_MAX");
35 }
36 } else {
37 pathmax++; /* add one since it's relative to root */
38 }
39 }
40 if (posix_version < SUSV3) {
41 size = pathmax + 1;
42 } else {
43 size = pathmax;
44 }
45
46 if ((ptr = malloc(size)) == NULL) {
47 err_sys("malloc error for pathname");
48 }
49 if (sizep != NULL) {
50 *sizep = size;
51 }
52
53 return (ptr);
54 }
#4
清单2-2和2-4如果有需要我再贴出来!
#5
这是清单2-1,是AWK程序,不懂!
不过看样子,printf()函数里面的全部打印出来就是一个C程序。
把代码保存到文件:
test.awk
执行命令:
awk -f test.awk
如果要把结果写入文件:
awk -f test.awk > test.c
#6
我想知道下面这个while() 循环是个什么意思? 跟awk 命令有关吗?
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
另外书上还有这样一段话:
该AWK程序读取两个输入文件——pathconf.sym和sysconfig.sym, 这两个文件中包含了用制表符分隔的限制名和符号列表。并非每种平台都 会定义所有符号,所以围绕每个pathconf和sysconf调用,AWK程序都使用了必要的#ifdef 语句。
例如,AWK程序将输入文件中类似于下列形式的行
NAME_MAX _PC_NAME_MAX
转换成下列C代码:
#ifdef NAME_MAX
printf("NAME_MAX is defined to be %d\n", NAME_MAX+0);
#else
printf("no symbol for NAME_MAX\n");
#endif
#ifdef _PC_NAME_MAX
pr_pathconf("NAME_MAX =", argv[1], _PC_NAME_MAX);
#else
printf("no symbol for _PC_NAME_MAX\n");
#endif
这一段代码是怎么转换过来的?
请教了!
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
27 close("sysconf.sym")
另外书上还有这样一段话:
该AWK程序读取两个输入文件——pathconf.sym和sysconfig.sym, 这两个文件中包含了用制表符分隔的限制名和符号列表。并非每种平台都 会定义所有符号,所以围绕每个pathconf和sysconf调用,AWK程序都使用了必要的#ifdef 语句。
例如,AWK程序将输入文件中类似于下列形式的行
NAME_MAX _PC_NAME_MAX
转换成下列C代码:
#ifdef NAME_MAX
printf("NAME_MAX is defined to be %d\n", NAME_MAX+0);
#else
printf("no symbol for NAME_MAX\n");
#endif
#ifdef _PC_NAME_MAX
pr_pathconf("NAME_MAX =", argv[1], _PC_NAME_MAX);
#else
printf("no symbol for _PC_NAME_MAX\n");
#endif
这一段代码是怎么转换过来的?
请教了!
#7
我想知道下面这个while() 循环是个什么意思? 跟awk 命令有关吗?
循环读取sysconf.sym的每一行
#8
sysconf.sym 这个文件是系统里的文件吗?可以给我说说这是个什么文件,在哪个路径下?
#9
还有pathconf.sym 文件
#10
sysconf.sym 这个文件是系统里的文件吗?可以给我说说这是个什么文件,在哪个路径下?
你可以亲自查找一下:
find / -type f -name "sysconf.sym"
#11
15 while (getline <"sysconf.sym" > 0) {
16 printf("#ifdef %s\n", $1)
17 printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
18 printf("#else\n")
19 printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
20 printf("#endif\n")
21 printf("#ifdef %s\n", $2)
22 printf("\tpr_sysconf(\"%s = \", %s);\n", $1, $2)
23 printf("#else\n")
24 printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
25 printf("#endif\n")
26 }
清单2-1的上面这几行中的$1 和 $2 指的是什么,是使用AWK 命令的后面两个参数吗?
如果命令是:awk -f test.awk > test.c
$1 = -f, $2 = test.awk吗?
#12
清单2-1的上面这几行中的$1 和 $2 指的是什么,是使用AWK 命令的后面两个参数吗?
如果命令是:awk -f test.awk > test.c
$1 = -f, $2 = test.awk吗?
不是
$1表示文件的第一列
$2表示文件的第二列
#13
这是清单2-1,是AWK程序,不懂!
不过看样子,printf()函数里面的全部打印出来就是一个C程序。
把代码保存到文件:
test.awk
执行命令:
awk -f test.awk
如果要把结果写入文件:
awk -f test.awk > test.c
写入文件时,提示: bash: test.c 权限不够。怎么破?要先建立test.c文件,才能写入?
#14
写入文件时,提示: bash: test.c 权限不够。怎么破?要先建立test.c文件,才能写入?
cd /tmp
awk -f test.awk > test.c
#15
首先 chmod u+x test.awk
再 awk -f test.awk > test.c
再 awk -f test.awk > test.c