📄 global.cc
字号:
#include <config.h>#include <stdio.h>#include <unistd.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif /* HAVE_STDLIB_H */#include <errno.h>#include <ctype.h>#include <fcntl.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>/* Custom headerfiles */#include <pwd.h>/* External variables/functions *//* Function prototypes *//* Global variables */ /* get homedir of user 'username' (username == NULL => user running this prog) * it returns a malloc'd string which you should free(), or NULL if user * does not exist */char *get_homedir(const char *username){ uid_t userid = 0; int match_uid = 0; struct passwd *pwdentry; short foundluser = 0; char *homedir = NULL; if (!username) // this user's homedir { userid = getuid(); match_uid = 1; } setpwent(); //rewind passwd file while (!foundluser && (pwdentry = getpwent())) { if (match_uid) { if (userid == pwdentry->pw_uid) { foundluser = 1; homedir = strdup(pwdentry->pw_dir); } } else { if (!strcmp(username, pwdentry->pw_name)) { foundluser = 1; homedir = strdup(pwdentry->pw_dir); } } } endpwent(); if (!foundluser) return NULL; else return homedir;}/* returns a malloc()'d string */char *crop_whitespace(const char *string){ char *orgstring, *tmpstring, *newstring; int index = 0; if (!string) return NULL; tmpstring = strdup(string); orgstring = tmpstring; while (tmpstring[0] != '\0' && isspace((int)tmpstring[0])) tmpstring++; while ( (index = strlen(tmpstring)-1) > -1 && isspace((int)tmpstring[index])) tmpstring[index]='\0'; newstring = (char*)malloc((strlen(tmpstring)+1)*sizeof(char)); strcpy(newstring, tmpstring); free(orgstring); return newstring;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -