Alguém poderia me ajudar com este algoritmo? Em nosso trabalho da faculdade precisamos passar nosso programa C para portugol
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <openssl/sha.h>
#include <sys/types.h>
#include <sys/stat.h>
int OWNER_USER = 1;
char usuario[128];
struct Cadastro {
char login[128];
int id;
char hash[128];
char nome[128];
char cargo[128];
};
struct Estoque {
char produto[128];
int quantidade;
};
struct Loja {
char produto[128];
int quantidade;
};
void printCadastro(struct Cadastro *ptr)
{
printf("Nome: %s\n", ptr->nome);
printf("Id: %d\n", ptr->id);
printf("Hash: %s\n", ptr->hash);
printf("Nome: %s\n", ptr->nome);
printf("Cargo: %s\n", ptr->cargo);
}
int pegarTAM(FILE **fptr)
{
char *buffer = (char *) malloc(4096 * sizeof(char));
char *ch;
int TAM = 0;
fread(buffer, 1, 4096 * sizeof(char), *fptr);
rewind(*fptr);
ch = strtok(buffer, "\n");
while (ch != NULL) {
ch = strtok(NULL, "\n");
TAM++;
}
free(buffer);
return TAM;
}
void pagina_login(struct Cadastro *ptr, int TAM)
{
int login_flag = 0, owner_user = 1, t = 3, i = 0, j = 0, key = -1;
char login[128], senha[128];
unsigned char tmp_hash[SHA256_DIGEST_LENGTH];
char hex[SHA256_DIGEST_LENGTH * 2];
printf("------------------------------------------------\n"
"Login: ");
scanf("%128s", login);
if ((strcmp("owner", login) == 0)) {
owner_user = 0;
}
for (i = 0; i < TAM; ++i) {
if ((strcmp(login, (ptr + i)->login) == 0)) {
key = i;
break;
}
if ((strcmp(login, (ptr + i)->login) != 0)) {
if (i == TAM - 1) {
printf("Usuário não encontrado.\n");
printf("------------------------------------------------\n");
exit(EXIT_FAILURE);
}
}
}
do {
printf("Senha: ");
scanf("%128s", senha);
SHA256((const unsigned char *) senha, strlen(senha), tmp_hash);
for (i = 0, j = 0; i < SHA256_DIGEST_LENGTH; ++i, j += 2) {
sprintf((hex + j), "%02x", tmp_hash[i]);
}
login_flag = (strcmp((ptr + key)->hash, hex) == 0) ? 0 : 1;
if (login_flag == 1) {
printf("Senha Incorreta.\n");
}
else {
printf("Senha Correta.\n");
printf("------------------------------------------------\n");
strcpy(usuario, (ptr + key)->login);
if (owner_user == 0)
OWNER_USER = 0;
break;
}
t--;
} while (t > 0);
}
void cadastrar(FILE **fptr)
{
if (OWNER_USER != 0) {
printf( "\nOperação Negada.\n\n");
return;
}
int i = 0, j = 0;
char login[128], senha[128], nome[128], cargo[128], hex[64];
unsigned char hash[32];
fseek(*fptr, 0, SEEK_END);
srand(time(NULL));
printf("Cadastrando novo funcionário.\n");
printf("Novo login: ");
scanf("%128s", login);
printf("Nova senha: ");
scanf("%128s", senha);
printf("Nome:" );
scanf("%128s", nome);
printf("Cargo:" );
scanf("%128s", cargo);
fputs(login, *fptr);
fputc(':', *fptr);
fputc('1', *fptr);
fputc(':', *fptr);
SHA256(senha, strlen(senha), hash);
for (i = 0, j = 0; i < SHA256_DIGEST_LENGTH; ++i, j += 2) {
sprintf((hex + j), "%02x", hash[i]);
}
fputs(hex, *fptr);
fputc(':', *fptr);
fputs(nome, *fptr);
fputc(':', *fptr);
fputs(cargo, *fptr);
fputc('\n', *fptr);
rewind(*fptr);
}
void preencherCadastro(struct Cadastro *ptr, FILE **fptr, int TAM)
{
int i = 0;
char buffer[4096];
char *ch;
for (i = 0; i < TAM; ++i) {
fgets(buffer, sizeof(buffer), *fptr);
ch = strtok(buffer, ":");
strcpy((ptr + i)->login, ch);
ch = strtok(NULL, ":");
(ptr + i)->id = *ch - 48;
ch = strtok(NULL, ":");
strcpy((ptr + i)->hash, ch);
ch = strtok(NULL, ":");
strcpy((ptr + i)->nome, ch);
ch = strtok(NULL, ":");
strcpy((ptr + i)->cargo, ch);
}
}
void removerLoja(struct Loja *l_ptr)
{
int quantidade = 0, i = 0;
char produto[128];
printf("Digite a o produto e a quantidade a ser removida da loja.\n");
scanf("%128s", produto);
for (i = 0; i < 256; ++i) {
if (strcmp(produto, (l_ptr + i)->produto) == 0) {
scanf("%d", &quantidade);
if (quantidade > (l_ptr + i)->quantidade) {
printf("Operação não permitida.\n");
printf("Quantidade maior que a da loja (%d)\n",
(l_ptr + i)->quantidade);
return;
} else {
(l_ptr + i)->quantidade = (l_ptr + i)->quantidade - quantidade;
printf("Nova quantidade na loja de %d.\n", (l_ptr + i)->quantidade);
return;
}
}
}
printf("Produto não encontrado.\n");
}
void adicionarProdutoEstoque(struct Estoque *ptr)
{
int i = 0;
for (i = 0; i < 256; ++i) {
if ((ptr + i)->quantidade == 0) {
printf("Digite o nome do produto e sua quantidade.\n");
scanf("%128s", (ptr + i)->produto);
scanf("%d", &((ptr + i)->quantidade));
printf("%d produto(s) %s adicionados ao estoque.\n\n\n",
(ptr + i)->quantidade, (ptr + i)->produto);
return;
}
}
}
void adicionarLoja(struct Estoque *e_ptr, struct Loja *l_ptr)
{
char produto[128];
int quantidade, i = 0, j = 0;
printf("Produto que deseja adicionar a loja.\n");
scanf("%128s", produto);
for (i = 0; i < 256; ++i) {
if ((strcmp(produto, (e_ptr + i)->produto) == 0)) {
printf("Qual quantidade deseja adicionar a loja: ");
scanf("%d", &quantidade);
if (quantidade > (e_ptr + i)->quantidade) {
printf("Operação não permitida.\n");
printf("Quantidade maior que a do estoque (%d)\n",
(e_ptr + i)->quantidade);
return;
} else {
for (j = 0; j < 256; ++j) {
if ((l_ptr + j)->quantidade == 0) {
strcpy((l_ptr + j)->produto, produto);
(l_ptr + j)->quantidade = quantidade;
break;
}
}
printf("%d produtos de %s adicionados a loja.\n",
quantidade, (e_ptr + i)->produto);
(e_ptr + i)->quantidade = (e_ptr + i)->quantidade - quantidade;
printf("Quantidade restante no estoque: %d\n", (e_ptr + i)->quantidade);
return;
}
}
}
printf("Produto não encontrado no estoque.\n");
}
FILE **preencherArquivo(struct Cadastro *cadastro, FILE **fptr, char *argv)
{
FILE **f_ptr = (FILE **) malloc(sizeof(FILE *));
char zeros[128];
int i = 0;
memset(zeros, 0, 128 * sizeof(char));
*f_ptr = freopen(NULL, "w", *fptr);
for (i = 0; i < 3; ++i) {
if ((strcmp(zeros, (cadastro + i)->login) == 0)) {
continue;
} else {
fputs((cadastro + i)->login, *f_ptr);
fputc(':', *fptr);
fputc((cadastro + i)->id + 48, *f_ptr);
fputc(':', *fptr);
fputs((cadastro + i)->hash, *f_ptr);
fputc(':', *fptr);
fputs((cadastro + i)->nome, *f_ptr);
fputc(':', *fptr);
fputs((cadastro + i)->cargo, *f_ptr);
}
}
return f_ptr;
}
void removerUsuario(FILE **fptr, struct Cadastro *cadastro, char *argv)
{
if (OWNER_USER != 0) {
printf( "\nOperação Negada.\n\n");
return;
}
char login[128];
int i = 0;
printf("Digite o nome do login: ");
scanf("%128s", login);
for (i = 0; i < 3; ++i) {
printf("user: %s\n", (cadastro + i)->login);
if ((strcmp(login, (cadastro + i)->login) == 0)) {
memset((cadastro + i), 0, 128);
fptr = preencherArquivo(cadastro, fptr, argv);
return;
}
}
printf("Usuário não encontrado.\n");
}
void menu(FILE **fptr, struct Cadastro *cadastro, char *argv)
{
printf("Bem vindo usuário %s.\n", usuario);
struct Estoque *estoque = (struct Estoque *)
malloc(256 * sizeof(struct Estoque));
struct Loja *loja = (struct Loja *)
malloc(256 * sizeof(struct Loja));
int choice = 0;
printf("\n\n\n\n");
while (1) {
printf("Opções de usuário:\n");
printf("[1] Cadastrar novo funcionário.\n");
printf("[2] Remover funcionário.\n");
printf("[3] Adicionar produto ao estoque.\n");
printf("[4] Adicionar produto a loja.\n");
printf("[5] Remover produto da loja.\n");
printf("[*] Sair do Programa.\n");
scanf("%d", &choice);
#ifdef WINDOWS
system("cls");
#else
system("clear");
#endif
switch (choice) {
case 1:
cadastrar(fptr);
break;
case 2:
removerUsuario(fptr, cadastro, argv);
break;
case 3:
adicionarProdutoEstoque(estoque);
break;
case 4:
adicionarLoja(estoque, loja);
break;
case 5:
removerLoja(loja);
break;
default:
exit(EXIT_SUCCESS);
}
}
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Error in argc.\n");
exit(EXIT_FAILURE);
}
struct stat st;
if (stat(argv[1], &st)) {
fprintf(stderr, "Erro no arquivo.\n");
exit(EXIT_FAILURE);
}
FILE *fptr = fopen(argv[1], "r+");
int TAM = pegarTAM(&fptr);
struct Cadastro *cadastro = (struct Cadastro *) malloc(TAM * sizeof(struct Cadastro));
preencherCadastro(cadastro, &fptr, TAM);
pagina_login(&cadastro[0], TAM);
menu(&fptr, cadastro, argv[1]);
free(cadastro);
return 0;
}