Exemplo n.º 1
0
Arquivo: glob.c Projeto: muennich/rc3
static List *dmatch(char *d, char *p, char *m) {
    bool matched;
    List *top, *r;
    static DIR *dirp;
    static struct dirent *dp;
    static struct stat s;
    int i;

    /*
       return a match if there are no metacharacters; allows globbing through
       directories with no read permission. make sure the file exists, though.
     */
    matched = TRUE;
    if (m != NULL)
        for (i = 0; p[i] != '\0'; i++)
            if (m[i]) {
                matched = FALSE;
                break;
            }

    if (matched) {
        char *path = nprint("%s/%s", d, p);
        if (lstat(path, &s) < 0)
            return NULL;
        r = nnew(List);
        r->w = ncpy(p);
        r->m = NULL;
        r->n = NULL;
        return r;
    }

    top = r = NULL;
    if (*d == '\0') d = "/";
    if ((dirp = opendir(d)) == NULL)
        return NULL;
    /* opendir succeeds on regular files on some systems, so the stat() call is necessary (sigh) */
    if (stat(d, &s) < 0 || (s.st_mode & S_IFMT) != S_IFDIR) {
        closedir(dirp);
        return NULL;
    }
    while ((dp = readdir(dirp)) != NULL)
        if ((*dp->d_name != '.' || *p == '.') && match(p, m, dp->d_name)) { /* match ^. explicitly */
            matched = TRUE;
            if (top == NULL)
                top = r = nnew(List);
            else
                r = r->n = nnew(List);
            r->w = ncpy(dp->d_name);
            r->m = NULL;
        }
    closedir(dirp);
    if (!matched)
        return NULL;
    r->n = NULL;
    return top;
}
Exemplo n.º 2
0
int main()
{
    int len = length("Paritosh");
    printf("%d\n", len);

    len = lengthPtr("Paritosh");
    printf("%d\n", len);

    char s[123] = "Paritosh";
    cat(s, "Kulkarni");
    printf("%s\n", s);

    char s1[123] = "Paritosh";
    ncpy(s1, "Kulkarni",3);
    printf("%s\n", s1);

    int ret = cmp(s,s1);
    printf("%d\n", ret);

    char* p = "Paritosh";
    //rev1(p);
    //rightShift(p, 3);
    leftShift(p, 3);
    printf("%s", p);

    return 0;
}
Exemplo n.º 3
0
static List *dmatch(char *d, char *p, char *m) {
	bool matched = FALSE;
	List *top, *r;
	static DIR *dirp;
	static struct dirent *dp;
	static struct stat s;
	/* prototypes for XXXdir functions. comment out if necessary */
	extern DIR *opendir(const char *);
	extern struct dirent *readdir(DIR *);
	/*extern int closedir(DIR *);*/

	top = r = NULL;
	/* opendir succeeds on regular files on some systems, so the stat() call is necessary (sigh) */
	if (stat(d, &s) < 0 || (s.st_mode & S_IFMT) != S_IFDIR || (dirp = opendir(d)) == NULL)
		return NULL;
	while ((dp = readdir(dirp)) != NULL)
		if ((*dp->d_name != '.' || *p == '.') && match(p, m, dp->d_name)) { /* match ^. explicitly */
			matched = TRUE;
			if (top == NULL)
				top = r = nnew(List);
			else
				r = r->n = nnew(List);
			r->w = ncpy(dp->d_name);
			r->m = NULL;
		}
	closedir(dirp);
	if (!matched)
		return NULL;
	r->n = NULL;
	return top;
}
Exemplo n.º 4
0
void dupRedirection(char *pIn, int i, char *type) {

	char filepath[500];
	int len = strcspn(pIn + i, "<>");
	ncpy(filepath, pIn + i, len);
	char* rmspace = removeWhiteSpace(filepath);

	//printf("dupred: [%s] [%s] [%s] [%d]\n", pIn + i, rmspace, type, len);

	if (!strcmp(type, ">>")) {
		int fd = open(rmspace, O_APPEND | O_CREAT | O_RDWR, 0666);
		dup2(fd, 1);
		close(fd);
	} else if (!strcmp(type, ">")) {
		int fd = open(rmspace, O_CREAT | O_RDWR, 0666);
		dup2(fd, 1);
		close(fd);
	} else if (!strcmp(type, "<")) {
		int fd = open(rmspace, O_RDWR);
		dup2(fd, 0);
		close(fd);
	}

}
Exemplo n.º 5
0
extern int yylex() {
	static bool dollar = FALSE;
	bool saw_meta = FALSE;
	int c;
	SIZE_T i;			/* The purpose of all these local assignments is to	*/
	char *meta;		/* allow optimizing compilers like gcc to load these	*/
	char *buf = realbuf;		/* values into registers. On a sparc this is a		*/
	YYSTYPE *y = &yylval;		/* win, in code size *and* execution time		*/
	if (errset) {
		errset = FALSE;
		return '\n';
	}
	/* rc variable-names may contain only alnum, '*' and '_', so use dnw if we are scanning one. */
	meta = (dollar ? dnw : nw);
	dollar = FALSE;
	if (newline) {
		--lineno; /* slight space optimization; print_prompt2() always increments lineno */
		print_prompt2();
		newline = FALSE;
	}
top:	while ((c = gchar()) == ' ' || c == '\t')
		w = NW;
	if (c == EOF)
		return END;
	if (!meta[(unsigned char) c]) {	/* it's a word or keyword. */
		checkfreecaret;
		w = RW;
		i = 0;
	read:	do {
			buf[i++] = c;
			if (c == '?' || c == '[' || c == '*')
				saw_meta = TRUE;
			if (i >= bufsize)
				buf = realbuf = erealloc(buf, bufsize *= 2);
		} while ((c = gchar()) != EOF && !meta[(unsigned char) c]);
		while (c == '\\') {
			if ((c = gchar()) == '\n') {
				print_prompt2();
				c = ' '; /* Pretend a space was read */
				break;
			} else {
	bs:			if (meta != dnw) { /* all words but varnames may have a bslash */
					buf[i++] = '\\';
					if (i >= bufsize)
						buf = realbuf = erealloc(buf, bufsize *= 2);
					if (!meta[(unsigned char) c])
						goto read;
				} else {
					ugchar(c);
					c = '\\';
					break;
				}
			}
		}
		ugchar(c);
		buf[i] = '\0';
		w = KW;
		if (i == 2) {
			if (*buf == 'i' && buf[1] == 'f') return IF;
			if (*buf == 'f' && buf[1] == 'n') return FN;
			if (*buf == 'i' && buf[1] == 'n') return IN;
		}
		if (streq(buf, "for")) return FOR;
		if (streq(buf, "else")) return ELSE;
		if (streq(buf, "switch")) return SWITCH;
		if (streq(buf, "while")) return WHILE;
		if (streq(buf, "case")) return CASE;
		w = RW;
		y->word.w = ncpy(buf);
		if (saw_meta) {
			char *r, *s;

			y->word.m = nalloc(strlen(buf) + 1);
			for (r = buf, s = y->word.m; *r != '\0'; r++, s++)
				*s = (*r == '?' || *r == '[' || *r == '*');
		} else {
			y->word.m = NULL;
		}
		return WORD;
	}
	if (c == '`' || c == '!' || c == '@' || c == '~' || c == '$' || c == '\'') {
		checkfreecaret;
		if (c == '!' || c == '@' || c == '~')
			w = KW;
	}
	switch (c) {
	case '!':
		return BANG;
	case '@':
		return SUBSHELL;
	case '~':
		return TWIDDLE;
	case '`':
		c = gchar();
		if (c == '`')
			return BACKBACK;
		ugchar(c);
		return '`';
	case '$':
		dollar = TRUE;
		c = gchar();
		if (c == '#')
			return COUNT;
		if (c == '^')
			return FLAT;
		ugchar(c);
		return '$';
	case '\'':
		w = RW;
		i = 0;
		do {
			buf[i++] = c;
			if (c == '\n')
				print_prompt2();
			if (c == EOF) {
				w = NW;
				scanerror("eof in quoted string");
				return HUH;
			}
			if (i >= bufsize)
				buf = realbuf = erealloc(buf, bufsize *= 2);
		} while ((c = gchar()) != '\'' || (c = gchar()) == '\''); /* quote "'" thus: 'how''s it going?' */
		ugchar(c);
		buf[i] = '\0';
		y->word.w = ncpy(buf);
		y->word.m = NULL;
		return WORD;
	case '\\':
		if ((c = gchar()) == '\n') {
			print_prompt2();
			goto top; /* Pretend it was just another space. */
		}
		ugchar(c);
		c = '\\';
		checkfreecaret;
		c = gchar();
		i = 0;
		goto bs;
	case '(':
		if (w == RW) /* SUB's happen only after real words, not keyowrds, so if () and while () work */
			c = SUB;
		w = NW;
		return c;
	case '#':
		while ((c = gchar()) != '\n') /* skip comment until newline */
			if (c == EOF)
				return END;
		/* FALLTHROUGH */
	case '\n':
		lineno++;
		newline = TRUE;
		/* FALLTHROUGH */
	case ';':
	case '^':
	case ')':
	case '=':
	case '{': case '}':
		w = NW;
		return c;
	case '&':
		w = NW;
		c = gchar();
		if (c == '&')
			return ANDAND;
		ugchar(c);
		return '&';
	case '|':
		w = NW;
		c = gchar();
		if (c == '|')
			return OROR;
		getpair(c);
		if (errset)
			return HUH;
		if ((y->pipe.left = fd_left) == UNSET)
			y->pipe.left = 1;				/* default to fd 1 */
		if ((y->pipe.right = fd_right) == UNSET)
			y->pipe.right = 0;				/* default to fd 0 */
		if (y->pipe.right == CLOSED) {
			scanerror("expected digit after '='");		/* can't close a pipe */
			return HUH;
		}
		return PIPE;
	case '>':
		c = gchar();
		if (c == '>') {
			c = gchar();
			y->redir.type = rAppend;
		} else
			y->redir.type = rCreate;
		y->redir.fd = 1;
		goto common;
	case '<':
		c = gchar();
		if (c == '<') {
			c = gchar();
			if (c == '<') {
				c = gchar();
				y->redir.type = rHerestring;
			} else {
				y->redir.type = rHeredoc;
			}
		} else
			y->redir.type = rFrom;
		y->redir.fd = 0;
	common:
		w = NW;
		getpair(c);
		if (errset)
			return HUH;
		if (fd_right == UNSET) { /* redirection, not dup */
			if (fd_left != UNSET) {
				y->redir.fd = fd_left;
				return SREDIR;
			}
			return (y->redir.type == rFrom || y->redir.type == rCreate) ? REDIR : SREDIR;
		} else { /* dup; recast yylval */
			y->dup.type = y->redir.type;
			y->dup.left = fd_left;
			y->dup.right = fd_right;
			return DUP;
		}
	default:
		w = NW;
		return c; /* don't know what it is, let yacc barf on it */
	}
}