{"id":87,"date":"2011-06-17T20:04:05","date_gmt":"2011-06-17T20:04:05","guid":{"rendered":"http:\/\/62.253.218.153\/~rpmsoft\/45rpmdump\/?p=87"},"modified":"2011-06-17T20:04:05","modified_gmt":"2011-06-17T20:04:05","slug":"snippet-of-code-1-gathering-disk-information-in-c","status":"publish","type":"post","link":"https:\/\/www.45rpmsoftware.com\/blog\/?p=87","title":{"rendered":"Snippet of code #1 &#8211; Gathering Disk Information in C"},"content":{"rendered":"<p>One of the people I work with (an excellent fellow, I might add) was grumbling about how he could gather information about the mounted disks on a computer. \u00a0He was mucking around with &#8216;df&#8217; in the command line, and giving serious thought to the business of parsing the result into his program.<\/p>\n<p><!--more--><\/p>\n<p>I was concerned that this might be rather slow &#8211; especially since his program was written in C, and he&#8217;d already demonstrated his mettle with a marvellously efficient ring buffer. So this isn&#8217;t mere incompetence here &#8211; it&#8217;s just unfamiliarity with the language and all the wonderful modules that are available. \u00a0I offered him the following snippet of code and, since others might encounter the same problem, I offer it here as well.<\/p>\n<p>Firstly, you&#8217;ll need to include the necessary module:<\/p>\n<pre>#include &lt;sys\/statvfs.h&gt;<\/pre>\n<p style=\"font-family: 'Courier New';\"><span style=\"color: #444444; font-family: Georgia, 'Bitstream Charter', serif; line-height: 24px; white-space: normal; font-size: 16px;\">Then you&#8217;ll need to define the structure to store the disk information:<\/span><\/p>\n<pre>typedef struct _diskInfo\r\n{\r\n   unsigned long size;\r\n   unsigned long used;\r\n   unsigned long free;\r\n   unsigned long blockSize;\r\n   unsigned long blocks;\r\n} diskInfo;<\/pre>\n<p>And finally, you&#8217;ll need the function to gather this wonderful information for you:<\/p>\n<pre>diskInfo diskUsage(char * path,unsigned int function)\r\n{\r\n   struct statvfs buf;\r\n   diskInfo disk;\r\n   unsigned long freeblks;\r\n   int ret;\r\n\r\n   ret = statvfs(path,&amp;buf);\r\n   disk.blockSize = buf.f_frsize;\r\n   disk.blocks = buf.f_blocks;\r\n   freeblks = buf.f_bfree;\r\n   disk.size = disk.blockSize * disk.blocks;\r\n   disk.free = disk.blockSize * freeblks;\r\n   disk.used = disk.size - disk.free;\r\n\r\n   return disk;\r\n}<\/pre>\n<p>I&#8217;ll keep publishing snippets until I loose interest. \u00a0So this might be part one of an occasional series, or it might be the only one I ever write.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the people I work with (an excellent fellow, I might add) was grumbling about how he could gather information about the mounted disks on a computer. \u00a0He was mucking around with &#8216;df&#8217; in the command line, and giving serious thought to the business of parsing the result into his program.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":0,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}