Simple application of standard IO, dynamic static library, reading system time and printing, simulating ls-l function

Source: Internet
Author: User
Tags lstat

2015.2.27
Friday, light rain

Replication capabilities for standard IO implementations:

#include <stdio.h>
#include <errno.h>

#define N 64

int main (int argc, char *argv[])
{
int n;
Char Buf[n];
FILE *fps, *FPD;

if (ARGC < 3)
{
printf ("Usage:%s <src_file><dst_file>\n", argv[0]);
return-1;
}

if (fps = fopen (argv[1], "r") = = = NULL)
{
fprintf (stderr, "fail to fopen%s:%s\n", Argv[1],strerror (errno));
return-1;
}

if (FPD = fopen (Argv[2], "w") = = = NULL)
{
fprintf (stderr, "fail to fopen%s:%s\n", Argv[2],strerror (errno));
Fclose (fps);
return-1;
}

while ((n = fread (buf, 1, N, fps)) > 0)
{
Fwrite (buf, 1, N, FPD);
}

Fclose (fps);
Fclose (FPD);
return 0;

}

Code called by Fgets ()/fputs ()/get ()/PUTC ():

int main (void)
{
int C;
while ((c = getc (stdin))! = EOF)
{
if (PUTC (c, stdout) = = EOF)
{
Err_sys ("Output error");
}
}
if (Ferror (stdin))
{
Err_sys ("input error");
}
Exit (0);
}

Compare top and bottom two programs:

int main (void)
{
Char Buf[maxline];
while ((c = fgets (buf, MAXLINE, stdin)) = NULL)
{
if (fputs (buf, stdout) = = EOF)
{
Err_sys ("Output error");
}
}
if (Ferror (stdin))
{
Err_sys ("input error");
}
Exit (0);
}
Hard Links: Ln can be deleted, deleting links will only reduce the number of links, size indicates the size of the content inside the link
Soft Link: Also known as symbolic Link: ln-s, delete the linked object will not be able to access the previous content, the size of the link object symbol itself size, not inside the same size

[Email protected]:~/file/link$ ls-l
Total 0
-rw-r--r--1 LG LG 0 2015-02-27 14:13 Hello
[Email protected]:~/file/link$ echo 123 > Hello
[Email protected]:~/file/link$ cat Hello
123
[Email protected]:~/file/link$ ls-l
Total 4
-rw-r--r--1 LG LG 4 2015-02-27 14:14 Hello
[Email protected]:~/file/link$ ln Hello hardlink
[Email protected]:~/file/link$ ls-l
Total 8
-rw-r--r--2 LG LG 4 2015-02-27 14:14 hardlink number of links became 2
-rw-r--r--2 LG LG 4 2015-02-27 14:14 Hello
[Email protected]:~/file/link$ ln-s Hello slink
[Email protected]:~/file/link$ ls-l
Total 8
-rw-r--r--2 LG LG 4 2015-02-27 14:14 Hardlink
-rw-r--r--2 LG LG 4 2015-02-27 14:14 Hello
lrwxrwxrwx 1 LG LG 5 2015-02-27 14:15 slink, hello//Here 5 means the number of characters slink, soft link does not increase the number of links
[Email protected]:~/file/link$ cat hardlink//Show hard-link content
123
[Email protected]:~/file/link$ cat slink//Show Soft link content
123
[Email protected]:~/file/link$ rm Hello//Delete the original link and see the soft and hard link effect again
[Email protected]:~/file/link$ cat hardlink//Hard link can be displayed because the contents of the disk are not deleted, only the number of links becomes 0 to delete the disk contents
123
[Email protected]:~/file/link$ cat slink//Soft link cannot display the previous content, the disk contents are deleted
Cat:slink:No such file or directory
[Email protected]:~/file/link$

The difference between a static library and a dynamic library is that the code is loaded in a different time:

The static library is linked to the target code when the program is compiled, and the static library is no longer needed when the program is run, so it is large in size.
Dynamic libraries are not linked to the target code when the program is compiled, but are loaded when the program is run, so the program runs with the presence of a dynamic library and the code is small.

Static libraries:
Gcc-c xx.c generating multiple XX.O files
AR-CRSV libpr.a pr1.o pr2.o Static Library package complete
GCC Main.c-lpr-l. (The last-L. is the find library under the current path) link just created the static library, others do not have source files, only the compiled library

