Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <fcntl.h>
  7.  
  8. /*
  9.  * -t float
  10.  * -s text
  11.  * -d int
  12.  **/
  13. char *str;
  14.  
  15. struct timespec t_spec;
  16.  
  17. void timeToStruct(float whole_secs){
  18.     int sec;
  19.     float nsec;
  20.     sec = whole_secs;
  21.     nsec = whole_secs - sec;
  22.  
  23.     t_spec.tv_sec = sec;
  24.     t_spec.tv_nsec = nsec*1000000000;
  25. }
  26.  
  27.  
  28. void parse_args(int argc, char **argv){
  29.     int opt, desc;
  30.     float timeArg;
  31.     char *descriptor;
  32.  
  33.     while((opt = getopt(argc, argv,"t:s:d:"))!= -1){
  34.         switch(opt){
  35.             case 't':
  36.                 timeArg = strtod(optarg, NULL);
  37.                 timeToStruct(timeArg);
  38.                 printf("%d %.9ld\n", t_spec.tv_sec, t_spec.tv_nsec);
  39.             break;
  40.             case 's':
  41.                 str = malloc(strlen(optarg));
  42.                 strcpy(str,optarg);
  43.                 for(int i = 0; i<strlen(optarg); i++){
  44.                     printf("%c", str[i]);
  45.                 }
  46.             break;
  47.             case 'd':
  48.                 descriptor = malloc(strlen(optarg));
  49.                 strcpy(descriptor, optarg);
  50.                 for(int counter = 0; counter<strlen(optarg); counter++){
  51.                     if(descriptor[counter] < '0' || descriptor[counter] > '9'){
  52.                         printf("not a number\n");
  53.                         exit(0);
  54.                     }
  55.                 }
  56.                 desc = atoi(descriptor);
  57.                 printf("%d\n", desc);
  58.             break;
  59.             default:
  60.             break;
  61.         }
  62.     }
  63. }
  64.  
  65. void progDeathRand(){
  66.     int r = 0;
  67.     srand(time(NULL));
  68.     while( 1 ){
  69.         r = rand()%100;
  70.         printf("sleep %d\n", r);
  71.         if(r < 50){
  72.             printf("should exit\n");
  73.             exit(0);
  74.         }
  75.         nanosleep(&t_spec, NULL);
  76.     }
  77. }
  78.  
  79.  
  80. void main(int argc, char **argv){
  81.     if(argc != 7){
  82.         printf("USAGE: %s -t <f> -s <string> -d <int[0-2]>\n",argv[0]);
  83.         exit(0);
  84.     }
  85.    
  86.     parse_args(argc, argv);
  87.     FILE* file;
  88.     file = fopen("./startup", "w+");
  89.     if(file == -1){
  90.         perror("open:");
  91.     }  
  92.     close(file);
  93.  
  94.     //progDeathRand()   ;
  95.  
  96. }
  97.  
  98.  
  99. /*  if(pipe(pipefd) == -1){
  100.         perror("pipe");
  101.         exit(0);
  102.     }
  103.  
  104.     c_pid = fork();
  105.     if(c_pid == 0){
  106.         close(pipefd[0]);
  107.         while(write(pipefd[1], str, sizeof(str))){
  108.             nanosleep(&t_spec, NULL);
  109.         }
  110.     }
  111.     if(c_pid < 0){
  112.         perror("fork:");
  113.         exit(0);
  114.     }
  115.     else{
  116.         close(pipefd[1]);
  117.         while(read(pipefd[0], readBuffer, sizeof(readBuffer))){
  118.             printf("Received string: %s", readBuffer);
  119.         }
  120.     }
  121.  
  122.     //
  123.  
  124.  
  125. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement