Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. struct log{
  10.     struct tm date;
  11.     char argument[10];
  12. };
  13.  
  14. int main(int argc, char *argv[]){
  15.  
  16.     if(argc<2){
  17.         printf("You cannot run this program without arguments\n");
  18.     return -1;
  19.     }
  20.  
  21.  
  22.     if(strcmp(argv[1],"-r")==0){
  23.        
  24.        
  25.             if(argc!=2){
  26.                 printf("You cannot run this program with arguments\n");
  27.                 return -1;
  28.             }
  29.  
  30.  
  31.     }
  32.     else if(strcmp(argv[1],"-w")==0){
  33.  
  34.  
  35.  
  36.             if(argc!=3){
  37.                 printf("You can run this program with only one argument\n");
  38.                 return -1;
  39.             }
  40.        
  41.             char *argument;
  42.             if(strlen(argv[2])>9){
  43.                 printf("This argument seems to be too long, we need to trim it!\n");
  44.                 argument=malloc(9*sizeof(char));
  45.                 strncpy(argument,argv[2],9);
  46.                 printf("Your argument after trimming is:%s\n",argument);   
  47.             }
  48.             else{
  49.                 argument=argv[2];
  50.                 printf("Your argument is:%s\n",argument);
  51.             }
  52.  
  53.             struct log* temp=(struct log*)malloc(sizeof(struct log));
  54.             time_t current_time;
  55.             time(&current_time);       
  56.             localtime_r(&current_time, &temp->date);
  57.             strcpy(temp->argument,argument);
  58.  
  59.  
  60.             char buffer[21];
  61.             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);
  62.  
  63.             FILE* file_save=fopen(buffer,"ab");
  64.             fwrite(temp,sizeof(struct log),1,file_save);
  65.             fclose(file_save);
  66.  
  67.                    
  68.     }
  69.     else{
  70.         printf("Wrong argument provided!\n");
  71.         return -1;
  72.     }
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement