Information Security System Design Foundation Tenth Week study summary

Source: Internet
Author: User

Cp1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFERSIZE 4096
#define Copymode 0644

void oops (char *, char *);

int main (int argc, char *argv[])
{
int in_fd, OUT_FD, N_chars;
Char Buf[buffersize];
if (argc! = 3) {
fprintf (stderr, "Usage:%s source destination\n", *argv);
Exit (1);
}

if ((in_fd = open (argv[1], o_rdonly)) = =-1)
Oops ("Cannot open", argv[1]);

if ((out_fd = creat (argv[2], copymode)) = = =-1)
Oops ("Cannot creat", argv[2]);

while ((N_chars = Read (IN_FD, buf, buffersize)) > 0)
if (Write (OUT_FD, buf, n_chars)! = n_chars)
Oops ("Write error to", Argv[2]);
if (N_chars = =-1)
Oops ("Read error from", Argv[1]);


if (Close (in_fd) = =-1 | | close (OUT_FD) = =-1)
Oops ("Error Closing Files", "");
}

void oops (char *s1, char *s2)
{
fprintf (stderr, "Error:%s", S1);
Perror (S2);
Exit (1);
}

Ls1

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

void Do_ls (char []);

int main (int argc, char *argv[])
{
if (argc = = 1)
Do_ls (".");
Else
while (--ARGC) {
printf ("%s:\n", *++ARGV);
Do_ls (*ARGV);
}

return 0;

}

void Do_ls (char dirname[])
{
Dir*dir_ptr;
struct DIRENT*DIRENTP;

if ((Dir_ptr = Opendir (dirname)) = = NULL)
fprintf (stderr, "Ls1:cannot open%s\n", dirname);
Else
{
while ((DIRENTP = Readdir (dir_ptr)) = NULL)
printf ("%s\n", direntp->d_name);
Closedir (DIR_PTR);
}
}

Ls2

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>

void Do_ls (char[]);
void Dostat (char *);
void Show_file_info (char *, struct stat *);
void Mode_to_letters (int, char []);
Char *uid_to_name (uid_t);
Char *gid_to_name (gid_t);

int main (int argc, char *argv[])
{
if (argc = = 1)
Do_ls (".");
Else
while (--ARGC) {
printf ("%s:\n", *++ARGV);
Do_ls (*ARGV);
}

return 0;
}

void Do_ls (char dirname[])
{
Dir*dir_ptr;
struct DIRENT*DIRENTP;

if ((Dir_ptr = Opendir (dirname)) = = NULL)
fprintf (stderr, "Ls1:cannot open%s\n", dirname);
Else
{
while ((DIRENTP = Readdir (dir_ptr)) = NULL)
Dostat (Direntp->d_name);
Closedir (DIR_PTR);
}
}

void Dostat (char *filename)
{
struct STAT info;

if (stat (filename, &info) = =-1)
perror (filename);
Else
Show_file_info (filename, &info);
}

void Show_file_info (char *filename, struct stat *info_p)
{
Char*uid_to_name (), *ctime (), *gid_to_name (), *filemode ();
Voidmode_to_letters ();
Char modestr[11];

Mode_to_letters (Info_p->st_mode, MODESTR);

printf ("%s", MODESTR);
printf ("%4d", (int) info_p->st_nlink);
printf ("%-8s", Uid_to_name (Info_p->st_uid));
printf ("%-8s", Gid_to_name (Info_p->st_gid));
printf ("%8ld", (long) info_p->st_size);
printf ("%.12s", 4+ctime (&info_p->st_mtime));
printf ("%s\n", filename);

}

void Mode_to_letters (int mode, char str[])
{
strcpy (str, "----------");

if (S_isdir (mode)) str[0] = ' d ';
if (S_ISCHR (mode)) str[0] = ' C ';
if (s_isblk (mode)) str[0] = ' B ';

if (Mode & S_IRUSR) str[1] = ' R ';
if (Mode & S_IWUSR) str[2] = ' W ';
if (Mode & S_IXUSR) str[3] = ' x ';

if (Mode & S_IRGRP) str[4] = ' R ';
if (Mode & S_IWGRP) str[5] = ' W ';
if (Mode & S_IXGRP) str[6] = ' x ';

if (Mode & S_iroth) str[7] = ' R ';
if (Mode & S_iwoth) str[8] = ' W ';
if (Mode & S_ixoth) str[9] = ' x ';
}

#include <pwd.h>

Char *uid_to_name (uid_t uid)
{
STRUCTPASSWD *getpwuid (), *pw_ptr;
static Char numstr[10];

if ((pw_ptr = Getpwuid (uid)) = = = NULL) {
sprintf (Numstr, "%d", UID);
return numstr;
}
Else
return pw_ptr->pw_name;
}

#include <grp.h>

Char *gid_to_name (gid_t GID)
{
struct group *getgrgid (), *grp_ptr;
static Char numstr[10];

if ((grp_ptr = Getgrgid (gid)) = = NULL) {
sprintf (Numstr, "%d", GID);
return numstr;
}
Else
Return grp_ptr->gr_name;
}

Who1

#include <stdio.h>
#include <stdlib.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>

#defineSHOWHOST

