{"id":2157,"date":"2015-07-01T11:30:29","date_gmt":"2015-07-01T11:30:29","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2157"},"modified":"2016-10-07T16:30:12","modified_gmt":"2016-10-07T16:30:12","slug":"get-free-disk-space","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/get-free-disk-space","title":{"rendered":"Get free disk space"},"content":{"rendered":"<h4>\nGet free disk space<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tint64_t free_disk_space;\r\n\tget_int64_value_from_ascii_string(do_console_command_get_result((char*)&quot;df -k \/tmp | tail -1 | awk &#39;{print $4}&#39;&quot;), 0, &amp;free_disk_space);\t\t\/\/Get free space in kB\r\n\r\n\tstd::cout &lt;&lt; &quot;Free disk space: &quot; &lt;&lt; free_disk_space &lt;&lt; &quot;kB&quot; &lt;&lt; std::endl;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<pre>\r\n<code>\r\nint get_int64_value_from_ascii_string (string source_string, int char_index, int64_t *result)\r\n{\r\n\tint32_t value = -1;\r\n\tint32_t value_last = 0;\r\n\tchar *p_source_string;\r\n\t\r\n\t\r\n\tp_source_string = (char*)source_string.c_str();\r\n\tp_source_string += char_index;\r\n\r\n\t\/\/Ignore any leading spaces\r\n\twhile (*p_source_string == &#39; &#39;)\r\n\t\tp_source_string++;\r\n\r\n\twhile ((*p_source_string &gt;= &#39;0&#39;) &amp;&amp; (*p_source_string &lt;= &#39;9&#39;))\r\n\t{\r\n\t\tif (value &lt; 0)\r\n\t\t\tvalue = 0;\r\n\t\tvalue_last = value;\r\n\r\n\t\tvalue *= 10;\r\n\t\tvalue += (*p_source_string++ - 0x30);\r\n\r\n\t\tif (value_last &gt; value)\r\n\t\t{\r\n\t\t\tvalue = -1;\t\t\t\t\/\/Value is &gt; max possible\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\t*result = value;\r\n\treturn((int)(p_source_string - (char*)source_string.c_str()));\r\n}\r\n\r\nstring do_console_command_get_result (char* command)\r\n{\r\n\tFILE* pipe = popen(command, &quot;r&quot;);\r\n\tif (!pipe)\r\n\t\treturn &quot;ERROR&quot;;\r\n\t\r\n\tchar buffer[128];\r\n\tstring result = &quot;&quot;;\r\n\twhile(!feof(pipe))\r\n\t{\r\n\t\tif(fgets(buffer, 128, pipe) != NULL)\r\n\t\t\tresult += buffer;\r\n\t}\r\n\tpclose(pipe);\r\n\treturn(result);\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get free disk space int64_t free_disk_space; get_int64_value_from_ascii_string(do_console_command_get_result((char*)&quot;df -k \/tmp | tail -1 | awk &#39;{print $4}&#39;&quot;), 0, &amp;free_disk_space); \/\/Get free space in kB std::cout &lt;&lt; &quot;Free disk space: &quot; &lt;&lt; free_disk_space &lt;&lt; &quot;kB&quot; &lt;&lt; std::endl; &nbsp; int get_int64_value_from_ascii_string (string source_string, int char_index, int64_t *result) { int32_t value = -1; int32_t value_last = 0; char *p_source_string; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2157","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2157"}],"version-history":[{"count":2,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2157\/revisions"}],"predecessor-version":[{"id":2159,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2157\/revisions\/2159"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}