apue.3第二章2-13 运行awk脚本输出到.c文件

在当前目录新建一个1.awk

#!/usr/bin/awk -f 

BEGIN	{
	printf("#include \"apue.h\"\n")
	printf("#include <errno.h>\n")
	printf("#include <limits.h>\n")
	printf("\n")
	printf("static void	pr_sysconf(char *, int);\n")
	printf("static void	pr_pathconf(char *, char *, int);\n")
	printf("\n")
	printf("int\n")
	printf("main(int argc, char *argv[])\n")
	printf("{\n")
	printf("\tif (argc != 2)\n")
	printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")
	FS="\t+"
	while (getline <"sysconf.sym" > 0) {
		printf("#ifdef %s\n", $1)
		printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
		printf("#else\n")
		printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
		printf("#endif\n")
		printf("#ifdef %s\n", $2)
		printf("\tpr_sysconf(\"%s =\", %s);\n", $1, $2)
		printf("#else\n")
		printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
		printf("#endif\n")
	}
	close("sysconf.sym")
	while (getline <"pathconf.sym" > 0) {
		printf("#ifdef %s\n", $1)
		printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
		printf("#else\n")
		printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
		printf("#endif\n")
		printf("#ifdef %s\n", $2)
		printf("\tpr_pathconf(\"%s =\", argv[1], %s);\n", $1, $2)
		printf("#else\n")
		printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
		printf("#endif\n")
	}
	close("pathconf.sym")
	exit
}
END	{
	printf("\texit(0);\n")
	printf("}\n\n")
	printf("static void\n")
	printf("pr_sysconf(char *mesg, int name)\n")
	printf("{\n")
	printf("\tlong	val;\n\n")
	printf("\tfputs(mesg, stdout);\n")
	printf("\terrno = 0;\n")
	printf("\tif ((val = sysconf(name)) < 0) {\n")
	printf("\t\tif (errno != 0) {\n")
	printf("\t\t\tif (errno == EINVAL)\n")
	printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
	printf("\t\t\telse\n")
	printf("\t\t\t\terr_sys(\"sysconf error\");\n")
	printf("\t\t} else {\n")
	printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
	printf("\t\t}\n")
	printf("\t} else {\n")
	printf("\t\tprintf(\" %%ld\\n\", val);\n")
	printf("\t}\n")
	printf("}\n\n")
	printf("static void\n")
	printf("pr_pathconf(char *mesg, char *path, int name)\n")
	printf("{\n")
	printf("\tlong	val;\n")
	printf("\n")
	printf("\tfputs(mesg, stdout);\n")
	printf("\terrno = 0;\n")
	printf("\tif ((val = pathconf(path, name)) < 0) {\n")
	printf("\t\tif (errno != 0) {\n")
	printf("\t\t\tif (errno == EINVAL)\n")
	printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
	printf("\t\t\telse\n")
	printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")
	printf("\t\t} else {\n")
	printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
	printf("\t\t}\n")
	printf("\t} else {\n")
	printf("\t\tprintf(\" %%ld\\n\", val);\n")
	printf("\t}\n")
	printf("}\n")
}

根据awk文件的命令,再新建两个文件sysconf.sympathconf.sym,内容分别是:
ARG_MAX _SC_ARG_MAXNAME_MAX _PC_NAME_MAX。一定注意中间不是空格是tab,tab应该是awk命令的默认分隔符。
然后运行awk -f 1.awk > 1.c

  • -f 1.awk表示从这个脚本中读取awk命令以执行
  • > 1.c表示所有的输出都重定向到1.c
  • 不需要重定向输入,因为命令是自己读取当前目录中的sysconf.sympathconf.sym的。(跟命令的写法有关,因为这里是写死的,你也可以写成某个变量,然后在命令参数指定好这个输入文件)

运行后生成1.c文件:

#include "apue.h"
#include <errno.h>
#include <limits.h>

static void	pr_sysconf(char *, int);
static void	pr_pathconf(char *, char *, int);

int
main(int argc, char *argv[])
{
	if (argc != 2)
		err_quit("usage: a.out <dirname>");

#ifdef ARG_MAX
	printf("ARG_MAX defined to be %d\n", ARG_MAX+0);
#else
	printf("no symbol for ARG_MAX\n");
#endif
#ifdef _SC_ARG_MAX
	pr_sysconf("ARG_MAX =", _SC_ARG_MAX);
#else
	printf("no symbol for _SC_ARG_MAX\n");
#endif
#ifdef NAME_MAX
	printf("NAME_MAX 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
	exit(0);
}

static void
pr_sysconf(char *mesg, int name)
{
	long	val;

	fputs(mesg, stdout);
	errno = 0;
	if ((val = sysconf(name)) < 0) {
		if (errno != 0) {
			if (errno == EINVAL)
				fputs(" (not supported)\n", stdout);
			else
				err_sys("sysconf error");
		} else {
			fputs(" (no limit)\n", stdout);
		}
	} else {
		printf(" %ld\n", val);
	}
}

static void
pr_pathconf(char *mesg, char *path, int name)
{
	long	val;

	fputs(mesg, stdout);
	errno = 0;
	if ((val = pathconf(path, name)) < 0) {
		if (errno != 0) {
			if (errno == EINVAL)
				fputs(" (not supported)\n", stdout);
			else
				err_sys("pathconf error, path = %s", path);
		} else {
			fputs(" (no limit)\n", stdout);
		}
	} else {
		printf(" %ld\n", val);
	}
}

再运行gcc 1.c -o 1 -l apue生成可执行文件。
然后就可以进行各种测试了:

  • ./1 .测当前目录
  • ./1 1.c

在这里插入图片描述

posted @ 2019-06-09 15:54  allMayMight  阅读(155)  评论(0编辑  收藏  举报