Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <ctype.h>
- #include <string.h>
- struct user_info {
- unsigned int uid;
- char* uname;
- };
- int main(int argc, char* argv[]){
- struct user_info* usernameId= malloc(sizeof(struct user_info));
- char * username = malloc(sizeof(char));
- char * password = malloc(sizeof(char));
- int UID;
- int GID;
- char * GECOS;
- char * directory;
- char * shell;
- int coloncount=0;
- //char kolon[5];
- char* test= malloc(1024*sizeof(char));
- int rows = 0;
- FILE * f;
- f=fopen(argv[1],"r");
- while( fgets(test,1023,f)!=NULL){
- rows++;
- readLine(test,rows);
- }
- fclose(f);
- return 0;
- }
- bool readLine(char* line, int rows){
- if(checkLine(line,rows)){
- sscanf(test,"%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]", username, password,
- &UID,&GID,GECOS,directory,shell);
- }
- else{
- fprintf(stderr,"ss");
- }
- return
- }
- bool checkLine(char* line, int rows){
- int i=0;
- int colonCount=0;
- if(line[0]=='\0'){
- fprintf(stderr,"%d: bajs", rows);
- return false;
- }
- while(line[i]!='\0'){
- if(line[i] == ':'){
- colonCount++;
- }
- i++
- }
- if(colonCount!=6){
- fprintf(stderr,"%d:", rows);
- return false;
- }
- return true;
- }
- void checkusername(char* line, int rows){
- int i=0;
- int colonCount=0;
- int usernameCount=0;
- while(line[i]!='\0'){
- if(colonCount==1){
- usernameCount= i-1;
- username=line[i-1];
- break;
- }
- if(line[i] == ':'){
- colonCount++;
- }
- i++
- }
- if(usernameCount==0){
- fprintf(stderr,"%d",rows);
- free(username);
- }
- else{
- if(usernameCount>=1||usernameCount<=32){
- //allokerar minne för strlen
- //sätt in
- usernameId->uname=username;
- free(username);
- }
- // om username är mer än 32 eller mindre än 0;
- else{
- fprintf(stderr"Error, detta handlar om usernames storlek");
- free(username);
- }
- }
- }
- void checkUid(char* UID, int rows){
- if (isdigit(UID)){
- //OM UID;
- if(UID>0){
- usernameId->uid=UID;
- free(UID);
- }
- else{
- fprintf(stderr,"UID has to be a positive number");
- free(UID);
- }
- }
- else{
- fprintf(stderr,"UID contains something else than intergers");
- free(UID);
- }
- }
- void checkGID(char* GID, int rows){
- //bool faileCheck = true;
- if(!isdigit(GID)){
- fprintf(stderr,"bajs");
- faileCheck = false;
- free(GID);
- }
- if(GID<0){
- fprintf(stderr,"GID has to be a positive number");
- faileCheck=false;
- free(GID);
- }
- //return faileCheck;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement