jekyll-theme-gaeblogx 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +9 -0
- data/_includes/algolia.html +75 -0
- data/_includes/backToTop.html +5 -0
- data/_includes/category.html +19 -0
- data/_includes/comments.html +46 -0
- data/_includes/footer.html +33 -0
- data/_includes/head.html +58 -0
- data/_includes/header.html +34 -0
- data/_includes/previousAndNext.html +13 -0
- data/_includes/sidebar-search.html +25 -0
- data/_includes/tag.html +16 -0
- data/_layouts/default.html +26 -0
- data/_layouts/demo.html +10 -0
- data/_layouts/page.html +31 -0
- data/_layouts/post.html +107 -0
- data/_sass/_backToTop.scss +49 -0
- data/_sass/_demo.scss +65 -0
- data/_sass/_footer.scss +63 -0
- data/_sass/_header.scss +174 -0
- data/_sass/_index.scss +174 -0
- data/_sass/_layout.scss +242 -0
- data/_sass/_page.scss +238 -0
- data/_sass/_post-old.scss +109 -0
- data/_sass/_post.scss +83 -0
- data/_sass/_reset.scss +119 -0
- data/_sass/_scrollbar.scss +35 -0
- data/_sass/_syntax-highlighting.scss +99 -0
- data/assets/10.jpg +0 -0
- data/assets/AWS-Introduction-ec2.png +0 -0
- data/assets/Cache-Oblivious-Algorithms-FunnelSort.jpg +0 -0
- data/assets/Condition-Variables-fig1.png +0 -0
- data/assets/Dynamic-Compilation-adaptJVM.pdf +0 -0
- data/assets/Dynamic-Compilation-dyncomp.pdf +0 -0
- data/assets/Fibonacci-Heap-Fig3.png +0 -0
- data/assets/Fibonacci-Heap-Fig5.png +0 -0
- data/assets/Fibonacci-Heap-Fig6.png +0 -0
- data/assets/Fibonacci-Heap-Fig7.png +0 -0
- data/assets/File-System-bc.jpg +0 -0
- data/assets/File-System-filetable.rich.jpg +0 -0
- data/assets/GIL-Battle.png +0 -0
- data/assets/GIL-CPU.png +0 -0
- data/assets/GIL-Check.png +0 -0
- data/assets/GIL-IO.png +0 -0
- data/assets/GIL-Measure-1.png +0 -0
- data/assets/GIL-Measure-2.png +0 -0
- data/assets/GIL-Signal.png +0 -0
- data/assets/GIL-Tick.png +0 -0
- data/assets/Garbage-Collection-Slides-gc.pdf +0 -0
- data/assets/Garbage-Collection-copying-1.png +0 -0
- data/assets/Garbage-Collection-copying-2.png +0 -0
- data/assets/Garbage-Collection-free-list.png +0 -0
- data/assets/Garbage-Collection-generation-1.png +0 -0
- data/assets/Garbage-Collection-mark-sweep.png +0 -0
- data/assets/Garbage-Collection-nursery-1.png +0 -0
- data/assets/Garbage-Collection-nursery-2.png +0 -0
- data/assets/Garbage-Collection-nursery-3.png +0 -0
- data/assets/Garbage-Collection-nursery-4.png +0 -0
- data/assets/Garbage-Collection-nursery-5.png +0 -0
- data/assets/Garbage-Collection-process.png +0 -0
- data/assets/Garbage-Collection-ref-counting.png +0 -0
- data/assets/Interpreter-Optimization-interp.pdf +0 -0
- data/assets/Memory-Management-fig1.png +0 -0
- data/assets/Memory-Management-fig2.png +0 -0
- data/assets/Memory-Management-fig3.png +0 -0
- data/assets/Memory-Management-fig4.png +0 -0
- data/assets/Memory-Management-fig5.png +0 -0
- data/assets/Memory-Management-fig6.png +0 -0
- data/assets/Memory-Management-fig7.png +0 -0
- data/assets/Memory-Management-fig8.png +0 -0
- data/assets/Multicore-GIL-1.png +0 -0
- data/assets/Multicore-GIL-2.png +0 -0
- data/assets/My-Photo.JPG +0 -0
- data/assets/Operating-System-Virtualization-ept.png +0 -0
- data/assets/Operating-System-Virtualization-guest-page.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-arch.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-process.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-qemu.png +0 -0
- data/assets/Operating-System-Virtualization-kvm-state.png +0 -0
- data/assets/Operating-System-Virtualization-vtx.png +0 -0
- data/assets/Operating-System-Virtualization-xen-arch.png +0 -0
- data/assets/Program-Profiling-profiling.pdf +0 -0
- data/assets/Programming-Language-Virtual-Machine-vm.pdf +0 -0
- data/assets/Remote-Shell-Session-Setup-1.png +0 -0
- data/assets/Signals-1.png +0 -0
- data/assets/Signals-2.png +0 -0
- data/assets/Signals-3.png +0 -0
- data/assets/Some-Slides-about-Containers-Kubernetes.pdf +0 -0
- data/assets/Some-Slides-about-Containers-UCSB-nurmi.pdf +0 -0
- data/assets/Some-Slides-about-PaaS-CS293B_CloudPlatforms.pdf +0 -0
- data/assets/avg-1thread.c +172 -0
- data/assets/avg-manythread.c +284 -0
- data/assets/avg-nothread.c +71 -0
- data/assets/file-create1.c +75 -0
- data/assets/file-create2.c +120 -0
- data/assets/file-fd1.c +56 -0
- data/assets/file-fd2.c +25 -0
- data/assets/file-fd3.c +23 -0
- data/assets/file-read1.c +62 -0
- data/assets/file-read2.c +66 -0
- data/assets/file-seek1.c +87 -0
- data/assets/fork-1.c +35 -0
- data/assets/fork-2.c +44 -0
- data/assets/fork-3.c +60 -0
- data/assets/fork-4.c +64 -0
- data/assets/joinall-1.c +163 -0
- data/assets/joinall-2.c +162 -0
- data/assets/market-kthreads.c +465 -0
- data/assets/market-semaphore.c +504 -0
- data/assets/market1.c +478 -0
- data/assets/market2.c +490 -0
- data/assets/market3.c +503 -0
- data/assets/market4.c +509 -0
- data/assets/my-cat.c +35 -0
- data/assets/pipe-1.c +45 -0
- data/assets/pipe-2.c +80 -0
- data/assets/pipe-3.c +83 -0
- data/assets/pipe-4.c +116 -0
- data/assets/race1.c +75 -0
- data/assets/race2.c +77 -0
- data/assets/race3.c +87 -0
- data/assets/race_ABC.c +144 -0
- data/assets/search-by-algolia.svg +1 -0
- data/assets/semaphore.book.ps +8827 -0
- data/assets/sys-call1.c +20 -0
- data/assets/sys-call2.c +16 -0
- data/assets//350/265/253/347/202/2161.PNG +0 -0
- data/feed.xml +30 -0
- data/index.html +164 -0
- data/js/jekyll-search.min.js +1 -0
- data/js/lunr.min.js +2977 -0
- data/js/main.js +57 -0
- data/js/masonry.pkgd.min.js +9 -0
- data/js/pageContent.js +166 -0
- data/js/search.js +17 -0
- data/js/smooth-scroll.min.js +2 -0
- data/js/waterfall.js +214 -0
- data/page/0archives.html +83 -0
- data/page/1category.html +62 -0
- data/page/2tags.html +64 -0
- data/page/3search.html +112 -0
- data/page/4about.md +34 -0
- metadata +270 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* program to find the average value of a set of random numbers
|
|
7
|
+
*
|
|
8
|
+
* usage: avg-nothread count
|
|
9
|
+
*
|
|
10
|
+
* where count is the number of random values to generate
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
char *Usage = "usage: avg-nothread count";
|
|
14
|
+
|
|
15
|
+
#define RAND() (drand48()) /* basic Linux random number generator */
|
|
16
|
+
|
|
17
|
+
int main(int argc, char **argv)
|
|
18
|
+
{
|
|
19
|
+
int i;
|
|
20
|
+
int n;
|
|
21
|
+
double *data;
|
|
22
|
+
double sum;
|
|
23
|
+
int count;
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
* santity check the arguments
|
|
27
|
+
*/
|
|
28
|
+
if(argc <= 1) {
|
|
29
|
+
fprintf(stderr,"must specify count\n");
|
|
30
|
+
fprintf(stderr,"%s",Usage);
|
|
31
|
+
exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
count = atoi(argv[1]); /* count is first argument */
|
|
35
|
+
|
|
36
|
+
if(count <= 0) {
|
|
37
|
+
fprintf(stderr,"invalid count %d\n",count);
|
|
38
|
+
exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
* make an array large enough to hold #count# doubles
|
|
43
|
+
*/
|
|
44
|
+
data = (double *)malloc(count * sizeof(double));
|
|
45
|
+
if(data == NULL) {
|
|
46
|
+
exit(1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/*
|
|
50
|
+
* pick a bunch of random numbers
|
|
51
|
+
*/
|
|
52
|
+
for(i=0; i < count; i++) {
|
|
53
|
+
data[i] = RAND();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* now scan through them and compute the sum
|
|
58
|
+
*/
|
|
59
|
+
sum = 0;
|
|
60
|
+
for(i=0; i < count; i++) {
|
|
61
|
+
sum += data[i];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
printf("the average over %d random numbers on (0,1) is %f\n",
|
|
65
|
+
count, sum/(double)count);
|
|
66
|
+
|
|
67
|
+
free(data);
|
|
68
|
+
|
|
69
|
+
return(0);
|
|
70
|
+
}
|
|
71
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* open to create a file and write a string into it
|
|
9
|
+
*/
|
|
10
|
+
int main(int argc, char **argv)
|
|
11
|
+
{
|
|
12
|
+
int my_file_desc;
|
|
13
|
+
char file_name[4096];
|
|
14
|
+
char write_buffer[4096];
|
|
15
|
+
int write_length;
|
|
16
|
+
char *string;
|
|
17
|
+
int i;
|
|
18
|
+
int w;
|
|
19
|
+
|
|
20
|
+
if(argc < 2) {
|
|
21
|
+
printf("need to specify a file name as the first argument\n");
|
|
22
|
+
exit(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
* zero out the buffer for the file name
|
|
27
|
+
*/
|
|
28
|
+
for(i=0; i < sizeof(file_name); i++) {
|
|
29
|
+
file_name[i] = 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* copy the argument into a local buffer
|
|
34
|
+
*/
|
|
35
|
+
strncpy(file_name,argv[1],sizeof(file_name));
|
|
36
|
+
file_name[sizeof(file_name)-1] = 0;
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* try and open the file for creation
|
|
40
|
+
*/
|
|
41
|
+
my_file_desc = open(file_name,O_CREAT | O_WRONLY,0600);
|
|
42
|
+
if(my_file_desc < 0) {
|
|
43
|
+
printf("failed to open %s for creation\n",file_name);
|
|
44
|
+
exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* file is open, write a string into it
|
|
49
|
+
*/
|
|
50
|
+
string =
|
|
51
|
+
"This program brought to you by the council for better C programming";
|
|
52
|
+
for(i=0; i < sizeof(write_buffer); i++) {
|
|
53
|
+
write_buffer[i] = 0;
|
|
54
|
+
}
|
|
55
|
+
strncpy(write_buffer,string,sizeof(write_buffer));
|
|
56
|
+
|
|
57
|
+
write_length = strlen(write_buffer);
|
|
58
|
+
|
|
59
|
+
w = write(my_file_desc,write_buffer,write_length);
|
|
60
|
+
|
|
61
|
+
if(w != write_length) {
|
|
62
|
+
printf("write didn't complete, w: %d, length: %d\n",
|
|
63
|
+
w,
|
|
64
|
+
write_length);
|
|
65
|
+
close(my_file_desc);
|
|
66
|
+
exit(1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
close(my_file_desc);
|
|
70
|
+
|
|
71
|
+
return(0);
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* open to create a couple of files and write a strings into each
|
|
9
|
+
*/
|
|
10
|
+
int main(int argc, char **argv)
|
|
11
|
+
{
|
|
12
|
+
int fd1;
|
|
13
|
+
int fd2;
|
|
14
|
+
char file_name1[4096];
|
|
15
|
+
char file_name2[4096];
|
|
16
|
+
char write_buffer[4096];
|
|
17
|
+
int write_length;
|
|
18
|
+
char *string;
|
|
19
|
+
int i;
|
|
20
|
+
int w;
|
|
21
|
+
|
|
22
|
+
if(argc < 3) {
|
|
23
|
+
printf("need to specify two file names as the first and second arguments\n");
|
|
24
|
+
exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* zero out the buffers for the file names
|
|
29
|
+
*/
|
|
30
|
+
for(i=0; i < sizeof(file_name1); i++) {
|
|
31
|
+
file_name1[i] = 0;
|
|
32
|
+
file_name2[i] = 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* copy the first argument into a local buffer
|
|
37
|
+
*/
|
|
38
|
+
strncpy(file_name1,argv[1],sizeof(file_name1));
|
|
39
|
+
file_name1[sizeof(file_name1)-1] = 0;
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
* and the second
|
|
43
|
+
*/
|
|
44
|
+
strncpy(file_name2,argv[2],sizeof(file_name2));
|
|
45
|
+
file_name2[sizeof(file_name2)-1] = 0;
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* try and open the first file for creation
|
|
49
|
+
*/
|
|
50
|
+
fd1 = open(file_name1,O_CREAT | O_WRONLY, 0600);
|
|
51
|
+
if(fd1 < 0) {
|
|
52
|
+
printf("failed to open %s for creation\n",file_name1);
|
|
53
|
+
exit(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* and the second
|
|
58
|
+
*/
|
|
59
|
+
fd2 = open(file_name2,O_CREAT | O_WRONLY, 0600);
|
|
60
|
+
if(fd2 < 0) {
|
|
61
|
+
printf("failed to open %s for creation\n",file_name2);
|
|
62
|
+
close(fd1);
|
|
63
|
+
exit(1);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* both files are open, write a string into first
|
|
68
|
+
*/
|
|
69
|
+
string =
|
|
70
|
+
"This program brought to you by the council for better C programming";
|
|
71
|
+
for(i=0; i < sizeof(write_buffer); i++) {
|
|
72
|
+
write_buffer[i] = 0;
|
|
73
|
+
}
|
|
74
|
+
strncpy(write_buffer,string,sizeof(write_buffer));
|
|
75
|
+
|
|
76
|
+
write_length = strlen(write_buffer);
|
|
77
|
+
|
|
78
|
+
w = write(fd1,write_buffer,write_length);
|
|
79
|
+
|
|
80
|
+
if(w != write_length) {
|
|
81
|
+
printf("write didn't complete, w: %d, length: %d in %s\n",
|
|
82
|
+
w,
|
|
83
|
+
write_length,
|
|
84
|
+
file_name1);
|
|
85
|
+
close(fd1);
|
|
86
|
+
close(fd2);
|
|
87
|
+
exit(1);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
* and the second
|
|
92
|
+
*/
|
|
93
|
+
string = "C programming is both nutritious and great tasting.";
|
|
94
|
+
for(i=0; i < sizeof(write_buffer); i++) {
|
|
95
|
+
write_buffer[i] = 0;
|
|
96
|
+
}
|
|
97
|
+
strncpy(write_buffer,string,sizeof(write_buffer));
|
|
98
|
+
|
|
99
|
+
write_length = strlen(write_buffer);
|
|
100
|
+
|
|
101
|
+
w = write(fd2,write_buffer,write_length);
|
|
102
|
+
|
|
103
|
+
if(w != write_length) {
|
|
104
|
+
printf("write didn't complete, w: %d, length: %d in %s\n",
|
|
105
|
+
w,
|
|
106
|
+
write_length,
|
|
107
|
+
file_name2);
|
|
108
|
+
close(fd1);
|
|
109
|
+
close(fd2);
|
|
110
|
+
exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
close(fd1);
|
|
114
|
+
close(fd2);
|
|
115
|
+
|
|
116
|
+
return(0);
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
data/assets/file-fd1.c
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* print out some file descriptors
|
|
9
|
+
*/
|
|
10
|
+
int main(int argc, char **argv)
|
|
11
|
+
{
|
|
12
|
+
int my_file_desc;
|
|
13
|
+
char file_name[4096];
|
|
14
|
+
int i;
|
|
15
|
+
int r;
|
|
16
|
+
char *string;
|
|
17
|
+
|
|
18
|
+
if(argc < 2) {
|
|
19
|
+
printf("need to specify a file name\n");
|
|
20
|
+
exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* zero out the buffer for the file name
|
|
25
|
+
*/
|
|
26
|
+
for(i=0; i < sizeof(file_name); i++) {
|
|
27
|
+
file_name[i] = 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* copy the argument into a local buffer
|
|
32
|
+
*/
|
|
33
|
+
strncpy(file_name,argv[1],sizeof(file_name));
|
|
34
|
+
file_name[sizeof(file_name)-1] = 0;
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* try and open the file for reading
|
|
38
|
+
*/
|
|
39
|
+
my_file_desc = open(file_name,O_RDONLY,0);
|
|
40
|
+
if(my_file_desc < 0) {
|
|
41
|
+
printf("failed to open %s for reading\n",file_name);
|
|
42
|
+
exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
printf("my_file_desc: %d\n",my_file_desc);
|
|
46
|
+
|
|
47
|
+
string = "a string written to standard out\n";
|
|
48
|
+
write(1,string,strlen(string));
|
|
49
|
+
|
|
50
|
+
close(my_file_desc);
|
|
51
|
+
|
|
52
|
+
return(0);
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
data/assets/file-fd2.c
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* write to stdout and stderr
|
|
9
|
+
*/
|
|
10
|
+
int main(int argc, char **argv)
|
|
11
|
+
{
|
|
12
|
+
char *string;
|
|
13
|
+
char *string_err;
|
|
14
|
+
|
|
15
|
+
string = "a string written to standard out\n";
|
|
16
|
+
write(1,string,strlen(string));
|
|
17
|
+
|
|
18
|
+
string_err = "a string written to standard error\n";
|
|
19
|
+
write(2,string_err,strlen(string_err));
|
|
20
|
+
|
|
21
|
+
return(0);
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
data/assets/file-fd3.c
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* read from stdin and write to stdout
|
|
9
|
+
*/
|
|
10
|
+
int main(int argc, char **argv)
|
|
11
|
+
{
|
|
12
|
+
char buffer[4096];
|
|
13
|
+
|
|
14
|
+
memset(buffer,0,sizeof(buffer));
|
|
15
|
+
|
|
16
|
+
read(0,buffer,sizeof(buffer));
|
|
17
|
+
write(1,buffer,strlen(buffer));
|
|
18
|
+
|
|
19
|
+
return(0);
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
data/assets/file-read1.c
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* open and read the contents of a file, printing them out as ascii
|
|
9
|
+
* characters
|
|
10
|
+
*/
|
|
11
|
+
int main(int argc, char **argv)
|
|
12
|
+
{
|
|
13
|
+
int my_file_desc;
|
|
14
|
+
char file_name[4096];
|
|
15
|
+
char read_buffer[4096];
|
|
16
|
+
int i;
|
|
17
|
+
int r;
|
|
18
|
+
|
|
19
|
+
if(argc < 2) {
|
|
20
|
+
printf("need to specify a file name as the first argument\n");
|
|
21
|
+
exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* zero out the buffer for the file name
|
|
26
|
+
*/
|
|
27
|
+
for(i=0; i < sizeof(file_name); i++) {
|
|
28
|
+
file_name[i] = 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
* copy the argument into a local buffer
|
|
33
|
+
*/
|
|
34
|
+
strncpy(file_name,argv[1],sizeof(file_name));
|
|
35
|
+
file_name[sizeof(file_name)-1] = 0;
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* try and open the file for reading
|
|
39
|
+
*/
|
|
40
|
+
my_file_desc = open(file_name,O_RDONLY,0);
|
|
41
|
+
if(my_file_desc < 0) {
|
|
42
|
+
printf("failed to open %s for reading\n",file_name);
|
|
43
|
+
exit(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for(i=0; i < sizeof(read_buffer); i++) {
|
|
47
|
+
read_buffer[i] = 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
r = read(my_file_desc,read_buffer,sizeof(read_buffer)-1);
|
|
51
|
+
|
|
52
|
+
printf("file: %s contains the string: %s\n",
|
|
53
|
+
file_name,
|
|
54
|
+
read_buffer);
|
|
55
|
+
|
|
56
|
+
close(my_file_desc);
|
|
57
|
+
|
|
58
|
+
return(0);
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
data/assets/file-read2.c
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#include <unistd.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <fcntl.h>
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* read the file in its entirety (like the cat utility)
|
|
9
|
+
*/
|
|
10
|
+
int main(int argc, char **argv)
|
|
11
|
+
{
|
|
12
|
+
int my_file_desc;
|
|
13
|
+
char file_name[4096];
|
|
14
|
+
char read_buffer[4096];
|
|
15
|
+
int i;
|
|
16
|
+
int r;
|
|
17
|
+
|
|
18
|
+
if(argc < 2) {
|
|
19
|
+
printf("need to specify a file name as the first argument\n");
|
|
20
|
+
exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* zero out the buffer for the file name
|
|
25
|
+
*/
|
|
26
|
+
for(i=0; i < sizeof(file_name); i++) {
|
|
27
|
+
file_name[i] = 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* copy the argument into a local buffer
|
|
32
|
+
*/
|
|
33
|
+
strncpy(file_name,argv[1],sizeof(file_name));
|
|
34
|
+
file_name[sizeof(file_name)-1] = 0;
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* try and open the file for reading
|
|
38
|
+
*/
|
|
39
|
+
my_file_desc = open(file_name,O_RDONLY,0);
|
|
40
|
+
if(my_file_desc < 0) {
|
|
41
|
+
printf("failed to open %s for reading\n",file_name);
|
|
42
|
+
exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for(i=0; i < sizeof(read_buffer); i++) {
|
|
46
|
+
read_buffer[i] = 0;
|
|
47
|
+
}
|
|
48
|
+
r = read(my_file_desc,read_buffer,sizeof(read_buffer)-1);
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
* read and print until EOF
|
|
52
|
+
*/
|
|
53
|
+
while(r > 0) {
|
|
54
|
+
printf("%s",read_buffer);
|
|
55
|
+
for(i=0; i < sizeof(read_buffer); i++) {
|
|
56
|
+
read_buffer[i] = 0;
|
|
57
|
+
}
|
|
58
|
+
r = read(my_file_desc,read_buffer,sizeof(read_buffer)-1);
|
|
59
|
+
}
|
|
60
|
+
close(my_file_desc);
|
|
61
|
+
|
|
62
|
+
return(0);
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|