Example #1
0
int main(int argc, const char* argv[]) {

    size_t count = 0;
    char file[65536];
    char **input = NULL;
    FILE *ifp;
    ifp = fopen((argc >= 1) ? argv[1] : "test.txt", "r");
    if(!ifp)
    {
    	PERROR("Failed to open file\n");
    }

    fread((void *)file, 1, 65536, ifp );
    puts(file);
    count = strspl(file, "\n", &input);
    fclose(ifp);
    int i, j;
    module_vector v = malloc(sizeof(_module_vector));
    bool passes = modules_parse(input, count, v);
    printf("[%s] Compile Order:\n", (passes) ? "PASS" : "FAIL");
    for(i = 0; i  < v->n_cycles; i++)
    {
        printf("\t[unorderable]");
        for(j = 0; j < v->unordered[i]->size; j++)
            printf(" %s", v->modules[v->unordered[i]->vals[j]].name);
        printf("\n");
        free(v->unordered[i]->vals);
        free(v->unordered[i]);
    }
    for(i = 0; i < v->n_steps; i++)
    {
        printf("%d:", i + 1);
        for(j = 0; j < v->ordered[i]->size; j++)
            printf(" %s", v->modules[v->ordered[i]->vals[j]].name);
        printf("\n");
        free(v->ordered[i]->vals);
        free(v->ordered[i]);
    }


    for(i = 0; i < v->n_modules; i++) free(v->modules[i].deps);
    //for(i = 0; (unsigned) i < count; i++) free(input[i]);

    if(input) free(input);

    //free(line);
    //free(v->modules->name);

    free(v->modules);
    if(v->n_cycles) free(v->unordered);
    if(v->n_steps) free(v->ordered);
    free(v);

    return 0;
}
Example #2
0
int topo_parse(dag *ret, char *str)
{
    int n_items = 0;
    int parent, idx;
    dag list = 0;

    char **e, **w;
    int ei, wj, i, j;
    for(ei = strspl(str, "\n", &e), i = 0;i < ei;i++)
    {
        for(wj = strspl(e[i], " \t", &w), j = 0; j < wj;j++)
        {
            idx = topo_item(&list, &n_items, w[j]);
            if(!j) parent = idx;
            else topo_dep(list + parent, idx);
        }
    }

    *ret = list;
    return n_items;
}
Example #3
0
static void Gcat (char *s1, char *s2)
{
int len;

	len = strlen(s1) + strlen(s2) + 1;

	if (len >= gnleft || gargc >= GAVSIZ - 1)
		globerr = "Arguments too long";
	else {
		gargc++;
		gnleft -= len;
		gargv[gargc] = 0;
		gargv[gargc - 1] = strspl(s1, s2);
	}
}
Example #4
0
static 
void
Gcat(const char *s1, const char *s2)
{
	int len = strlen(s1) + strlen(s2) + 1;

	if (len >= gnleft || gargc >= GAVSIZ - 1) {
		globerr = "Arguments too long";
	}
	else {
		gargc++;
		gnleft -= len;
		gargv[gargc].text = NULL;
		gargv[gargc - 1].text = strspl(s1, s2);
	}
}
Example #5
0
static void Gcat(register char *s1, register char *s2)
{
    register size_t len = strlen(s1) + strlen(s2) + 1;

    if (globerr)
	return;
    if (len >= gnleft || gargc >= GAVSIZ - 1)
	globerr = "Arguments too long";
    else if (len > MAXPATHLEN)
	globerr = "Pathname too long";
    else {
	gargc++;
	gnleft -= len;
	gargv[gargc] = 0;
	gargv[gargc - 1] = strspl(s1, s2);
    }
}
Example #6
0
    int
