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->D_NAME,&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->D_NAME,&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);
}