Beispiel #1
0
static void default_env(){
	rrdpath = strdup(DATA_DIR);

	struct passwd* passwd = getpwnam(drop_username);
	struct group* group = getgrnam(drop_group);
	if ( passwd ){
		drop_uid = passwd->pw_uid;
	} else {
		fprintf(stderr, "%s: no such user `%s': defaulting to current user\n", program_name, drop_username);
		drop_uid = getuid();
		drop_username = get_username(drop_uid);
	}
	if ( group ){
		drop_gid = group->gr_gid;
	} else {
		fprintf(stderr, "%s: no such group `%s': defaulting to current primary group\n", program_name, drop_group);
		drop_gid = getgid();
		drop_group = get_groupname(drop_gid);
	}

	/* set database username to current user */
	struct passwd* user = getpwuid(getuid());
	if ( user ){
		strncpy(db_username, user->pw_name, sizeof(db_username));
		db_username[sizeof(db_username)-1] = '\0';
	} else {
		fprintf(stderr, "%s: failed to get current user\n", program_name);
	}
	if ( strcmp(program_name, "MArelayD") == 0 ){
		have_relay_daemon = true;
	} else {
		have_control_daemon = true;
	}
}
Beispiel #2
0
Datei: ls.c Projekt: Kaikaiw/apue
static void
format(FTSENT *cur, FTSENT_PRT *prt)
{
    char buf[NAME_BUFSZ];
    cur->fts_pointer=prt;

    if (f_inodenum)
        UP(buf, "%llu", (LLU)cur->fts_statp->st_ino, prt->s_inode);

    /**
     * st_blocks is a calculated number of blocks via file system block size
     * the value is the same with that displayed when using default ls
     * it can also be calculated via:
     * howmany(st_blocks, blocksize) e.g. st_blocks/blocksize
     */
    if (f_longfmt || f_dispblock) {
        LL b=(LL)cur->fts_statp->st_blocks;
        b/=block_size_factor;
        UP(buf, "%lld", b, prt->s_block);
        prt->t_block+=b;
    }

    if (f_longfmt) {
        mode_t m=cur->fts_statp->st_mode;
        if (f_kilobytes) {
            LLU s=cur->fts_statp->st_size;
            UP(buf, "%llu", (LLU)(s/1024.0+0.5), prt->s_float);
        } else if (f_readable) {
            LLU s=cur->fts_statp->st_size;
            char *read=readable(s);
            prt->s_read=MAX(prt->s_read, strlen(read));
            free(read);
        } else {
            UP(buf, "%llu", (LLU)cur->fts_statp->st_size, prt->s_size);
        }
        UP(buf, "%hu", (short)cur->fts_statp->st_nlink, prt->s_link);
        if (S_ISCHR(m) || S_ISBLK(m)) {
            UP(buf, "%d", major(cur->fts_statp->st_rdev), prt->s_major);
            UP(buf, "%d", minor(cur->fts_statp->st_rdev), prt->s_minor);
        }
    }

    if (f_numric) {
        UP(buf, "%d", cur->fts_statp->st_uid, prt->s_uid);
        UP(buf, "%d", cur->fts_statp->st_gid, prt->s_gid);
    } else if (f_longfmt) {
        char *username=get_username(cur->fts_statp->st_uid);
        char *groupname=get_groupname(cur->fts_statp->st_uid);
        if (username!=NULL)
            UP(buf, "%s", username, prt->s_uname);
        if (groupname!=NULL)
            UP(buf, "%s", groupname, prt->s_gname);
        free(username);
        free(groupname);
    }

    UP(buf, "%s", cur->fts_name, prt->s_name);
    prt->col_length=MAX(prt->col_length, prt->s_name);
    prt->entries++;
}
Beispiel #3
0
void build_ustar_header_from_file(FILE_HEADER* header, char* filename) {
	struct stat buffer;
	errno = 0;

	if(stat(filename, &buffer) == 0 && errno == 0) {
		clean_up_header(header);

		get_prefix_and_name(filename, header->name, header->prefix);
		get_file_mode(header->mode, buffer.st_mode);
		get_id(header->uid, buffer.st_uid);
		get_id(header->gid, buffer.st_gid);
		get_size(header->size, buffer.st_size);
		get_modification_time(header->mtime, buffer.st_mtime);
		get_file_type(header->typeflag, buffer.st_mode);
		strcpy(header->magic, "ustar ");
		strcpy(header->version, " \0");
		get_username(header->uname, buffer.st_uid);
		get_groupname(header->gname, buffer.st_gid);
		get_device_numbers(header->devmajor, header->devminor, buffer.st_dev);
		calculate_checksum(header);
	}
	else
		fprintf(stderr, "%s '%s': %s\n", STAT_FAILURE_ERR, filename,
															strerror(errno));
}
Beispiel #4
0
static int
printgname(FTSENT *p, int length)
{
    char *groupname=get_groupname(p->fts_statp->st_gid);
    if (groupname!=NULL) {
        printf("%-*s  ", length, groupname);
        free(groupname);
        return 0;
    }
    return -1;
}
Beispiel #5
0
void top(char* argv) {

    UNUSED(argv);

    struct ProcessInfo data[20];
    int pcount;

    termios oldStatus;
    termios topStatus = { 0, 0 };

    ioctl(0, TCGETS, (void*) &oldStatus);
    ioctl(0, TCSETS, (void*) &topStatus);

    // Reset the screen
    moveCursor(1, 1);
    clearScreen(CLEAR_ALL);


    printf("PID   PPID  USER\t\t  GROUP\t\t %%CPU   PRIO STATE  COMMAND\n");

    do {
        moveCursor(2, 1);
        clearScreen(CLEAR_BELOW);

        pcount = pinfo(data, 20);

        for (int i = 0; i < pcount; i++) {
            printf("%2d\t", data[i].pid);
            printf("%2d\t", data[i].ppid);
            printf("%10s\t", get_username(data[i].uid));
            printf("%10s\t", get_groupname(data[i].gid));
            printf("%3d\t", data[i].cputime);
            printf("%1d\t", data[i].priority);
            printf("%6s ", (data[i].state)? "alive":  "zombie");
            //printf("%s\t", asctime(localtime(&data[i].timeStart)));
            printf("%s", data[i].name);
            printf("\n");
        }

    } while(getchar() != 'q');

    clearScreen(CLEAR_ALL);
    moveCursor(0, 0);
    ioctl(0, TCSETS, (void*) &oldStatus);
}