Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <pthread.h>
- #include <semaphore.h> /* Semaphore */
- #define NR_THREADS 10
- //memset and calloc
- //This structure is used to store relevand information about the message
- typedef struct
- {
- char sender[30];
- char data[255];
- int method;
- } Message;
- Message message, help;
- //code from Ivett
- // 1- successul 0 -failed, alread exists
- char registerUser(char *userdata, char *username, char *password){
- // Open the file containing the user names and passwords
- FILE *fp;
- fp = fopen(userdata, "a+");
- if (fp == NULL)
- {
- printf("Couldn't open file\n");
- return 1;
- }
- // retains the user name read from the file
- char fusername[30];
- memset(&fusername[0], 0, sizeof(fusername));
- // retains the password read from the file
- char fpassword[255];
- memset(&fpassword[0], 0, sizeof(fpassword));
- while(!feof(fp)){
- // read the first line of the file
- fscanf(fp, "%s %s", fusername, fpassword);
- // a user with the chosen name already exists
- if(strcmp(username, fusername)==0)
- return '0';
- // jump to the next line
- fscanf(fp, "\n");
- }
- // if eof was reached and the chosen user name does not exist in the file, save the user name and password
- if(feof(fp)){
- // write user name and password to the file
- fprintf(fp, "%s %s\n", username, password);
- fclose(fp);
- // successful registration => return 1
- return '1';
- }
- }
- char loginUser(char *userdata, char *username, char *password){
- // Open the file containing the user names and passwords
- FILE *fp;
- fp = fopen (userdata, "r");
- if (fp == NULL)
- {
- printf ("open\n");
- return 2;
- }
- // retains the user name read from the file
- char fusername[30];
- memset(&fusername[0], 0, sizeof(fusername));
- // retains the password read from the file
- char fpassword[255];
- memset(&fpassword[0], 0, sizeof(fpassword));
- while(!feof(fp)){
- // read the first line of the file
- fscanf(fp, "%s %s", fusername, fpassword);
- // a user name exists in the file
- if(strcmp(username, fusername)==0)
- // check if the matching password corresponds
- if(strcmp(password, fpassword)==0)
- //successful login
- return '2';
- else
- // the given password is incorrect
- return '1';
- // jump to the next line
- fscanf(fp, "\n");
- }
- // eof was reached and the chosen user name does not exist in the file
- if(feof(fp)){
- fclose(fp);
- // user does not exist
- return '0';
- }
- }
- typedef struct {
- int fd;
- Message msg;
- } Thread_data;
- Thread_data d;
- sem_t mutex;
- int counter = 0;
- void* doSomething1(void* data){
- sem_wait(&mutex); /* down semaphore */
- counter++;
- printf("Some thread shit \n");
- sem_post(&mutex); /* up semaphore */
- pthread_exit(0);
- }
- void* doSomething(void* data){
- }
- int main(int argc, char *argv[])
- {
- //testing purpose
- char *path = "/home/nix/Desktop/dp2/me.data";
- // char *user = "nicu";
- // char *pass = "sore";
- // printf("%s %s %s \n", path, user, pass);
- // char c = loginUser( path, user, pass );
- // char x = registerUser(path, "someone122", "2hispassword");
- // printf("login %c\n", c);
- // c = loginUser( path, "newlines", "many" );
- // printf("login %c\n", c);
- // printf("register %c\n", x);
- int a = NULL;
- printf("%d\n", a);
- int err,i;
- pthread_t some[NR_THREADS];
- sem_init(&mutex, 0, 1);
- for (i=0; i<NR_THREADS;i++)
- {
- err = pthread_create(&(some[i]), NULL, (void *) &doSomething1, (void *) &d);
- if(err)
- {
- fprintf(stderr,"Error - pthread_create() return code: %d\n",err);
- exit(EXIT_FAILURE);
- }
- }
- for(i=0; i<NR_THREADS; i++)
- pthread_join(some[i],NULL);
- sem_destroy(&mutex); /* destroy semaphore */
- printf("counter is: %2d\n", counter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement