Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <syslog.h>
  9. #include <string.h>
  10. #include <sys/wait.h>
  11. #include <time.h>
  12. #include <signal.h>
  13. #include <assert.h>
  14. #include <stdbool.h> /* false */
  15.  
  16.  
  17. #define CET 2
  18. struct tm* getTime(){
  19. time_t seconds;
  20. time (&seconds);
  21. struct tm *ptm = gmtime(&seconds);
  22. return ptm;
  23. }
  24. //----------------------------------------------------------------zmienna globalna xd
  25. int row = 0;
  26.  
  27. int getTaskSeconds(char* buffer){
  28. int hour = (buffer[0]%48)*10 + (buffer[1]%48);
  29. int minute = (buffer[3]%48)*10 + (buffer[4]%48);
  30. return 60*minute + hour*60*60;
  31. }
  32.  
  33.  
  34. void doTask(char* buffline, char* outfile){
  35. if(buffline == NULL) return;
  36. pid_t pid, status;
  37. pid = fork();
  38. if(pid == (pid_t) 0){
  39. FILE *file = fopen(outfile, "a");
  40. char *command = (char*)calloc((strlen(buffline)-8),sizeof(char));
  41. memmove(command, buffline+6, (strlen(buffline)-9));
  42. command[strlen(command)]='\0';
  43. fprintf(file, ">>%s\n", command);
  44. fflush(file);
  45. fclose(file);
  46. char mode = buffline[strlen(buffline)-2];
  47. char *arg[]={"pipe",command, outfile, &mode, NULL};
  48. //printf("mincron: %s %s %c\n",command, outfile, mode);
  49. execvp("./pipe", arg);
  50. }
  51. else {
  52. waitpid(pid, &status, 0);
  53. exit(0);
  54. }
  55. }
  56.  
  57. char* getNextTask(char *taskfile){
  58. /*
  59. &buffer is the address of the first character position where the input string will be stored. It’s not the base address of the buffer, but of the first character in the buffer. This pointer type (a pointer-pointer or the ** thing) causes massive confusion.
  60.  
  61. &buf_size is the address of the variable that holds the size of the input buffer, another pointer.
  62.  
  63. stream is the input file handle. So you could use getline() to read a line of text from a file, but when stdin is specified, standard input is read.
  64. */
  65. //printf("%p\n",(void *)stream);
  66. FILE *stream = fopen(taskfile, "r");
  67. if(stream == NULL)
  68. perror("Blad otwarcia pliku");
  69.  
  70. char* buffer = (char*)calloc(sizeof(char),1024);
  71. size_t buf_size = 128;
  72. int j = 0;
  73. while(!feof (stream) && !ferror (stream) && getline(&buffer,&buf_size,stream)!=EOF){
  74. if(j++ >= row){
  75. row = j;
  76. fclose(stream);
  77. //printf("%s",buffer);
  78. return buffer;
  79. }
  80. }
  81. return NULL;
  82. }
  83.  
  84. void writeFromFile (FILE** stream, char* filename) {
  85. FILE *file = fopen(filename, "r");
  86. if(file == NULL){
  87. perror("Blad otwarcia pliku");
  88. return;
  89. }
  90. //printf("writeFromFile--> %s\n", filename);
  91. char c = getc(file);
  92. while (c != EOF){
  93. // printf("%c", c);
  94. fprintf(*stream, "%c", c);
  95. fflush(*stream);
  96. c = getc(file);
  97. }
  98. fclose(file);
  99. }
  100.  
  101. void sortTaskFile(char* taskfile){
  102. pid_t pid, status;
  103. int fds[2];
  104. pipe(fds);
  105. pid = fork();
  106. if (pid == (pid_t) 0) { //child
  107. dup2(fds[0], STDIN_FILENO);
  108. int fd = open(taskfile, O_WRONLY);
  109. dup2(fd, STDOUT_FILENO);
  110. close(fds[0]);
  111. close(fds[1]);
  112. execlp("sort", "sort", (char* )0);
  113. }
  114. else { //parent
  115. FILE* stream;
  116. close(fds[0]);
  117. stream = fdopen(fds[1], "w");
  118. writeFromFile(&stream, taskfile);
  119. close(fds[1]);
  120. waitpid(pid, &status, 0);
  121. fclose(stream);
  122. }
  123. FILE *main_file = fopen(taskfile, "r");
  124. FILE *extra_file = fopen("extra.txt", "w");
  125.  
  126. struct tm *ptm = getTime();
  127. int act_seconds = ptm->tm_sec + ptm->tm_min*60 + ((ptm->tm_hour+CET)%24)*60*60;
  128. char* buffer = (char*)calloc(sizeof(char),1024);
  129. size_t buf_size = 128;
  130. while(!feof (main_file) && !ferror (main_file) && getline(&buffer,&buf_size,main_file)!=EOF){
  131. int task_seconds = getTaskSeconds(buffer);
  132. if(task_seconds > act_seconds){
  133. fprintf(extra_file,"%s",buffer);
  134. fflush(extra_file);
  135. }
  136. }
  137. fclose(extra_file);
  138. fclose(main_file);
  139. main_file = fopen(taskfile, "r");
  140. extra_file = fopen("extra.txt", "a");
  141. while(!feof (main_file) && !ferror (main_file) && getline(&buffer,&buf_size,main_file)!=EOF){
  142. int task_seconds = getTaskSeconds(buffer);
  143. if(task_seconds < act_seconds){
  144. fprintf(extra_file,"%s",buffer);
  145. fflush(extra_file);
  146. }
  147. }
  148. fclose(main_file);
  149. remove(taskfile);
  150. rename("extra.txt", taskfile);
  151. fclose(extra_file);
  152. }
  153.  
  154. void sleepIfNeeded(char* buffer){
  155. if(buffer == NULL) return;
  156. int task_seconds = getTaskSeconds(buffer);
  157. struct tm *ptm = getTime();
  158. int act_seconds = ptm->tm_sec + ptm->tm_min*60 + ((ptm->tm_hour+CET)%24)*60*60;
  159. if(act_seconds != task_seconds){
  160. //printf("godz:%d:%d:%d, odliczanie->%d\n",(ptm->tm_hour+CET)%24, ptm->tm_min, ptm->tm_sec, (task_seconds - act_seconds));
  161. if(task_seconds - act_seconds < 0){
  162. //sleep(24*60*60 - act_seconds + task_seconds);
  163. }
  164. //sleep(task_seconds - act_seconds);
  165. sleep(10);
  166. }
  167. }
  168.  
  169. int main(int argc, char* argv[]) {
  170. /* Our process ID and Session ID */
  171. pid_t pid, sid, status;
  172. char* taskfile = argv[1];
  173. char* outfile = argv[2];
  174.  
  175. sortTaskFile(taskfile);
  176. //-------------inicjalizacja DEMONA-------------------------------
  177.  
  178. /* Fork off the parent process */
  179. pid = fork();
  180. if(pid < 0) {
  181. exit(EXIT_FAILURE);
  182. }
  183. /* If we got a good PID, then
  184. we can exit the parent process. */
  185. if(pid > 0) {
  186. exit(EXIT_SUCCESS);
  187. }
  188. /* Change the file mode mask */
  189. umask(0);
  190. /* Open any logs here */
  191.  
  192. /* Create a new SID for the child process */
  193. sid = setsid();
  194. if(sid < 0) {
  195. /* Log the failure */
  196. exit(EXIT_FAILURE);
  197. }
  198. /* Change the current working directory */
  199. /*if ((chdir("/")) < 0) {
  200. //Log the failure
  201. exit(EXIT_FAILURE);
  202. }
  203. /* Close out the standard file descriptors */
  204. close(STDIN_FILENO);
  205. //close(STDOUT_FILENO);
  206. close(STDERR_FILENO);
  207. /*-------------------koniec inicjalizacji !!----------------------------------*/
  208.  
  209. /* Daemon-specific initialization goes here */
  210. printf("AAAAAAA\n");
  211. sortTaskFile(taskfile);
  212. printf("BBBBBBB\n");
  213.  
  214. char* buffer = NULL;
  215. /* The Big Loop */
  216.  
  217. while (1) {
  218. buffer = getNextTask(taskfile);
  219. if(buffer == NULL){
  220. printf("Koniec listy zadan\n");
  221. break;
  222. }
  223. //sleepIfNeeded(buffer);
  224. pid = fork();
  225. if(pid==(pid_t)0){
  226. printf("%s",buffer);
  227. doTask(buffer, outfile);
  228. exit(0);
  229. }
  230. else {
  231. waitpid(pid, NULL, 0);
  232. }
  233. }
  234. exit(EXIT_SUCCESS);
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement