Advertisement
Guest User

Untitled

a guest
Sep 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. struct user_info {
  7. unsigned int uid;
  8. char* uname;
  9. };
  10.  
  11. int main(int argc, char* argv[]){
  12.  
  13. struct user_info* usernameId= malloc(sizeof(struct user_info));
  14. char * username = malloc(sizeof(char));
  15. char * password = malloc(sizeof(char));
  16. int UID;
  17. int GID;
  18. char * GECOS;
  19. char * directory;
  20. char * shell;
  21. int coloncount=0;
  22. //char kolon[5];
  23. char* test= malloc(1024*sizeof(char));
  24. int rows = 0;
  25.  
  26. FILE * f;
  27.  
  28. f=fopen(argv[1],"r");
  29.  
  30. while( fgets(test,1023,f)!=NULL){
  31. rows++;
  32. readLine(test,rows);
  33. }
  34.  
  35. fclose(f);
  36. return 0;
  37.  
  38. }
  39. bool readLine(char* line, int rows){
  40. if(checkLine(line,rows)){
  41. sscanf(test,"%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]", username, password,
  42. &UID,&GID,GECOS,directory,shell);
  43. }
  44. else{
  45. fprintf(stderr,"ss");
  46. }
  47. return
  48.  
  49. }
  50. bool checkLine(char* line, int rows){
  51. int i=0;
  52. int colonCount=0;
  53. if(line[0]=='\0'){
  54.  
  55. fprintf(stderr,"%d: bajs", rows);
  56. return false;
  57. }
  58. while(line[i]!='\0'){
  59. if(line[i] == ':'){
  60. colonCount++;
  61. }
  62. i++
  63. }
  64. if(colonCount!=6){
  65. fprintf(stderr,"%d:", rows);
  66. return false;
  67. }
  68. return true;
  69. }
  70. void checkusername(char* line, int rows){
  71. int i=0;
  72. int colonCount=0;
  73. int usernameCount=0;
  74. while(line[i]!='\0'){
  75. if(colonCount==1){
  76. usernameCount= i-1;
  77. username=line[i-1];
  78. break;
  79. }
  80. if(line[i] == ':'){
  81. colonCount++;
  82. }
  83. i++
  84. }
  85.  
  86. if(usernameCount==0){
  87. fprintf(stderr,"%d",rows);
  88. free(username);
  89. }
  90. else{
  91. if(usernameCount>=1||usernameCount<=32){
  92. //allokerar minne för strlen
  93. //sätt in
  94. usernameId->uname=username;
  95. free(username);
  96. }
  97. // om username är mer än 32 eller mindre än 0;
  98. else{
  99. fprintf(stderr"Error, detta handlar om usernames storlek");
  100. free(username);
  101. }
  102. }
  103. }
  104. void checkUid(char* UID, int rows){
  105.  
  106. if (isdigit(UID)){
  107. //OM UID;
  108. if(UID>0){
  109. usernameId->uid=UID;
  110. free(UID);
  111. }
  112. else{
  113. fprintf(stderr,"UID has to be a positive number");
  114. free(UID);
  115. }
  116. }
  117. else{
  118. fprintf(stderr,"UID contains something else than intergers");
  119. free(UID);
  120. }
  121.  
  122. }
  123. void checkGID(char* GID, int rows){
  124. //bool faileCheck = true;
  125. if(!isdigit(GID)){
  126. fprintf(stderr,"bajs");
  127. faileCheck = false;
  128. free(GID);
  129. }
  130. if(GID<0){
  131. fprintf(stderr,"GID has to be a positive number");
  132. faileCheck=false;
  133. free(GID);
  134. }
  135.  
  136. //return faileCheck;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement