int _builtin_cd(char **cmd, t_shenv *shenv) { int ret; char *cwd; static char *oldpwd = NULL; cwd = my_getcwd(); if (cmd[1] == NULL) ret = chdir_home(); else if (!strcmp(cmd[1], "-")) { if ((ret = my_chdir(oldpwd)) == EXIT_SUCCESS) { printf("%s\n", (oldpwd == NULL) ? my_getcwd() : oldpwd); xfree(1, &oldpwd); oldpwd = cwd; update_env(oldpwd, shenv); return (EXIT_SUCCESS); } return (EXIT_FAILURE); } else ret = my_chdir(cmd[1]); update_env(cwd, shenv); if (ret == EXIT_SUCCESS && xfree(1, &oldpwd)) oldpwd = cwd; return ((ret) ? (EXIT_FAILURE) : (EXIT_SUCCESS)); }
void TFileList::doOnChangeDir () { char tmp[80], *dir; my_getcwd (tmp, 80); // Guardar el path actual. dir = getabsolute (); // Tomar el nombre como absoluto al path if (chdir (dir)==0) { // intentar cambiar el directorio. my_getcwd (path, 80); // si se pudo, era directorio, reescanearlo. chdir (tmp); scandir (); doDraw (); } }
static void update_env(char *oldpwd, t_shenv *shenv) { char *cmd[3]; cmd[0] = strdup("setenv"); cmd[1] = strdup("PWD"); cmd[2] = my_getcwd(); _builtin_setenv(cmd, shenv); xfree(1, &cmd[1]); cmd[1] = strdup("OLDPWD"); cmd[2] = oldpwd ? oldpwd : my_getcwd(); _builtin_setenv(cmd, shenv); xfree(2, &cmd[0], &cmd[1]); }
/** Return absolute filename of specified file */ static char *get_fullfile( char *file ) { char *fullfile; if( file[0] == '/' ) { fullfile = file; } else { char *cwd = my_getcwd(); if( !cwd ) { error = 1; perror( "getcwd" ); return 0; } int l = strlen(cwd); fullfile = my_malloc( l + strlen(file)+2 ); if( !fullfile ) { free(cwd); return 0; } strcpy( fullfile, cwd ); if( cwd[l-1] != '/' ) strcat(fullfile, "/" ); strcat( fullfile, file ); free(cwd); } return fullfile; }
int cmd_get(t_com c, t_server *s) { int in_file; char buf[MAX_MSG + 1]; struct stat st; if (!(c.cmd[1])) { dprintf(s->connectSocket, "Usage: put _FILE_\n"); return (EXIT_SUCCESS); } bzero(buf, sizeof(buf)); my_getcwd(buf, c.cmd[1]); if ((in_file = open(buf, O_RDONLY)) == -1) return (my_send_err(c.cmd[1], s->connectSocket, EXIT_SUCCESS)); fstat(in_file, &st); dprintf(s->connectSocket, "%d", (int)st.st_size); printf("\t--> %s gets %s (%d o)\n", s->clt_info->nickname, c.cmd[1], (int)st.st_size); sleep(1); if ((read_file(s, in_file, st.st_size)) == EXIT_FAILURE) return (EXIT_FAILURE); if ((get_confim(s)) == EXIT_FAILURE) return (xclose(in_file, EXIT_FAILURE)); return (xclose(in_file, EXIT_SUCCESS)); }
static VALUE load_path_getcwd(void) { char *cwd = my_getcwd(); VALUE cwd_str = rb_filesystem_str_new_cstr(cwd); xfree(cwd); return cwd_str; }
int isDir (char *s) { int r=0; char olddir[256]; my_getcwd (olddir, 255); if (chdir (s)==0) { r = 1; chdir (olddir); } return r; }
void TFileList::doOnSelectFile () { char tmp[80], *dir; my_getcwd (tmp, 80); // Guardar el path actual. dir = getabsolute (); // Tomar el nombre como absoluto al path if (chdir (dir)==0) { // intentar cambiar el directorio. chdir (tmp); // reestablecer el directorio. } else // Sino, era archivo regular, llamar selector if (OnSelectFile) // (sy hay callback) OnSelectFile->do_callback (this); }
/* * call-seq: * Dir.getwd => string * Dir.pwd => string * * Returns the path to the current working directory of this process as * a string. * * Dir.chdir("/tmp") #=> 0 * Dir.getwd #=> "/tmp" */ static VALUE dir_s_getwd(VALUE dir) { char *path; VALUE cwd; rb_secure(4); path = my_getcwd(); cwd = rb_tainted_str_new2(path); xfree(path); return cwd; }
int main(int argc, char *argv[]) { char *path; path = my_getcwd(); if (!path) { perror("getcwd"); exit(1); } puts(path); free(path); exit(0); }
void TFileList::scandir () { char olddir[256]; DIR *d; clearlist (); my_getcwd (olddir, 255); chdir (path); d = opendir (path); if (d) { struct dirent *ent; rewinddir (d); while ((ent=readdir(d))!=NULL) if (passFilt (ent->d_name)) additem (ent->d_name); closedir (d); } chdir (olddir); }
/** Return absolute filename of specified file */ static const char *get_fullfile( const char *file ) { const char *fullfile; if( file[0] == '/' ) { fullfile = file; } else { char *cwd = my_getcwd(); if( !cwd ) { error = 1; perror( "getcwd" ); return 0; } int l = strlen(cwd); char *tmp = (char *)my_malloc( l + strlen(file)+2 ); if( !tmp ) { free(cwd); return 0; } strcpy( tmp, cwd ); if( cwd[l-1] != '/' ) strcat(tmp, "/" ); strcat( tmp, file ); free(cwd); fullfile = tmp; } return fullfile; }
/* * call-seq: * Dir.chdir( [ string] ) => 0 * Dir.chdir( [ string] ) {| path | block } => anObject * * Changes the current working directory of the process to the given * string. When called without an argument, changes the directory to * the value of the environment variable <code>HOME</code>, or * <code>LOGDIR</code>. <code>SystemCallError</code> (probably * <code>Errno::ENOENT</code>) if the target directory does not exist. * * If a block is given, it is passed the name of the new current * directory, and the block is executed with that as the current * directory. The original working directory is restored when the block * exits. The return value of <code>chdir</code> is the value of the * block. <code>chdir</code> blocks can be nested, but in a * multi-threaded program an error will be raised if a thread attempts * to open a <code>chdir</code> block while another thread has one * open. * * Dir.chdir("/var/spool/mail") * puts Dir.pwd * Dir.chdir("/tmp") do * puts Dir.pwd * Dir.chdir("/usr") do * puts Dir.pwd * end * puts Dir.pwd * end * puts Dir.pwd * * <em>produces:</em> * * /var/spool/mail * /tmp * /usr * /tmp * /var/spool/mail */ static VALUE dir_s_chdir(int argc, VALUE *argv, VALUE obj) { VALUE path = Qnil; rb_secure(2); if (rb_scan_args(argc, argv, "01", &path) == 1) { FilePathValue(path); } else { const char *dist = getenv("HOME"); if (!dist) { dist = getenv("LOGDIR"); if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set"); } path = rb_str_new2(dist); } if (chdir_blocking > 0) { if (!rb_block_given_p() || rb_thread_current() != chdir_thread) rb_warn("conflicting chdir during another chdir block"); } if (rb_block_given_p()) { struct chdir_data args; char *cwd = my_getcwd(); args.old_path = rb_tainted_str_new2(cwd); xfree(cwd); args.new_path = path; args.done = Qfalse; return rb_ensure(chdir_yield, (VALUE)&args, chdir_restore, (VALUE)&args); } dir_chdir(path); return INT2FIX(0); }
TFileList::TFileList (TRect& r, char *_ext): TListBox (r), ext(_ext) { my_getcwd (path, 80); OnSelectFile = NULL; scandir (); }
/* * Initialize shttpd context */ static void initialize_context(struct shttpd_ctx *ctx, const char *config_file, int argc, char *argv[], char **tmpvars) { //sizeof(line) = sizeof(char)*FILENAME_MAX; char line[FILENAME_MAX], root[FILENAME_MAX], var[sizeof(line)], val[sizeof(line)]; const char *arg; size_t i; //config的数组指针 const struct opt *opt; FILE *fp; //时间 struct tm *tm; //全局变量 //time(NULL)返回的是整型的偏移量 localtime()是把时间转换成年月日时分秒 current_time = time(NULL); tm = localtime(¤t_time); tz_offset = 0; #if 0 tm->tm_gmtoff - 3600 * (tm->tm_isdst > 0 ? 1 : 0); #endif //ctx是动态分配的 (void) memset(ctx, 0, sizeof(*ctx)); ctx->start_time = current_time; //unix-like 这是个空的宏定义 InitializeCriticalSection(&ctx->mutex); //初始化核心数据结构的各种双链表头 //此时各种头应该是0值 并没有实际空间 LL_INIT(&ctx->connections); LL_INIT(&ctx->mime_types); LL_INIT(&ctx->registered_uris); LL_INIT(&ctx->uri_auths); LL_INIT(&ctx->error_handlers); #if !defined(NO_SSI) LL_INIT(&ctx->ssi_funcs); #endif /* NO_SSI */ /* First pass: set the defaults */ //my_strdup 是个字符串的拷贝函数 内部是malloc空间 //有值的才进行拷贝 opt->def字段 for (opt = options; opt->sw != 0; opt++) if (tmpvars[opt - options] == NULL && opt->def != NULL) tmpvars[opt - options] = my_strdup(opt->def); /* Second pass: load config file */ // putchar(c); is equivalent to putc(c,stdout). // DBG是个宏 打印到标准输出 // 有配置文件就读取配置文件,否则读取命令行 // config_file默认是个宏 "shttpd.conf" // 若没有通过命令行传值 就使用默认的配置文件路径 - 应该和程序处于同一目录 if (config_file != NULL && (fp = fopen(config_file, "r")) != NULL) { DBG(("init_ctx: config file %s", config_file)); /* Loop through the lines in config file */ while (fgets(line, sizeof(line), fp) != NULL) { /* Skip comments and empty lines */ if (line[0] == '#' || line[0] == '\n') continue; /* Trim trailing newline character */ line[strlen(line) - 1] = '\0'; //例如:"%[^=]" 读入任意多的字符,直到遇到"="停止 if (sscanf(line, "%s %[^#\n]", var, val) != 2) elog(E_FATAL,0,"init_ctx: bad line: [%s]",line); //返回最末一个结构体指针或者name匹配的指针 if ((opt = find_option(0, var)) == NULL) elog(E_FATAL, NULL, "set_option: unknown variable [%s]", var); //把default的值去掉,换成文件中的值 set_option(opt, val, tmpvars); } (void) fclose(fp); } /* Third pass: process command line args */ for (i = 1; i < (size_t) argc && argv[i][0] == '-'; i++) if ((opt = find_option(argv[i][1], NULL)) != NULL) { arg = argv[i][2] ? &argv[i][2] : argv[++i]; if (arg == NULL) usage(argv[0]); set_option(opt, arg, tmpvars); } else { usage(argv[0]); } /* Call setters functions now */ //setter 是个函数指针 // typedef void (*optset_t)(struct shttpd_ctx *, void *ptr, const char *string); // void set_str(struct shttpd_ctx *ctx, void *ptr, const char *string) // 做一次拷贝,把临时的值拷贝到options数组中 for (i = 0; i < NELEMS(options); i++) if (tmpvars[i] != NULL) { options[i].setter(ctx, ((char *) ctx) + options[i].ofs, tmpvars[i]); free(tmpvars[i]); } //默认root目录是程序当前的绝对路径 /* If document_root is not set, set it to current directory */ if (ctx->document_root == NULL) { //The getcwd() function copies an absolute pathname of the current working directory to the array pointed // to by buf, which is of length size. (void) my_getcwd(root, sizeof(root)); ctx->document_root = my_strdup(root); } #ifdef _WIN32 {WSADATA data; WSAStartup(MAKEWORD(2,2), &data);} #endif /* _WIN32 */ DBG(("init_ctx: initialized context %p", (void *) ctx)); }