Advertisement
Guest User

Untitled

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