{"id":4801,"date":"2021-06-12T00:01:15","date_gmt":"2021-06-12T07:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4801"},"modified":"2021-06-26T06:42:45","modified_gmt":"2021-06-26T13:42:45","slug":"reading-wildcards-from-the-command-line","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4801","title":{"rendered":"Reading Wildcards from the Command Line"},"content":{"rendered":"<p>Back in May, I wondered how command line input could be processed when a wildcard is present (<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4775\">Lesson link<\/a>). My research lead me to the <em>glob()<\/em> function, but you don&#8217;t use this function to process a command line wildcard argument. The reason is that these wildcards are handled by the shell; your code has no direct way to determine when a wildcard is present as a command line argument.<br \/>\n<!--more--><br \/>\nMy <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4782\">glob Lesson<\/a> helped present the globbing concept. It&#8217;s useful, but when it comes to processing a wildcard in a command line argument, things work differently &mdash; though the principles of glob remain in effect.<\/p>\n<p>When your code attempts to read a wildcard argument, only the first matching file is returned. Why is that? And why isn&#8217;t the wildcard returned directly?<\/p>\n<p>The answer deals with how the shell processes wildcard command line arguments: They are expanded from a single argument into multiple arguments, one for each matching filename.<\/p>\n<p>For example, if you have 14 C source code files in a directory, and you have a command line with a single <code>*.c<\/code> wildcard as its argument, the operating system creates 15 command line arguments: the program name plus the 14 matching C source code files. The value of <code>argc<\/code> in the <em>main()<\/em> function reflects this condition:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_06_12-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_06_12-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\r\nint main(int argc, char *argv[])\r\n{\r\n    int x;\r\n\r\n    for( x=0; x&lt;argc; x++ )\r\n        printf(\"%d = %s\\n\",x,argv[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the above code, each command line argument is output. A <em>for<\/em> loop at Line 8 outputs each argument and its <code>argv[]<\/code> element number, starting with zero. Here is the output when only the program name is present:<\/p>\n<p><code>$ .\/a.out<br \/>\n0 = .\/a.out<\/code><\/p>\n<p>And here is the program&#8217;s output with a wildcard argument:<\/p>\n<p><code>$ .\/a.out *.c<br \/>\n0 = .\/a.out<br \/>\n1 = 0424a.c<br \/>\n2 = 0424b.c<br \/>\n3 = 0529.c<\/code><\/p>\n<p>Though only two arguments are specified, the operating system generates four <code>argv[]<\/code> elements: the program filename and the three matching C source code files.<\/p>\n<p>It&#8217;s important to remember that if your code requires several command line arguments, you must properly parse them and not rely upon their discrete positions. I&#8217;ll cover a technique for processing command line switches in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4823\">a future Lesson<\/a>.<\/p>\n<p>For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4813\">next week&#8217;s Lesson<\/a>, I present a function similar to <em>glob()<\/em>, which also uses wildcards.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t detect when a wildcard is present as a command line argument, but your code can properly process such an argument. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4801\">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-4801","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\/4801","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=4801"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4801\/revisions"}],"predecessor-version":[{"id":4850,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4801\/revisions\/4850"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}