Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <fcntl.h>
- /*
- * -t float
- * -s text
- * -d int
- **/
- char *str;
- struct timespec t_spec;
- void timeToStruct(float whole_secs){
- int sec;
- float nsec;
- sec = whole_secs;
- nsec = whole_secs - sec;
- t_spec.tv_sec = sec;
- t_spec.tv_nsec = nsec*1000000000;
- }
- void parse_args(int argc, char **argv){
- int opt, desc;
- float timeArg;
- char *descriptor;
- while((opt = getopt(argc, argv,"t:s:d:"))!= -1){
- switch(opt){
- case 't':
- timeArg = strtod(optarg, NULL);
- timeToStruct(timeArg);
- printf("%d %.9ld\n", t_spec.tv_sec, t_spec.tv_nsec);
- break;
- case 's':
- str = malloc(strlen(optarg));
- strcpy(str,optarg);
- for(int i = 0; i<strlen(optarg); i++){
- printf("%c", str[i]);
- }
- break;
- case 'd':
- descriptor = malloc(strlen(optarg));
- strcpy(descriptor, optarg);
- for(int counter = 0; counter<strlen(optarg); counter++){
- if(descriptor[counter] < '0' || descriptor[counter] > '9'){
- printf("not a number\n");
- exit(0);
- }
- }
- desc = atoi(descriptor);
- printf("%d\n", desc);
- break;
- default:
- break;
- }
- }
- }
- void progDeathRand(){
- int r = 0;
- srand(time(NULL));
- while( 1 ){
- r = rand()%100;
- printf("sleep %d\n", r);
- if(r < 50){
- printf("should exit\n");
- exit(0);
- }
- nanosleep(&t_spec, NULL);
- }
- }
- void main(int argc, char **argv){
- if(argc != 7){
- printf("USAGE: %s -t <f> -s <string> -d <int[0-2]>\n",argv[0]);
- exit(0);
- }
- parse_args(argc, argv);
- FILE* file;
- file = fopen("./startup", "w+");
- if(file == -1){
- perror("open:");
- }
- close(file);
- //progDeathRand() ;
- }
- /* if(pipe(pipefd) == -1){
- perror("pipe");
- exit(0);
- }
- c_pid = fork();
- if(c_pid == 0){
- close(pipefd[0]);
- while(write(pipefd[1], str, sizeof(str))){
- nanosleep(&t_spec, NULL);
- }
- }
- if(c_pid < 0){
- perror("fork:");
- exit(0);
- }
- else{
- close(pipefd[1]);
- while(read(pipefd[0], readBuffer, sizeof(readBuffer))){
- printf("Received string: %s", readBuffer);
- }
- }
- //
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement