pipe(p); // create the new pipeline connect the it and the it's child
char num[1]; close(p1[1]); // close the old write if( read(p1[0], num, 1) == 1 ){ int prime = num[0]; // the first number is the prime printf("prime %d\n", prime);
if(fork() == 0){ func(p); // recursion } else { close(p[0]); // close the read while ( read(p1[0], num, 1) == 1 ){ int n = num[0]; if(n % prime != 0){ write(p[1], num, 1); } } close(p1[0]); close(p[1]); wait(0); } } else { // no data avaliable close(p[0]); close(p[1]); exit(0); } }
// Look at user/ls.c to see how to read directories. // Use recursion to allow find to descend into sub-directories. // Don't recurse into "." and "..". // Changes to the file system persist across runs of qemu; to get a clean file system run make clean and then make qemu. // You'll need to use C strings. Have a look at K&R (the C book), for example Section 5.5. // Note that == does not compare strings like in Python. Use strcmp() instead. // Add the program to UPROGS in Makefile.
// find <dir_name> <file_name> // find all the <file_name> in the <dir_name> int main(int argc, char* argv[]){ if(argc < 3){ fprintf(1, "the arguments is too few...\n"); exit(1); }
find(argv[1], argv[2]); exit(0); }
6、xargs(moderate)
首先需要知道这个命令是干什么的。
Run COMMAND with arguments INITIAL-ARGS and more arguments read from
input.
intmain(int argc, char* argv[]) { if (argc < 2) { fprintf(2, "too few arguments..."); exit(1); }
char buf[512]; // store the input from the previous cmd int read_n = 0; int read_total = 0;
// attention: if exits '\n', just a read can not read all while( (read_n = read(0, buf + read_total, 512)) > 0 ){ read_total += read_n; // stat the number of input } int len = read_total;
// switch all the '\n' to ' ' for(int i = 0; i < len; i ++){ if(buf[i] == '\n') buf[i] = ' '; }
int cmd_ptr = 1; // the ptr to cmd bool is_set_n = false;
// set the max args if(strcmp(argv[1], "-n") == 0){ is_set_n = true; cmd_ptr = 3; }
char* cmd_argv[MAXARG]; // the cmd argv // put the arg into the cmd_argv int idx = 0; for (int i = cmd_ptr; i < argc; i++) { char *arg = (char *)malloc(strlen(argv[i])+1); // attention: must use malloc, put the data in heap // if use char arg[MAXARG] will cause the error strcpy(arg, argv[i]); cmd_argv[idx] = arg; idx ++; }
if(is_set_n){ int index = 0; char arg[MAXARG]; memset(arg, 0, MAXARG); // clear the space for(int i = 0; i < len; i++){ if(buf[i] == ' ') { // can spilt out a arg
arg[index++] = '\0'; cmd_argv[idx++] = arg;
// run exec1(cmd_argv);
index = 0; memset(arg, 0, MAXARG); // clear the space
idx --; continue; } arg[index ++] = buf[i]; } } else { int prev = 0; // should spilt the args with ' ' // and put them into cmd_argv sequentially for(int i = 0; i < len; i ++){ if(buf[i] == ' '){ char* add_arg = (char *)malloc(i - prev); memcpy(add_arg, buf+prev, i - prev); prev = i+1; // attention cmd_argv[idx++] = add_arg; } } exec1(cmd_argv); }
exit(0); }
记录一个小问题
发生在将argv里的参数拷贝到cmd_argv的过程中
1 2 3 4
for(int i = 0; i < len; i++){ char agr[32]; // 每次分配到的地址都是一样的!!! cmd_argv[idx ++] = strcpy(arg, argv[i]); }