C language to obtain the Linux local directory and native information of the small program instance _c language

Source: Internet
Author: User
Tags lstat strcmp

Small program that shows the directory
shows the applet for the specified directory:

#include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys
  /stat.h> #include <stdlib.h> void Printdir (char *dir,int depth) {dir *dp;
  struct Dirent *entry;
 
  struct stat statbuf;
    if (DP = Opendir (dir)) = = NULL) {fprintf (stderr, "Cannot Open Directory:%s\n", dir);
  Return
  } chdir (dir);
    while ((entry = Readdir (DP))!= NULL) {lstat (ENTRY-&GT;D_NAME,&AMP;STATBUF); if (S_isdir (Statbuf.st_mode)) {/*found a directory,but ignore.
      */if (strcmp (".", entry->d_name) = = 0 | | strcmp ("..", entry->d_name) = = 0) {continue;
      printf ("%*s%s/\", Depth, "" ", Entry->d_name);
    /*recurse at a new indent level*/printdir (entry->d_name,depth+4);
    }else{printf ("%*s%s \", Depth, "", entry->d_name);
  int main () {/* Show directory */printf ("Directory scan of/home:\n");
  Printdir ("/home", 0); printf ("Done.")
   
  \ n "); Exit0);
 }

According to the parameters output directory structure

#include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys
  /stat.h> #include <stdlib.h> void Printdir (char *dir,int depth) {dir *dp;
  struct Dirent *entry;
 
  struct stat statbuf;
    if (DP = Opendir (dir)) = = NULL) {fprintf (stderr, "Cannot Open Directory:%s\n", dir);
  Return
  } chdir (dir);
    while ((entry = Readdir (DP))!= NULL) {lstat (ENTRY-&GT;D_NAME,&AMP;STATBUF); if (S_isdir (Statbuf.st_mode)) {/*found a directory,but ignore.
      */if (strcmp (".", entry->d_name) = = 0 | | strcmp ("..", entry->d_name) = = 0) {continue;
      printf ("%*s%s/\", Depth, "" ", Entry->d_name);
    /*recurse at a new indent level*/printdir (entry->d_name,depth+4);
    }else{printf ("%*s%s \", Depth, "", entry->d_name);
  int main (int argc, char* argv[]) {/* show directory/char *topdir = ".";
  if (argc >= 2) {topdir = argv[1]; } prinTF ("Directory scan of%s:\n", Topdir);
  Printdir (topdir,0); printf ("Done.")
   
  \ n ");
Exit (0);

 }

Get host basic information
Get host User information:

#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
 
int main () {
  uid_t uid;
  gid_t GID;
 
  struct passwd *pw;
  UID = Getuid ();
  GID = Getgid ();
 
  printf ("User is%s\n", GetLogin ());
 
  printf ("User ids:uid=%d, gid=%d \ n", uid, GID);
 
  PW = Getpwuid (UID);
  printf ("UID passwd entry: \ n name=%s, uid=%d, gid=%d, home=%s, shell=%s\n", Pw->pw_name, Pw->pw_uid, Pw->pw_gid, Pw->pw_dir, Pw->pw_shell);
 
  PW = Getpwnam ("root");
  printf ("Root passwd entry: \ n");
  printf ("name=%s, uid=%d, gid=%d, home=%s, shell=%s \ n", Pw->pw_name, Pw->pw_uid, Pw->pw_gid, Pw->pw_dir, pw- >pw_shell);
  Exit (0);
}

To obtain the host's own information:

#include <sys/utsname.h>
#include <unistd.h>
#include <stdio.h>
 
 
int main () {
  Char COMPUTER[256];
  struct utsname UTS;
  if (gethostname (computer, 255)!= 0 | | uname (&uts) < 0) {
    fprintf (stderr, "could not get host information \ n"); 
   exit (1);
  }
 
  printf ("Computer host name is%s \ n", Computer);
  printf ("System is%s on%s hardware \ n", Uts.sysname, uts.machine);
  printf ("NodeName is%s \ n", uts.nodename);
  printf ("Version is%s,%s \ n", Uts.release, uts.version);
 
  Exit (0);
}

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.