Esempio n. 1
0
char *getmodes(File *file)
{
    char *unknownmodes = "???????????";
    char *modes = malloc((strlen(unknownmodes) + 1) * sizeof(*modes));
    if (!modes) {
        return strdup(unknownmodes);
    }
    struct stat *pstat = getstat(file);
    if (!pstat) {
        free(modes);
        return strdup(unknownmodes);
    }

    char *p = modes;
    if (islink(file))
        *p++ = 'l';
    else if (isdir(file))
        *p++ = 'd';
    else if (isblockdev(file))
        *p++ = 'b';
    else if (ischardev(file))
        *p++ = 'c';
    else if (isfifo(file))
        *p++ = 'p';
    else if (issock(file))
        *p++ = 's';
    else
        *p++ = '-';

    if (pstat->st_mode & S_IRUSR)
        *p++ = 'r';
    else
        *p++ = '-';

    if (pstat->st_mode & S_IWUSR)
        *p++ = 'w';
    else
        *p++ = '-';

    if (issetuid(file)) {
        if (pstat->st_mode & S_IXUSR)
            *p++ = 's';
        else
            *p++ = 'S';
    } else {
        if (pstat->st_mode & S_IXUSR)
            *p++ = 'x';
        else
            *p++ = '-';
    }

    if (pstat->st_mode & S_IRGRP)
        *p++ = 'r';
    else
        *p++ = '-';

    if (pstat->st_mode & S_IWGRP)
        *p++ = 'w';
    else
        *p++ = '-';

    if (issetgid(file)) {
        if (pstat->st_mode & S_IXGRP)
            *p++ = 's';
        else
            *p++ = 'S';
    } else {
        if (pstat->st_mode & S_IXGRP)
            *p++ = 'x';
        else
            *p++ = '-';
    }

    if (pstat->st_mode & S_IROTH)
        *p++ = 'r';
    else
        *p++ = '-';

    if (pstat->st_mode & S_IWOTH)
        *p++ = 'w';
    else
        *p++ = '-';

    if (issticky(file)) {
        if (pstat->st_mode & S_IXOTH)
            *p++ = 't';
        else
            *p++ = 'T';
    } else {
        if (pstat->st_mode & S_IXOTH)
            *p++ = 'x';
        else
            *p++ = '-';
    }

    /* POSIX says we should print a space if there are no extended ACLs,
       GNU ls prints nothing
       follow POSIX */
    if (!islink(file) && hasacls(file))
        *p++ = '+';
    else
        *p++ = ' ';

    *p++ = '\0';
    return modes;
}
void
munchinput(Engine *E, char *buf)
{	
	int 	eaten = 0;


	if (strlen(buf) > 0)
	{
              	while (eaten < strlen(buf))
               	{
			char *tptr;
			Datum *t1;

			t1 = (Datum *) mmalloc(E, sizeof(Datum),
					"Datum *t1 in munchinput(), main.c");

			if (t1 == NULL)
			{
				mprint(E, NULL, siminfo, "Could not allocate memory for Datum *t1");
				mexit(E, "See above messages.", -1);
			}

			t1->next = NULL;
			t1->prev = NULL;
			t1->value = 0;

			t1->data = (char*) mmalloc(E, (strlen(buf)+1)*sizeof(char), 
						"char *t1->data in munchinput(), main.c");
			if (t1->data == NULL)
			{
				mfree(E, t1, "char *t1 in main.c");
				mprint(E, NULL, siminfo, "Could not allocate memory for char *t1->data");
				mexit(E, "See above messages.", -1);
			}
			tptr = t1->data;

			/* 	Throw away all chars till we see a non-sepchar 	*/
			while (isasmsep(buf[eaten]) && (eaten < strlen(buf)))
			{
				eaten++;
			}

			/*							*/
			/*   I refer to tokens such as '(' etc. as "sticky":	*/
			/*   they may be "stuck" onto another token, and are	*/
			/*   NOT separators : we have to allocate a list entry 	*/
			/*   for them. 	So, get one sticky char:		*/
			/*							*/
			if (issticky(buf[eaten]))
			{
				*tptr++ = buf[eaten++];
			}
			else while(!isasmsep(buf[eaten]) &&
					!issticky(buf[eaten]) &&
					(eaten < strlen(buf))
				)
			{
				/*						*/
				/*	Get all non sepchars into t1->data	*/
				/*	If we see a quoted string, "*" (_not_	*/
				/*	'*'), gobble it including any sepchars,	*/
				/*	till end of input (we get line @a time)	*/
				/*	or matching quote.			*/
				/*						*/
				if (buf[eaten] == '"')
				{
					/*		Discard opening quote:		*/
					eaten++;

					/*	Must make not to go past newline:	*/
					while ((buf[eaten] != '"') &&
						(buf[eaten] != '\n') &&
						(eaten < strlen(buf)))
					{
						*tptr++ = buf[eaten++];
					}
					
					/*	Discard trailing quote if its there:	*/
					if (eaten < strlen(buf) && (buf[eaten] == '"'))
					{
						eaten++;
					}
				}
				else
				{
					*tptr++ = buf[eaten++];
				}
			}

			*tptr = '\0';

			/*	If we actually ate any food, put it in our stomach	*/
			if (tptr != t1->data)
			{
				if ((E->istream.head == NULL)  || (E->istream.tail == NULL))
				{
					E->istream.tail = E->istream.head = E->istream.masthead = t1;

					/*							*/
					/*    NOTE tail and head now point to the lone datum 	*/
					/*    and they _both_ have null pre- and next-.		*/
					/*							*/
					E->istream.masthead->prev = NULL;
					E->istream.masthead->next = NULL;

					E->istream.head->prev = NULL;
					E->istream.head->next = NULL;

					E->istream.tail->prev = NULL;
					E->istream.tail->next = NULL;
				}
				else
				{
					/*							*/
					/*  Add new datum to _tail_ of list. MUST keep it FIFO 	*/
					/*  for the asm to be parsed correctly.			*/
					/*							*/
					t1->next = E->istream.tail;
					E->istream.tail->prev = t1;
					E->istream.tail = t1;
				}
			}
			else
			{
				mfree(E, t1->data, "t1->data in munchinput");
				mfree(E, t1, "t1 in munchinput");
			}
			/* careful : DO NOT free t1 ! */
		}
	}


	return;
}