Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //user_ops.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "user_ops.h"
- #include <sys/types.h>
- size_t strcpy_safe(char* dst, const char* src, size_t size) {
- char *d = dst;
- const char *s = src;
- size_t n = size;
- /* Copy as many bytes as will fit */
- if (n != 0 && --n != 0) {
- do {
- if ((*d++ = *s++) == 0)
- break;
- } while (--n != 0);
- }
- /* Not enough room in dst, add NUL and traverse rest of src */
- if (n == 0) {
- if (size != 0)
- *d = '\0'; /* NUL-terminate dst */
- while (*s++)
- ;
- }
- return(s - src - 1); /* count does not include NUL */
- }
- userdata* create_user(const char* name, const char* password, int ispremium) {
- userdata *user;
- if(name == NULL || password == NULL) return NULL;
- if (strlen(name) > 15 || strlen(password) > 15) {return NULL;}
- user = calloc(1,sizeof(userdata));
- user->ispremium = ispremium;
- strcpy_safe(user->username,name, 16);
- strcpy_safe(user->password,password, 16);
- return user;
- }
- //user_ops.h
- typedef struct userdata {
- char username[16];
- int ispremium;
- char password[16];
- } userdata;
- size_t strcpy_safe(char* dst, const char* src, size_t size);
- userdata* create_user(const char* name, const char* password, int ispremium);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement