示例#1
0
文件: myls.c 项目: rgv26/L3_2015_2016
void ls(int argc, char **path, int *args, int size)
{
    struct stat status;
    struct dirent *entry;
    DIR *rep;
    int i, j = 0;

    if (argc == 2 && (option_a || option_d || option_l || option_R || option_r))
        ls_from_directory(DEFAULT_DIRECTORY);

    for (i = 1; i < argc; i++)
    {
        if (i == args[j])
        {
            j++;
            continue;
        }

        if (stat(path[i], &status) == ERROR)
        {
            warn(" impossible acceder a %s: ", path[i]);
            continue;
        }

        if (S_ISDIR(status.st_mode))
        {
            ls_from_directory(path[i]);
        }

        if (S_ISREG(status.st_mode))
        {
            if (!option_l)
                printf("%s  ", path[i]);
            else
            {
                print_permissions(&status);
                printf("%s\n", path[i]);
            }
        }

        if (S_ISLNK(status.st_mode))
        {
            if (!option_l)
                printf("%s  ", path[i]);
            else
            {
                print_permissions(&status);
                printf("%s\n", path[i]);
            }
        }
    }
}
示例#2
0
int main(int argc, const char * argv[])
{
    
    int i;
    struct stat buf;
    char *ptr;
    const char *filename=argv[1];
    struct stat statbuf;
    
//    print_permissions(argv[i],&statbuf);
    
    for(i=1;i<argc;i++){
        printf("%s:",argv[i]);
        if(lstat(argv[i],&buf)<0){
            perror("lstat error");
            continue;
        }
        // /etc/passwd
        if (S_ISREG(buf.st_mode))
            ptr = "regular";
        // /etc
        else if (S_ISDIR(buf.st_mode))
            ptr = "directory";
        // /dev/tty
        else if (S_ISCHR(buf.st_mode))
            ptr = "character special";
        // /dev/scsi/host0/bus0/target0/lun0/cd
        else if (S_ISBLK(buf.st_mode))
            ptr = "block special";
        // /dev/initctl
        else if (S_ISFIFO(buf.st_mode))
            ptr = "fifo";
        //  /dev/cdrom
        else if (S_ISLNK(buf.st_mode))
            ptr = "symbolic link";
        // /dev/log:
        else if (S_ISSOCK(buf.st_mode))
            ptr = "socket";
        // else
        else
            {
                ptr = "** unknown mode **";
                printf("%s\n", ptr);
            }
        printf("%s",ptr);
        print_permissions(argv[i],&statbuf);
        
    }
    return 0;
}
示例#3
0
文件: ls.c 项目: hoangt/vmwos
static void list_file(char *name) {

	struct stat stat_buf;

	stat(name,&stat_buf);
	print_permissions(stat_buf.st_mode);
	printf(" %d %d %d %d ",
		stat_buf.st_nlink,
		stat_buf.st_uid,
		stat_buf.st_gid,
		stat_buf.st_size);
	print_date(stat_buf.st_mtime);
	printf(" %s\n",name);
}
示例#4
0
文件: long.c 项目: PlatonV/ls
void			print_long_file(char *file, t_maxes maxes)
{
	t_stat	sstat;

	if (check_flag('a') || !(get_filename(file)[0] == '.'))
	{
		lstat(file, &sstat);
		print_permissions(file);
		print_nmax(sstat.st_nlink, maxes.link);
		ft_putchar(' ');
		print_strmax_uid(sstat, maxes.owner);
		print_strmax_gid(sstat, maxes.group);
		print_size(sstat, maxes);
		print_date(sstat);
		print_file2(file);
		ft_putendl("");
	}
}
示例#5
0
char *listoutput(char *input, char *format) {
    struct filedetails details;
    int inputindex;
    char output[MAX_LINE_SIZE], *temp;
    strcpy(output, input);
    if (ftp_split_ls(output, &details))
        return NULL;
    memset(output, 0, MAX_LINE_SIZE);
    for (inputindex = 0; format[inputindex] != 0; inputindex++) {
        if (format[inputindex] == '%') {
            inputindex++;
            switch (format[inputindex]) {
            case '%' :
                sprintf(output, "%s%%", output);
                break;
            case 'f' :
                sprintf(output, "%s%s", output, details.filename);
                break;
            case 's' :
                sprintf(output, "%s%lu", output, details.size);
                break;
            case 't' :
                sprintf(output, "%s%s", output, asctime(&details.timestamp));
                break;
            case 'p' :
                temp = print_permissions(details.permissions);
                sprintf(output, "%s%s", output, temp);
                free(temp);
                break;
            case 'o' :
                sprintf(output, "%s%s", output, details.owner);
                break;
            case 'g' :
                sprintf(output, "%s%s", output, details.group);
                break;
            case 'l' :
                sprintf(output, "%s%d", output, details.links);
                break;
            }
        } else
            sprintf(output, "%s%c", output, format[inputindex]);
    }
    return strdup(output);
}
示例#6
0
文件: myls.c 项目: rgv26/L3_2015_2016
void ls_from_directory(char *dir)
{
    struct stat status;
    struct dirent *entry;
    DIR *rep;
    int i = 0, k = 1;

    if ((rep = opendir(dir)) == NULL)
    {
        perror("opendir - ls_from_current_directory()\n");
        return;
    }

    while ((entry = readdir(rep)) != NULL)
    {
        if (!option_a)
        {
            if (entry->d_name[0] == '.')
                continue;
        }

        if (stat(entry->d_name, &status) == ERROR)
        {
            warn(" impossible acceder a %s: ", entry->d_name);
            continue;
        }

        if (!option_l)
        {
            printf("%s  ", entry->d_name);

            if (k++ % 5 == 0)
                printf("\n");
        }

        else
        {
            print_permissions(&status);
            printf("%s\n", entry->d_name);
        }
    }
    closedir(rep);
}
示例#7
0
文件: ls_new.c 项目: TimJung/cis452
int main(int argc,char *argv[]){
    DIR *dirPtr;
    struct dirent *entryPtr;
    struct stat statBuf;
    time_t t;
    

    if(argc < 2){
      printf("No argument supplied. Please use -l or -i with an optional directory path\n");
      return 0;
    } else if(argc > 3) {
	printf("Too many arguments supplied. Please use only -l or -i\n [directory path]");
	return 0;
    }
    
    if(argv[2]!=NULL){ //directory supplied as second argument
      if((chdir(argv[2]))==-1){ //not valid
	printf("lsn: cannot access %s: No such file or directory\n", argv[2]);
	return 0;
      }
     
    }
    dirPtr = opendir("."); 
    
    //Show long listing
    if(strcmp(argv[1], "-l") == 0){
      int blocks = 0;
      while ((entryPtr = readdir(dirPtr))){
        stat(entryPtr->d_name, &statBuf);
	struct passwd* user = getpwuid(statBuf.st_uid);
	struct group* group = getgrgid(statBuf.st_gid);

	//need to filter out some unneeded results
	if(strcmp(entryPtr->d_name, ".") != 0 &&
	    strcmp(entryPtr->d_name, "..") != 0){
	  char timebuff[20]; //buffer to store time
	  //convert time to human readable form
	  strftime(timebuff, 20, "%b %2d %H:%M", localtime(&statBuf.st_mtime));

	  print_permissions(statBuf); //file permissions
	  printf("%2d ", statBuf.st_nlink); //number of links
	  if(user!=NULL)
	    printf("%s ", user->pw_name); //user id
	  else
	    printf("%d ", statBuf.st_uid); //protects against possible segfault
	  if(group!=NULL)
	    printf("%s ", group->gr_name); //group id
	  else
	    printf("%d ", statBuf.st_gid); //protects against possible segfault
	  printf("%5d ", statBuf.st_size); //file size
	  printf("%s ", timebuff); //time modified
	  if(S_ISDIR(statBuf.st_mode)) //make name blue if directory
	    printf("\033[1;34m");
	  else if((statBuf.st_mode & S_IXOTH)) //make name green if executible
	    printf("\033[1;32m");
	  printf("%s\033[0m\n", entryPtr->d_name); //file name

	  blocks += statBuf.st_blocks;
	}
      }
      closedir(dirPtr);
      printf("total %d\n", blocks/2); //total number of blocks

    } else if(strcmp(argv[1], "-i") == 0) { //Show inode number
	while ((entryPtr = readdir(dirPtr))){
	  stat(entryPtr->d_name, &statBuf);
	  //need to filter out some unneeded results
	  if(strcmp(entryPtr->d_name, ".") != 0 &&
	      strcmp(entryPtr->d_name, "..") != 0){
	    printf("%lu ", statBuf.st_ino); //inode number
	    if(S_ISDIR(statBuf.st_mode)) //make name blue if directory
	      printf("\033[1;34m");
	    else if((statBuf.st_mode & S_IXOTH)) //make name green if executible
	      printf("\033[1;32m");
	    printf("%-10s\033[0m ", entryPtr->d_name); //file name
	  }
	}
	printf("\n");
	closedir(dirPtr);
    } else {
	printf("Unknown argument %s. Please use -l or -i", argv[1]);
    }

    return 0;
}
void display_file_info(const char *filename)
{
  // Local parameters
  int i; // Loop index
  char *token, *prev_token; // For string tokenizing
  struct stat buf;

  if (lstat(filename, &buf) < 0) {
    fprintf(stderr, "stat error: %s\n", strerror(errno));
    return;
  }

  // Print permissions
  print_permissions(&buf);
  printf("\t");

  // Print the number of links to the file
  printf("%lu\t", (size_t) buf.st_nlink);

  // Print the user id of the file
  struct passwd *pwd = getpwuid(buf.st_uid);
  printf("%s\t", pwd->pw_name);

  // Print the group id of the file
  struct group *grp = getgrgid(buf.st_gid);
  printf("%s\t", grp->gr_name);

  // Print the size of the file
  printf("%lu\t", (size_t) buf.st_size);

  // Print the time of last modification
  char buffer[MAX_STRING_SIZE];
  strftime(buffer, MAX_STRING_SIZE, "%b %d %H:%M", localtime(&(buf.st_mtime)));
  printf("%s\t", buffer);

  // Duplicate filename contents
  for (i = 0; i < strlen(filename); ++i) {
    buffer[i] = filename[i];
  }
  buffer[ strlen(filename) ] = 0;

  // Get the last '/' delimeted string; the local filename
  token = strtok(buffer, "/"), prev_token = 0;
  while ((token = strtok(0, "/")) != 0) {
    prev_token = token;
  }

  printf("%s", prev_token);

  // Determine the type of file
  print_file_type(&buf);

  if (S_ISLNK(buf.st_mode)) {
    printf(" -> ");

    // Zero buffer contents
    int i;
    char buffer[MAX_STRING_SIZE];
    for (i = 0; i < MAX_STRING_SIZE; ++i) {
      buffer[i] = '\0';
    }

    readlink(filename, buffer, MAX_STRING_SIZE);

    printf("%s", buffer);
  }

  printf("\n");
}
示例#9
0
int
main(int argc, char **argv)
{
	const char * newkeyfile = NULL;
	int keyswanted = 0;
	char * tok, * brkb = NULL, * eptr;
	long keynum;
	uint64_t machinenum = (uint64_t)(-1);
	uint64_t kfmachinenum;
	const char * missingkey;
	int passphrased = 0;
	uint64_t maxmem = 0;
	double maxtime = 1.0;
	char * passphrase;
	const char * print_key_id_file = NULL;
	const char * print_key_permissions_file = NULL;
	const char * ch;
	char * optarg_copy;	/* for strtok_r. */

	WARNP_INIT;

	/* Initialize key cache. */
	if (crypto_keys_init()) {
		warnp("Key cache initialization failed");
		exit(1);
	}

	/* Parse arguments. */
	while ((ch = GETOPT(argc, argv)) != NULL) {
		GETOPT_SWITCH(ch) {
		GETOPT_OPTARG("--outkeyfile"):
			if (newkeyfile != NULL)
				usage();
			newkeyfile = optarg;
			break;
		GETOPT_OPT("-r"):
			keyswanted |= CRYPTO_KEYMASK_READ;
			break;
		GETOPT_OPT("-w"):
			keyswanted |= CRYPTO_KEYMASK_WRITE;
			break;
		GETOPT_OPT("-d"):
			/*
			 * Deleting data requires both delete authorization
			 * and being able to read archives -- we need to be
			 * able to figure out which bits are part of the
			 * archive.
			 */
			keyswanted |= CRYPTO_KEYMASK_READ;
			keyswanted |= CRYPTO_KEYMASK_AUTH_DELETE;
			break;
		GETOPT_OPT("--nuke"):
			keyswanted |= CRYPTO_KEYMASK_AUTH_DELETE;
			break;
		GETOPT_OPTARG("--keylist"):
			/*
			 * This is a deliberately undocumented option used
			 * mostly for testing purposes; it allows a list of
			 * keys to be specified according to their numbers in
			 * crypto/crypto.h instead of using the predefined
			 * sets of "read", "write" and "delete" keys.
			 */
			if ((optarg_copy = strdup(optarg)) == NULL) {
				warn0("Out of memory");
				exit(0);
			}
			for (tok = strtok_r(optarg_copy, ",", &brkb);
			     tok;
			     tok = strtok_r(NULL, ",", &brkb)) {
				keynum = strtol(tok, &eptr, 0);
				if ((eptr == tok) ||
				    (keynum < 0) || (keynum > 31)) {
					warn0("Not a valid key number: %s",
					    tok);
					free(optarg_copy);
					exit(1);
				}
				keyswanted |= (uint32_t)(1) << keynum;
			}
			free(optarg_copy);
			break;
		GETOPT_OPTARG("--passphrase-mem"):
			if (maxmem != 0)
				usage();
			if (humansize_parse(optarg, &maxmem)) {
				warnp("Cannot parse --passphrase-mem"
				    " argument: %s", optarg);
				exit(1);
			}
			break;
		GETOPT_OPTARG("--passphrase-time"):
			if (maxtime != 1.0)
				usage();
			maxtime = strtod(optarg, NULL);
			if ((maxtime < 0.05) || (maxtime > 86400)) {
				warn0("Invalid --passphrase-time argument: %s",
				    optarg);
				exit(1);
			}
			break;
		GETOPT_OPT("--passphrased"):
			if (passphrased != 0)
				usage();
			passphrased = 1;
			break;
		GETOPT_OPTARG("--print-key-id"):
			if (print_key_id_file != NULL)
				usage();
			print_key_id_file = optarg;
			break;
		GETOPT_OPTARG("--print-key-permissions"):
			if (print_key_permissions_file != NULL)
				usage();
			print_key_permissions_file = optarg;
			break;
		GETOPT_MISSING_ARG:
			warn0("Missing argument to %s\n", ch);
			/* FALLTHROUGH */
		GETOPT_DEFAULT:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* We can't print ID and permissions at the same time. */
	if ((print_key_id_file != NULL) && (print_key_permissions_file != NULL))
		usage();

	if ((print_key_id_file != NULL) ||
	    (print_key_permissions_file != NULL)) {
		/* We can't combine printing info with generating a new key. */
		if (newkeyfile != NULL)
			usage();

		/* We should have processed all arguments. */
		if (argc != 0)
			usage();

		/* Print info. */
		if (print_key_id_file != NULL)
			print_id(print_key_id_file);
		if (print_key_permissions_file != NULL)
			print_permissions(print_key_permissions_file);
	}

	/* We should have an output key file. */
	if (newkeyfile == NULL)
		usage();

	/*
	 * It doesn't make sense to specify --passphrase-mem or
	 * --passphrase-time if we're not using a passphrase.
	 */
	if (((maxmem != 0) || (maxtime != 1.0)) && (passphrased == 0))
		usage();

	/* Warn the user if they're being silly. */
	if (keyswanted == 0) {
		warn0("None of {-r, -w, -d, --nuke} options are specified."
		    "  This will create a key file with no keys, which is"
		    " probably not what you intended.");
	}

	/* Read the specified key files. */
	while (argc-- > 0) {
		/*
		 * Suck in the key file.  We could mask this to only load the
		 * keys we want to copy, but there's no point really since we
		 * export keys selectively.
		 */
		if (keyfile_read(argv[0], &kfmachinenum, ~0)) {
			warnp("Cannot read key file: %s", argv[0]);
			exit(1);
		}

		/*
		 * Check that we're not using key files which belong to
		 * different machines.
		 */
		if (machinenum == (uint64_t)(-1)) {
			machinenum = kfmachinenum;
		} else if (machinenum != kfmachinenum) {
			warn0("Keys from %s do not belong to the "
			    "same machine as earlier keys", argv[0]);
			exit(1);
		}

		/* Move on to the next file. */
		argv++;
	}

	/* Make sure that we have the necessary keys. */
	if ((missingkey = crypto_keys_missing(keyswanted)) != NULL) {
		warn0("The %s key is required but not in any input key files",
		    missingkey);
		exit(1);
	}

	/* If the user wants to passphrase the keyfile, get the passphrase. */
	if (passphrased != 0) {
		if (readpass(&passphrase,
		    "Please enter passphrase for keyfile encryption",
		    "Please confirm passphrase for keyfile encryption", 1)) {
			warnp("Error reading password");
			exit(1);
		}
	} else {
		passphrase = NULL;
	}

	/* Write out new key file. */
	if (keyfile_write(newkeyfile, machinenum, keyswanted,
	    passphrase, maxmem, maxtime))
		exit(1);

	/* Success! */
	return (0);
}