oprava pri volani jako funkce
[mirrors/Programs.git] / c / cano / old / cano4.c
1 #define _GNU_SOURCE
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/syscall.h>
6 #include <dlfcn.h>
7 //#include <unistd.h>
8 #include <stdarg.h>
9 #include <time.h>
10 #include <string.h>
11 #include <linux/limits.h>
12
13 #define i { srand(23); fprintf(stderr,"%s();\n",__func__); }
14 //Catch open() calls (while redirecting filename):
15
16 static const char *fd_to_path(int fd) {
17 static char path[PATH_MAX];
18 sprintf(path,"/proc/self/fd/%d",fd);
19 int l = (int) readlink((char * __restrict__) path, path, PATH_MAX);
20 path[l]=0;
21 return path;
22 }
23
24
25 int pipe_read() {
26 /*
27 int pipefd[2];
28 pipe(pipefd);
29 if(!fork()) {
30 //write(pipefd[1],"ahoj\n",5);
31 syscall(SYS_write,pipefd[1],"ahoj\n",5);
32 close(pipefd[1]);
33 exit(0);
34 }
35 return pipefd[0];
36 */
37 return dup(fileno(popen("echo lolpajp", "r")));
38 }
39 int fdd;
40 ssize_t read(int fd, void *buf, size_t count) {
41 fprintf(stderr,"%s(%d,%d,%s);\n",__func__,fd,count,fd_to_path(fd));
42 if(!fdd) fdd=pipe_read();
43 if(strcmp(fd_to_path(fd),"/usr/share/terminfo/x/xterm")!=0) {
44 return syscall(SYS_read,fdd,buf,count);
45 } else {
46 return syscall(SYS_read,fd,buf,count);
47 }
48 }
49 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
50 fprintf(stderr,"%s(%d,%d);\n",__func__,stream,fileno(stream));
51 return read(fileno(stream),ptr,size*nmemb);
52 }
53 /*
54 ssize_t write(int fd, void *buf, size_t count) {
55 fprintf(stderr,"%s(%d,%d);\n",__func__,fd,count);
56 return syscall(SYS_write,fd,buf,count);
57 }
58 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
59 return write(fileno(stream),(void *)ptr,size*nmemb);
60 }
61 */
62 /*
63 int open(const char *filename, int flags, ...)
64 {
65 //srand(23);
66 static int (*open_orig)(const char *, int, mode_t);
67 int ret;
68 va_list ap;
69 mode_t mode;
70
71 if (!open_orig) {
72 open_orig = dlsym(RTLD_NEXT, "open");
73 }
74
75 va_start(ap, flags);
76 mode = va_arg(ap, mode_t);
77 va_end(ap);
78
79 if(strcmp(filename,"/tmp/a") != 0) {
80 ret = open_orig(redirect_name(filename), flags, mode);
81 //ret = syscall(SYS_open,redirect_name(filename),flags,mode);
82 } else {
83 //ret = fileno(popen("cat aaaa", "r"));
84 int pipefd[2];
85 pipe(pipefd);
86 ret = pipefd[0];
87 //ssize_t write(int fd, const void *buf, size_t count);
88 if(!fork()) { write(pipefd[1],"ahoj\n",5); close(pipefd[1]); exit(0); }
89 }
90
91 //printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret);
92
93 return ret;
94 }
95
96 int open64(const char *filename, int flags, ...)
97 {
98 //srand(23);
99 static int (*open64_orig)(const char *, int, mode_t);
100 int ret;
101 va_list ap;
102 mode_t mode;
103
104 if (!open64_orig) {
105 open64_orig = dlsym(RTLD_NEXT, "open64");
106 }
107
108 va_start(ap, flags);
109 mode = va_arg(ap, mode_t);
110 va_end(ap);
111
112 if(strcmp(filename,"/tmp/a") != 0) {
113 ret = open64_orig(redirect_name(filename), flags, mode);
114 //ret = syscall(SYS_open64,redirect_name(filename),flags,mode);
115 } else {
116 //ret = fileno(popen("cat aaaa", "r"));
117 int pipefd[2];
118 pipe(pipefd);
119 ret = pipefd[0];
120 //ssize_t write(int fd, const void *buf, size_t count);
121 if(!fork()) { write(pipefd[1],"ahoj\n",5); close(pipefd[1]); exit(0); }
122 }
123
124 //printf("open64(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret);
125
126 return ret;
127 }
128 */
This page took 0.708556 seconds and 4 git commands to generate.