1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#define VOPT 1;
#define AOPT 2;
#define OFF1 4;
#define OFF2 8;
#define _XOPEN_SOURCE
void cpp(int n, char* arg[]) {
int off1, off2;
int opt = opt(n, arg, off1, off2);
int c = O_CREAT;
if (opt & VOPT == 1) {
c = c & O_EXCL;
}
if (opt & AOPT == 1) {
c = c & O_APPEND;
}
/*if (opt & OFF1 == 1) {
c = c & O_EXCL;
}
if (opt & OFF2 == 1) {
c = c & O_EXCL;
}*/
int dest = open(arg[0], c);
if (dest == - 1) {
perror("open");
exit(EXIT_FAILURE);
} else {
char buf[256];
int src = open(arg[n-1], O_RDONLY);
if (src == -1) {
perror("open");
}
int r = read(src, buf, 256);
while (r != 0) {
if (r == -1) {
perror("read");
exit(EXIT_FAILURE);
}
if (write(dest, buf, 256)) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
r = read(src, buf, 256);
}
}
}
int opt(int n, char* arg[], int *off1, int *off2) {
int mask = 0;
for (int i = 0; i < n; i++) {
int opt = getopt(n, arg, "vab:e:");
/*if (opt == -1) {
perror("getopt");
exit(EXIT_FAILURE);
}*/
switch(opt) {
case "v" :
mask = mask | VOPT;
break;
case "a" :
mask = mask | AOPT;
break;
case "b:" :
mask = mask | OFF1;
off1 = optarg;
break;
case "e:" :
mask = mask | OFF2;
off2 = optarg;
break;
}
return mask;
} |
Partager