/*
* Rewrite the results of a directory read to reflect current
* name space bindings and mounts. Specifically, replace
* directory entries for bind and mount points with the results
* of statting what is mounted there. Except leave the old names.
*/
static long
mountfix(Chan *c, uchar *op, long n, long maxn)
{
char *name;
int nbuf, nname;
Chan *nc;
Mhead *mh;
Mount *m;
uchar *p;
int dirlen, rest;
long l;
uchar *buf, *e;
Dir d;
p = op;
buf = nil;
nbuf = 0;
for(e=&p[n]; p+BIT16SZ<e; p+=dirlen){
dirlen = dirfixed(p, e, &d);
if(dirlen < 0)
break;
nc = nil;
mh = nil;
if(findmount(&nc, &mh, d.type, d.dev, d.qid)){
/*
* If it's a union directory and the original is
* in the union, don't rewrite anything.
*/
for(m=mh->mount; m; m=m->next)
if(eqchantdqid(m->to, d.type, d.dev, d.qid, 1))
goto Norewrite;
name = dirname(p, &nname);
/*
* Do the stat but fix the name. If it fails, leave old entry.
* BUG: If it fails because there isn't room for the entry,
* what can we do? Nothing, really. Might as well skip it.
*/
if(buf == nil){
buf = smalloc(4096);
nbuf = 4096;
}
if(waserror())
goto Norewrite;
l = devtab[nc->type]->stat(nc, buf, nbuf);
l = dirsetname(name, nname, buf, l, nbuf);
if(l == BIT16SZ)
error("dirsetname");
poperror();
/*
* Shift data in buffer to accomodate new entry,
* possibly overflowing into rock.
*/
rest = e - (p+dirlen);
if(l > dirlen){
while(p+l+rest > op+maxn){
mountrock(c, p, &e);
if(e == p){
dirlen = 0;
goto Norewrite;
}
rest = e - (p+dirlen);
}
}
if(l != dirlen){
memmove(p+l, p+dirlen, rest);
dirlen = l;
e = p+dirlen+rest;
}
/*
* Rewrite directory entry.
*/
memmove(p, buf, l);
Norewrite:
cclose(nc);
putmhead(mh);
}
}
if(buf)
free(buf);
if(p != e)
error("oops in rockfix");
return e-op;
}
开发者ID:Akheon23,项目名称:nix-os,代码行数:95,代码来源:sysfile.c
示例5: printf
/* ----------------------------------------------------------------------
* Command line reading and parsing.
*/
struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
{
char *line;
int linelen, linesize;
struct sftp_command *cmd;
char *p, *q, *r;
int quoting;
if ((mode == 0) || (modeflags & 1)) {
printf("psftp> ");
}
fflush(stdout);
cmd = smalloc(sizeof(struct sftp_command));
cmd->words = NULL;
cmd->nwords = 0;
cmd->wordssize = 0;
line = NULL;
linesize = linelen = 0;
while (1) {
int len;
char *ret;
linesize += 512;
line = srealloc(line, linesize);
ret = fgets(line + linelen, linesize - linelen, fp);
if (!ret || (linelen == 0 && line[0] == '\0')) {
cmd->obey = sftp_cmd_quit;
if ((mode == 0) || (modeflags & 1))
printf("quit\n");
return cmd; /* eof */
}
len = linelen + strlen(line + linelen);
linelen += len;
if (line[linelen - 1] == '\n') {
linelen--;
line[linelen] = '\0';
break;
}
}
if (modeflags & 1) {
printf("%s\n", line);
}
p = line;
while (*p && (*p == ' ' || *p == '\t'))
p++;
if (*p == '!') {
/*
* Special case: the ! command. This is always parsed as
* exactly two words: one containing the !, and the second
* containing everything else on the line.
*/
cmd->nwords = cmd->wordssize = 2;
cmd->words = srealloc(cmd->words, cmd->wordssize * sizeof(char *));
cmd->words[0] = "!";
cmd->words[1] = p+1;
} else {
/*
* Parse the command line into words. The syntax is:
* - double quotes are removed, but cause spaces within to be
* treated as non-separating.
* - a double-doublequote pair is a literal double quote, inside
* _or_ outside quotes. Like this:
*
* firstword "second word" "this has ""quotes"" in" and""this""
*
* becomes
*
* >firstword<
* >second word<
* >this has "quotes" in<
* >and"this"<
*/
while (*p) {
/* skip whitespace */
while (*p && (*p == ' ' || *p == '\t'))
p++;
/* mark start of word */
q = r = p; /* q sits at start, r writes word */
quoting = 0;
while (*p) {
if (!quoting && (*p == ' ' || *p == '\t'))
break; /* reached end of word */
else if (*p == '"' && p[1] == '"')
p += 2, *r++ = '"'; /* a literal quote */
else if (*p == '"')
p++, quoting = !quoting;
else
*r++ = *p++;
}
if (*p)
p++; /* skip over the whitespace */
//.........这里部分代码省略.........
开发者ID:rdebath,项目名称:sgt,代码行数:101,代码来源:psftp.c
示例6: LoadClustersFromFile
//.........这里部分代码省略.........
if (repeatcount == 1) representative = repeatkey;
/* read symbol */
i = fscanf(fp, " %c", &symbol);
if (i != 1 || (symbol != '\'' && symbol != '"')) {
printf("\n\nUnexpected value qualifier detected in cluster file!");
exit(1);
}
/* assign direction */
if (symbol == '\'')
direction = 0;
else
direction = 1;
/* do something with it */
//printf(" %d%c",repeatkey,symbol);
if (NULL != cstart) { EasyListInsertHead(elist, RDCreate(repeatkey, direction)); }
/* read a character */
symbol = 0;
i = fscanf(fp, " %c", &symbol);
/* if no more characters then break */
if (i != 1) break;
/* if comment, read it and then break */
if (symbol == '#') {
fscanf(fp, "%s", comment);
//fscanf(fp," %c",&symbol); // scan extra character
break;
}
/* put character back */
ungetc(symbol, fp);
/* if character was a digit continue otherwise break */
if ((symbol >= '0' && symbol <= '9') || symbol == '-') continue;
else break;
}
//printf("\n");
if (NULL != citem) {
/* count pos and neg */
pos = 0; neg = 0;
for (nof1 = elist->head; nof1 != NULL; nof1 = nof1->next) {
a = (RD *)EasyListItem(nof1);
if (a->id >= 0) pos++; else neg++;
}
TotalReads += pos;
TotalRefs += neg;
citem->idlist = smalloc(pos * sizeof(int));
citem->dirlist = smalloc(pos * sizeof(char));
citem->negidlist = smalloc(neg * sizeof(int));
citem->negdirlist = smalloc(neg * sizeof(char));
citem->next = NULL;
/* insert into RLINK structure and free temp list */
pos = 0; neg = 0;
for (nof1 = elist->head; nof1 != NULL; nof1 = nof1->next) {
a = (RD *)EasyListItem(nof1);
if (a->id >= 0) {
citem->idlist[pos] = a->id;
citem->dirlist[pos] = a->dir;
pos++;
}
else {
citem->negidlist[neg] = a->id;
citem->negdirlist[neg] = a->dir;
neg++;
}
}
citem->refs = neg;
citem->reads = pos;
EasyListDestroy(elist);
}
}
/* close file */
fclose(fp);
return clustersread;
}
//.........这里部分代码省略.........
*/
static const struct plug_function_table fn_table = {
cygterm_log,
cygterm_closing,
cygterm_receive,
cygterm_sent,
cygterm_accepting
};
Local local;
const char *command;
char cmdline[2 * MAX_PATH];
int cport;
const char *err;
int cmdlinelen;
cygterm_debug("top");
local = snew(struct cygterm_backend_data);
local->fn = &fn_table;
local->a = NULL;
local->s = NULL;
local->cfg = *cfg;
local->editing = 0;
local->echoing = 0;
local->exitcode = 0;
*backend_handle = local;
local->frontend = frontend_handle;
/* set up listen socket for communication with child */
cygterm_debug("setupCygTerm");
/* let sk use INADDR_LOOPBACK and let WinSock choose a port */
local->a = sk_newlistener(0, 0, (Plug)local, 1, ADDRTYPE_IPV4);
if ((err = sk_socket_error(local->a)) != NULL)
goto fail_free;
/* now, get the port that WinSock chose */
/* XXX: Is there another function in PuTTY to do this? */
cygterm_debug("getting port");
cport = sk_getport(local->a);
if (cport == -1) {
err = "Failed to get port number for cthelper";
goto fail_close;
}
if (strchr(local->cfg.termtype, ' ')) {
err = "term type contains spaces";
goto fail_close;
}
/* Build cthelper command line */
cmdlinelen = sprintf(cmdline, CTHELPER" %u %s ", cport, local->cfg.termtype);
cmdlinelen += makeAttributes(cmdline + cmdlinelen, &local->cfg);
command = cfg->cygcmd;
cygterm_debug("command is :%s:", command);
/* A command of "." or "-" tells us to pass no command arguments to
* cthelper which will then run the user's shell under Cygwin. */
if ((command[0]=='-'||command[0]=='.') && command[1]=='\0')
;
else if (cmdlinelen + strlen(command) + 2 > sizeof cmdline) {
err = "command is too long";
goto fail_close;
}
else {
cmdlinelen += sprintf(cmdline + cmdlinelen, " %s", command);
}
/* Add the Cygwin /bin path to the PATH. */
if (cfg->cygautopath) {
char *cygwinBinPath = getCygwinBin();
if (!cygwinBinPath) {
/* we'll try anyway */
cygterm_debug("cygwin bin directory not found");
}
else {
cygterm_debug("found cygwin directory: %s", cygwinBinPath);
appendPath(cygwinBinPath);
sfree(cygwinBinPath);
}
}
cygterm_debug("starting cthelper: %s", cmdline);
if ((err = spawnChild(cmdline, &local->pi, &local->ctl)))
goto fail_close;
/* This should be set to the local hostname, Apparently, realhost is used
* only to set the window title.
*/
strcpy(*realhost = smalloc(sizeof CYGTERM_NAME), CYGTERM_NAME);
cygterm_debug("OK");
return 0;
fail_close:
sk_close(local->a);
fail_free:
sfree(local);
return err;
}
void
wbpcover (
int n_left, /* number of vertices on left side */
int n_right, /* number of vertices on right side */
int *pointers, /* start/stop of adjacency lists */
int *indices, /* adjacency list for each vertex */
int *vweight, /* vertex weights */
int *psep_size, /* returned size of separator */
int *psep_weight, /* returned weight of separator */
int **psep_nodes /* list of separator nodes */
)
{
extern int DEBUG_COVER; /* debug flag for this routine */
int *touched; /* flags for each vertex */
int *resid; /* remaining, unmatched vertex weight */
int *flow; /* flow on each right->left edge */
int *sep_nodes; /* list of separator nodes */
int sep_size; /* returned size of separator */
int sep_weight; /* returned weight of separator */
int nedges; /* number of edges in bipartite graph */
int i, j; /* loop counter */
int wleft, wright, wedges;
void confirm_cover();
if (DEBUG_COVER) {
printf("-> Entering wbpcover, nleft = %d, nright = %d, 2*nedges = %d\n",
n_left, n_right, pointers[n_left+n_right]-pointers[0]);
wleft = wright = 0;
wedges = 0;
for (i = 0; i < n_left; i++) {
wleft += vweight[i];
for (j = pointers[i]; j < pointers[i + 1]; j++) {
wedges += vweight[i] * vweight[indices[j]];
}
}
for (i = n_left; i < n_left + n_right; i++) {
wright += vweight[i];
for (j = pointers[i]; j < pointers[i + 1]; j++) {
wedges += vweight[i] * vweight[indices[j]];
}
}
printf(" Corresponds to unweighted, nleft = %d, nright = %d, 2*nedges = %d\n",
wleft, wright, wedges);
}
nedges = pointers[n_left + n_right] - pointers[0];
resid = smalloc((n_left + n_right) * sizeof(int));
touched = smalloc((n_left + n_right) * sizeof(int));
flow = smalloc((nedges + 1) * sizeof(int));
/* Not a matching. I can be connected to multiple nodes. */
bpflow(n_left, n_right, pointers, indices, vweight, resid, flow, touched);
reachability(n_left, n_right, pointers, indices, resid, flow, touched);
/* Separator includes untouched nodes on left, touched on right. */
/* Left separator nodes if unconnected to unmatched right node via */
/* augmenting path, right separator nodes otherwise. */
/* First count the separator size for malloc. */
sep_size = 0;
for (i = 0; i < n_left; i++) {
if (!touched[i]) {
sep_size++;
}
}
for (i = n_left; i < n_left + n_right; i++) {
if (touched[i]) {
sep_size++;
}
}
sep_nodes = smalloc((sep_size + 1) * sizeof(int));
sep_size = sep_weight = 0;
for (i = 0; i < n_left; i++) {
if (!touched[i]) {
sep_nodes[sep_size++] = i;
sep_weight += vweight[i];
}
}
for (i = n_left; i < n_left + n_right; i++) {
if (touched[i]) {
sep_nodes[sep_size++] = i;
sep_weight += vweight[i];
}
}
sep_nodes[sep_size] = 0;
*psep_size = sep_size;
*psep_weight = sep_weight;
*psep_nodes = sep_nodes;
/* Check the answer. */
//.........这里部分代码省略.........
static long
ipread(Chan *ch, void *a, long n, vlong off)
{
int r;
Conv *c;
Proto *x;
char *p, *s;
Fs *f;
ulong offset = off;
f = ipfs[ch->dev];
p = a;
switch(TYPE(ch->qid)) {
default:
error(Eperm);
case Qprotodir:
case Qtopdir:
case Qconvdir:
return devdirread(ch, a, n, 0, 0, ipgen);
case Qarp:
error(Eperm); /* TO DO */
case Qndb:
return readstr(off, a, n, f->ndb);
case Qctl:
sprint(up->genbuf, "%lud", CONV(ch->qid));
return readstr(offset, p, n, up->genbuf);
case Qremote:
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
sprint(up->genbuf, "%I!%d\n", c->raddr, c->rport);
return readstr(offset, p, n, up->genbuf);
case Qlocal:
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
sprint(up->genbuf, "%I!%d\n", c->laddr, c->lport);
return readstr(offset, p, n, up->genbuf);
case Qstatus:
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
s = smalloc(Statelen);
if(waserror()){
free(s);
nexterror();
}
snprint(s, Statelen, "%s\n", ipstates[c->state]);
n = readstr(offset, p, n, s);
poperror();
free(s);
return n;
case Qdata:
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
if(c->sfd < 0)
error(Ehungup);
if(c->headers) {
if(n < c->headers)
error(Ebadarg);
p = a;
r = so_recv(c->sfd, p + c->headers, n - c->headers, p, c->headers);
if(r > 0)
r += c->headers;
} else
r = so_recv(c->sfd, a, n, nil, 0);
if(r < 0)
oserror();
return r;
case Qstats:
error("stats not implemented");
return n;
}
}
开发者ID:Vykook,项目名称:acme-sac,代码行数:72,代码来源:devip.c
示例13: ipread
static long
ipread(Chan *ch, void *a, long n, vlong off)
{
Conv *c;
Proto *x;
char *buf, *p;
long rv;
Fs *f;
ulong offset = off;
f = ipfs[ch->dev];
p = a;
switch(TYPE(ch->qid)) {
default:
error(Eperm);
case Qtopdir:
case Qprotodir:
case Qconvdir:
return devdirread(ch, a, n, 0, 0, ipgen);
case Qarp:
return arpread(f->arp, a, offset, n);
case Qbootp:
return bootpread(a, offset, n);
case Qndb:
return readstr(offset, a, n, f->ndb);
case Qiproute:
return routeread(f, a, offset, n);
case Qipselftab:
return ipselftabread(f, a, offset, n);
case Qlog:
return netlogread(f, a, offset, n);
case Qctl:
buf = smalloc(16);
snprint(buf, 16, "%lud", CONV(ch->qid));
rv = readstr(offset, p, n, buf);
free(buf);
return rv;
case Qremote:
buf = smalloc(Statelen);
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
if(x->remote == nil) {
snprint(buf, Statelen, "%I!%d\n", c->raddr, c->rport);
} else {
(*x->remote)(c, buf, Statelen-2);
}
rv = readstr(offset, p, n, buf);
free(buf);
return rv;
case Qlocal:
buf = smalloc(Statelen);
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
if(x->local == nil) {
snprint(buf, Statelen, "%I!%d\n", c->laddr, c->lport);
} else {
(*x->local)(c, buf, Statelen-2);
}
rv = readstr(offset, p, n, buf);
free(buf);
return rv;
case Qstatus:
buf = smalloc(Statelen);
x = f->p[PROTO(ch->qid)];
c = x->conv[CONV(ch->qid)];
(*x->state)(c, buf, Statelen-2);
rv = readstr(offset, p, n, buf);
free(buf);
return rv;
case Qdata:
c = f->p[PROTO(ch->qid)]->conv[CONV(ch->qid)];
return qread(c->rq, a, n);
case Qerr:
c = f->p[PROTO(ch->qid)]->conv[CONV(ch->qid)];
return qread(c->eq, a, n);
case Qsnoop:
c = f->p[PROTO(ch->qid)]->conv[CONV(ch->qid)];
return qread(c->sq, a, n);
case Qstats:
x = f->p[PROTO(ch->qid)];
if(x->stats == nil)
error("stats not implemented");
buf = smalloc(Statelen);
(*x->stats)(x, buf, Statelen);
rv = readstr(offset, p, n, buf);
free(buf);
return rv;
}
}
请发表评论