Changeset b640738 in mainline


Ignore:
Timestamp:
2021-08-18T22:12:33Z (3 years ago)
Author:
Manuele Conti <manuele.conti@…>
Children:
12f5a1be
Parents:
d6a9824
Message:

Fix redirect using pipe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/input.c

    rd6a9824 rb640738  
    200200                        pipe_pos[pipe_count++] = i;
    201201                        cmd_argc = i;
    202                         redir_to = tmpnam(NULL);
     202                        redir_from = redir_to = tmpnam(NULL);
    203203                        break;
    204204
     
    251251        FILE *to = NULL;
    252252
    253         if (redir_from) {
    254                 from = fopen(redir_from, "r");
    255                 if (from == NULL) {
    256                         printf("Cannot open file %s\n", redir_from);
    257                         rc = errno;
    258                         goto finit_with_files;
    259                 }
    260                 new_iostate.stdin = from;
    261         }
    262 
    263         if (redir_to) {
    264                 to = fopen(redir_to, "w");
    265                 if (to == NULL) {
    266                         printf("Cannot open file %s\n", redir_to);
    267                         rc = errno;
    268                         goto finit_with_files;
    269                 }
    270                 new_iostate.stdout = to;
    271         }
    272253
    273254        for (unsigned p = 0; p <= pipe_count; p++) {
     
    302283                }
    303284
    304                 if (p < pipe_count) {
    305                         new_iostate.stdout = to;
    306                 }
    307                 if (p && p == pipe_count) {
    308                         fseek(to, 0, SEEK_SET);
    309                         new_iostate.stdin = to;
     285                if (redir_to) {
     286                        if ((p < pipe_count) || (pipe_count == 0)) {
     287                                to = fopen(redir_to, "w");
     288                                if (to == NULL) {
     289                                        printf(
     290                                            "Cannot open file %s redirect to\n",
     291                                            redir_to);
     292                                        rc = errno;
     293                                        goto finit_with_files;
     294                                }
     295                                new_iostate.stdout = to;
     296                        }
     297                }
     298
     299                if (redir_from) {
     300                        if ((p && p == pipe_count) || (pipe_count == 0)) {
     301                                from = fopen(redir_from, "r");
     302                                if (from == NULL) {
     303                                        printf("Cannot open file %s redirect "
     304                                               "from\n",
     305                                            redir_from);
     306                                        rc = errno;
     307                                        goto finit_with_files;
     308                                }
     309                                new_iostate.stdin = from;
     310                        }
    310311                }
    311312
     
    316317                }
    317318
     319                if (to) {
     320                        fclose(to);
     321                        to = NULL;
     322                }
     323                if (from) {
     324                        fclose(from);
     325                        from = NULL;
     326                }
    318327                // Restore the Standard Input, Output and Error file descriptors
    319328                new_iostate.stdin = stdin;
Note: See TracChangeset for help on using the changeset viewer.