int main (int argc, char **argv) { char *serverhost; int cmde; int serverfd; if (argc < 3) usage (); serverhost = argv[1]; if ((strcmp (argv[2], "get") == 0) && (argc == 5)) cmde = REQUEST_GET; else if ((strcmp (argv[2], "list") == 0) && (argc == 3)) cmde = REQUEST_DIR; else usage(); serverfd = connection (serverhost, PORT); switch (cmde) { case REQUEST_GET: get_file (serverfd, serverhost, argv[3], argv[4]); break; case REQUEST_DIR: dir_file (serverfd, serverhost); break; } close (serverfd); return 0; }
/** Find a subdirectory. * Look for subdirectory \a sub of parent directory \a path. * Return null if it doesn't already exist. * \param path Parent directory to search. * \param sub Subdirectory to find. * \return The full name of the subdirectory. */ struct gale_text sub_dir(struct gale_text path,struct gale_text sub) { struct stat buf; struct gale_text ret = dir_file(path,sub); if ((stat(gale_text_to(gale_global->enc_filesys,ret),&buf) || !S_ISDIR(buf.st_mode))) return null_text;; return ret; }
/** Find a subdirectory. * Look for subdirectory \a sub of parent directory \a path, and create the * subdirectory if it does not already exist. * \param path Parent directory to search. * \param sub Subdirectory to find or create. * \return The full name of the subdirectory. */ struct gale_text submk_dir(struct gale_text path,struct gale_text sub,int mode) { struct stat buf; struct gale_text ret = dir_file(path,sub); if ((stat(gale_text_to(gale_global->enc_filesys,ret),&buf) || !S_ISDIR(buf.st_mode))) if (mkdir(gale_text_to(gale_global->enc_filesys,ret),mode)) gale_alert(GALE_WARNING,ret,errno); return ret; }
void _gale_globals(void) { struct gale_global_data *G = gale_malloc_safe(sizeof(*gale_global)); struct gale_text conf; memset(G,'\0',sizeof(*gale_global)); gale_global = G; setlocale(LC_CTYPE, ""); _gale_charsets(); /* These are in this particular order to allow each 'conf' file to redirect the location of the next one. */ assert(NULL == G->error); assert(NULL == G->cleanup_list); G->home_dir = gale_var(G_("HOME")); make_dir(G->home_dir,0777); G->dot_gale = gale_var(G_("GALE_DIR")); if (0 != G->dot_gale.l) make_dir(G->dot_gale,0700); else G->dot_gale = submk_dir(G->home_dir,G_(".gale"),0700); conf = gale_var(G_("GALE_CONF")); if (0 != conf.l) read_conf(dir_file(G->dot_gale,conf)); read_conf(dir_file(G->dot_gale,G_("conf"))); G->sys_dir = gale_var(G_("GALE_SYS_DIR")); if (0 == G->sys_dir.l) G->sys_dir = gale_text_from( gale_global->enc_filesys,GALE_SYS_DIR,-1); make_dir(G->sys_dir,0); read_conf(dir_file(G->sys_dir,G_("conf"))); _gale_charsets(); }
int main(int argc, char **argv) { char buf[1024]; FILE *fp = dir_file(argv[1]); while( fgets(buf, sizeof(buf), fp) ) { fputs(buf, stdout); } fclose(fp); return 0; }
static void *on_report(oop_source *source,int sig,void *user) { struct gale_text fn = dir_file(gale_global->dot_gale, gale_text_concat(4, G_("report."), gale_text_from(NULL,gale_global->error_prefix,-1), G_("."), gale_text_from_number(getpid(),10,0))); FILE *fp = fopen(gale_text_to(gale_global->enc_filesys,fn),"w"); if (NULL == fp) gale_alert(GALE_WARNING,fn,errno); else { fputs(gale_text_to(gale_global->enc_filesys, gale_report_run(gale_global->report)),fp); fclose(fp); } return OOP_CONTINUE; }
void handle_request (int f) { struct request r; printf ("Process %d, handling connection from %s\n", getpid(), get_callername (f)); read(f, &r, sizeof(r)); if (r.kind == REQUEST_PUT) { put_file(f, r.path, r.nbbytes); } else if (r.kind == REQUEST_DEL) { del_file(f, r.path); } else if (r.kind == REQUEST_DIR) { dir_file(f, r.path); } else { get_file(f, r.path); } }
/** Search for a file in a list of directories. * This function searches for a file named \a fn relative to each of the * directories supplied as arguments. (If \a f is nonzero, the current * directory is also searched.) If the file is found, its full pathname * is returned. * \param fn The filename to search for. * \param f Nonzero to search the current directory * (and allow absolute filenames). * \param t List of directories to search, terminated by ::null_text. * \return The full pathname of the file, if it was found; ::null_text * otherwise. */ struct gale_text dir_search(struct gale_text fn,int f,struct gale_text t,...) { va_list ap; struct gale_text r = null_text; if (fn.l > 0 && fn.p[0] == '/') { if (access(gale_text_to(gale_global->enc_filesys,fn),F_OK)) return null_text; else return fn; } if (f && !access(gale_text_to(gale_global->enc_filesys,fn),F_OK)) return fn; va_start(ap,t); while (0 == r.l && 0 != t.l) { r = dir_file(t,fn); if (access(gale_text_to(gale_global->enc_filesys,r),F_OK)) r.l = 0; t = va_arg(ap,struct gale_text); } va_end(ap); return r; }