Linux Programming P1 البرمجة تحت نظام اللينكس الجزء الاول

download Linux Programming P1  البرمجة تحت نظام اللينكس الجزء الاول

of 13

Transcript of Linux Programming P1 البرمجة تحت نظام اللينكس الجزء الاول

  • 8/14/2019 Linux Programming P1

    1/13

    ..

    programming-fr34ks net ; linux arabic programming tutorialauthor : St0rM-MaNlicense : Rippers License , Gnu/Gpl ; based on : advanced linux programming

    OMG!!! Linux Programmin

    Cool Ain't :DVisit Us ( Http://Programming-Fr34ks.Net ) ;

    friends :http://securitygurus.net http://arab4services.com

    http://linuxac.orghttp://linux-fr34k.comhttp://sechost-it.com /*security rockers */

    .

    http://programming-fr34ks.net/http://securitygurus.net/http://arab4services.com/http://linuxac.org/http://linux-fr34k.com/http://sechost-it.com/http://programming-fr34ks.net/http://programming-fr34ks.net/http://securitygurus.net/http://securitygurus.net/http://securitygurus.net/http://arab4services.com/http://linuxac.org/http://linux-fr34k.com/http://sechost-it.com/http://sechost-it.com/http://sechost-it.com/http://programming-fr34ks.net/
  • 8/14/2019 Linux Programming P1

    2/13

    : Programming-Fr34ks.net :

    :

    1 linux 1

    2 2 root directory

    directories directories

    /home/St0rM/Desktop/File c dir homedir St0rM dir Desktop

    3 linux dir 3 Linux sockets

    socket dirs d Linux 4

    r = readw = writex = execute

    type users{r/w/x} , groups{ r/w/x} , others{ r/w/x}dirs d

    hard link ) )

    4 4 dirs sockets pipe fifo

    5 5 read , write bytes

    binary bytes streams bytes

    6 6 255

    XX xx )

    ( linux dir

    this/is/me txt me txt this/me

    http://linuxac org/forum/showthread php?t=1586

    http://linuxac.org/forum/showthread.php?t=1586http://linuxac.org/forum/showthread.php?t=1586
  • 8/14/2019 Linux Programming P1

    3/13

    .

    7 7

    8 8

    memory addresses

    files descriptors ( )

    stander portable C standard files manipulating functions

    files descriptor

    file descriptor ) )

    C standard files manipulating functions

    datac structure FILE

    ...

    9

    10

    #include

    int main void{

    FILE* file_to_open;

    structure file (stdio typedef data structure

    #include

    FILE *fopen const char * restrict path , const char * restrict mode ;

    FILE structure

  • 8/14/2019 Linux Programming P1

    4/13

    mode

    r , w , a

    FILE structure (

    ( r+ , w+ , a+

    .

    .

    NULL perror .

    Int fclose FILE *file ;

    #include#include#include

    int main void{

    FILE* file_to_open;

    file_to_open = fopen "textfile" , "r+" ;

    if file_to_open == NULL

    { perror "fopen " ;_exit 1 ;

    }fclose file_to_open ;

    return 0;}

    .

    fgets , fputs , fprintf , fscanf , fputc , fgetc ;

    char *fgets char * restrict str , int size , FILE * restrict stream ;

    str size stream

  • 8/14/2019 Linux Programming P1

    5/13

    stream ) enter )str

    .

    stdin streams.

    /etc/services

    #include#include#include

    int main void{

    FILE* file_to_open;char buffer[81]; /*enough to hold one line*/

    file_to_open = fopen "/etc/services" , "r" ; /*open for read and write if exist*/

    if file_to_open == NULL{

    perror "fopen " ;_exit 1 ;

    }

    fgets buffer , 250 , file_to_open ;printf "File /etc/services first line is\n"

    "%s",buffer ;fclose file_to_open ;

    return 0;}

    #include

    int fputs const char *str , FILE *stream ;

    str stream

    #include

    #include

    #include

  • 8/14/2019 Linux Programming P1

    6/13

    int main void

    {

    FILE* file_to_open;

    char buffer[81]; /*enough to hold one line*/

    file_to_open = fopen "test" , "w" ; /*open for read and write if exist*/

    strcpy buffer , "Hello Files\n" ; /*copy our string */

    if file_to_open == NULL

    {

    perror "fopen " ;

    _exit 1 ;

    }

    fputs buffer , file_to_open ; /*write it */

    printf "File test file has line \n"

    "%s",buffer ;

    fclose file_to_open ;

    return 0;

    }

    file_to_read ;rewind

    void

    rewind FILE *stream ;

    long

    ftell FILE *stream ;

  • 8/14/2019 Linux Programming P1

    7/13

    int

    fseek FILE *stream , long offset , int whence ;

    rewind rewind fseek ) (bytes

    #include

    #include

    #include

    #include

    int main void

    {

    FILE* file_to_open;

    char buffer[81]; /*enough to hold one line*/

    char buf[81]; /*enough to read one line*/

    unsigned long int pos;

    file_to_open = fopen "test txt" , "w+" ; /*open for read and write if exist*/

    strcpy buffer , "Hello Files\n" ; /*copy our string */

    if file_to_open == NULL

    {

    perror "fopen " ;

    _exit 1 ;

    }

    fputs buffer , file_to_open ; /*write it */

    rewind file_to_open ;

    pos = ftell file_to_open ;

    printf "Current position %lu\n",pos ;

    fgets buf , 81 , file_to_open ;

    printf "File test has %s\n",buf ;

  • 8/14/2019 Linux Programming P1

    8/13

  • 8/14/2019 Linux Programming P1

    9/13

    P_SHLOCK , O_EXLOCK =

    O_RDONLY =

    O_WRONLY =

    O_APPEND =

    O_RDWR =

    modes permissions access users

    S_IRWXU ,S_IRUSR , S_IWUSR , S_IXUSR users ; = read/write , read , write , execute

    S_IRWXG , S_IRGRP , S_IWGRP , S_IXGRP groups ; = read/write , read , write , execute

    S_IROTH , S_IROTH , S_IWOTH , S_IXOTH others ; = read/write , read , write , execute

    12 modes flags ?

    bitwise| oring flags modes

    (users (

    int fd;

    fd = open "test txt" , O_CREAT O_EXCL , I_RWUSR ;

    int fd;

    fd = open "test txt" , O_CREAT O_TRUCN , I_RWUSR I_RWGRP , I_RWOTH ;

    #include

    int close ( int d );

    file descriptor .

    .

    file descriptor hard link ! unlink )(link !

  • 8/14/2019 Linux Programming P1

    10/13

    file descriptors hard links.

    open 1 file descriptor . perror .

    13

    read , write.

    #include

    #include

    ssize _ tread( int d , void *buf , size _ t nbytes );

    file descriptor d

    buf nbytes d

    nbytes

    d

    #include

    #include

    #include

    #include

    int main(void)

    {

    int fd;

    char buf[80];

    size_t bytes_read;

    fd = open("test.txt" , O_RDONLY);

    bytes_read = read(fd , buf , sizeof(buf));

  • 8/14/2019 Linux Programming P1

    11/13

    if(bytes_read < 0)

    {

    perror("read()");

    _exit(1);

    }

    buf[bytes_read] = '\x0';

    printf("%s with length %d\n",buf , (int)bytes_read);

    close(fd);

    }

    read bytes

    0 perror

    terminating character

    read

    14

    .write

    ssize _ t

    write ( int d , const void *buf , size _ t nbytes );

    fd .buf

    Hello World

    #include

    #include

    #include

  • 8/14/2019 Linux Programming P1

    12/13

    #include

    #include

    int main(void)

    {

    int fd;

    char buf[80];

    size_t bytes_write;

    fd = open("test.txt" , O_WRONLY);

    strcpy(buf , "Hello World");

    bytes_write = write(fd , buf , strlen(buf));

    if(bytes_write < 0)

    {

    perror("read()");

    _exit(1);

    }

    printf("%s with length %d has been written\n",buf , (int)bytes_write);

    close(fd);

    }

    15

    #include

    off _ t lseek ( int fildes , off _ t offset , int whence );

    whenceoffest bytes 10

  • 8/14/2019 Linux Programming P1

    13/13

    lseek(fd , 10 , SEEK_CUR);

    10 bytes

    lseek(fd , 10 , SEEK_SET);

    man pages

    file .ftell

    .

    bytes

    .

    .

    dirs.