Skip to content

Instantly share code, notes, and snippets.

@atamrawi
Created October 28, 2020 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atamrawi/38af7f0f75de6cd7d941e494ead791f1 to your computer and use it in GitHub Desktop.
Save atamrawi/38af7f0f75de6cd7d941e494ead791f1 to your computer and use it in GitHub Desktop.
Assignment 4 - Problem 3
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
struct auth {
char name[32];
int auth;
};
struct auth *authO;
char *service;
struct auth *authPtr;
int main(int argc, char **argv) {
char line[128];
int pos = 0;
while(1) {
printf("[ auth = %p, service = %p , authPtr = %p ]\n", authO, service, authPtr);
if(fgets(line, sizeof(line), stdin) == NULL) {
break;
}
if(strncmp(line, "auth ", 5) == 0) {
authO = malloc(sizeof(struct auth));
memset(authO, 0, sizeof(struct auth));
if(strlen(line + 5) < 31) {
strcpy(authO->name, line + 5);
}
}
if(strncmp(line, "reset", 5) == 0) {
free(authO);
}
if(strncmp(line, "alias", 5) == 0) {
authPtr = authO;
}
if(strncmp(line, "service", 6) == 0) {
if(service == NULL) {
service = malloc(sizeof(line));
memset(service, 0, sizeof(line));
}
if(strlen(line + 6) < 15) {
strcat(service, line + 6);
pos += strlen(line + 6);
}
}
if(strncmp(line, "login", 5) == 0) {
if(authPtr->auth) {
printf("you have logged in already!\n");
} else {
printf("please enter your password\n");
}
}
}
}
@atamrawi
Copy link
Author

atamrawi commented Oct 28, 2020

This is based on a Youtube video by liveoverflow channel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment