why segmentation fault
#include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * Create a new user with the given name.  Insert it at the tail of the list
 * of users whose head is pointed to by *user_ptr_add.
 *
 * Return:
 *   - 0 if successful
 *   - 1 if a user by this name already exists in this list
 *   - 2 if the given name cannot fit in the 'name' array
 *       (don't forget about the null terminator)
 */
int create_user(const char *name, User **user_ptr_add) {
    User *new_user = malloc(sizeof(User));
    if (new_user == NULL) {
        perror("malloc");
        exit(-1);
    }

    if (strlen(name) + 1 > MAX_NAME) {
        return 2;
    }
    strncpy(new_user->name, name, MAX_NAME);
    
    if(*user_ptr_add == NULL) {
        *user_ptr_add = new_user;
    }
    else{
        User *curr = *user_ptr_add; 

        while(curr->next != NULL) {
            if (strcmp(curr->name, name) == 0) {
                return 1;
            }

            curr = curr->next;
        }

        curr->next = new_user;    
    }

    return 0;
}


/*
 * Return a pointer to the user with this name in
 * the list starting with head. Return NULL if no such user exists.
 *
 * NOTE: You'll likely need to cast a (const User *) to a (User *)
 * to satisfy the prototype without warnings.
 */
User *find_user(const char *name, const User *head) {
    User *curr = (User *) head;

    if (curr != NULL) {
        while((strcmp(curr->name, name) != 0) && (curr != NULL)) {
            curr = curr->next;
        } 

        if (strcmp(curr->name, name) == 0) {
            return curr;
        }
    }

    return NULL;
}


/*
 * Print the usernames of all users in the list starting at curr.
 * Names should be printed to standard output, one per line.
 */
void list_users(const User *curr) {
    User *new_curr = (User *) curr;

    printf("User List\n");
    while(new_curr != NULL) {
        printf("\t%s\n", new_curr->name);
        new_curr = new_curr->next;
    }
}


/*
 * Change the filename for the profile pic of the given user.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if the file does not exist or cannot be opened.
 *   - 2 if the filename is too long.
 */
int update_pic(User *user, const char *filename) {
    if (strlen(filename) + 1 > MAX_NAME) {
        return 2;
    }

    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        return 1;
    }

    strncpy(user->profile_pic, filename, MAX_NAME);
    
    fclose(file);
    return 0;
}


/*
 * Make two users friends with each other.  This is symmetric - a pointer to
 * each user must be stored in the 'friends' array of the other.
 *
 * New friends must be added in the first empty spot in the 'friends' array.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if the two users are already friends.
 *   - 2 if the users are not already friends, but at least one already has
 *     MAX_FRIENDS friends.
 *   - 3 if the same user is passed in twice.
 *   - 4 if at least one user does not exist.
 *
 * Do not modify either user if the result is a failure.
 * NOTE: If multiple errors apply, return the *largest* error code that applies.
 */
int make_friends(const char *name1, const char *name2, User *head) {
    User *friend1 = find_user(name1, head);
    User *friend2 = find_user(name2, head);

    if ((friend1 == NULL) || friend2 == NULL) {
        return 4;
    }

    if (strcmp(name1, name2) == 0) {
        return 3;
    }

    int len_f1 = 0;
    int len_f2 = 0;

    for(int i = 0; i < MAX_FRIENDS; i++) {
        if (friend1->friends[i] == NULL) {
            break;
        }

        if (strcmp(friend1->friends[i]->name, name2) == 0) {
            return 1;
        }

        len_f1 += 1;
    }
    for(int i = 0; i < MAX_FRIENDS; i++) {
        if (friend2->friends[i] == NULL) {
            break;
        }

        len_f2 += 1;
    }

    if ((len_f1 == MAX_FRIENDS) || (len_f2 == MAX_FRIENDS)) {
        return 2;
    }

    friend1->friends[len_f1] = friend2;
    friend2->friends[len_f2] = friend1;

    return 0;
}


