.


:




:

































 

 

 

 


. .

1.

2.

3.

4.

5.

6.


, :
[_] [-a |-b] [-o ],
-a -b .
-a - , ;
-b - .
, -o.


 

.

, , , , . open().

#include <fcntl.h.>

#include<sys/types.h

#include<sys/stat.h>

int open(char *filename, int flags, [mode_t mode]);

char *filename , ( ).

int flags .

[mode_t mode] . . POSIX , mode_t mode, :

S_IRWXU , ;

S_IRUSR ;

S_IWUSR ;

S_IXUSR ;

S_IRWXG , ;

.

sys/stat.h. .

open() , .

, (||). open(), fcntl.h.

open(), fcntl.h:

O_RDONLY ;

O_WRONLY ;

O_RDWR ;

O_CREAT , ;

O_TRUNC , . ;

O_APPEND . , . , lseek();

O_EXCL , ( O_CREATE)

O_NONBLOCK , . , . .

, . . , , O_RDONLY O_WRONLY, .

open() , -1, , errno .

.

(environment) =. . , :

$ env

env. Linux- , :

USER ;

HOME ;

PATH , ;

SHELL ;

PWD .

:

$ echo $VARIABLE

VARIABLE . , $VARIABLE .

:

1. environ, =. unistd.h.

2. getenv(), . stdlib.h

getenv() .

:

#include <stdlib.h>

char *getenv(const char *name);

name , , HOME PATH. , getenv() . , NULL.

environ , argv . . POSIX, environ .

extern char **environ;

argv, environ NULL. , , argc.

. UNIX , , .

, main():

int main(int argc, char *argv[], char *envp[])

envp , environ.

, :

int setenv(const char *name, const char *value, int overwrite); //

int putenv(char *string); // , .

void unsetenv(const char *name); // .

int clearenv(void); // .

.

getopt() . getopt() , optstring.

, extern, .

#include<unistd.h>

int getopt(int argc, char *const argv[], const char *optstring);

extern char *optarg;

extern int optind, opterr, optopt;

- argc ( ) argv ( ) , main();

- optstring ;

- optind (c 1) argv[].

(argc), (argv) (optstring), getopt() . . , , -1, .

, getopt(), :

- optarg , ( ).

- optind argv, getopt().

- optopt , ( ) .

optstring .

getopt() , -1; - , .

getopt() , , -1. , . getopt() optstring ( ). optstring , . optarg optind. , optstring , . , opstring, , opstring : , ? .

optopt . , ( getopt , . opterr ). opterr . getopt() , , optopt "?". , opterr.

opterr ( ), getopt() . opterr , getopt() "?" ":"

 

, optopt .

, optind , argv . , , optind.

, getopt() argv[optind] (, ), -1 , .

.

close() : , . . close() unistd.h :

#include <unistd.h>

int close (int fd);

0, -1. , . errno.

.

write() read() POSIX:

#include<unistd.h>

#include<sys/types.h>

ssize_t write(int fd, const void *buf, size_t size);

write(), , fd, size buf. , , , .

, . -1 errno. write() 0, , .

. .

, , ( ) , . , . . \0, , , . , ,

 

 

, .

sprintf().

sprintf() , printf, .

:

#include <stdio.h>

int sprintf(char *string, const char *format, apr1, apr2, )

sprintf() apr1, apr2 .., format, (stdout), string ( ). , string .

strlen().

char, . , . \0. . 10 , 11 .

:

#include <string.h>

int strlen(char *string);

strlen(). strlen() , , \0. strlen() <string.h>.

strlen() . sizeof , "" -, . , , .

getenv() , , name. = .

getenv () NULL, . Sysinfo - .

Uname - .

, .

 

, b . ,

: , .

 


 

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

 

int main(int argc, char*argv[])

{

int t;

int c;

int aflag=0;

int bflag=0;

int errflag=0;

int flag='?';

char*ofile=NULL;

opterr=0;

if (argc!=4)

{ printf(":%s:[-a|-b][-o ]\n",argv[0]);

return 1;

}

 

while((c=getopt(argc,argv,":abo:"))!=-1)

{

switch(c)

{

case 'a':aflag++;

 

flag=c;

if(bflag)

{

printf(" a b \n");

errflag++;

}

break;

case 'b':bflag++;

 

flag=c;

if(aflag)

{

printf(" a b \n");

errflag++;

}

break;

 

case 'o':ofile=optarg;

break;

 

case':':printf(" -%c\n",optopt);

errflag++;

break;

}

 

}

if(errflag)

{(void)printf(":%s:[-a|-b][-o ]\n",argv[0]);

return(1);

}

 

printf(" :%c\n",flag);

printf(" o: %s\n",ofile);

printf(" : ");

 

char pr;

printf(" ?");

printf(" [m] / [g] \n");

scanf("%c", &pr);

if (pr=='m')

{

 

for (;optind<argc;optind++)

{

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

}

printf("\n");

 

{

int outfile;

char buf[100];

 

switch(flag)

{

case 'a':

printf("\n");

 

system("echo 'Imya directorii v kotoroy okazivaetsya koristuva4 posle zapuska v sistemu: ';cd; pwd");

sprintf (buf,"pwd > %s",argv[optind-1]);

system(buf);

 

break;

case 'b':

system("echo 'Imya terminala: '; tty");

 

sprintf(buf,"tty > %s",argv[optind-1]);

system(buf);

break;

}

}

}

else

 

{

 

for (;optind<argc;optind++)

{

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

}

printf("\n");

 

char * var;

char buf[100];

 

switch(flag)

{

case 'a':

var=getenv ("HOME");

printf(" :%s\n",var);

sprintf(buf," :%s\n",var);

 

break;

case 'b':

var=getenv ("TERM");

printf(" :%s\n",var);

sprintf(buf," :%s\n",var);

break;

}

 

char t;

 

int outfile;

 

outfile=open(argv[optind-1],O_RDWR,0644);

if (outfile!=-1)

{

printf("\n ! \n ( [c]? \n ? [r]");

scanf("%c",&t);

scanf("%c",&t);

if (t=='y')

lseek(outfile,0,SEEK_END);

}

else if (outfile==-1)

outfile=open(argv[optind-1],O_RDWR|O_CREAT,0644);

write(outfile,buf,100);

 

{

 

printf("\n");

close(outfile);

}

}

return (0);

}

 


 

 

 

 


 

, Linux.

uname, sysinfo getenv system.

system string Linux.

 



<== | ==>
. . |
:


: 2016-11-19; !; : 428 |


:

:

, .
==> ...

1561 - | 1415 -


© 2015-2024 lektsii.org - -

: 0.145 .