#include #include #include #include #include #include "error.h" #include "message.h" #define MAX_NO_RECORD 1000 RECORD data[MAX_NO_RECORD]; int no_account; void write_data_file(); int fd; int TOTAL=0; main(argc, argv) int argc; char *argv[]; { char data_file_name[40] ; char Machine[32],ShortHost[32]; if (argc != 2) { fprintf(stderr,"Form: add_data \n"); exit(1); } strcpy(data_file_name,"DATA."); strcat(data_file_name,argv[1]); strcat(data_file_name,"."); gethostname(Machine,32); strcpy(ShortHost,strtok(Machine,".")); strcat(data_file_name,Machine); fprintf(stderr,"Opening: %s\n",data_file_name); printf("\nWhen you want to stop the input,"); printf(" INTERRUPT the program.\n\n"); fd = open(data_file_name, O_RDWR | O_CREAT, 0600); if(signal(SIGINT, write_data_file) == SIG_ERR) /* if(signal(SIGINT, write_data_file) == BAD_SIGNAL) */ perror("signal"); read_data_file(); while(query(&data[no_account++])) { } } /* return 999 if succeed */ query(rec) RECORD *rec; { printf("Input the bar code.\n"); scanf("%d", &(rec->bar_code)); printf("Input the unit price.\n"); scanf("%f", &(rec->unit_price)); printf("Input the quantity.\n"); scanf("%d", &(rec->quantity)); TOTAL += rec->quantity; printf("Input a short description\n"); while(getchar() != '\n') { } if(gets(rec->description) == NULL) { printf("The description reading is wrong.\n"); exit(1); } } read_data_file() { int i, read_byte; if((read_byte=read(fd, &no_account, sizeof(int)))<=0) { no_account = 0; if(read_byte==0) fprintf(stderr, "The file DATA is currently empty.\n"); else perror("READ ERROR"); } else if(no_account>0) { printf("Current no. of item = %d \n", no_account); read_byte = sizeof(RECORD)*no_account; if(read(fd, data, read_byte) != read_byte) { fprintf(stderr, "The file DATA has wrong format\n"); exit(2); } for(i=0; i