Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // RCH1
- //
- // Created by George Huzum on 10/22/17.
- // Copyright © 2017 George Huzum. All rights reserved.
- //
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <signal.h>
- char* isLoginValid(char *accountPasswordPair);
- int userLogIn();
- void displayPortal();
- int getInputCode(char *command);
- void printMyFind(char* fileName);
- int main(int argc, const char * argv[]) {
- displayPortal(0);
- return 0;
- }
- void displayPortal(int isUserLogged){
- int sockp[2], child;
- char msg[1024];
- int inputCode = 0;
- char charInput[50];;
- printf("Available commands are:login, myfile, mystat, quit:\n");
- if(!scanf("%s",charInput)){
- printf("You typed something wrong, bye!\n");
- return;
- };
- inputCode = getInputCode(charInput);
- switch(inputCode){
- case 1:
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0)
- {
- perror("Err... socketpair");
- exit(1);
- }
- if ((child = fork()) == -1) perror("Err...fork");
- else
- if (child) //parinte
- {
- close(sockp[0]);
- char username[50];
- char password[50];
- printf("Insert username:");
- scanf("%49s",username);
- printf("Insert password:");
- scanf("%49s",password);
- char* accountPasswordPair=malloc(strlen(username)+strlen(password));
- char* dot=".";;
- strcpy(accountPasswordPair,username);
- strcat(accountPasswordPair,dot);
- strcat(accountPasswordPair,password);
- if (write(sockp[1], accountPasswordPair, strlen(accountPasswordPair)) < 0) perror("[parinte]Err...write");
- if (read(sockp[1], msg, 1024) < 0) perror("[copil]Err..read");
- close(sockp[1]);
- displayPortal(atoi(msg));
- }
- else //copil
- {
- close(sockp[1]);
- if (read(sockp[0], msg, 1024) < 0) perror("[copil]Err..read");
- if(write(sockp[0],isLoginValid(msg),sizeof(int))<0) perror("[parinte]Err...write");
- close(sockp[0]);
- }
- break;
- case 2:
- if(isUserLogged){
- printf("Command executed successfully!");
- }else{
- printf("Can't use command myfile if you're not logged in!");
- }
- displayPortal(isUserLogged);
- break;
- case 3:
- if(isUserLogged){
- printf("Command executed successfully!");
- }else{
- printf("Can't use command mystat if you're not logged in!");
- }
- displayPortal(isUserLogged);
- break;
- case 4:
- printf("Bye!");
- exit(10);
- break;
- default:
- printf("You typed something wrong, man...\n");
- displayPortal(isUserLogged);
- break;
- }
- }
- char* isLoginValid(char *accountPasswordPair){
- FILE * file;
- long size;
- char* buffer;
- size_t result;
- file = fopen("/Users/georgehuzum/Desktop/RC/TEMA1/RCH1/RCH1/accounts.txt","rb");
- if(file==NULL){
- fputs("File error:",stderr);
- exit(1);
- }
- fseek(file,0,SEEK_END);
- size=ftell(file);
- rewind(file);
- buffer = (char*) malloc (sizeof(char)*size);
- if(buffer==NULL){
- fputs("Memory error",stderr);
- exit(2);
- }
- result=fread(buffer,1,size,file);
- if(result!=size){
- fputs("Reading error:",stderr);
- exit(3);
- }
- if(strstr(buffer,accountPasswordPair)!=NULL){
- printf("Login successfull!\n");
- return "1";
- }
- printf("Login failed!\n");
- return "0";
- }
- int getInputCode(char *command){
- if(!strcmp(command,"login")){
- return 1;
- }
- if(!strcmp(command,"myfile")){
- return 2;
- }
- if(!strcmp(command,"mystat")){
- return 3;
- }
- if(!strcmp(command,"quit")){
- return 4;
- }
- return 0;
- }
- void printMyFind(char* fileName){
- struct stat fileStat;
- if(stat("/Users/georgehuzum/desktop/rc/TEMA1/RCH1/RCH1/main.c",&fileStat)<0){
- printf("Error getting stat!");
- return;
- }
- printf("Information for %s:",fileName);
- printf("File size: %lld bytes",fileStat.st_size);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement