Linux編程-回憶錄三

來源:互聯網
上載者:User

標籤:

====系統內容====
#include <unistd.h>
pid_t getpid(void);
得到當前進程的進程ID號,永遠成功

#include <stdlib.h>
char *getenv(const char *name);
得到給定環境變數名稱的值,如果沒有該變數,返回NULL值

#include <stdlib.h>
int putenv(char *string);
添加一個新的環境變數,傳入的字串格式為: "name=value"
傳入的string會成為系統內容變數的一部分,也就是說,string不能是局部變數
或者是自動變數,錯誤返回-1值,添加成功返回0

#include <stdlib.h>
int setenv(const char *name, const char *value, int overwrite);
作用同上,不過比putenv更安全,該函數會在環境變數表分配空間,添加該
環境變數,如果overwrite非0,已有同名的環境變數會被覆蓋,返回0表示
成功,-1表示錯誤

#include <stdlib.h>
int unsetenv(const char *name);
刪除一個環境變數,返回0表示刪除成功,-1表示刪除失敗,或者沒有該環境變數

====記憶體配置====
#include <stdlib.h>
void *malloc(size_t size);
分配size大小的記憶體,並返回指向該記憶體的地址指標,返回NULL表示分配出錯

#include <stdlib.h>
void free(void *ptr);
釋放記憶體,同一指標不能釋放兩次,否則段錯誤

include <stdlib.h>
void *calloc(size_t numitems, size_t size);
分配並返回numitems個size大小的連續記憶體,並清空記憶體,位元組全置
為0值,返回NULL表示分配出錯

include <stdlib.h>
void *realloc(void *ptr, size_t size);
重新設定已經分配的ptr記憶體的大小為size,返回新的記憶體位址
指標,返回NULL表示出錯

====使用者和組====
/etc/passwd     使用者密碼檔案
/etc/shadow     使用者影子密碼檔案
/etc/group      使用者分組資訊檔

#include <pwd.h>
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
使用使用者名稱或是使用者ID號擷取系統使用者的帳號資訊, 成功都返回
一個指標,返回NULL表示出錯,擷取的資訊來自/etc/passwd檔案

#include <grp.h>
struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);
使用組名,或是組ID號擷取系統使用者組的資訊,成功都返回一個
指標,返回NULL表示出錯

#include <pwd.h>
struct passwd *getpwent(void);
void setpwent(void);
void endpwent(void);
getpwent從/etc/passwd檔案中按序擷取使用者的資訊,成功返回一個
指標,返回NULL表示擷取完畢,需要迴圈調用該函數
getpwent擷取完畢後,調用endpwent函數關閉清理資源
setpwent函數用來重設當前的使用者資訊為檔案的第一條使用者記錄

#include <shadow.h>
struct spwd *getspnam(const char *name);
擷取一個賬戶的shadow密碼資訊

struct spwd *getspent(void);
void setspent(void);
void endspent(void);
作用同getpwent函數系列,一個個擷取賬戶的密碼資訊

#include <unistd.h>
uid_t getuid(void);
uid_t geteuid(void);
gid_t getgid(void);
gid_t getegid(void);
擷取賬戶的使用者ID,組ID,有效使用者ID,有效組ID
使用者ID是指運行該程式的實際擁有者
組ID是指運行該程式的實際使用者組
有效使用者ID是指執行一定許可權任務的使用者ID
有效組ID是指執行一定許可權任務的使用者組ID

include <unistd.h>
int setuid(uid_t uid);
int setgid(gid_t gid);
設定使用者ID/組ID,成功返回0,錯誤返回-1

#include <unistd.h>
int seteuid(uid_t euid);
int setegid(gid_t egid);
設定有效許可權的使用者ID/組ID,成功返回0,錯誤返回-1

 

Linux編程-回憶錄三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.