Generation of shared libraries:
MAIN.C Makefile pr1.c pr2.c
Gcc-c pr1.c Pr2.c-fpic (created in a location-independent compiler)
Gcc-o libpr.so-shared pr1.o PR2.O//Shared library creation complete
GCC main.c-lpr-l.

The system time is read and written to the file in 1 second intervals:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

#define N 64

int main (int argc, char *argv[])
{
FILE *FP;
time_t T;

if (ARGC < 2)
{
printf ("Usage:%s <file>\n", argv[0]);
return-1;
}

if (fp = fopen (Argv[1], "w") = = = NULL)
{
Perror ("fail to open");
return-1;
}

while (1)
{
Time (&t);
fprintf (FP, "%s\n", CTime (&t));
Fwrite (CTime (&t), 1,24,FP); Achieve the same effect with fwrite
fprintf (FP, "\ n");
Fflush (FP); Need to strengthen the system refresh
Sleep (1);
}

Fclose (FP);

return 0;
}

Program Run Effect:
[Email protected]:/mnt/hgfs/source test/file io$ gcc mytime.c
[Email Protected]:/mnt/hgfs/source test/file io$./a.out time
^c
[Email protected]:/mnt/hgfs/source test/file io$ cat Time
Fri Feb 27 20:09:36 2015

Fri Feb 27 20:09:37 2015

Fri Feb 27 20:09:38 2015

[Email Protected]:/mnt/hgfs/source test/file io$

Features of the analog ls-l:


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

#define Maxlen_symblink 64
#define TEST_TYPE 2

int main (int argc, char *argv[])
{
struct stat buf;
Char Out[maxlen_symblink + 1];
struct TM *t;
ssize_t N;

if (argc! = 2)
{
fprintf (stderr, "Usage:%s <pathname>\n", argv[0]);
return 0;
}

if (Lstat (ARGV[1],&BUF) < 0)
{
Perror ("Lstat error");
return-1;
}

Switch (Buf.st_mode & s_ifmt)
{
Case S_ifreg:
default:printf ("-");
Case s_ifdir:printf ("D"); Break
Case s_ifchr:printf ("C"); Break
Case s_ifblk:printf ("B"); Break
Case s_ififo:printf ("P"); Break
Case s_iflnk:printf ("L"); Break
Case s_ifsock:printf ("S"); Break

}

if (S_IRUSR & Buf.st_mode) printf ("R");
else printf ("-");

if (S_IWUSR & Buf.st_mode) printf ("w");
else printf ("-");

if (S_IXUSR & Buf.st_mode) printf ("X");
else printf ("-");

if (S_irgrp & Buf.st_mode) printf ("R");
else printf ("-");

if (S_iwgrp & Buf.st_mode) printf ("w");
else printf ("-");

if (S_ixgrp & Buf.st_mode) printf ("X");
else printf ("-");

if (S_iroth & Buf.st_mode) printf ("R");
else printf ("-");

if (S_iwoth & Buf.st_mode) printf ("w");
else printf ("-");

if (S_ixoth & Buf.st_mode) printf ("X");
else printf ("-");

printf ("%d", buf.st_nlink);

struct passwd *pw;
PW = Getpwuid (BUF.ST_UID);
printf ("%s", pw->pw_name);

struct group *gr;
GR = Getgrgid (Buf.st_gid);
printf ("%s", gr->gr_name);

printf ("%4ld", buf.st_size);

t = localtime (&buf.st_mtime);
printf ("%04d-%02d-%02d%02d:%02d",
T->tm_year + 1900,
T->tm_mon + 1,
T->tm_mday,
T->tm_hour,
T->tm_min);

printf ("%s", argv[1]);

if (S_islnk (Buf.st_mode))
{
printf (",");
if ( -1 = = (n = readlink (argv[1], out, maxlen_symblink)))
{
Perror ("Readlink error");
}
Else
{
Out[n] = 0x00;
printf ("%s", out);
}
}

printf ("\ n");

return 0;
}

Program Run Effect:
[Email protected]:/mnt/hgfs/source test/file io$ ls-l time
-rwxrwxrwx 1 root root 2015-02-27 20:09 time
[Email Protected]:/mnt/hgfs/source test/file io$./a.out time
-rwxrwxrwx 1 root root 2015-02-27 20:09 time
[Email Protected]:/mnt/hgfs/source test/file io$

*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************

Simple application of standard IO, dynamic static library, reading system time and printing, simulating ls-l function

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.