main(int ac, char **av)
{
    int i, j;
    int aflag = 0;
    int uflag = 0;
    int fpos;    /* current position in time format buffer */
    int chrcnt;    /* # of chars formatted by current sprintf */
    int bl, wtmp;
    char *ct;
    char *ut_host;
    char *ut_user;
    struct utmpx *bp;
    time_t otime;
    struct stat stb;
    int print = 0;
    char *crmsg = (char *)0;
    long outrec = 0;
    long maxrec = 0x7fffffffL;
    char *wtmpfile = "/var/adm/wtmpx";
    size_t hostf_len;

    (void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)        /* Should be defined by cc -D */
#define    TEXT_DOMAIN "SYS_TEST"        /* Use this only if it weren't. */
#endif
    (void) textdomain(TEXT_DOMAIN);

    (void) time(&buf[0].ut_xtime);
    ac--, av++;
    argc = ac;
    argv = av;
    names = malloc(argc * sizeof (char *));
    if (names == NULL) {
        perror("first");
        exit(2);
    }
    names_num = 0;
    for (i = 0; i < argc; i++) {
        if (argv[i][0] == '-') {

            /* -[0-9]*   sets max # records to print */
            if (isdigit(argv[i][1])) {
                maxrec = atoi(argv[i]+1);
                continue;
            }

            for (j = 1; argv[i][j] != '\0'; ++j) {
                switch (argv[i][j]) {

                    /* -f name sets filename of wtmp file */
                    case 'f':
                        if (argv[i][j+1] != '\0') {
                            wtmpfile = &argv[i][j+1];
                        } else if (i+1 < argc) {
                            wtmpfile = argv[++i];
                        } else {
                            (void) fprintf(stderr,
                                    gettext("first: argument to "
                                        "-f is missing\n"));
                            (void) fprintf(stderr,
                                    gettext(USAGE));
                            exit(1);
                        }
                        goto next_word;

                        /* -n number sets max # records to print */
                    case 'n': {
                              char *arg;

                              if (argv[i][j+1] != '\0') {
                                  arg = &argv[i][j+1];
                              } else if (i+1 < argc) {
                                  arg = argv[++i];
                              } else {
                                  (void) fprintf(stderr,
                                          gettext("first: argument to "
                                              "-n is missing\n"));
                                  (void) fprintf(stderr,
                                          gettext(USAGE));
                                  exit(1);
                              }

                              if (!isdigit(*arg)) {
                                  (void) fprintf(stderr,
                                          gettext("first: argument to "
                                              "-n is not a number\n"));
                                  (void) fprintf(stderr,
                                          gettext(USAGE));
                                  exit(1);
                              }
                              maxrec = atoi(arg);
                              goto next_word;
                          }

                          /* -a displays hostname last on the line */
                    case 'a':
                          aflag++;
                          break;

                          /* -u displays times in UNIX epoch format */
                    case 'u':
                          uflag++;
                          break;

                    default:
                          (void) fprintf(stderr, gettext(USAGE));
                          exit(1);
                }
            }

next_word:
            continue;
        }

        if (strlen(argv[i]) > 2 || strcmp(argv[i], "~") == 0 ||
                getpwnam(argv[i]) != NULL) {
            /* Not a tty number. */
            names[names_num] = argv[i];
            ++names_num;
        } else {
            /* tty number.  Prepend "tty". */
            names[names_num] = strspl("tty", argv[i]);
            ++names_num;
        }
    }

    wtmp = open(wtmpfile, O_RDONLY | O_LARGEFILE);
    if (wtmp < 0) {
        perror(wtmpfile);
        exit(1);
    }
    (void) fstat(wtmp, &stb);
    bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf);
    if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
        (void) signal(SIGINT, onintr);
        (void) signal(SIGQUIT, onintr);
    }
    lines = CHUNK_SIZE;
    ttnames = calloc(lines, sizeof (char *));
    logouts = calloc(lines, sizeof (time_t));
    if (ttnames == NULL || logouts == NULL) {
        (void) fprintf(stderr, gettext("Out of memory \n "));
        exit(2);
    }
    for (bl--; bl >= 0; bl--) {
        (void) lseek(wtmp, (off_t)(bl * sizeof (buf)), 0);
        bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof (buf[0]) - 1];
        for (; bp >= buf; bp--) {
            if (want(bp, &ut_host, &ut_user)) {
                for (i = 0; i <= lines; i++) {
                    if (i == lines)
                        reallocate_buffer();
                    if (ttnames[i] == NULL) {
                        memory_alloc(i);
                        /*
                         * LMAX+HMAX+NMAX+3 bytes have been
                         * allocated for ttnames[i].
                         * If bp->ut_line is longer than LMAX,
                         * ut_host is longer than HMAX,
                         * and ut_user is longer than NMAX,
                         * truncate it to fit ttnames[i].
                         */
                        (void) strlcpy(ttnames[i], bp->ut_line,
                                LMAX+1);
                        (void) strlcpy(ttnames[i]+LMAX+1,
                                ut_host, HMAX+1);
                        (void) strlcpy(ttnames[i]+LMAX+HMAX+2,
                                ut_user, NMAX+1);
                        record_time(&otime, &print,
                                i, bp);
                        break;
                    } else if (linehostnameq(ttnames[i],
                                bp->ut_line, ut_host, ut_user)) {
                        record_time(&otime,
                                &print, i, bp);
                        break;
                    }
                }
            }
            if (print) {
                if (strncmp(bp->ut_line, "ftp", 3) == 0)
                    bp->ut_line[3] = '\0';
                if (strncmp(bp->ut_line, "uucp", 4) == 0)
                    bp->ut_line[4] = '\0';

                ct = ctime(&bp->ut_xtime);
                (void) printf(gettext("%-*.*s  %-*.*s "),
                        NMAX, NMAX, bp->ut_name,
                        LMAX, LMAX, bp->ut_line);
                hostf_len = strlen(bp->ut_host);
                (void) snprintf(hostf, sizeof (hostf),
                        "%-*.*s", hostf_len, hostf_len,
                        bp->ut_host);
                if(uflag) {
                    fpos = snprintf(timef, sizeof (timef),
                            "%10lu ", bp->ut_xtime);
                } else {
                    fpos = snprintf(timef, sizeof (timef),
                            "%10.10s %13.13s ", /* MJC 8 extra chars */
                            ct, 11 + ct);
                }
                if (!lineq(bp->ut_line, "system boot") &&
                        !lineq(bp->ut_line, "system down")) {
                    if (otime == 0 &&
                            bp->ut_type == USER_PROCESS) {

                        if (fpos < sizeof (timef)) {
                            /* timef still has room */
                            (void) snprintf(timef + fpos, sizeof (timef) - fpos,
                                    gettext("  still logged in"));
                        }

                    } else {
                        time_t delta;
                        if (otime < 0) {
                            otime = -otime;
                            /*
                             * TRANSLATION_NOTE
                             * See other notes on "down"
                             * and "- %5.5s".
                             * "-" means "until".  This
                             * is displayed after the
                             * starting time as in:
                             *     16:20 - down
                             * You probably don't want to
                             * translate this.  Should you
                             * decide to translate this,
                             * translate "- %5.5s" too.
                             */

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                if(uflag) {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %-10s"), crmsg);
                                } else {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %-24s"), crmsg);
                                }
                                fpos += chrcnt;
                            }

                        } else {

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                if(uflag) {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %10lu"), otime);
                                } else {
                                    chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
                                            gettext("- %10.10s %13.13s"), ctime(&otime), ctime(&otime) + 11); /* MJC 19 extra chars */
                                }
                                fpos += chrcnt;
                            }

                        }
                        delta = otime - bp->ut_xtime;
                        if (delta < SECDAY) {

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                (void) snprintf(timef + fpos, sizeof (timef) - fpos,
                                        gettext("  (%5.5s)"), asctime(gmtime(&delta)) + 11);
                            }

                        } else {

                            if (fpos < sizeof (timef)) {
                                /* timef still has room */
                                (void) snprintf(timef + fpos, sizeof (timef) - fpos,
                                        gettext(" (%ld+%5.5s)"), delta / SECDAY,
                                        asctime(gmtime(&delta)) + 11);
                            }

                        }
                    }
                }
                if (aflag)
                    (void) printf("%-62.62s %-.*s\n", /* MJC */
                            timef, strlen(hostf), hostf);
                else
                    (void) printf("%-16.16s %-.62s\n", /* MJC */
                            hostf, timef);
                (void) fflush(stdout);
                if (++outrec >= maxrec)
                    exit(0);
            }
            /*
             * when the system is down or crashed.
             */
            if (bp->ut_type == BOOT_TIME) {
                for (i = 0; i < lines; i++)
                    logouts[i] = -bp->ut_xtime;
                bootxtime = -bp->ut_xtime;
                /*
                 * TRANSLATION_NOTE
                 * Translation of this "down " will replace
                 * the %s in "- %s".  "down" is used instead
                 * of the real time session was ended, probably
                 * because the session ended by a sudden crash.
                 */
                crmsg = gettext("down ");
            }
            print = 0;    /* reset the print flag */
        }
    }
    if(uflag) {
        (void) printf(gettext("\nwtmp begins %lu \n"), buf[0].ut_xtime);
    } else {
        ct = ctime(&buf[0].ut_xtime);
        (void) printf(gettext("\nwtmp begins %24.24s \n"), ct);
    }

    /* free() called to prevent lint warning about names */
    free(names);

    return (0);
}
Example #7
0
void
doexec(struct command *t)
{
	tchar *sav;
	tchar *dp, **pv, **av;
	struct varent *v;
	bool slash;
	int hashval, hashval1, i;
	tchar *blk[2];
#ifdef TRACE
	tprintf("TRACE- doexec()\n");
#endif

	/*
	 * Glob the command name.  If this does anything, then we
	 * will execute the command only relative to ".".  One special
	 * case: if there is no PATH, then we execute only commands
	 * which start with '/'.
	 */
	dp = globone(t->t_dcom[0]);
	sav = t->t_dcom[0];
	exerr = 0; t->t_dcom[0] = dp;
	setname(dp);
	xfree(sav);
	v = adrof(S_path /* "path" */);
	if (v == 0 && dp[0] != '/') {
		pexerr();
	}
	slash = gflag;

	/*
	 * Glob the argument list, if necessary.
	 * Otherwise trim off the quote bits.
	 */
	gflag = 0; av = &t->t_dcom[1];
	tglob(av);
	if (gflag) {
		av = glob(av);
		if (av == 0)
			error("No match");
	}
	blk[0] = t->t_dcom[0];
	blk[1] = 0;
	av = blkspl(blk, av);
#ifdef VFORK
	Vav = av;
#endif
	trim(av);
	slash |= any('/', av[0]);

	xechoit(av);		/* Echo command if -x */
	/*
	 * Since all internal file descriptors are set to close on exec,
	 * we don't need to close them explicitly here.  Just reorient
	 * ourselves for error messages.
	 */
	SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0;

	/*
	 * We must do this AFTER any possible forking (like `foo`
	 * in glob) so that this shell can still do subprocesses.
	 */
	(void) sigsetmask(0);

	/*
	 * If no path, no words in path, or a / in the filename
	 * then restrict the command search.
	 */
	if (v == 0 || v->vec[0] == 0 || slash)
		pv = justabs;
	else
		pv = v->vec;
	sav = strspl(S_SLASH /* "/" */, *av); /* / command name for postpending */
#ifdef VFORK
	Vsav = sav;
#endif
	if (havhash)
		hashval = hashname(*av);
	i = 0;
#ifdef VFORK
	hits++;
#endif
	do {
		if (!slash && pv[0][0] == '/' && havhash) {
			hashval1 = hash(hashval, i);
			if (!bit(xhash, hashval1))
				goto cont;
		}

		if (pv[0][0] == 0 || eq(pv[0], S_DOT /* "." */)) { /* don't make ./xxx */
			texec(t, *av, av);
		} else {
			dp = strspl(*pv, sav);
#ifdef VFORK
			Vdp = dp;
#endif
			texec(t, dp, av);
#ifdef VFORK
			Vdp = 0;
#endif
			xfree(dp);
		}
#ifdef VFORK
		misses++;
#endif
cont:
		pv++;
		i++;
	} while (*pv);
#ifdef VFORK
	hits--;
#endif
#ifdef VFORK
	Vsav = 0;
	Vav = 0;
#endif
	xfree(sav);
	xfree((char *)av);
	pexerr();
}
FILE *
ftpd_popen(char *program, char *type, int closestderr)
{
    register char *cp;
    FILE *iop;
    int argc,
      gargc,
      pdes[2],
      pid;
    char **pop,
     *argv[100],
     *gargv[1000],
     *vv[2];
    extern char **ftpglob(register char *v),
    **copyblk(register char **v),
     *strspl(register char *cp, register char *dp);

    if (*type != 'r' && *type != 'w' || type[1])
        return (NULL);

    if (!pids) {
#ifndef HAVE_GETDTABLESIZE
        struct rlimit rlp;

		rlp.rlim_cur = rlp.rlim_max = RLIM_INFINITY;
		if (getrlimit( RLIMIT_NOFILE, &rlp ) )
			return(NULL);
		fds = rlp.rlim_cur;
#else
        if ((fds = getdtablesize()) <= 0)
            return (NULL);
#endif

        if ((pids = (int *) malloc((u_int) (fds * sizeof(int)))) == NULL)
              return (NULL);
#ifdef USG
        (void) memset((char *)pids, fds * sizeof(int), 0);
#else
        bzero((char *) pids, fds * sizeof(int));
#endif
    }
    if (pipe(pdes) < 0)
        return (NULL);

    /* break up string into pieces */
    for (argc = 0, cp = program;; cp = NULL)
        if (!(argv[argc++] = strtok(cp, " \t\n")))
            break;

    /* glob each piece */
    gargv[0] = argv[0];
    for (gargc = argc = 1; argv[argc]; argc++) {
        if (!(pop = ftpglob(argv[argc]))) { /* globbing failed */
            vv[0] = strspl(argv[argc], "");
            vv[1] = NULL;
            pop = copyblk(vv);
        }
        argv[argc] = (char *) pop;  /* save to free later */
        while (*pop && gargc < 1000)
            gargv[gargc++] = *pop++;
    }
    gargv[gargc] = NULL;

    iop = NULL;
    switch (pid = vfork()) {
    case -1:                    /* error */
        (void) close(pdes[0]);
        (void) close(pdes[1]);
        goto pfree;
        /* NOTREACHED */
    case 0:                 /* child */
        if (*type == 'r') {
            if (pdes[1] != 1) {
                dup2(pdes[1], 1);
                if (closestderr)
                    (void) close(2);
                else 
                    dup2(pdes[1], 2);  /* stderr, too! */
                (void) close(pdes[1]);
            }
            (void) close(pdes[0]);
        } else {
            if (pdes[0] != 0) {
                dup2(pdes[0], 0);
                (void) close(pdes[0]);
            }
            (void) close(pdes[1]);
        }
        execv(gargv[0], gargv);
        _exit(1);
    }
    /* parent; assume fdopen can't fail...  */
    if (*type == 'r') {
        iop = fdopen(pdes[0], type);
        (void) close(pdes[1]);
    } else {
        iop = fdopen(pdes[1], type);
        (void) close(pdes[0]);
    }
    pids[fileno(iop)] = pid;

  pfree:for (argc = 1; argv[argc] != NULL; argc++) {
        blkfree((char **) argv[argc]);
        free((char *) argv[argc]);
    }
    return (iop);
}