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

您現(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ī)版

客服電話:




主站蜘蛛池模板: 《小道童》舞蹈| kaylani lei| 王韧| 寰宇地理| 日本电影幻想| 焕羽电视剧免费播放在线观看| 今天是你的生日bb伴奏正谱| 哈尔的移动城堡免费完整版| 故乡之恋简谱| 黄金太阳2| silk铃木一彻| 清水美里| xiee| 男同视频在线| 发型男2024流行发型图片 | 天堂在线电影| 冷门森系ins冷淡风壁纸| 尹馨演过的三部电影| 大秧歌演员表全部| 富二代| 土壤动植物的乐园教学反思| 苹果广告| 春节到,人欢笑,贴窗花,放鞭炮| 小姐诱心国语未删减版| 放学我当家| 黄秋生的电影| 老外毛片| 寡妇一级毛片免费看| 曲丹个人资料简介| 电影频道直播| 我和我的祖国教案| 美女出水| 护航 电影| 海洋天堂电影免费观看高清| 日本电影高校教师| 传说的打火机在线观看| 红灯区1996| 常虹| 挠vk| 武汉日夜| 每日一句话|