# Pastebin fXAwFgdM #include #include #include #include #include void home(char*); int main() { FILE *fp; char homedir[255], buffer[255]; char line[128]; home(homedir); strcpy(buffer, homedir); strcat(buffer, "/file.txt"); fp = fopen(buffer, "r"); if (!fp) { switch (errno) { case ENOENT: { perror("File not found"); fp = fopen(buffer, "w+"); if (!fp) { perror("Failed to create file"); return EXIT_FAILURE; } /* write default file contents here */ fprintf(fp, "SHAREDIR = ~/share\n"); fprintf(fp, "LOGFILE = ~/file.log\n"); fprintf(fp, "MOTD = Message of the day\n"); /* LOG THIS! */ break; } default: perror("Failed to open file"); return EXIT_FAILURE; } } /* read existing config here */ while(fgets(line, 128, fp)!=NULL) { /* do something with the line that has been read */ printf("%s" , line); } fclose(fp); return EXIT_SUCCESS; } void home(char *homedir) { struct passwd *user; uid_t userid; strcpy(homedir, getenv("HOME")); if (homedir) { return; } else { userid = getuid(); user = getpwuid(userid); if (user) { strcpy(homedir, user->pw_dir); return; } else { return; } } }