试图了解plist()的作用

问题描述 投票:0回答:1

我试图了解plist()在c语言中的作用,特别是下面星号之间的for循环。我假设for循环的作用是浏览一个comm文件,如果到达它的末尾,它将通过将换行符替换为null来终止它,然后退出循环。那是对的还是我要离开?

谢谢!

void plist()
{
        DIR *proc;
        struct dirent *e;
        int result;
        char comm[COMM_SIZE];       
        char comm_fn[512];
        int fd, i, n;

        proc = opendir(proc_prefix);

        if (proc == NULL) {
                fprintf(stderr, "ERROR: Couldn't open /proc.\n");
        }

        for (e = readdir(proc); e != NULL; e = readdir(proc)) {
                if (isdigit(e->d_name[0])) {
                        setup_comm_fn(e->d_name, comm_fn);
                        fd = open(comm_fn, O_RDONLY);
                        if (fd > -1) {                                
                                n = read(fd, comm, COMM_SIZE);
                                close(fd);
                             *******   

                                    for (i=0; i < n; i++) {
                                    if (comm[i] == '\n') {
                                    comm[i] = '\0';
                                    break;
                                  } 

                             *******
                                }
                                printf("%s: %s\n", e->d_name, comm);
                        } else {
                                printf("%s\n", e->d_name);
                        }
                }
        }

        result = closedir(proc);
        if (result) {
                fprintf(stderr, "ERROR: Couldn't close /proc.\n");
        }
}
c linux plist manpage
1个回答
0
投票

是,它只是将换行符更改为字符串终止符。

© www.soinside.com 2019 - 2024. All rights reserved.