Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SO IS1 222B LAB07
- //Grzegorz Muth
- //mg44436@zut.edu.pl
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <time.h>
- void str_copy(char *to, const char *from)
- {
- while(*to++ = *from++);
- }
- int str_len(char *s)
- {
- int i = 0;
- while(*s++ != '\0')
- i++;
- return i;
- }
- void str_cut(char *s, size_t n)
- {
- int i;
- for(i = 0; i < n; i++)
- *s++;
- *s = '\0';
- }
- char int_to_char(int n)
- {
- char result;
- while(n != 0)
- {
- result += n;
- }
- n + 48;
- return result;
- }
- struct log {
- struct tm date; // czas
- char argument[10]; // napisz argumentu wywołania
- };
- int main(int argc, char *argv[]){
- if(argc == 1)
- exit(1);
- int opt;
- struct log obiekcik;
- time_t ptr;
- time(&ptr);
- char* rezultat;
- int handle;
- int ile;
- int wsk = 0;
- int len;
- while((opt = getopt(argc, argv, "rw:")) != -1)
- {
- switch(opt)
- {
- case 'w':
- localtime_r(&ptr, &obiekcik.date);
- printf("%d\n", obiekcik.date.tm_mon + 1);
- len = str_len(argv[2]);
- if(len < 9)
- str_cut(argv[2], len);
- else
- {
- str_cut(argv[2], 9);
- len = 9;
- }
- str_copy(obiekcik.argument, argv[2]);
- printf("%s\n", obiekcik.argument);
- char name[20];
- char a[5];
- //sprintf(obiekcik.date.tm_year + 1900, a, 10);
- //str_copy(name, a);
- //int_to_char(obiekcik.date.tm_year + 1900, a);
- ile = obiekcik.date.tm_year + 1900;
- wsk++;
- while ( ile / 10 > 0)
- {
- wsk++;
- ile = ile/10;
- }
- sprintf(a, "%d", obiekcik.date.tm_year + 1900);
- str_copy(name, a);
- str_copy(&name[wsk], ".");
- sprintf(a, "%d", obiekcik.date.tm_mon + 1);
- //printf("to a %s\n", a);
- str_copy(&name[++wsk], a);
- ile = obiekcik.date.tm_mon + 1;
- wsk++;
- while ( ile / 10 > 0)
- {
- wsk++;
- ile = ile/10;
- }
- str_copy(&name[wsk], ".");
- sprintf(a, "%d", obiekcik.date.tm_mday);
- //printf("to a %s\n", a);
- str_copy(&name[++wsk], a);
- ile = obiekcik.date.tm_mday;
- wsk++;
- while ( ile / 10 > 0)
- {
- wsk++;
- ile = ile/10;
- }
- str_copy(&name[wsk], ".");
- sprintf(a, "%d", obiekcik.date.tm_hour);
- //printf("to a %s\n", a);
- str_copy(&name[++wsk], a);
- ile = obiekcik.date.tm_hour;
- wsk++;
- while ( ile / 10 > 0)
- {
- wsk++;
- ile = ile/10;
- }
- str_copy(&name[wsk], ".");
- sprintf(a, "%d", obiekcik.date.tm_min);
- //printf("to a %s\n", a);
- str_copy(&name[++wsk], a);
- ile = obiekcik.date.tm_min;
- wsk++;
- while ( ile / 10 > 0)
- {
- wsk++;
- ile = ile/10;
- }
- str_copy(&name[wsk], ".");
- str_copy(&name[++wsk], "txt");
- /*str_copy(name[5], itoa(obiekcik.date.tm_mon + 1));
- str_copy(name[7], ".");
- str_copy(name[, itoa(obiekcik.date.tm_mday));
- str_copy(name[, ".");
- str_copy(name, itoa(obiekcik.date.tm_hour));
- str_copy(name, ".");
- str_copy(name, itoa(obiekcik.date.tm_min));
- str_copy(name, ".");
- str_copy(name, "log");*/
- //printf("%s\n", name);
- handle = open(name, O_RDWR | O_CREAT, S_IRWXU);
- if(handle >= 0)
- {
- //read(0, &input, 1);
- lseek(handle, 0, SEEK_END);
- write(handle, &obiekcik.argument, str_len(obiekcik.argument));
- write(handle, "\n", sizeof(char));
- write(handle, &name, str_len(name) - 4);
- //write(handle, &obiekcik.date, sizeof(obiekcik.date));
- write(handle, "\n", sizeof(char));
- }
- close(handle);
- /*handle = open("msg.txt", O_RDWR | O_CREAT, S_IRWXU);
- if(handle >= 0)
- {
- read(0, &input, 1);
- write(handle, &input, sizeof(input));
- }
- close(handle);*/
- break;
- case 'r':
- break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement