A mix of Rust and C language

Source: Internet
Author: User
Tags sprintf

There is a memory leak

extern Crate libc;use libc::size_t;use libc::{file,c_char};use std:: String;#[repr (C)]pub struct mntent {mnt_fsname:* mut c_char,/* Name of mounted file system */Mnt_dir:* Mut C_char,/* mount point * /Mnt_type:* Mut c_char,/* File system Type: UFS, NFS etc */mnt_opts:* Mut c_char,/* option, comma delimiter */mnt_freq:size_t ,/* The frequency of the Dump (in days) */mnt_passno:size_t,/* fsck Check order */}#[link (name = "Diskstat", kind= "static")]ext    Ern "C" {fn getdisk (), *const C_char;    PUB fn setmntent (filename: *const c_char, Ttype: *const C_char), FILE;    PUB fn getmntent (filep:file), mntent; PUB fn endmntent (filep:file), size_t;}    fn Main () {let mut mnt = unsafe {(Setmntent ("/etc/mtab". To_c_str (). As_ptr (), "R". To_c_str (). As_ptr ())};    Let Mut disk_info = unsafe {string::raw::from_buf (Getdisk () as *const U8)}; println! ("{}", disk_info);} <!--lang:cpp--> 

//Gcc-c Diskstat.c-o diskstat.o-fpic
//ar-r libdiskstat.a DISKSTAT.O

#include <stdio.h> #include <mntent.h> #include <string.h> #include <sys/vfs.h> static const u  nsigned Long Long G = 1024*1024*1024ull;  Static const unsigned long long M = 1024*1024;  Static const unsigned long long K = 1024;  static Char str[20];      char* Kscale (unsigned long b, unsigned long BS) {unsigned long long size = b * (unsigned long long) BS;          if (Size > G) {sprintf (str, "%0.2f GB", size/(g*1.0));      return str;          } else if (Size > M) {sprintf (str, "%0.2f MB", size/(1.0*m));      return str;          } else if (Size > k) {sprintf (str, "%0.2f K", size/(1.0*k));      return str;          } else {sprintf (str, "%0.2f B", size*1.0);      return str;      }}char* Getdisk () {file* mount_table;      struct Mntent *mount_entry;      struct STATFS s;      unsigned long blocks_used;      unsigned blocks_percent_used; const CHAR *DISP_UNITS_HDR = NULL;      mount_table = NULL;      mount_table = Setmntent ("/etc/mtab", "R");          if (!mount_table) {fprintf (stderr, "Set Mount Entry error\n");      return-1;      } DISP_UNITS_HDR = "Size";      printf ("Filesystem%-15sused Available%s mounted on\n",//Disp_units_hdr, "use%");    Char *disk_info = (char *) malloc (10000*sizeof (char)); strcpy (Disk_info, "[");    A//*disk_info = "[";    Char *z = (char *) malloc (1000*sizeof (char));          while (1) {const char *device;          const char *mount_point;        const char *fs_type;              if (mount_table) {mount_entry = Getmntent (mount_table);                  if (!mount_entry) {endmntent (mount_table);              Break          }} else continue;          device = mount_entry->mnt_fsname;          Mount_point = mount_entry->mnt_dir;        Fs_type = mount_entry->mnt_type; //fprintf (stderr, "Error Mount info:device=%s mountpoint=%s\n", device, Mount_point);                  if (Statfs (Mount_point, &s)! = 0) {fprintf (stderr, "Statfs failed!\n");          Continue              } if ((S.f_blocks > 0) | | |!mount_table) {blocks_used = S.f_blocks-s.f_bfree;              blocks_percent_used = 0;                          if (blocks_used + s.f_bavail) {blocks_percent_used = (blocks_used * 100ULL              + (blocks_used + s.f_bavail)/2)/(blocks_used + s.f_bavail);              } if (strcmp (device, "rootfs") = = 0) continue;              if (printf ("Hello dksks \n%-20s" + 1, Device) >)//printf ("\n%-20s", "");              Char s1[20];              Char s2[20];              Char s3[20];              strcpy (S1, Kscale (S.f_blocks, s.f_bsize)); strcpy (S2, Kscale (s.f_blocks-S.f_bfree, s.f_bsize));              strcpy (S3, Kscale (S.f_bavail, s.f_bsize)); sprintf (z, "{\" device\ ": \"%s\ ", \" fs_type\ ": \"%s\ ", \" mount_point\ ": \"%s\ ", \" size\ ": \"%s\ ", \" used\ ":%s,\"                    Available\ ": \"%s\ ", \" use_percent\ ": \"%3u%%\ "},", Device, Fs_type,             Mount_point, S1, S2, S3, blocks_percent_used             );        Strncat (disk_info,z, 1000);    }} strncat (Disk_info, "]", 1); return disk_info;}

One-time Rust and C-language mashup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.