naufalphew

Untitled

Apr 16th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.67 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 <time.h>
  11. #include <sys/wait.h>
  12. #include <dirent.h>
  13.  
  14. int main() {
  15.   pid_t pids, sids;        // Variabel untuk menyimpan PID
  16.  
  17.   pids = fork();     // Menyimpan PID dari Child Process
  18.  
  19.   /* Keluar saat fork gagal
  20.   * (nilai variabel pid < 0) */
  21.   if (pids < 0) {
  22.     exit(EXIT_FAILURE);
  23.   }
  24.  
  25.   /* Keluar saat fork berhasil
  26.   * (nilai variabel pid adalah PID dari child process) */
  27.   if (pids > 0) {
  28.     exit(EXIT_SUCCESS);
  29.   }
  30.  
  31.   umask(0);
  32.  
  33.   sids = setsid();
  34.   if (sids < 0) {
  35.     exit(EXIT_FAILURE);
  36.   }
  37.  
  38.   if ((chdir("/home/osd0081/Desktop/Sisop/soal-shift-sisop-modul-2-A06-2021/soal1")) < 0) {
  39.     exit(EXIT_FAILURE);
  40.   }
  41.  
  42.   close(STDIN_FILENO);
  43.   close(STDOUT_FILENO);
  44.   close(STDERR_FILENO);
  45.  
  46.   while (1) {
  47.     // Tulis program kalian di sini
  48.     time_t rawtime;
  49.     struct tm * timeinfo;
  50.     int i, stat;
  51.     pid_t pid;
  52.     time ( &rawtime );
  53.     timeinfo = localtime ( &rawtime );
  54.  
  55.     char link[3][100]={
  56.         "https://drive.google.com/uc?id=1ZG8nRBRPquhYXq_sISdsVcXx5VdEgi-J&export=download",
  57.         "https://drive.google.com/uc?id=1FsrAzb9B5ixooGUs0dGiBr-rC7TS9wTD&export=download",
  58.         "https://drive.google.com/uc?id=1ktjGgDkL0nNpY-vT7rT7O6ZI47Ke9xcp&export=download"
  59.     };
  60.     char rn[3][20]={
  61.         "Musyik.zip",
  62.         "Pyoto.zip",
  63.         "Fylm.zip"
  64.     };
  65.     char n1[3][20]={
  66.         "MUSIK",
  67.         "FOTO",
  68.         "FILM"
  69.     };
  70.     char n[3][20]={
  71.         "Musyik",
  72.         "Pyoto",
  73.         "Fylm"
  74.     };
  75.  
  76.     if(timeinfo->tm_mon == 3 && timeinfo->tm_mday== 9 && timeinfo->tm_hour== 16 && timeinfo->tm_min==22 && timeinfo->tm_sec==0){
  77.    
  78.         // komponen donwload
  79.  
  80.  
  81.  
  82.         DIR *dp;
  83.         struct dirent *ep;
  84.         char file[50];
  85.         char target[50];
  86.         char path[100];
  87.        
  88.         for (i=0; i<3; i++)
  89.         {
  90.             if ((pid = fork()) == 0)
  91.             {
  92.                 if ((pid = fork()) == 0)
  93.                 {    
  94.                     if((pid=fork())==0)
  95.                     {
  96.                         char *argv[] = {"mkdir",n[i], NULL};
  97.                         execv("/bin/mkdir", argv);
  98.                         sleep(10);
  99.                         exit(100 + i);
  100.                     }else{
  101.                         waitpid(pid,&stat,0);
  102.                         if (WIFEXITED(stat))
  103.                         {
  104.                             char *argv[] = {"wget", "--no-check-certificate",link[i],"-O",rn[i], NULL};
  105.                             execv("/usr/bin/wget", argv);
  106.                             sleep(10);
  107.                             exit(100 + i);
  108.                         }
  109.                     }
  110.                 }else{
  111.                     waitpid(pid,&stat,0);
  112.                     if (WIFEXITED(stat))
  113.                     {
  114.                         if((pid=fork())==0)
  115.                         {
  116.                             char *argv[] = {"unzip",rn[i], NULL};
  117.                             execv("/usr/bin/unzip", argv);
  118.                             sleep(10);
  119.                             exit(100 + i);
  120.                         }else{
  121.                             waitpid(pid,&stat,0);
  122.                             if (WIFEXITED(stat))
  123.                             {
  124.                                 getcwd(path, sizeof(path));
  125.                                 strcat(path,"/");
  126.                                 strcpy(target,"");
  127.                                 strcpy(target,path);
  128.                                 strcat(target,n[i]);
  129.                                 strcat(path,n1[i]);
  130.                                 dp = opendir(path);
  131.                                 while ((ep = readdir (dp))) {
  132.                                     strcpy(file,"");
  133.                                     strcpy(file,path);
  134.                                     if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0){
  135.                                         if ((pid = fork()) == 0)
  136.                                         {
  137.                                                 strcat(file,"/");
  138.                                                 strcat(file,ep->d_name);
  139.                                                 char *argv[] = {"mv",file, target, NULL};
  140.                                                 execv("/bin/mv", argv);      
  141.                                             sleep(10);
  142.                                             exit(100 + i);
  143.                                         }else{
  144.                                             waitpid(pid, &stat,0);
  145.                                         }
  146.                                     }
  147.                                 }
  148.                             }
  149.                         }
  150.                     }
  151.                 }
  152.             }else{
  153.                 waitpid(pid, &stat,0);
  154.             }
  155.         }
  156.  
  157.         if (WIFEXITED(stat))
  158.             printf("\ndone\n");
  159.    
  160.     }
  161.    
  162.     else if (timeinfo->tm_mon == 3 && timeinfo->tm_mday== 9 && timeinfo->tm_hour== 22 && timeinfo->tm_min==22 && timeinfo->tm_sec==0){
  163.  
  164.         // komponen zip
  165.        
  166.         if ((pid = fork()) == 0)
  167.         {    
  168.             char *argv[] = {"zip","-r","Lopyu_Stevany.zip",n[0],n[1],n[2], NULL};
  169.             execv("/usr/bin/zip", argv);
  170.             sleep(10);
  171.             exit(100 + i);
  172.         }else{
  173.             waitpid(pid,&stat,0);
  174.             if (WIFEXITED(stat)){
  175.                 char *argv[] = {"rm","-r",n[0],n[1],n[2],n1[0],n1[1],n1[2], NULL};
  176.                 execv("/bin/rm", argv);
  177.             }
  178.         }
  179.  
  180.     }
  181.   sleep(1);
  182.   }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment