int parsing(t_all *all, char *str) { get_next_line(0, all); if (check_file(str, all) == 2) return (0); if (all->flag == 0) { /* compil(all.fd, av[--index]); */ x_close(all->fd); } return (1); }
int main(int argc, char ** argv) { if ( argc != 2 ) { fprintf(stderr, "Usage: pricelist FILENAME\n"); return EXIT_FAILURE; } const int dbfd = x_open(argv[1], O_RDWR | O_CREAT, 0644); bool should_quit = false; while ( !should_quit ) { print_summary(dbfd, argv[1]); int choice = get_menu_choice(); struct record record; switch ( choice ) { case MENU_LIST: list_records(dbfd); break; case MENU_ADD: get_new_record(&record); write_record(dbfd, &record, -1); break; case MENU_DELETE: printf("Enter record number to delete: "); fflush(stdout); delete_record(dbfd, get_integer()); break; case MENU_QUIT: printf("Goodbye!\n"); should_quit = true; break; default: printf("Invalid menu choice. Try again.\n\n"); break; } } x_close(dbfd); return 0; }
static int print_file(WINDOW *w, int item) { /* int handle, error = 0, i, key, result = 0, r; DjV 033 010203 */ int handle, error = 0, i, result = 0; /* DjV 033 010203 */ char *buffer; const char *name; long l; /* index in buffer[] */ int ll = 0; /* DjV 031 150203 line length counter */ boolean stop = FALSE; if ((name = itm_fullname(w, item)) == NULL) return XFATAL; buffer = x_alloc(PBUFSIZ); if (buffer != NULL) { graf_mouse(HOURGLASS, NULL); if ((handle = x_open(name, O_DENYW | O_RDONLY)) >= 0) { do { if ((l = x_read(handle, PBUFSIZ, buffer)) >= 0) { for (i = 0; i < (int) l; i++) { /* DjV 031 150203 ---vvv--- */ /* * line wrap & new line handling; */ ll++; if ( (buffer[i] == (char)13) || (buffer[i] == (char)10) || (buffer[i] == (char)12) ) ll = 0; /* reset linelength counter at CR, LF or FF */ else if ( ll >= plinelen ) { ll = 0; if (( stop = print_eol() ) == TRUE) break; } /* DjV 031 150203 ---^^^--- */ if ((stop = prtchar(buffer[i])) == TRUE) break; } /* DjV 033 010203 ---vvv--- */ /* if ((r = key_state(&key, TRUE)) > 0) { if (key == ESCAPE) stop = TRUE; } else if (r < 0) stop = TRUE; */ if ( escape_abort(TRUE) ) stop = TRUE; /* DjV 033 010203 ---^^^--- */ } else error = (int) l; } while ((l == PBUFSIZ) && (stop == FALSE)); x_close(handle); print_eol(); /* DjV 031 150203 print cr lf at end of file */ } else error = handle; if (stop == TRUE) result = XABORT; if (error != 0) result = xhndl_error(MEPRINT, error, itm_name(w, item)); graf_mouse(ARROW, NULL); x_free(buffer); } else { xform_error(ENSMEM); result = XFATAL; } free(name); return result; }
int main(void) { int in[2]; int out[2]; pid_t pid; printf("type 'quit' for exit\n\n"); x_pipe(in); x_pipe(out); pid = x_fork(); if(IS_CHILD(pid)) { x_dup2(in[READ], STDIN_FD); x_dup2(out[WRITE], STDOUT_FD); x_close(in[READ]); x_close(in[WRITE]); x_close(out[READ]); x_close(out[WRITE]); execl("/usr/bin/bc", "runbc", NULL); perror("execl"); exit(1); } else if(IS_PARENT(pid)) { char buf[BUFSIZE + 1]; fd_set rfds; int count; int flag = 1; x_close(in[READ]); x_close(out[WRITE]); while(13) { FD_ZERO(&rfds); FD_SET(STDIN_FD, &rfds); FD_SET(out[READ], &rfds); count = 0; x_select(out[READ] + 1, &rfds, NULL, NULL, NULL); if(FD_ISSET(out[READ], &rfds)) { // read messages from BC count = x_read(out[READ], buf, BUFSIZE); if(count) { x_write(STDOUT_FD, buf, count); } else { break; } } if(FD_ISSET(STDIN_FD, &rfds) && flag) { // send commands to BC count = x_read(STDIN_FD, buf, BUFSIZE); if(count) { x_write(in[WRITE], buf, count); } else { x_write(in[WRITE], "quit\n", 6); flag = 0; } } } x_waitpid(pid, NULL, 0); x_close(in[WRITE]); x_close(out[READ]); } return 0; }