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

📄 which.c

📁 操作系统源代码
💻 C
字号:
/* which - search paths for executable */#define DELIMITER ':'#include <sys/types.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <stdio.h>_PROTOTYPE(int main, (int argc, char **argv));int main(ac, av)int ac;char **av;{  char *path, *cp;  char buf[400];  char prog[400];  char patbuf[512];  int quit, none;  int excode = 0;  if (ac < 2) {	fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);	exit(1);  }  av[ac] = 0;  for (av++; *av; av++) {	quit = 0;	none = 1;	if ((path = getenv("PATH")) == NULL) {		fprintf(stderr, "Null path.\n");		exit(0);	}	strcpy(patbuf, path);	path = patbuf;	cp = path;	while (1) {		cp = strchr(path, DELIMITER);		if (cp == NULL)			quit++;		else			*cp = '\0';		if (strcmp(path, "") == 0 && quit == 0) {			sprintf(buf, "%s./%s", path, *av);		} else			sprintf(buf, "%s/%s", path, *av);		/* Fprintf(stderr,"Trying %s, path %s\n",buf,path); */		path = ++cp;		if (access(buf, 1) == 0) {			printf("%s\n", buf);			none = 0;		}		sprintf(prog, "%s.%s", buf, "prg");		if (access(prog, 1) == 0) {			printf("%s\n", prog);			none = 0;		}		sprintf(prog, "%s.%s", buf, "ttp");		if (access(prog, 1) == 0) {			printf("%s\n", prog);			none = 0;		}		sprintf(prog, "%s.%s", buf, "tos");		if (access(prog, 1) == 0) {			printf("%s\n", prog);			none = 0;		}		if (quit) {			if (none) {				fprintf(stderr, "No %s in %s\n", *av, getenv("PATH"));				excode = 1;			}			break;		}	}  }  return(excode);}

⌨️ 快捷键说明

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