Advertisement
Guest User

sebi4

a guest
Mar 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <pthread.h>
  10. #include <semaphore.h> /* Semaphore */
  11. #define NR_THREADS 10
  12. //memset and calloc
  13. //This structure is used to store relevand information about the message
  14. typedef struct
  15. {
  16. char sender[30];
  17. char data[255];
  18. int method;
  19. } Message;
  20.  
  21. Message message, help;
  22.  
  23. //code from Ivett
  24.  
  25. // 1- successul 0 -failed, alread exists
  26. char registerUser(char *userdata, char *username, char *password){
  27.  
  28. // Open the file containing the user names and passwords
  29. FILE *fp;
  30. fp = fopen(userdata, "a+");
  31. if (fp == NULL)
  32. {
  33. printf("Couldn't open file\n");
  34. return 1;
  35. }
  36. // retains the user name read from the file
  37. char fusername[30];
  38. memset(&fusername[0], 0, sizeof(fusername));
  39. // retains the password read from the file
  40. char fpassword[255];
  41. memset(&fpassword[0], 0, sizeof(fpassword));
  42.  
  43. while(!feof(fp)){
  44. // read the first line of the file
  45. fscanf(fp, "%s %s", fusername, fpassword);
  46. // a user with the chosen name already exists
  47. if(strcmp(username, fusername)==0)
  48. return '0';
  49. // jump to the next line
  50. fscanf(fp, "\n");
  51. }
  52.  
  53. // if eof was reached and the chosen user name does not exist in the file, save the user name and password
  54. if(feof(fp)){
  55. // write user name and password to the file
  56. fprintf(fp, "%s %s\n", username, password);
  57. fclose(fp);
  58. // successful registration => return 1
  59. return '1';
  60. }
  61. }
  62.  
  63. char loginUser(char *userdata, char *username, char *password){
  64. // Open the file containing the user names and passwords
  65.  
  66. FILE *fp;
  67. fp = fopen (userdata, "r");
  68. if (fp == NULL)
  69. {
  70. printf ("open\n");
  71. return 2;
  72. }
  73. // retains the user name read from the file
  74. char fusername[30];
  75. memset(&fusername[0], 0, sizeof(fusername));
  76. // retains the password read from the file
  77. char fpassword[255];
  78. memset(&fpassword[0], 0, sizeof(fpassword));
  79.  
  80.  
  81. while(!feof(fp)){
  82. // read the first line of the file
  83. fscanf(fp, "%s %s", fusername, fpassword);
  84.  
  85. // a user name exists in the file
  86. if(strcmp(username, fusername)==0)
  87. // check if the matching password corresponds
  88. if(strcmp(password, fpassword)==0)
  89. //successful login
  90. return '2';
  91. else
  92. // the given password is incorrect
  93. return '1';
  94. // jump to the next line
  95. fscanf(fp, "\n");
  96. }
  97. // eof was reached and the chosen user name does not exist in the file
  98. if(feof(fp)){
  99. fclose(fp);
  100. // user does not exist
  101. return '0';
  102. }
  103. }
  104.  
  105. typedef struct {
  106. int fd;
  107. Message msg;
  108. } Thread_data;
  109.  
  110. Thread_data d;
  111. sem_t mutex;
  112. int counter = 0;
  113.  
  114. void* doSomething1(void* data){
  115. sem_wait(&mutex); /* down semaphore */
  116. counter++;
  117. printf("Some thread shit \n");
  118. sem_post(&mutex); /* up semaphore */
  119. pthread_exit(0);
  120. }
  121.  
  122. void* doSomething(void* data){
  123.  
  124. }
  125.  
  126. int main(int argc, char *argv[])
  127. {
  128. //testing purpose
  129. char *path = "/home/nix/Desktop/dp2/me.data";
  130. // char *user = "nicu";
  131. // char *pass = "sore";
  132. // printf("%s %s %s \n", path, user, pass);
  133. // char c = loginUser( path, user, pass );
  134. // char x = registerUser(path, "someone122", "2hispassword");
  135. // printf("login %c\n", c);
  136. // c = loginUser( path, "newlines", "many" );
  137. // printf("login %c\n", c);
  138. // printf("register %c\n", x);
  139. int a = NULL;
  140. printf("%d\n", a);
  141. int err,i;
  142. pthread_t some[NR_THREADS];
  143. sem_init(&mutex, 0, 1);
  144.  
  145. for (i=0; i<NR_THREADS;i++)
  146. {
  147. err = pthread_create(&(some[i]), NULL, (void *) &doSomething1, (void *) &d);
  148. if(err)
  149. {
  150. fprintf(stderr,"Error - pthread_create() return code: %d\n",err);
  151. exit(EXIT_FAILURE);
  152. }
  153. }
  154.  
  155. for(i=0; i<NR_THREADS; i++)
  156. pthread_join(some[i],NULL);
  157.  
  158.  
  159. sem_destroy(&mutex); /* destroy semaphore */
  160. printf("counter is: %2d\n", counter);
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement