⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unlink.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
字号:
#include "lib.h"#include <unistd.h>#include <errno.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include "sys9.h"#include "dir.h"/* * BUG: errno mapping */intunlink(const char *path){	int n, i, fd;	long long nn;	Dir *db1, *db2, nd;	Fdinfo *f; 	char *p, newname[PATH_MAX], newelem[32];	/* if the file is already open, make it close-on-exec (and rename to qid) */	if((db1 = _dirstat(path)) == nil) {		_syserrno();		return -1;	}	fd = -1;	for(i=0, f = _fdinfo;i < OPEN_MAX; i++, f++) {		if((f->flags&FD_ISOPEN) && (db2=_dirfstat(i)) != nil) {			if(db1->qid.path == db2->qid.path &&			   db1->qid.vers == db2->qid.vers &&			   db1->type == db2->type &&			   db1->dev == db2->dev) {				sprintf(newelem, "%8.8lx%8.8lx", (ulong)(db2->qid.path>>32), (ulong)db2->qid.path);				_nulldir(&nd);				nd.name = newelem;				if(_dirfwstat(i, &nd) < 0)					p = (char*)path;				else {					p = strrchr(path, '/');					if(p == 0)						p = newelem; 					else {						memmove(newname, path, p-path);						newname[p-path] = '/';						strcpy(newname+(p-path)+1, newelem);						p = newname;					}				}				/* reopen remove on close */				fd = _OPEN(p, 64|(f->oflags)); 				if(fd < 0){					free(db2);					continue;				}				nn = _SEEK(i, 0, 1);				if(nn < 0)					nn = 0;				_SEEK(fd, nn, 0);				_DUP(fd, i);				_CLOSE(fd);				free(db1);				return 0;			}			free(db2);		}	}	if(fd == -1)		if((n=_REMOVE(path)) < 0)			_syserrno();	free(db1);	return n;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -