Posted on 2006-06-19 17:37
郭大伟 阅读(233)
评论(0) 编辑 收藏 引用 所属分类:
Linux
1
#include
<
stdio.h
>
2
#include
<
unistd.h
>
3
#include
<
pwd.h
>
4
#include
<
sys
/
types.h
>
5
#include
<
unistd.h
>
6
#include
<
fcntl.h
>
7
#include
<
sys
/
stat.h
>
8
#include
<
errno.h
>
9
#include
<
string
.h
>
10
#include
<
sys
/
time.h
>
11
#include
<
signal.h
>
12
13
#define
PROMPT "时间已经过去了两秒钟\n\a"
14
#define
COMPANY "北京市东城区金宝街金宝大厦602\n\a"
15
char
*
prompt
=
PROMPT;
16
char
*
company
=
COMPANY;
17
unsigned
int
len;
18
19
#define
BUFFER_SIZE 1024
20
void
my_print (
char
*
string
)
21
{
22
printf (
"
The string is %s\n
"
,
string
);
23
}
24
void
my_print2 (
char
*
string
)
25
{
26
char
*
string2;
27
int
size, i,size2;
28
size
=
strlen (
string
);
29
size2
=
size
-
1
;
30
string2
=
(
char
*
) malloc (size
+
1
);
31
for
(i
=
0
; i
<
size; i
++
)
32
string2[size2
-
i]
=
string
[i];
33
string2[size]
=
'
\0
'
;
34
printf (
"
The string printed backward is %s\n
"
, string2);
35
}
36
37
void
prompt_info(
int
signo)
38
{
39
write(STDERR_FILENO,prompt,len);
40
}
41
void
init_sigaction(
void
)
42
{
43
struct
sigaction act;
44
act.sa_handler
=
prompt_info;
45
act.sa_flags
=
0
;
46
sigemptyset(
&
act.sa_mask);
47
sigaction(SIGPROF,
&
act,NULL);
48
}
49
void
init_time()
50
{
51
struct
itimerval value;
52
value.it_value.tv_sec
=
2
;
53
value.it_value.tv_usec
=
0
;
54
value.it_interval
=
value.it_value;
55
setitimer(ITIMER_PROF,
&
value,NULL);
56
}
57
58
int
main (
int
argc,
char
**
argv)
59
{
60
char
my_string[]
=
"
hello there
"
;
61
printf(
"
Garland 哈尔滨工业大学\n
"
);
62
printf(
"
黑龙江省哈尔滨市南岗区西大直街副90号\n
"
);
63
my_print (my_string);
64
my_print2 (my_string);
65
66
67
pid_t my_pid,parent_pid;
68
uid_t my_uid,my_euid;
69
gid_t my_gid,my_egid;
70
struct
passwd
*
my_info;
71
my_pid
=
getpid();
72
parent_pid
=
getppid();
73
my_uid
=
getuid();
74
my_euid
=
geteuid();
75
my_gid
=
getgid();
76
my_egid
=
getegid();
77
my_info
=
getpwuid(my_uid);
78
printf(
"
Process ID:%ld\n
"
,my_pid);
79
printf(
"
Parent ID:%ld\n
"
,parent_pid);
80
printf(
"
User ID:%ld\n
"
,my_uid);
81
printf(
"
Effective User ID:%ld\n
"
,my_euid);
82
printf(
"
Group ID:%ld\n
"
,my_gid);
83
printf(
"
Effective Group ID:%ld\n
"
,my_egid);
84
if
(my_info)
85
{
86
printf(
"
My Login Name:%s\n
"
,my_info
->
pw_name);
87
printf(
"
My Password :%s\n
"
,my_info
->
pw_passwd);
88
printf(
"
My User ID :%ld\n
"
,my_info
->
pw_uid);
89
printf(
"
My Group ID :%ld\n
"
,my_info
->
pw_gid);
90
printf(
"
My Real Name:%s\n
"
,my_info
->
pw_gecos);
91
printf(
"
My Home Dir :%s\n
"
, my_info
->
pw_dir);
92
printf(
"
My Work Shell:%s\n
"
, my_info
->
pw_shell);
93
}
94
95
96
int
from_fd,to_fd;
97
int
bytes_read,bytes_write;
98
char
buffer[BUFFER_SIZE];
99
char
*
ptr;
100
if
(argc
!=
3
)
101
{
102
fprintf(stderr,
"
Usage:%s fromfile tofile\n\a
"
,argv[
0
]);
103
exit(
1
);
104
}
105
/**/
/*
打开源文件
*/
106
if
((from_fd
=
open(argv[
1
],O_RDONLY))
==-
1
)
107
{
108
fprintf(stderr,
"
Open %s Error:%s\n
"
,argv[
1
],strerror(errno));
109
exit(
1
);
110
}
111
/**/
/*
创建目的文件
*/
112
if
((to_fd
=
open(argv[
2
],O_WRONLY
|
O_CREAT,S_IRUSR
|
S_IWUSR))
==-
1
)
113
{
114
fprintf(stderr,
"
Open %s Error:%s\n
"
,argv[
2
],strerror(errno));
115
exit(
1
);
116
}
117
/**/
/*
以下代码是一个经典的拷贝文件的代码
*/
118
while
(bytes_read
=
read(from_fd,buffer,BUFFER_SIZE))
119
{
120
/**/
/*
一个致命的错误发生了
*/
121
if
((bytes_read
==-
1
)
&&
(errno
!=
EINTR))
122
break
;
123
else
if
(bytes_read
>
0
)
124
{
125
ptr
=
buffer;
126
while
(bytes_write
=
write(to_fd,ptr,bytes_read))
127
{
128
/**/
/*
一个致命错误发生了
*/
129
if
((bytes_write
==-
1
)
&&
(errno
!=
EINTR))
break
;
130
/**/
/*
写完了所有读的字节
*/
131
else
if
(bytes_write
==
bytes_read)
break
;
132
/**/
/*
只写了一部分,继续写
*/
133
else
if
(bytes_write
>
0
)
134
{
135
ptr
+=
bytes_write;
136
bytes_read
-=
bytes_write;
137
}
138
}
139
/**/
/*
写的时候发生的致命错误
*/
140
if
(bytes_write
==-
1
)
break
;
141
}
142
}
143
close(from_fd);
144
close(to_fd);
145
146
int
nlen
=
strlen(company);
147
write(STDERR_FILENO,company,nlen);
148
149
len
=
strlen(prompt);
150
init_sigaction();
151
init_time();
152
while
(
1
);
153
exit(
0
);