#include<stdio.h>#include<stdlib.h>#include<time.h>#include<string.h>#include<curses.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>voidprint_new_activity();voidstart_new_activity();voidstop_new_activity();voidedit_new_activity();voidprint_activities();voidsave_to_file(char*filepath);voidload_file(char*filepath);char*create_data_files();typedefstructactivities{time_tstart_time;time_tend_time;chardescription[100];}activity;activitynew_activity;activityactivities_list[100];intmain(){strcpy(new_activity.description,"N/A");new_activity.start_time=time(NULL);load_file(create_data_files());charcommand;while(command!='q'){system("clear");print_new_activity();print_activities();printf("\n\n[s]tart/sto[p]/[e]dit new activity | sa[v]e to file | [q]uit: ");fgets(&command,256,stdin);switch(command){case's':start_new_activity();break;case'p':stop_new_activity();break;case'e':edit_new_activity();break;case'v':save_to_file(create_data_files());break;case'q':break;default:break;}}return0;}voidprint_new_activity(){time_ttime_now=time(NULL);charbuf[80];strftime(buf,sizeof(buf),"%H:%M:%S",localtime(&new_activity.start_time));printf("==================================================""==================================================\n""Current activity: %s\nStart time: %s\nDuration: %ld mins.\n""==================================================""==================================================\n",new_activity.description,buf,(time_now-new_activity.start_time)/60);}voidprint_activities(){charbuf1[80],buf2[80];printf("\nPast activities:\n""--------------------------------------------------""--------------------------------------------------");for(inti=0;activities_list[i].start_time;i++){strftime(buf1,sizeof(buf1),"%H:%M:%S",localtime(&activities_list[i].start_time));strftime(buf2,sizeof(buf2),"%H:%M:%S",localtime(&activities_list[i].end_time));printf("\nStart time: %s\nEnd time: %s\nActivity: %s\n""--------------------------------------------------""--------------------------------------------------",buf1,buf2,activities_list[i].description);}}voidstart_new_activity(){new_activity.start_time=time(NULL);printf("\nWhat are you doing: ");fgets(new_activity.description,256,stdin);new_activity.description[strcspn(new_activity.description,"\n")]=0;}voidstop_new_activity(){new_activity.end_time=time(NULL);for(inti=0;i<100;i++){if(!activities_list[i].start_time){activities_list[i]=new_activity;strcpy(new_activity.description,"N/A");break;}}}voidedit_new_activity(){printf("\nWhat are you doing: ");fgets(new_activity.description,256,stdin);new_activity.description[strcspn(new_activity.description,"\n")]=0;}char*create_data_files(){FILE*fp;time_trawtime;structtm*info;chartimesheets_dir[80],date[11];staticcharfilepath[80];structstatst={0};snprintf(timesheets_dir,sizeof(timesheets_dir),"%s/Timesheets",getenv("HOME"));if(stat(timesheets_dir,&st)==-1)mkdir(timesheets_dir,0700);time(&rawtime);strftime(date,sizeof(date),"%Y-%m-%d",localtime(&rawtime));snprintf(filepath,sizeof(filepath),"%s/timesheet-%s.txt",timesheets_dir,date);if(access(filepath,F_OK)==-1){fp=fopen(filepath,"w");fclose(fp);}returnfilepath;}voidsave_to_file(char*filepath){FILE*fp;fp=fopen(filepath,"w");for(inti=0;activities_list[i].start_time;i++){fprintf(fp,"%ld;%ld;%s\n",activities_list[i].start_time,activities_list[i].end_time,activities_list[i].description);}fclose(fp);}voidload_file(char*filepath){FILE*fp;inti;fp=fopen(filepath,"r");while(EOF!=fscanf(fp,"%ld;%ld;%s",&activities_list[i].start_time,&activities_list[i].end_time,activities_list[i].description))i++;}