Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  RCH1
  4. //
  5. //  Created by George Huzum on 10/22/17.
  6. //  Copyright © 2017 George Huzum. All rights reserved.
  7. //
  8.  
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <signal.h>
  17.  
  18. char* isLoginValid(char *accountPasswordPair);
  19. int userLogIn();
  20. void displayPortal();
  21. int getInputCode(char *command);
  22. void printMyFind(char* fileName);
  23.  
  24. int main(int argc, const char * argv[]) {
  25.     displayPortal(0);
  26.    
  27.     return 0;
  28. }
  29.  
  30.  
  31. void displayPortal(int isUserLogged){
  32.     int sockp[2], child;
  33.     char msg[1024];
  34.    
  35.     int inputCode = 0;
  36.     char charInput[50];;
  37.     printf("Available commands are:login, myfile, mystat, quit:\n");
  38.     if(!scanf("%s",charInput)){
  39.         printf("You typed something wrong, bye!\n");
  40.         return;
  41.     };
  42.     inputCode = getInputCode(charInput);
  43.     switch(inputCode){
  44.         case 1:
  45.             if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0)
  46.             {
  47.                 perror("Err... socketpair");
  48.                 exit(1);
  49.             }
  50.             if ((child = fork()) == -1) perror("Err...fork");
  51.             else
  52.                 if (child)   //parinte
  53.                 {
  54.                     close(sockp[0]);
  55.                     char username[50];
  56.                     char password[50];
  57.                     printf("Insert username:");
  58.                     scanf("%49s",username);
  59.                     printf("Insert password:");
  60.                     scanf("%49s",password);
  61.                     char* accountPasswordPair=malloc(strlen(username)+strlen(password));
  62.                     char* dot=".";;
  63.                     strcpy(accountPasswordPair,username);
  64.                     strcat(accountPasswordPair,dot);
  65.                     strcat(accountPasswordPair,password);
  66.                     if (write(sockp[1], accountPasswordPair, strlen(accountPasswordPair)) < 0) perror("[parinte]Err...write");
  67.                     if (read(sockp[1], msg, 1024) < 0) perror("[copil]Err..read");
  68.                     close(sockp[1]);
  69.                     displayPortal(atoi(msg));
  70.                 }
  71.                 else     //copil
  72.                 {
  73.                     close(sockp[1]);
  74.                     if (read(sockp[0], msg, 1024) < 0) perror("[copil]Err..read");
  75.                     if(write(sockp[0],isLoginValid(msg),sizeof(int))<0) perror("[parinte]Err...write");
  76.                     close(sockp[0]);
  77.                 }
  78.             break;
  79.         case 2:
  80.             if(isUserLogged){
  81.                 printf("Command executed successfully!");
  82.             }else{
  83.                 printf("Can't use command myfile if you're not logged in!");
  84.             }
  85.             displayPortal(isUserLogged);
  86.             break;
  87.         case 3:
  88.             if(isUserLogged){
  89.                 printf("Command executed successfully!");
  90.             }else{
  91.                 printf("Can't use command mystat if you're not logged in!");
  92.             }
  93.             displayPortal(isUserLogged);
  94.             break;
  95.         case 4:
  96.             printf("Bye!");
  97.             exit(10);
  98.             break;
  99.         default:
  100.             printf("You typed something wrong, man...\n");
  101.             displayPortal(isUserLogged);
  102.             break;
  103.     }
  104. }
  105.  
  106.  
  107. char* isLoginValid(char *accountPasswordPair){
  108.    
  109.     FILE * file;
  110.     long size;
  111.     char* buffer;
  112.     size_t result;
  113.     file = fopen("/Users/georgehuzum/Desktop/RC/TEMA1/RCH1/RCH1/accounts.txt","rb");
  114.     if(file==NULL){
  115.         fputs("File error:",stderr);
  116.         exit(1);
  117.     }
  118.    
  119.     fseek(file,0,SEEK_END);
  120.     size=ftell(file);
  121.     rewind(file);
  122.    
  123.     buffer = (char*) malloc (sizeof(char)*size);
  124.     if(buffer==NULL){
  125.         fputs("Memory error",stderr);
  126.         exit(2);
  127.     }
  128.    
  129.     result=fread(buffer,1,size,file);
  130.    
  131.     if(result!=size){
  132.         fputs("Reading error:",stderr);
  133.         exit(3);
  134.     }
  135.    
  136.     if(strstr(buffer,accountPasswordPair)!=NULL){
  137.         printf("Login successfull!\n");
  138.         return "1";
  139.     }
  140.     printf("Login failed!\n");
  141.     return "0";
  142.    
  143. }
  144.  
  145. int getInputCode(char *command){
  146.     if(!strcmp(command,"login")){
  147.         return 1;
  148.     }
  149.     if(!strcmp(command,"myfile")){
  150.         return 2;
  151.     }
  152.     if(!strcmp(command,"mystat")){
  153.         return 3;
  154.     }
  155.     if(!strcmp(command,"quit")){
  156.         return 4;
  157.     }
  158.     return 0;
  159. }
  160.  
  161. void printMyFind(char* fileName){
  162.     struct stat fileStat;
  163.     if(stat("/Users/georgehuzum/desktop/rc/TEMA1/RCH1/RCH1/main.c",&fileStat)<0){
  164.         printf("Error getting stat!");
  165.         return;
  166.     }
  167.     printf("Information for %s:",fileName);
  168.     printf("File size: %lld bytes",fileStat.st_size);
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement