在unix下用C实现pwd和cd

mj0023_cn 2007-12-05 12:45:40
可以使用stat, opendir,readdir等...可能要用到inode的知识

执行: systemcalls <命令> <参数>
<命令>可以为 myPwd, 或myCD
当为 myCD时,<参数>被使用
myPwd实现和系统的pwd一样的功能,显示当前目录
myCD实现和cd一样的功能,工作目录变为<参数>

很着急,大家帮帮忙吧
...全文
223 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞哥 2007-12-06
  • 打赏
  • 举报
回复
直接查cd的源码
lovejklife 2007-12-06
  • 打赏
  • 举报
回复
可以使用stat, opendir,readdir等..
为什么就不能用chdir()呢
mj0023_cn 2007-12-06
  • 打赏
  • 举报
回复
pwd已经解决了。。还剩cd。
注意不能调用原来的系统命令阿
lovejklife 2007-12-05
  • 打赏
  • 举报
回复
char * getcwd(char *buf, size_t size);
int chdir(const char *path);
zxg623 2007-12-05
  • 打赏
  • 举报
回复
system("pwd");
星羽 2007-12-05
  • 打赏
  • 举报
回复
http://www.programsalon.com/downloads38/sourcecode/unix_linux/detail126616.html
krums 2007-12-05
  • 打赏
  • 举报
回复
可以看下《UNIX/LINUX编程实践教程》,CSDN就有电子版下。
GeomaticMm 2007-12-05
  • 打赏
  • 举报
回复
我在DOS操作系统源码找的 第一个是CD命令的源码

/****************************************************************/
/* */
/* cd.c */
/* */
/* DOS "cd" Command */
/* */
/* November 6, 1991 */
/* */
/* Copyright (c) 1995 */
/* Pasquale J. Villani */
/* All Rights Reserved */
/* */
/* This file is part of DOS-C. */
/* */
/* DOS-C is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation; either version */
/* 2, or (at your option) any later version. */
/* */
/* DOS-C is distributed in the hope that it will be useful, but */
/* WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
/* the GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public */
/* License along with DOS-C; see the file COPYING. If not, */
/* write to the Free Software Foundation, 675 Mass Ave, */
/* Cambridge, MA 02139, USA. */
/****************************************************************/


/* $Logfile: C:/dos-c/src/command/cd.c_v $ */

/* $Log: C:/dos-c/src/command/cd.c_v $
*
* Rev 1.3 31 Jan 1998 8:12:30 patv
* Put preprocessor switch for version strings and changed log strings
*
* Rev 1.2 29 Aug 1996 13:07:04 patv
* Bug fixes for v0.91b
*
* Rev 1.1 01 Sep 1995 18:04:34 patv
* First GPL release.
*
* Rev 1.0 02 Jul 1995 10:01:46 patv
* Initial revision.
*/
/* $EndLog$ */


#include "../../hdr/portab.h"
#include "globals.h"
#include "proto.h"

#ifdef VERSION_STRINGS
static BYTE *RcsId = "$Header: C:/dos-c/src/command/cd.c_v 1.3 31 Jan 1998 8:12:30 patv $";
#endif

BOOL cd(argc, argv)
WORD argc;
BYTE *argv[];
{
COUNT OldDrive, NewDrive = -1;
BYTE CurDir[MAX_CMDLINE] = "";

/* Initialize where we are */
OldDrive = DosGetDrive();

/* Do command line sanity checks */
if(argc > 2)
{
error_message(INV_NUM_PARAMS);
return FALSE;
}

/* get command line options and switch to the requested drive */
dosopt("$d*", (BYTE FAR *)tail, &NewDrive, CurDir);
if(NewDrive < 0)
NewDrive = default_drive;
DosSetDrive(NewDrive);

/* Do pwd function for no parameter case */
if(*CurDir == '\0')
{
DosPwd(NewDrive + 1, CurDir);
printf("%c:\\%s\n\n", 'A' + NewDrive, CurDir);
DosSetDrive(OldDrive);
return TRUE;
}

/* Otherwise, change the directory, and then switch back to the */
/* old directory. */
if((DosCd((BYTE FAR *)CurDir)) != SUCCESS)
{
error_message(INV_DIR);
DosSetDrive(OldDrive);
return FALSE;
}
else
{
DosSetDrive(OldDrive);
return TRUE;
}
}


好长时间没接触C了 PWD命令是返回当前位置的绝对路径吧 怎么实现现在想不到了
希望上面那个CD的源码对你有用

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