int show_info (struct utmp *UTBUFP)
{
printf ("%-8.8s", utbufp->ut_name);
printf ("");
printf ("%-8.8s", utbufp->ut_line);
printf ("");
printf ("%10ld", utbufp->ut_time);
printf ("");
#ifdefSHOWHOST
printf ("(%s)", utbufp->ut_host);
#endif
printf ("\ n");

return 0;
}
int main ()
{
struct UTMP Current_record;
INTUTMPFD;
Intreclen = sizeof (Current_record);

if ((UTMPFD = open (Utmp_file, o_rdonly)) = =-1) {
Perror (Utmp_file);
Exit (1);
}
while (read (UTMPFD, &current_record, reclen) = = Reclen)
Show_info (&current_record);
Close (UTMPFD);
return 0;
}

Who2

#include <stdio.h>
#include <stdlib.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>

#defineSHOWHOST

int show_info (struct utmp *UTBUFP)
{
printf ("%-8.8s", utbufp->ut_name);
printf ("");
printf ("%-8.8s", utbufp->ut_line);
printf ("");
printf ("%10ld", utbufp->ut_time);
printf ("");
#ifdefSHOWHOST
printf ("(%s)", utbufp->ut_host);
#endif
printf ("\ n");

return 0;
}
int main ()
{
struct UTMP Current_record;
INTUTMPFD;
Intreclen = sizeof (Current_record);

if ((UTMPFD = open (Utmp_file, o_rdonly)) = =-1) {
Perror (Utmp_file);
Exit (1);
}
while (read (UTMPFD, &current_record, reclen) = = Reclen)
Show_info (&current_record);
Close (UTMPFD);
return 0;
}

Echostate

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

int main ()
{
struct Termios info;
int RV;

RV = tcgetattr (0, &info); /* Read values from driver */

if (rv = =-1) {
Perror ("tcgetattr");
Exit (1);
}
if (Info.c_lflag & ECHO)
printf ("Echo is on, since it bit is 1\n");
Else
printf ("Echo is OFF, since it bit is 0\n");

return 0;
}

Setecho

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

#define OOPS (s,x) {perror (s); exit (x);}

int main (int argc, char *argv[])
{
struct Termios info;

if (argc = = 1)
Exit (0);

if (tcgetattr (0,&info) = =-1)/* Get attribs */
Oops ("Tcgettattr", 1);

if (argv[1][0] = = ' Y ')
Info.c_lflag |= ECHO; /* Turn on bit */
Else
Info.c_lflag &= ~echo; /* Turn off bit */

if (tcsetattr (0,tcsanow,&info) = =-1)/* Set Attribs */
Oops ("Tcsetattr", 2);

return 0;
}

FileInfo

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

void Show_stat_info (char *, struct stat *);

int main (int argc, char *argv[])
{
struct STAT info;

if (argc>1)
{

if (stat (argv[1], &info)! =-1) {
Show_stat_info (argv[1], &info);
return 0;
}
Else
Perror (argv[1]);
}
return 1;
}
void Show_stat_info (char *fname, struct stat *buf)
{
printf ("Mode:%o\n", Buf->st_mode);
printf ("Links:%d\n", Buf->st_nlink);
printf ("User:%d\n", buf->st_uid);
printf ("Group:%d\n", Buf->st_gid);
printf ("Size:%d\n", (int) buf->st_size);
printf ("Modtime:%d\n", (int) buf->st_mtime);
printf ("Name:%s\n", fname);
}

FileSize

#include <stdio.h>
#include <sys/stat.h>

int main ()
{
struct stat infobuf;

if (Stat ("/etc/passwd", &infobuf) = =-1)
Perror ("/etc/passwd");
Else
printf ("The size of/etc/passwd is%d\n", infobuf.st_size);
}

Spwd

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

Ino_tget_inode (char *);
void Printpathto (ino_t);
void Inum_to_name (ino_t, char *, int);

int main ()
{
Printpathto (Get_inode ("."));
Putchar (' \ n ');
return 0;
}

void Printpathto (ino_t this_inode)
{
Ino_tmy_inode;
Charits_name[bufsiz];

if (Get_inode (".")! = This_inode)
{
ChDir ("..");

Inum_to_name (This_inode,its_name,bufsiz);

My_inode = Get_inode (".");
Printpathto (My_inode);
printf ("/%s", its_name);

}
}

void Inum_to_name (ino_t inode_to_find, char *namebuf, int buflen)
{
Dir*dir_ptr;
struct DIRENT*DIRENTP;

Dir_ptr = Opendir (".");
if (dir_ptr = = NULL) {
Perror (".");
Exit (1);
}


while ((DIRENTP = Readdir (dir_ptr)) = NULL)
if (Direntp->d_ino = = Inode_to_find)
{
strncpy (Namebuf, Direntp->d_name, Buflen);
Namebuf[buflen-1] = ' + ';
Closedir (DIR_PTR);
Return
}
fprintf (stderr, "error looking for Inum%d\n", (int) inode_to_find);
Exit (1);
}

ino_t Get_inode (char *fname)
{
struct STAT info;

if (stat (fname, &info) = =-1) {
fprintf (stderr, "cannot stat");
Perror (fname);
Exit (1);
}
return Info.st_ino;
}

Testioctl

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main ()
{
struct winsize size;
if (Isatty (stdout_fileno) = = 0)
Exit (1);
if (IOCTL (Stdout_fileno, Tiocgwinsz, &size) < 0) {
Perror ("IOCTL Tiocgwinsz error");
Exit (1);
}

printf ("%d rows%d columns\n", Size.ws_row, Size.ws_col);
return 0;
}

Information Security System Design Foundation Tenth Week study summary

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.