函数chdir、fchdir和getcwd
函数chdir、fchdir和getcwd
chdir、fchdir函数
#include<unistd.h>int chdir(constchar*pathname);int fchdir(int fd);Bothreturn:0if OK,−1 on error
实例
/*** 文件内容:因为当前工作目录是进程的一个属性,所以它只影响到调用chdir的进程本身* 而不影响其他进程* 文件时间:* 作者:firewaywei@126.com*/#include<stdio.h>#include<stdlib.h>int main(void){if(chdir("/tmp")<0){err_sys("chdir failed");}printf("chdir to /tmp succeeded\n");exit(0);}
gcc main.c -lerror -Llib
$ pwd
/home/fireway/study/temp2
$ ./a.out
chdir to /tmp succeeded
getcwd函数
#include<unistd.h>char*getcwd(char*buf,size_t size);Returns: buf if OK, NULL on error
实例
/*** 文件名:mycwd.c* 文件内容: 将工作目录更改至一个指定目录,然后调用getcwd,最后打印该工作目录* 时间:2016年 11月 14日 星期一 07:59:08 CST* 作者:firewaywei@126.com*/#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include"pathalloc.h"int main(void){char*ptr = NULL;size_t size =0;if(chdir("/usr/spool/uucppublic")<0){err_sys("chdir failed");}ptr = path_alloc(&size);if(getcwd(ptr, size)== NULL){err_sys("getcwd failed");}printf("cwd = %s\n", ptr);if(ptr != NULL){free(ptr);ptr = NULL;}exit(0);}
gcc main.c -lerror -L../temp3
# ln -s /home/fireway/study/temp3 /usr/spool
# ./a.out
cwd = /home/fireway/study/temp3/uucppublic
# ls -l /usr/spool
lrwxrwxrwx 1 root root 25 11月 14 08:24 /usr/spool -> /home/fireway/study/temp3
参考
函数chdir、fchdir和getcwd的更多相关文章
- 文件和目录之chdir、fchdir和getcwd函数
每个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点(不以斜杠开始的路径名为相对路径名).当用户登录到UNIX系统时,其当前工作目录通常是口令文件(/etc/passwd)中该用户登录项的第 ...
- apue chapter 4 文件和目录
1.文件信息结构体 struct stat{ mode_t st_mode; //file type and permissions ino_t st_ino; //i-node number (se ...
- UNIX环境高级编程--4
函数stat fstat fstatat 和 lstat stat函数使用最多的地方可能就是ls -l 命令,用其可以获得有关一个文件的所有信息. 文件类型: (1)普通文件 (2)目录文件 (3)块 ...
- Files and Directories
Files and Directories Introduction In the previous chapter we coveredthe basic functions that pe ...
- 【APUE】Chapter4 File and Directories
4.1 Introduction unix的文件.目录都被当成文件来看待(vi也可以编辑目录):我猜这样把一起内容都当成文件的原因是便于统一管理权限这类的内容 4.2 stat, fstat, fst ...
- 文件权限控制篇access alphasort chdir chmod chown chroot closedir fchdir fchmod fchown fstat ftruncate getcwd
access(判断是否具有存取文件的权限) 相关函数 stat,open,chmod,chown,setuid,setgid 表头文件 #include<unistd.h> 定义函数 in ...
- getcwd函数学习
getcwd 函数原型:char *getcwd( char *buffer, int maxlen ); 功 能:获取当前工作目录 参数说明:getcwd()会将当前工作目录的绝对路径复制到参数bu ...
- Linux中文件函数(二)
一.link.linkat.unlink.unlinkat.remove函数 创建一个指向现有文件的链接的方法是使用link函数或linkat函数.函数的原型为: #include <unist ...
- 【Linux C中文函数手册】之 目录操作函数
目录操作函数 1)closedir 关闭目录 相关函数: opendir表头文件: #include<sys/types.h> #include<dirent.h>定义函数: ...
随机推荐
- 平稳切换nginx版本
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- TypeScript中的怪语法
TypeScript中的怪语法 如何处理undefined 和 null undefined的含义是:一个变量没有初始化. null的含义是:一个变量的值是空. undefined 和 null 的最 ...
- Ubuntu安装Java8和Java9
前言 系统:Ubuntu 16.04 软件: Java8, Java9 Tips: Java 9 的代码由于提供了新特性,所以有些代码并不支持向后兼容.也就是说,用 Java 9 写的代码,有可能在 ...
- 初学者易上手的SSH-hibernate02 三种查询方式
在上一章中已经搭建好了一个hibernate的环境,那么这一章我们就使用这个环境来进行基本CRUD.在这之前我们先了解一个东西:主键生成策略.就是当向数据库表中插入记录的时候,这个记录的主键该如何生成 ...
- 使用vim编写hexo文档,并用ultisnips/snipmates/snippets插件补全
作为一个vim使用者,编写markdown文档时若不能用vim这怎么能受的了! 下面是我编写markdown的时候用到的插件 Plugin 'Markdown'Plugin 'Markdown-syn ...
- Linux系列教程(四)——Linux文件和目录处理命令
这个系列教程的前面我们讲解了如何安装Linux系统,以及学习Linux系统的一些方法.那么从这篇博客开始,我们就正式进入Linux命令的学习.学习命令,首先要跟大家纠正的一点就是,我们不需要记住每一条 ...
- Linux系统查找
1. which:在当前用户环境变量path指定的路径下查找可执行程序/文件. 特点:(1)只在当前用户环境变量指定的路径下查找: (2)只找出可执行程序/文件的位置: (3)查找速度非常快. 注:使 ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- spring 学习笔记1
Spring 学习记录 任何一个成功的应用都是由多个为了实现某一个业务目标而相互协作的组件构成的.这些组件必须彼此了解,并相互协作来完成工作. 在Spring 中,对象无需自己负责查找或创建与其关联的 ...
- open-falcon Agent配置文件修改hostname后,还是有其他名称的endpoint
问题 open-falcon Agent在配置文件修改hostname后,log日志中还是发现其他名称的endpoint. 原因 Graph, Gateway组件会引用goperfcounter(gi ...