Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. int main(int argc, char** argv) {
  5.     int opt;
  6.     while((opt = getopt(argc, argv, ":h:m:s:")) != -1)  
  7.     {  
  8.         int num;
  9.         switch(opt)  
  10.         {  
  11.             case 'h':
  12.                 if (sscanf(optarg, "%2d", &num) != 1 || (num < 0 || num > 23)) {
  13.                     printf("rip hora\n");
  14.                     break;
  15.                 }
  16.                 printf("hour: %d\n", num);
  17.                 break;
  18.             case 'm':  
  19.                 printf("minute: %s\n", optarg);
  20.                 break;
  21.             case 's':
  22.                 printf("second: %s\n", optarg);
  23.                 break;
  24.             case ':':  
  25.                 printf("option needs a value\n");  
  26.                 break;  
  27.             case '?':  
  28.                 printf("unknown option: %c\n", optopt);
  29.                 break;  
  30.         }  
  31.     }
  32.    
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement