{"id":9220,"date":"2013-04-11T17:39:01","date_gmt":"2013-04-11T08:39:01","guid":{"rendered":"http:\/\/www.softantenna.com\/wp\/?p=9220"},"modified":"2013-04-11T17:39:01","modified_gmt":"2013-04-11T08:39:01","slug":"compare-echo-c","status":"publish","type":"post","link":"https:\/\/softantenna.com\/blog\/compare-echo-c\/","title":{"rendered":"UNIX\u306e\u5404\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u304a\u3051\u308becho.c\u306e\u6bd4\u8f03"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/softantenna.com\/blog\/wp-content\/uploads\/2013\/04\/blogecho.png\" alt=\"Echo\" title=\"echo.png\" border=\"0\" width=\"560\" height=\"420\" \/><\/p>\n<p>UNIX\u306e\u5404\u7a2e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u304a\u3051\u308b\u3001ehco.c\u306e\u6bd4\u8f03\u304c\u304a\u3082\u3057\u308d\u3044\u3067\u3059(<a href=\"https:\/\/gist.github.com\/dchest\/1091803\">UNIX V5, OpenBSD, Plan 9, FreeBSD, and GNU coreutils implementations of echo.c<\/a>)\u3002<\/p>\n<p><!--more--><br \/>\necho\u30b3\u30de\u30f3\u30c9\u306fecho \"abc\"\u3068\u3059\u308b\u3068\"abc\"\u3068\u8868\u793a\u3059\u308b\u5358\u7d14\u306a\u30b3\u30de\u30f3\u30c9\u3002\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001UNIX\u306e\u5404\u7a2e\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u304b\u306a\u308a\u9055\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<h3>UNIX Fifth Edition<\/h3>\n<pre><code class=\"c\">main(argc, argv)\nint argc;\nchar *argv[];\n{\n\tint i;\n \n\targc--;\n\tfor(i=1; i<=argc; i++)\n\t\tprintf(\"%s%c\", argv[i], i==argc? '\\n': ' ');\n}\n<\/code><\/pre>\n<p>\u5f15\u6570\u306e\u66f8\u304d\u65b9\u306b\u9055\u548c\u611f\u3092\u611f\u3058\u307e\u3059\u304c\u3059\u3054\u304f\u30b7\u30f3\u30d7\u30eb\u3002\u6614\u306f\u3088\u304b\u3063\u305f\u3002<\/p>\n<h3>OpenBSD<\/h3>\n<pre><code class=\"c\">#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n \n\/* ARGSUSED *\/\nint\nmain(int argc, char *argv[])\n{\n\tint nflag;\n \n\t\/* This utility may NOT do getopt(3) option parsing. *\/\n\tif (*++argv && !strcmp(*argv, \"-n\")) {\n\t\t++argv;\n\t\tnflag = 1;\n\t}\n\telse\n\t\tnflag = 0;\n \n\twhile (*argv) {\n\t\t(void)fputs(*argv, stdout);\n\t\tif (*++argv)\n\t\t\tputchar(' ');\n\t}\n\tif (!nflag)\n\t\tputchar('\\n');\n \n\treturn 0;\n}\n<\/code><\/pre>\n<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u30aa\u30d7\u30b7\u30e7\u30f3\"-n\"(\u6539\u884c\u3092\u51fa\u529b)\u304c\u306f\u3044\u3063\u305f\u3051\u3069\u8a31\u5bb9\u7bc4\u56f2\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<h3>GNU coreutils<\/h3>\n<pre><code class=\"c\">\/* echo.c, derived from code echo.c in Bash.\n   Copyright (C) 1987, 1989, 1991-1997, 1999-2005, 2007-2011 Free Software\n   Foundation, Inc.\n \n   This program is free software: you can redistribute it and\/or modify\n   it under the terms of the GNU General Public License as published by\n   the Free Software Foundation, either version 3 of the License, or\n   (at your option) any later version.\n \n   This program is distributed in the hope that it will be useful,\n   but WITHOUT ANY WARRANTY; without even the implied warranty of\n   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n   GNU General Public License for more details.\n \n   You should have received a copy of the GNU General Public License\n   along with this program.  If not, see <http:\/\/www.gnu.org\/licenses\/>.  *\/\n \n#include <config.h>\n#include <stdio.h>\n#include <sys\/types.h>\n#include \"system.h\"\n \n\/* The official name of this program (e.g., no `g' prefix).  *\/\n#define PROGRAM_NAME \"echo\"\n \n#define AUTHORS \\\n  proper_name (\"Brian Fox\"), \\\n  proper_name (\"Chet Ramey\")\n \n\/* If true, interpret backslash escapes by default.  *\/\n#ifndef DEFAULT_ECHO_TO_XPG\nenum { DEFAULT_ECHO_TO_XPG = false };\n#endif\n \nvoid\nusage (int status)\n{\n  if (status != EXIT_SUCCESS)\n    fprintf (stderr, _(\"Try `%s --help' for more information.\\n\"),\n             program_name);\n  else\n    {\n      printf (_(\"\\\nUsage: %s [SHORT-OPTION]... [STRING]...\\n\\\n  or:  %s LONG-OPTION\\n\\\n\"), program_name, program_name);\n      fputs (_(\"\\\nEcho the STRING(s) to standard output.\\n\\\n\\n\\\n  -n             do not output the trailing newline\\n\\\n\"), stdout);\n      fputs (_(DEFAULT_ECHO_TO_XPG\n               ? N_(\"\\\n  -e             enable interpretation of backslash escapes (default)\\n\\\n  -E             disable interpretation of backslash escapes\\n\")\n               : N_(\"\\\n  -e             enable interpretation of backslash escapes\\n\\\n  -E             disable interpretation of backslash escapes (default)\\n\")),\n             stdout);\n      fputs (HELP_OPTION_DESCRIPTION, stdout);\n      fputs (VERSION_OPTION_DESCRIPTION, stdout);\n      fputs (_(\"\\\n\\n\\\nIf -e is in effect, the following sequences are recognized:\\n\\\n\\n\\\n\"), stdout);\n      fputs (_(\"\\\n  \\\\\\\\      backslash\\n\\\n  \\\\a      alert (BEL)\\n\\\n  \\\\b      backspace\\n\\\n  \\\\c      produce no further output\\n\\\n  \\\\e      escape\\n\\\n  \\\\f      form feed\\n\\\n  \\\\n      new line\\n\\\n  \\\\r      carriage return\\n\\\n  \\\\t      horizontal tab\\n\\\n  \\\\v      vertical tab\\n\\\n\"), stdout);\n      fputs (_(\"\\\n  \\\\0NNN   byte with octal value NNN (1 to 3 digits)\\n\\\n  \\\\xHH    byte with hexadecimal value HH (1 to 2 digits)\\n\\\n\"), stdout);\n      printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);\n      emit_ancillary_info ();\n    }\n  exit (status);\n}\n \n\/* Convert C from hexadecimal character to integer.  *\/\nstatic int\nhextobin (unsigned char c)\n{\n  switch (c)\n    {\n    default: return c - '0';\n    case 'a': case 'A': return 10;\n    case 'b': case 'B': return 11;\n    case 'c': case 'C': return 12;\n    case 'd': case 'D': return 13;\n    case 'e': case 'E': return 14;\n    case 'f': case 'F': return 15;\n    }\n}\n \n\/* Print the words in LIST to standard output.  If the first word is\n   `-n', then don't print a trailing newline.  We also support the\n   echo syntax from Version 9 unix systems. *\/\n \nint\nmain (int argc, char **argv)\n{\n  bool display_return = true;\n  bool allow_options =\n    (! getenv (\"POSIXLY_CORRECT\")\n     || (! DEFAULT_ECHO_TO_XPG && 1 < argc && STREQ (argv[1], \"-n\")));\n \n  \/* System V machines already have a \/bin\/sh with a v9 behavior.\n     Use the identical behavior for these machines so that the\n     existing system shell scripts won't barf.  *\/\n  bool do_v9 = DEFAULT_ECHO_TO_XPG;\n \n  initialize_main (&argc, &argv);\n  set_program_name (argv[0]);\n  setlocale (LC_ALL, \"\");\n  bindtextdomain (PACKAGE, LOCALEDIR);\n  textdomain (PACKAGE);\n \n  atexit (close_stdout);\n \n  \/* We directly parse options, rather than use parse_long_options, in\n     order to avoid accepting abbreviations.  *\/\n  if (allow_options && argc == 2)\n    {\n      if (STREQ (argv[1], \"--help\"))\n        usage (EXIT_SUCCESS);\n \n      if (STREQ (argv[1], \"--version\"))\n        {\n          version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,\n                       (char *) NULL);\n          exit (EXIT_SUCCESS);\n        }\n    }\n \n  --argc;\n  ++argv;\n \n  if (allow_options)\n    while (argc > 0 && *argv[0] == '-')\n      {\n        char const *temp = argv[0] + 1;\n        size_t i;\n \n        \/* If it appears that we are handling options, then make sure that\n           all of the options specified are actually valid.  Otherwise, the\n           string should just be echoed.  *\/\n \n        for (i = 0; temp[i]; i++)\n          switch (temp[i])\n            {\n            case 'e': case 'E': case 'n':\n              break;\n            default:\n              goto just_echo;\n            }\n \n        if (i == 0)\n          goto just_echo;\n \n        \/* All of the options in TEMP are valid options to ECHO.\n           Handle them. *\/\n        while (*temp)\n          switch (*temp++)\n            {\n            case 'e':\n              do_v9 = true;\n              break;\n \n            case 'E':\n              do_v9 = false;\n              break;\n \n            case 'n':\n              display_return = false;\n              break;\n            }\n \n        argc--;\n        argv++;\n      }\n \njust_echo:\n \n  if (do_v9)\n    {\n      while (argc > 0)\n        {\n          char const *s = argv[0];\n          unsigned char c;\n \n          while ((c = *s++))\n            {\n              if (c == '\\\\' && *s)\n                {\n                  switch (c = *s++)\n                    {\n                    case 'a': c = '\\a'; break;\n                    case 'b': c = '\\b'; break;\n                    case 'c': exit (EXIT_SUCCESS);\n                    case 'e': c = '\\x1B'; break;\n                    case 'f': c = '\\f'; break;\n                    case 'n': c = '\\n'; break;\n                    case 'r': c = '\\r'; break;\n                    case 't': c = '\\t'; break;\n                    case 'v': c = '\\v'; break;\n                    case 'x':\n                      {\n                        unsigned char ch = *s;\n                        if (! isxdigit (ch))\n                          goto not_an_escape;\n                        s++;\n                        c = hextobin (ch);\n                        ch = *s;\n                        if (isxdigit (ch))\n                          {\n                            s++;\n                            c = c * 16 + hextobin (ch);\n                          }\n                      }\n                      break;\n                    case '0':\n                      c = 0;\n                      if (! ('0' <= *s && *s <= '7'))\n                        break;\n                      c = *s++;\n                      \/* Fall through.  *\/\n                    case '1': case '2': case '3':\n                    case '4': case '5': case '6': case '7':\n                      c -= '0';\n                      if ('0' <= *s && *s <= '7')\n                        c = c * 8 + (*s++ - '0');\n                      if ('0' <= *s && *s <= '7')\n                        c = c * 8 + (*s++ - '0');\n                      break;\n                    case '\\\\': break;\n \n                    not_an_escape:\n                    default:  putchar ('\\\\'); break;\n                    }\n                }\n              putchar (c);\n            }\n          argc--;\n          argv++;\n          if (argc > 0)\n            putchar (' ');\n        }\n    }\n  else\n    {\n      while (argc > 0)\n        {\n          fputs (argv[0], stdout);\n          argc--;\n          argv++;\n          if (argc > 0)\n            putchar (' ');\n        }\n    }\n \n  if (display_return)\n    putchar ('\\n');\n  exit (EXIT_SUCCESS);\n}\n<\/code><\/pre>\n<p>GNU\u306e\u5834\u5408\u3001coreutils\u306b\u542b\u307e\u308c\u3066\u3044\u308b\u305d\u3046\u3067\u3059\u304c\u3001\u3053\u306e\u5a01\u5727\u611f\u306f\u3059\u3054\u3044\u3002\u3055\u3059\u304cGNU\u3002\u7d20\u4eba\u306b\u306f\u7406\u89e3\u4e0d\u80fd\u3002<\/p>\n<h3>\u307e\u3068\u3081<\/h3>\n<p>\u8a73\u7d30\u306a\u89e3\u8aac\u306f<a href=\"http:\/\/cpplover.blogspot.jp\/2013\/04\/unixechoc.html\">\u672c\u306e\u866b<\/a>\u3055\u3093\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044(\u305f\u3081\u306b\u306a\u308a\u307e\u3059)\u3002\u4e00\u756a\u6c17\u306b\u5165\u3063\u305f\u306e\u306f\u3053\u308c\u3060\u3051\u306e\u529b\u4f5c\u306a\u306e\u306b\u3001shell\u306e\u30d3\u30eb\u30c8\u30a4\u30f3\u30b3\u30de\u30f3\u30c9\u3060\u304b\u3089\u4f7f\u308f\u308c\u306a\u3044\u3068\u3044\u3046\u3068\u3053\u308d\u3067\u3059\u306d\u3002<\/p>\n<pre>\nwhich echo\necho: shell built-in command\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>UNIX\u306e\u5404\u7a2e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u304a\u3051\u308b\u3001ehco.c\u306e\u6bd4\u8f03\u304c\u304a\u3082\u3057\u308d\u3044\u3067\u3059(UNIX V5, OpenBSD, Plan 9, FreeBSD, and GNU coreutils implementations of ech [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":9219,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[75],"tags":[759,3302],"class_list":["post-9220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-echo","tag-unix"],"_links":{"self":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/posts\/9220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/comments?post=9220"}],"version-history":[{"count":0,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/posts\/9220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/media\/9219"}],"wp:attachment":[{"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/media?parent=9220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/categories?post=9220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softantenna.com\/blog\/wp-json\/wp\/v2\/tags?post=9220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}