gpt4 book ai didi

c - 奇怪的 execlp() 命令

转载 作者:行者123 更新时间:2023-11-30 20:50:35 25 4
gpt4 key购买 nike

所以我基本上需要在 c 中执行这个命令

find . -type f -ls | cut -c 2- | sort -n -k 7 >file.txt ; less <file.txt

程序正在运行,但不执行 >,<>file.txtless <file.txt

我这样写 execlp()

execlp("sort", "sort","-n", "-k", "7",">file.txt", NULL)

execlp("less","less","<file.txt", NULL)

关于如何编写它们有什么技巧吗?谢谢

-第一个管道带有套接字 AF_UNIX

-第二个管道正常

#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>

char *socket_path = "/tmp/socket";
void child1();
void child2();
void child3(int *pipe_fd);
void father(pid_t *pid);

int main(int argc, char *argv []) {

pid_t childpid[3];
int fd[2];

if (argc != 1) {
puts("ERROR!");
exit(0);
}

//Pipe
if (pipe(fd) == -1) {
perror("pipe");
}

//Filhos
childpid[0]=fork();

if(childpid[0]==-1) perror("Erro filho 1");

else if (childpid[0]==0){
child1(); // Find
wait(0);
exit(1);
}
else{
childpid[1]=fork(); //filho 2

if(childpid[1]==-1) perror("Erro filho 2");

else if (childpid[1]==0){
child2();// 'cut'
wait(0);
exit(1);
}

else{
childpid[2]=fork();

if(childpid[2]==-1) perror("Erro filho 3");

else if (childpid[2]==0){
child3(fd); // Sort
wait(0);
exit(1);
}

else{
father(childpid); // Less
wait(0);
exit(1);
}
}
}
return(0);
}


void child1() { // FIND

struct sockaddr_un addr;
int socket_fd;


if ( (socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket filho1");
exit(-1);
}


memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;

if (*socket_path == '\0') {
*addr.sun_path = '\0';
strncpy(addr.sun_path+1, socket_path+1, sizeof(addr.sun_path)-2);
}

else {
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
}


while (connect(socket_fd, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) == -1) {
if (errno != ENOENT && errno != ECONNREFUSED) {
perror("Erro connect filho 1");
}
}


if( dup2(socket_fd,1) == -1) { //escreve no output!
perror("Erro no dup filho 1");
}

if( close(socket_fd) == -1) {
perror("Erro no close filho 1");
}

if( execlp("find","find", ".", "-type", "f", "-ls", NULL) == -1) {
perror("Find");
}
}

void child2() { // CUT

wait(0);
struct sockaddr_un local, remote;
socklen_t socket_length = (socklen_t)sizeof(remote);
int socket_fd, saida_fd;


if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("Erro no socket\n");
exit(-1);
}

memset(&local, 0, sizeof(local));
memset(&remote, 0, sizeof(remote));

local.sun_family = AF_UNIX;
strcpy(local.sun_path, socket_path);

if (unlink(socket_path) == -1){
if (errno != ENOENT){
perror("filho2 unlink");
}
}

if (bind(socket_fd, (struct sockaddr *)&local,(socklen_t)(strlen(local.sun_path) + sizeof(local.sun_family))) == -1){
perror("filho 2 bind");
}

if (listen(socket_fd, 1) == -1) {
perror("Erro no listen");
}

if ((socket_fd = accept(socket_fd, (struct sockaddr *)&remote, &socket_length)) == -1){
perror("filho 2 accept");
}

if( dup2(socket_fd,0) == -1) { // recebe do output!
perror("dup2(1) do filho 2");
}

if( close(socket_fd) == -1) {
perror("Failed to close(1) child 2");
}

//--------------------------------------------------//
if((saida_fd = open("file.txt", O_WRONLY )) == -1) {
perror("Open filho 2");
}

if( dup2(saida_fd,1) == -1) {
perror("Failed to dup2(2) child 2");
}

if(close(saida_fd) == -1) {
perror("Failed to close(2) child 2");
}

if(execlp("cut", "cut", "-c", "2-", (char *)NULL) == -1 ){
perror("Cut");
}
}


void child3(int *pipe_fd) // SORT
{
int fd;

if (close(pipe_fd[1]) == -1){
perror("erro no close da escrita do filho3");
}

if (dup2(pipe_fd[0], STDIN_FILENO) == -1){
perror("erro no dup do filho 3 ");
}

if (close(pipe_fd[0]) == -1){
perror("erro no close da leitura do filho 3 ");
}

if ((fd = open("file.txt", O_RDWR)) == -1) {
perror("erro no open do pai");
}

if (dup2(fd, 1) == -1) {
perror("erro no dup do filho 3");
}

if (close(fd) == -1){
perror("erro no close 3 do filho 3");
}

if (execlp("sort", "sort","-n", "-k", "7",">file.txt", (char*)NULL) == -1) {
perror("Sort");
}
}


void father(pid_t *pid) // 'less'
{
int i;
int ID_final;
int status[4];
int file_fd;

if ((file_fd = open("file.txt",O_WRONLY | O_TRUNC | O_CREAT, 0666)) == -1) {
perror("erro no open pai");
}

//espera pelos filhos
for (i = 0; i < 3; i++){
while ((ID_final = waitpid(pid[i], &status[i], 0)) != pid[i]){
if (ID_final == -1){
perror("erro no waitpid");
}
}
}

if ((file_fd = open("file.txt", O_RDONLY)) == -1) {
perror("erro no open do pai");
}

if (dup2(file_fd, STDIN_FILENO) == -1){
perror("erro no dup do pai");
}

if (close(file_fd) == -1){
perror("erro no close do pai");
}

if (execlp("less","less","<file.txt", (char*)NULL) == -1){
perror("erro no 'less'");
}
}

最佳答案

重定向由 shell 处理。在 C 中,您需要成为自己的 shell 并自己进行重定向:

int fd = open("file.txt", O_TRUNC|O_WRONLY);
if(0>fd) /*handle error*/;
dup2(fd, 1 /*STDOUT*/);
close(fd); /*file.txt is now on fd 1*/
///....
execlp("sort", "sort","-n", "-k", "7", (char*)NULL);

关于c - 奇怪的 execlp() 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43506554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com