/*
 * Print a user profile.
 * For an example of the required output format, see the example output
 * linked from the handout.
 * Return:
 *   - 0 on success.
 *   - 1 if the user is NULL.
 */
int print_user(const User *user) {
    if (user != NULL) {
        // FILE *file = fopen(user->profile_pic, "r");
        // char *ch = malloc(sizeof(char));
        // while(fread(ch, sizeof(char), 1, file) != 0) {
        //     printf("%s", ch);
        // }

        printf("Name: %s\n", user->name);
        printf("------------------------------------------\n");

        printf("Friends:\n");
        for(int i = 0; i < MAX_FRIENDS; i++) {
            if (user->friends[i] == NULL) {
                break;
            }
            
            printf("%s\n", user->friends[i]->name);
        }
        printf("------------------------------------------\n");

        printf("Posts:\n");

        Post *curr = user->first_post;

        while(curr != NULL) {
            printf("From: %s\n", curr->author);
            printf("Date: %s\n", ctime(curr->date));
            printf("\n");
            printf("%s\n", curr->contents);

            if (curr->next == NULL) {
                break;
            }

            printf("\n");
            printf("===\n");
            printf("\n");

            curr = curr->next;
        }
        printf("------------------------------------------\n");

        return 0;
    }

    return 1;
}


/*
 * Make a new post from 'author' to the 'target' user,
 * containing the given contents, IF the users are friends.
 *
 * Insert the new post at the *front* of the user's list of posts.
 *
 * 'contents' is a pointer to heap-allocated memory - you do not need
 * to allocate more memory to store the contents of the post.
 *
 * Return:
 *   - 0 on success
 *   - 1 if users exist but are not friends
 *   - 2 if either User pointer is NULL
 */
int make_post(const User *author, User *target, char *contents) {
    if ((author == NULL) || (target == NULL)) {
        return 2;
    }

    int found = 0;
    for(int i = 0; i < MAX_FRIENDS; i++) {
        if (target->friends[i] == NULL) {
            break;
        }

        if (strcmp(target->friends[i]->name, author->name) == 0) {
            found = 1;
            break;
        }
    }
    if (found == 0) {
        return 1;
    }
    
    Post *new_post = malloc(sizeof(Post));
    if (new_post == NULL) {
        perror("malloc");
        exit(-1);
    }

    strncpy(new_post->author, author->name, MAX_NAME);

    new_post->contents = contents;

    time_t *curr_datetime = malloc(sizeof(time_t));
    if (curr_datetime == NULL) {
        perror("malloc");
        exit(-1);
    }
    
    *curr_datetime = time(NULL);
    new_post->date = curr_datetime;

    new_post->next = target->first_post;
    target->first_post = new_post;

    return 0;
}


/*
 * From the list pointed to by *user_ptr_del, delete the user
 * with the given name.
 * Remove the deleted user from any lists of friends.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if a user with this name does not exist.
 */
int delete_user(const char *name, User **user_ptr_del) {
    User *user = find_user(name, *user_ptr_del);

    if (user == NULL) {
        return 1;
    }

    User *curr = *user_ptr_del; 

    if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
        *user_ptr_del = curr->next;
    }
    else {
        while(strcmp(curr->next->name, name) != 0) {
            curr = curr->next;
        }

        curr->next = curr->next->next;
    }
    
    for (int i = 0; i < MAX_FRIENDS; i++) {
        User *curr_friend = user->friends[i];

        if (curr_friend == NULL) {
            break;
        }

        int index = 0;
        for(int j = 0; j < MAX_FRIENDS; j++) {
            
            if (strcmp(curr_friend->friends[j]->name, name) == 0) {
                index = j;
                break;
            }
        }

        for (int k = index; k + 1 < MAX_FRIENDS; k++) {
            curr_friend->friends[k] = curr_friend->friends[k+1];
        }

        if (index == MAX_FRIENDS - 1) {
            curr_friend->friends[index] = NULL;
        }
    }

    Post *curr_post = user->first_post;

    while(curr_post != NULL) {
        Post *p = curr_post;

        curr_post = curr_post->next;

        free(p->date);
        free(p->contents);
        free(p);
    }

    free(user);

    return 0;
}



