{"id":4385,"date":"2020-10-03T00:01:41","date_gmt":"2020-10-03T07:01:41","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4385"},"modified":"2020-10-03T08:24:15","modified_gmt":"2020-10-03T15:24:15","slug":"the-terminal-has-a-name","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4385","title":{"rendered":"The Terminal Has a Name"},"content":{"rendered":"<p>In Linux and Unix, the terminal is assigned a name. Specifically, it&#8217;s the name of a file located in the <code>\/dev<\/code> directory. This configuration is necessary because the operating system treats all devices as files. Like a file, you can read and write from the terminal; it&#8217;s an I\/O device. To get started, you must know the current terminal&#8217;s filename.<br \/>\n<!--more--><br \/>\nThe <em>ttyname()<\/em> function obtains the name of a terminal. Here&#8217;s the <em>man<\/em> page format:<\/p>\n<p><code>char *ttyname(int fd);<\/code><\/p>\n<p>Argument <code>fd<\/code> is a file descriptor &#8211; not a <em>FILE<\/em> handle, but the number assigned to an open file. Read my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1952\">blog post here<\/a> for details on file descriptors.<\/p>\n<p>The value returned from <em>ttyname()<\/em> is a string representing terminal&#8217;s pathname.<\/p>\n<p>This function requires you to include the <code>unistd.h<\/code> header file. While this header file is available to Windows C compilers, the <em>ttyname()<\/em> function is available only in Unix and Linux.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10_03-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10_03-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    char *term;\r\n\r\n    term = ttyname(0);\r\n    printf(\"This is terminal %s\\n\",term);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>ttyname()<\/em> function is called with argument 0. This value represents the file descriptor for the standard input device, <code>stdin<\/code>, which is the current terminal. Here&#8217;s sample output:<\/p>\n<p><code>This is terminal \/dev\/tty1<\/code><\/p>\n<p>The terminal name you see may be different, depending on the OS and other weird things.<\/p>\n<p>Once you know the terminal&#8217;s filename, you can open it for I\/O just like any other file. You can use the formatted file functions, as demonstrated in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10_03-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10_03-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    char *term;\r\n    FILE *tout;\r\n\r\n    <span class=\"comments\">\/* obtain terminal name and open *\/<\/span>\r\n    term = ttyname(0);\r\n    tout = fopen(term,\"w\");\r\n    if( tout==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to open %s\\n\",term);\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* send text to the terminal *\/<\/span>\r\n    fprintf(tout,\"Hello, terminal %s\\n\",term);\r\n\r\n    <span class=\"comments\">\/* close *\/<\/span>\r\n    fclose(tout);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The terminal filename is obtained at Line 11: <code>term = ttyname(0);<\/code> The next statement attempts to open this file for writing (output) by using the <em>fopen()<\/em> function: <code>tout = fopen(term,\"w\");<\/code> The <code>tout<\/code> file handle is then tested to ensure that the file was opened.<\/p>\n<p>At Line 20, text is output to the terminal. The file is closed at Line 23.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Hello, terminal \/dev\/tty1<\/code><\/p>\n<p>Of course, you could always just write to standard output; the output goes to the current terminal by default. Yet in this sample code, the terminal&#8217;s device\/filename was obtained, opened for output, then the output is sent via the terminal&#8217;s file\/device instead. It&#8217;s a roundabout way of doing things, but it drives home the point that all devices in Unix are files and can be treated as such.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Unix, the terminal is an I\/O device but also a file. When you know it&#8217;s name, you can write text to the terminal just as you can output text to a file. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4385\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4385","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4385"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4385\/revisions"}],"predecessor-version":[{"id":4401,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4385\/revisions\/4401"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}