免费看欧美黑人毛片-免费看毛片网站-免费看毛片的网站-免费看久久-中文字幕日韩欧美-中文字幕日韩精品一区

您現(xiàn)在的位置:首頁 > IT認(rèn)證 > Linux認(rèn)證 >

unix環(huán)境高級編程


每個進(jìn)程都有一個當(dāng)前的工作目錄,此目錄是搜索所有相對路徑的起點(diǎn)。

  進(jìn)程通過調(diào)用chdir或fcdir函數(shù)可以更改當(dāng)前工作目錄。

  進(jìn)程通過調(diào)用chdir或者fchdir函數(shù)可以更改當(dāng)前工作目錄。

  view plain #include int chdir(const char* pathname);int fchdir(int filedes);

  這兩個函數(shù)分別用文件名和文件描述符來制定新的工作目錄。

  先查看GNU C手冊。

  int chdir (const char *filename) [Function] This function is used to set the process‘s working directory to filename. The normal, successful return value from chdir is 0. A value of -1 is returned to indicate an error. The errno error conditions defined for this function are the usual file name syntax errors (see Section 11.2.3 [File Name Errors], page 224), plus ENOTDIR if the file filename is not a directory. int fchdir (int filedes) [Function] This function is used to set the process’s working directory to directory associated with the file descriptor filedes. The normal, successful return value from fchdir is 0. A value of -1 is returned to indicate an error. The following errno error conditions are defined for this function:EACCES Read permission is denied for the directory named by dirname. EBADF The filedes argument is not a valid file descriptor. ENOTDIR The file descriptor filedes is not associated with a directory. EINTR The function call was interrupt by a signal.

  實(shí)例;

  view plain #include"apue.h"

  int main(void)

  { if(chdir("/devis/wangchenglin")<0)

  err_sys("chdir failed");printf("chdir to /devis/wangchenglin succeededn");exit(0);

  }

  運(yùn)行結(jié)果:

  看到并沒有改變工作目錄,這是因?yàn)閟hell創(chuàng)建了一個子進(jìn)程,又該自己子進(jìn)程執(zhí)行這個命令。可知,為了改變shell進(jìn)程自己的工作目錄,shell應(yīng)該直接調(diào)用chdir函數(shù),為此cd命令的執(zhí)行程序直接包含在shell程序中。

  函數(shù)getcwd這樣的功能,它從當(dāng)前工作目錄(。目錄項(xiàng)開始),用……目錄項(xiàng)找到其上一級的目錄,然后賭氣目錄項(xiàng),知道該目錄項(xiàng)中的i節(jié)點(diǎn)編號與工作目錄i節(jié)點(diǎn)編號相同,這就就找到了其對應(yīng)文件名。按照這種方法,組成上一,直到遇到跟。

  view plain #include

  char *getcwd(char* buf,size_t size);

  向此函數(shù)傳遞兩個參數(shù),一個是緩沖地址buf,領(lǐng)個是緩沖長度size.緩沖必須有足夠長度以容納絕對路徑名再加上一個null終止符,否則返回出錯。

  char * getcwd (char *buffer, size t size) [Function] The getcwd function returns an absolute file name representing the current working directory, storing it in the character array buffer that you provide. The size argument is how you tell the system the allocation size of buffer. The GNU library version of this function also permits you to specify a null pointer for the buffer argument. Then getcwd allocates a buffer automatically, as with malloc(see Section 3.2.2 [Unconstrained Allocation], page 33)。 If the size is greater than zero, then the buffer is that large; otherwise, the buffer is as large as necessary to hold the result. The return value is buffer on success and a null pointer on failure. The following errno error conditions are defined for this function:EINVAL The size argument is zero and buffer is not a null pointer. ERANGE The size argument is less than the length of the working directory name. You need to allocate a bigger array and try again. EACCES Permission to read or search a component of the file name was denied. You could implement the behavior of GNU‘s getcwd (NULL, 0) using only the standard behavior of getcwd:

  view plain char * gnu_getcwd ()

  { size_t size = 100;

  while (1)

  { char *buffer = (char *) xmalloc (size);if (getcwd (buffer, size) == buffer)

  return buffer;free (buffer);if (errno != ERANGE)

  return 0;size *= 2;}

  實(shí)例

  view plain #include "apue.h" char*path_alloc(int* size) { char *p = NULL; if(!size) return NULL; p = malloc(256); if(p) *size = 256; else *size = 0; return p; } int main(void)

  { char * ptr;int size;

  if(chdir("/devis/wangchenglin")<0)

  err_sys("chdir  failed");

  ptr=path_alloc(&size);if(getcwd(ptr,size)==NULL)

  err_sys("getcwd failed");

  printf("cwd=%sn",ptr);exit(0);

  }

相關(guān)文章

無相關(guān)信息
更新時間2022-03-13 11:05:09【至頂部↑】
聯(lián)系我們 | 郵件: | 客服熱線電話:4008816886(QQ同號) | 

付款方式留言簿投訴中心網(wǎng)站糾錯二維碼手機(jī)版

客服電話:




主站蜘蛛池模板: 王紫瑄| 1905电影网| av午夜| 纵横欲海| 杨玉环一级片| 夜电影| 洛克线| 人民日报评墨茶| 浙江卫视网络直播源| 秋天 课文| 赛虎| 高清图库| 外国小哥街头索吻狂魔| 我们的日子电视剧演员表| 洞房艳谭电影| 蓝心妍的电影《极乐宝鉴》免费国语| 卢昱晓电视剧| 《风流艳妇》| 阴道视频| 血色樱花剧情介绍| 经视直播| 彭丹丹主演的经典电影| 张猛龙魏碑字帖大全| 小小安妮| 生理卫生课程| 谭天谦| 傲娇与章经| 五年级语文上册第八课笔记| 汪佳辉| 6套电影频道节目表| 杯弓蛇影读后感| 聊斋花弄月普通话版免费| 梁美京| 正在恋爱中 电视剧韩剧免费全集结局| 明日战记| 电影《uhaw》完整版| 我是特种兵免费观看完整版| 国内性爱视频| 赤牙×柒cp| 暗潮危机电影完整版在线观看| 在线播放你懂|