#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"


/*
 * Print a formatted error message to stderr.
 */
void error(char *msg) {
    fprintf(stderr, "Error: %s\n", msg);
}

/*
 * Read and process commands
 * Return:  -1 for quit command
 *          0 otherwise
 */
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
    User *user_list = *user_list_ptr;

    if (cmd_argc <= 0) {
        return 0;
    } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
        return -1;
    } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
        switch (create_user(cmd_argv[1], user_list_ptr)) {
            case 1:
                error("user by this name already exists");
                break;
            case 2:
                error("username is too long");
                break;
        }
    } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
        list_users(user_list);
    } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
        User *user = find_user(cmd_argv[1], user_list);
        if (user == NULL) {
            error("user not found");
        } else {
            switch (update_pic(user, cmd_argv[2])) {
                case 1:
                    error("file not found");
                    break;
                case 2:
                    error("filename too long");
                    break;
            }
       }
    } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
        if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
            error("user by this name does not exist");
        }
    } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
        switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
            case 1:
                error("users are already friends");
                break;
            case 2:
                error("at least one user you entered has the max number of friends");
                break;
            case 3:
                error("you must enter two different users");
                break;
            case 4:
                error("at least one user you entered does not exist");
                break;
        }
    } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
        // first determine how long a string we need
        int space_needed = 0;
        for (int i = 3; i < cmd_argc; i++) {
            space_needed += strlen(cmd_argv[i]) + 1;
        }

        // allocate the space
        char *contents = malloc(space_needed);

        // copy in the bits to make a single string
        strcpy(contents, cmd_argv[3]);
        for (int i = 4; i < cmd_argc; i++) {
            strcat(contents, " ");
            strcat(contents, cmd_argv[i]);
        }

        User *author = find_user(cmd_argv[1], user_list);
        User *target = find_user(cmd_argv[2], user_list);
        switch (make_post(author, target, contents)) {
            case 1:
                error("the users are not friends");
                break;
            case 2:
                error("at least one user you entered does not exist");
                break;
        }
    } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
        User *user = find_user(cmd_argv[1], user_list);
        if (print_user(user) == 1) {
            error("user not found");
        }
    } else {
        error("Incorrect syntax");
    }
    return 0;
}


/*
 * Tokenize the string stored in cmd.
 * Return the number of tokens, and store the tokens in cmd_argv.
 */
int tokenize(char *cmd, char **cmd_argv) {
    int cmd_argc = 0;
    char *next_token = strtok(cmd, DELIM);
    while (next_token != NULL) {
        if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
            error("too many arguments!");
            cmd_argc = 0;
            break;
        }
        cmd_argv[cmd_argc] = next_token;
        cmd_argc++;
        next_token = strtok(NULL, DELIM);
    }

    return cmd_argc;
}


int main(int argc, char* argv[]) {
    int batch_mode = (argc == 2);
    char input[INPUT_BUFFER_SIZE];
    FILE *input_stream;

    // Create the heads of the empty data structure
    User *user_list = NULL;

    if (batch_mode) {
        input_stream = fopen(argv[1], "r");
        if (input_stream == NULL) {
            perror("Error opening file");
            exit(1);
        }
    } else {
        // interactive mode
        input_stream = stdin;
    }

    printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");

    while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
        // only echo the line in batch mode since in interactive mode the user
        // just typed the line
        if (batch_mode) {
            printf("%s", input);
        }

        char *cmd_argv[INPUT_ARG_MAX_NUM];
        int cmd_argc = tokenize(input, cmd_argv);

        if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
            break; // can only reach if quit command was entered
        }

        printf("> ");
    }

    if (batch_mode) {
        fclose(input_stream);
    }

    // Delete all users. This should free all heap memory that was allocated
    // during the run of the program. Uncomment after you've implemented
    // deleting users.
    printf("Freeing all heap space.\n");
    while (user_list != NULL) {
        delete_user(user_list->name, &user_list);
    }
    return 0;
 }

