Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- struct log{
- struct tm date;
- char argument[10];
- };
- int main(int argc, char *argv[]){
- if(argc<2){
- printf("You cannot run this program without arguments\n");
- return -1;
- }
- if(strcmp(argv[1],"-r")==0){
- if(argc!=2){
- printf("You cannot run this program with arguments\n");
- return -1;
- }
- }
- else if(strcmp(argv[1],"-w")==0){
- if(argc!=3){
- printf("You can run this program with only one argument\n");
- return -1;
- }
- char *argument;
- if(strlen(argv[2])>9){
- printf("This argument seems to be too long, we need to trim it!\n");
- argument=malloc(9*sizeof(char));
- strncpy(argument,argv[2],9);
- printf("Your argument after trimming is:%s\n",argument);
- }
- else{
- argument=argv[2];
- printf("Your argument is:%s\n",argument);
- }
- struct log* temp=(struct log*)malloc(sizeof(struct log));
- time_t current_time;
- time(¤t_time);
- localtime_r(¤t_time, &temp->date);
- strcpy(temp->argument,argument);
- char buffer[21];
- snprintf(buffer,sizeof(buffer),"%d.%d.%d.%d.%d.log",temp->date.tm_year+1900,temp->date.tm_mon+1,temp->date.tm_mday,temp->date.tm_hour,temp->date.tm_min);
- FILE* file_save=fopen(buffer,"ab");
- fwrite(temp,sizeof(struct log),1,file_save);
- fclose(file_save);
- }
- else{
- printf("Wrong argument provided!\n");
- return -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement