Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. //SO IS1 222B LAB07
  2. //Grzegorz Muth
  3. //mg44436@zut.edu.pl
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <fcntl.h>
  10. #include <time.h>
  11.  
  12. void str_copy(char *to, const char *from)
  13. {
  14.     while(*to++ = *from++);
  15. }
  16.  
  17. int str_len(char *s)
  18. {
  19.     int i = 0;
  20.     while(*s++ != '\0')
  21.         i++;
  22.  
  23.     return i;
  24. }
  25.  
  26. void str_cut(char *s, size_t n)
  27. {
  28.     int i;
  29.  
  30.     for(i = 0; i < n; i++)
  31.         *s++;
  32.  
  33.     *s = '\0';
  34. }
  35.  
  36. char int_to_char(int n)
  37. {
  38.     char result;
  39.     while(n != 0)
  40.     {
  41.         result += n;
  42.  
  43.     }
  44.     n + 48;
  45.     return result;
  46.  
  47. }
  48.  
  49. struct log {
  50.     struct tm date; // czas
  51.     char argument[10]; // napisz argumentu wywołania
  52. };
  53.  
  54. int main(int argc, char *argv[]){
  55.  
  56.     if(argc == 1)
  57.         exit(1);
  58.  
  59.     int opt;
  60.     struct log obiekcik;
  61.  
  62.     time_t ptr;
  63.     time(&ptr);
  64.     char* rezultat;
  65.  
  66.     int handle;
  67.     int ile;
  68.     int wsk = 0;
  69.     int len;
  70.  
  71.  
  72.     while((opt = getopt(argc, argv, "rw:")) != -1)  
  73.     {  
  74.         switch(opt)  
  75.         {  
  76.             case 'w':
  77.  
  78.                 localtime_r(&ptr, &obiekcik.date);
  79.                 printf("%d\n", obiekcik.date.tm_mon + 1);
  80.  
  81.                 len = str_len(argv[2]);
  82.                 if(len < 9)
  83.                     str_cut(argv[2], len);
  84.                 else
  85.                 {
  86.                     str_cut(argv[2], 9);
  87.                     len = 9;
  88.                 }
  89.  
  90.                 str_copy(obiekcik.argument, argv[2]);
  91.                 printf("%s\n", obiekcik.argument);
  92.                 char name[20];
  93.                 char a[5];
  94.  
  95.                 //sprintf(obiekcik.date.tm_year + 1900, a, 10);
  96.                 //str_copy(name, a);
  97.                 //int_to_char(obiekcik.date.tm_year + 1900, a);
  98.                 ile = obiekcik.date.tm_year + 1900;
  99.                 wsk++;
  100.                 while ( ile / 10 > 0)
  101.                 {
  102.                     wsk++;
  103.                     ile = ile/10;
  104.  
  105.                 }
  106.                 sprintf(a, "%d", obiekcik.date.tm_year + 1900);
  107.                 str_copy(name, a);
  108.                 str_copy(&name[wsk], ".");
  109.  
  110.  
  111.                
  112.                 sprintf(a, "%d", obiekcik.date.tm_mon + 1);
  113.                 //printf("to a %s\n", a);
  114.                 str_copy(&name[++wsk], a);
  115.                 ile = obiekcik.date.tm_mon + 1;
  116.                 wsk++;
  117.                 while ( ile / 10 > 0)
  118.                 {
  119.                     wsk++;
  120.                     ile = ile/10;
  121.                 }
  122.                 str_copy(&name[wsk], ".");
  123.  
  124.  
  125.  
  126.                 sprintf(a, "%d", obiekcik.date.tm_mday);
  127.                 //printf("to a %s\n", a);
  128.                 str_copy(&name[++wsk], a);
  129.                 ile = obiekcik.date.tm_mday;
  130.                 wsk++;
  131.                 while ( ile / 10 > 0)
  132.                 {
  133.                     wsk++;
  134.                     ile = ile/10;
  135.                 }
  136.                 str_copy(&name[wsk], ".");
  137.  
  138.  
  139.  
  140.                 sprintf(a, "%d", obiekcik.date.tm_hour);
  141.                 //printf("to a %s\n", a);
  142.                 str_copy(&name[++wsk], a);
  143.                 ile = obiekcik.date.tm_hour;
  144.                 wsk++;
  145.                 while ( ile / 10 > 0)
  146.                 {
  147.                     wsk++;
  148.                     ile = ile/10;
  149.                 }
  150.                 str_copy(&name[wsk], ".");
  151.  
  152.  
  153.                 sprintf(a, "%d", obiekcik.date.tm_min);
  154.                 //printf("to a %s\n", a);
  155.                 str_copy(&name[++wsk], a);
  156.                 ile = obiekcik.date.tm_min;
  157.                 wsk++;
  158.                 while ( ile / 10 > 0)
  159.                 {
  160.                     wsk++;
  161.                     ile = ile/10;
  162.                 }
  163.                 str_copy(&name[wsk], ".");
  164.  
  165.                 str_copy(&name[++wsk], "txt");
  166.  
  167.  
  168.                 /*str_copy(name[5], itoa(obiekcik.date.tm_mon + 1));
  169.                 str_copy(name[7], ".");
  170.                 str_copy(name[, itoa(obiekcik.date.tm_mday));
  171.                 str_copy(name[, ".");
  172.                 str_copy(name, itoa(obiekcik.date.tm_hour));
  173.                 str_copy(name, ".");
  174.                 str_copy(name, itoa(obiekcik.date.tm_min));
  175.                 str_copy(name, ".");
  176.                 str_copy(name, "log");*/
  177.  
  178.                 //printf("%s\n", name);
  179.  
  180.                 handle = open(name, O_RDWR | O_CREAT, S_IRWXU);
  181.                 if(handle >= 0)
  182.                 {
  183.                     //read(0, &input, 1);
  184.                     lseek(handle, 0, SEEK_END);
  185.                     write(handle, &obiekcik.argument, str_len(obiekcik.argument));
  186.                     write(handle, "\n", sizeof(char));
  187.                     write(handle, &name, str_len(name) - 4);
  188.                     //write(handle, &obiekcik.date, sizeof(obiekcik.date));
  189.                     write(handle, "\n", sizeof(char));
  190.                 }
  191.                 close(handle);
  192.  
  193.                 /*handle = open("msg.txt", O_RDWR | O_CREAT, S_IRWXU);
  194.                 if(handle >= 0)
  195.                 {
  196.                     read(0, &input, 1);
  197.                     write(handle, &input, sizeof(input));
  198.                 }
  199.                 close(handle);*/
  200.  
  201.  
  202.                 break;
  203.             case 'r':
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.            
  215.                 break;
  216.             default:
  217.                 break;
  218.  
  219.            
  220.         }  
  221.     }  
  222.  
  223.    
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement