static int spend(const int amount) { int monthly_budget, spent; int ret; if((ret = read_db(&monthly_budget, &spent)) != EXIT_SUCCESS) return ret; spent += amount; if(verbose) printf("Spending of %d.%02d " CURRENCY " checked in.", BREAKUP(amount)); else printf("OK;"); if(spent > monthly_budget) { if(verbose) { printf(" You overspent your monthly budget!"); } else { printf(" OVER;"); } } printf("\n"); return write_db(monthly_budget, spent); }
void do_signal(int sig) { switch (sig) { case SIGHUP: tell_chans("SIGHUP received!"); rehash(); return; case SIGINT: case SIGQUIT: case SIGTERM: me.crashing = 1; write_db(0); if (me.servtype == SERV_IRCNN) toserv(":%s OPERWALL :services terminating!\r\n", me.sclients[OS].nick, sig); else toserv(":%s OPERWALL :services terminating!\r\n", me.servname, sig); toserv(":%s QUIT :bye!\r\n", me.sclients[OS].nick); fclose(me.logfd); close_all_connections(); exit(1); case SIGILL: case SIGTRAP: case SIGBUS: case SIGSEGV: case SIGSYS: signal(sig, SIG_DFL); if (me.crashing == 1) abort(); me.crashing = 1; toserv(":%s OPERWALL :\2ACK!\2 signal %d recieved!\r\n", me.servname, sig); kill(getpid(), sig); } }
/* append contents to file and reset count to 0 */ int flush_to_disk(char *file, Parts db) { int rc = 0; if ((rc = write_db(file, db, "ab")) == 0) { destroy_nodes(db); db->head = NULL; db->count = 0; } return rc; }
int main (int argc, char *argv[]) { char *df=0; char *rf=0; int c; if (argc < 2) usage(argv); while ( (c=getopt(argc, argv, "vVr:d:o:l:")) != EOF) { switch (c) { case 'v' : verbose = true; break; case 'V' : printf("%s: V%s Db=%s\n", basename(argv[0]), progversion, dbv); exit(1); case 'd' : df = strdup(optarg); break; case 'r' : rf = strdup(optarg); break; case 'o' : ortszone = atoi(optarg); break; case 'l' : numlen = atoi(optarg); break; } } if (verbose) stdoutisatty = isatty(fileno(stdout)); read_rzfile(rf); make_table(); write_db(df); /* Uff this got longer as I thought, C is a real low level language - now it's clear, why I prefer Perl */ return(EXIT_SUCCESS); }
Reader() { while (TRUE) { down(&mutex); reader_count = reader_count + 1; if (reader_count == 1){ down(&db);} up(&mutex); read_db(); down(&mutex); reader_count = reader_count - 1; if (reader_count == 0){ up(&db);} up(&mutex);} Writer() { while (TRUE) { create_data(); down(&db); write_db(); up(&db);}
/* write contents to file, which is overwritten if it existed */ int dump(char *outfile, Parts db) { return write_db(outfile, db, "wb"); }
static int init_month(const int daily_budget) { return write_db(daily_budget * days_this_month(), 0); }