Exemplo n.º 1
0
Arquivo: dir.c Projeto: noakad/espl
int main(int argc, char *argv[])
{
        DIR             *dip;
        struct dirent   *dit; //ino_t  d_ino (file serial number)
			     //char   d_name[]    name of entry
        int i = 0;
	int sz;
	char *buf;
	size_t size;
	FILE* fp;
 
        // check to see if user entered the argumants OPTIONS and //directory name
        if (argc < 3)
        {
                printf("Command missing args\n");
                return 0;
        }
 
        /* DIR *opendir(const char *name);
         *
         * Open a directory stream to argv[2] and make sure
         * it's a readable and valid (directory) */
        if ((dip = opendir(argv[2])) == NULL)
        {
		printf("directory name is not specified - printing names of current dir\n");
		getcwd(buf, size);
		dip = opendir(argv[2]);
		printFiles(dip, dit, i);
        }
 
        /*  struct dirent *readdir(DIR *dir);
         *
         * Read in the files from argv[2] and print */
	
	printFiles(dip, dit, i);
 
        /* int closedir(DIR *dir);
         *
         * Close the stream to argv[1]. And check for errors. */
        if (closedir(dip) == -1)
        {
                perror("closedir");
                return 0;
        }
 
        printf("\nDirectory stream is now closed\n");
        return 1;
} 
Exemplo n.º 2
0
Arquivo: news.c Projeto: jschauma/news
/*
 * The news utility prints news items stored in /var/news.  When invoked
 * without any arguments, news prints the name and timestamp of each
 * current item followed by its contents, most recent first.
 */
int
main(int argc, char *argv[])
{
	int ch, len;
	char *home, *nf;

	extern int optind;

	f_all = f_name = f_short = 0;

#ifdef __NetBSD__
	setprogname(argv[0]);
#endif

	while ((ch = getopt(argc, argv, "ans")) != -1) {
		switch(ch) {
		case 'a':
			f_all = 1;
			break;
		case 'n':
			f_name = 1;
			f_short = 0;
			break;
		case 's':
			f_short = 1;
			f_name = 0;
			break;
		case '?':
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (*argv && (f_all || f_name || f_short))
		errx(EXIT_FAILURE, "Options are not allowed with file names.");
		/* NOTREACHED */

	if ((home = getenv("HOME")) == NULL)
		errx(EXIT_FAILURE, "Unable to find HOME variable.");
		/* NOTREACHED */

	len = strlen(home) + strlen(NEWS_TIME) + 2;
	if ((nf = malloc(len)) == NULL)
		errx(EXIT_FAILURE, "Unable to allocate memory.");
		/* NOTREACHED */

	if ((snprintf(nf, len, "%s/%s", home, NEWS_TIME)) == -1)
		errx(EXIT_FAILURE, "Unable to prepare path.");
		/* NOTREACHED */

	nf_time = updateTimestamp(nf);
	free(nf);

	return printFiles(argv);
}
Exemplo n.º 3
0
int main (int argc, char *argv[]) {
  if (argc==2) { 
    if( strcmp("-m", argv[1]) == 0 ) {
    return printFilesWithPermisions(".", 0, ""); 
    }
  } else if(argc ==3) { 
    return printFilesPrefixMatch(".", 0 , argv[2]);
  } else if (argc==1) {
    return printFiles(".", 0, "");
  } else {
    printf("usage ./<filename> <string> to match against\n" );
    return -1;
  }
  return 0;
}
Exemplo n.º 4
0
bool KPrinter::cmd(int c, QPainter *painter, QPDevCmdParam *p)
{
	bool value(true);
	if (c == QPaintDevice::PdcBegin)
	{
		d->m_impl->statusMessage(i18n("Initialization..."), this);
		d->m_pagenumber = 1;
		preparePrinting();
		d->m_impl->statusMessage(i18n("Generating print data: page %1").arg(d->m_pagenumber), this);
	}
	value = d->m_wrapper->cmd(c,painter,p);
	if (c == QPaintDevice::PdcEnd)
	{
		// this call should take care of everything (preview, output-to-file, filtering, ...)
		value = value && printFiles(QStringList(d->m_wrapper->outputFileName()),true);
		// reset "ready" state
		finishPrinting();
	}
	return value;
}
Exemplo n.º 5
0
int printFiles(char* directoryName, int indent, char* fileName) {
  DIR * directory = opendir(directoryName);
  struct dirent *currentFile;
  struct stat statp;
  char fileNameString[1024];
  if (directory == NULL) {
    perror("failed to open directory");
    return -1;
  }
  while (( currentFile = readdir(directory))) {
    if (!(strcmp(currentFile->d_name,".") && strcmp(currentFile->d_name,".."))) {
      continue;
    }
    strcpy(fileNameString,directoryName);
    strcat(fileNameString,"/");
    strcat(fileNameString,currentFile->d_name);
    if(stat(fileNameString,&statp)) {
      perror("stat failed");
      return -1;
    } if (S_ISREG(statp.st_mode)) {
      int i;
      for (i = 0; i < indent; i++) {
        printf("    ");
      } printf("%s (%d bytes)\n", currentFile->d_name, (int)statp.st_size);
    } else if (!S_ISREG(statp.st_mode)) {
      int i;
      for(i = 0; i < indent; i++) {
        printf("    ");
      } printf("%s (subdirectory)\n", currentFile->d_name);
      printFiles(fileNameString, indent + 1, fileName);
    }
  }
  if (errno!=0) {
    perror("readdir failed");
    return -1;
  } if (closedir(directory)) {
    perror("closedir failed");
    return -1;
  } 
  return 0;
}
Exemplo n.º 6
0
void readFile(char *file)
{
  int fd;
  int num_files;
  header *fileHeader[1000];

  if((fd = open(file, O_RDONLY)) == -1)
  {
    perror("open");
    exit(EXIT_FAILURE);
  }
  
  num_files = readTar(fileHeader, fd);
  
  if(v_flag)
    printVerbose(fileHeader, num_files);
  else
    printFiles(fileHeader, num_files);

  return;
}
Exemplo n.º 7
0
int main() {
	int flag,no,size,data;
	struct node *root;
	root = defaultFile();
	root->size=1000;
	data=100;
	flag=no=size=0;
	while( flag==0 ) {
		printf("Enter no's \n1.insert\n 2.Delete\n 3.Print files \n 4.Exit\n");
		scanf("%d",&no);
		printf(" no is %d\n",no);
		switch(no) {
			case 1:
				printf("Enter file size\n");
				scanf("%d",&size);
				root = insert(root, size, data);
				data = data+1;
				break;
			case 2:
				printf("Enter file name to delete\n");
				scanf("%d",&size);
				root = deleteFiles(root, size);
				break;
			case 3:
				printFiles(root);
				break;
			case 4:
				flag=1;
				printf("Quitting from loop\n");
				break;
			default:
				printf("Enter a valid no \n");
				break;
		}
	}
}
Exemplo n.º 8
0
Arquivo: c.c Projeto: LucasAbreu/c
int main (int argc, const char * argv[]) {

    // Get the current directory
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) != NULL)
        ;
    else
        perror("getcwd() error");

    int numargs, all=0, asc=0, desc=0, push=0, uk=0, help=0, current=0, clean=0;
    const char *c;

    numargs = argc;

    while (--numargs > 0) {
        c = argv[numargs];

        if (!strcmp(argv[numargs], "all"))
            all = 1;
        else if (!strcmp(argv[numargs], "-h") || !strcmp(argv[numargs], "--help"))
            help = 1;
        else if (!strcmp(argv[numargs], "."))
            current = 1;
        else if (!strcmp(argv[numargs], "-p") || !strcmp(argv[numargs], "--push"))
            push = 1;
        else if (!strcmp(argv[numargs], "-"))
            desc = 1;
        else if (!strcmp(argv[numargs], "+"))
            asc = 1;
        else if (!strcmp(argv[numargs], "clean"))
            clean = 1;
        else
            uk++;
    }

    int args;
    args = argc;

    if (uk > 2) {
        printUsage();
        return 1;
    }

    switch(args) {
        // Show the comment for the current directory
        case 1: printUsage();
                return 1;
                break;
        case 2:
                // Show the help menu
                if (help) {
                    printUsage();
                    return 1;
                }
                // Show all the files
                else if (all) {
                    makeDir(cwd);
                    printFiles(cwd);
                }
                // Show comment for the current directory
                else if (current) {
                    // checkComment(argv[1], cwd);
                    printCurrentComment(cwd);
                }
                // Show comment for the entered file
                else {
                    // char *file = argv[1];
                    makeDir(cwd);
                    checkComment(argv[1], cwd);
                }
                break;
        case 3:

                if ( current && !clean ) {										// Add comment to the current directory
                    // char *file;
                    // file = "CURRENT";
                    addComment(argv[1], cwd, argv[2], false);
                } else if (all && desc) {					// Print all files in descending order
                    makeDir(cwd);
                    printAllOrder(cwd, true);
                } else if (all && asc) {					// Print all files in ascending order
                    makeDir(cwd);
                    printAllOrder(cwd, false);
                } else if ( clean ){
                    cleanComment( argv[1] );                 // Clean file comment
                }
                else {														// Add comment to to given file
                    makeDir(cwd);
                    addComment(argv[1], cwd, argv[2], false);
                }
                break;

        case 4:
                // Push comment to the current directory
                if (current && push) {
                    addComment(argv[1], cwd, argv[3], true);
                    // Add a comment to the given filename
                }
                // Push comment to given file
                else	if ( push) {
                    makeDir(cwd);
                    addComment(argv[1], cwd, argv[3], true);
                }
                break;

                // Not certain
        default: printUsage();
    }
    return 0;
}
Exemplo n.º 9
0
Arquivo: c.c Projeto: AlwaysLately/c
int main (int argc, const char *argv[]) {
	// Get the current directory
	char cwd[1024];
	if (!getcwd(cwd, sizeof(cwd))) {
		perror("getcwd() error");
	}

	int numargs, all = 0, asc = 0, desc = 0, push = 0, uk = 0, help = 0, current = 0;
	const char *c;

	numargs = argc;

	// TODO: Convert to getopt
	while (--numargs > 0) {
		c = argv[numargs];

		if (!strcmp(argv[numargs], "all"))
			all = 1;
		else if (!strcmp(argv[numargs], "-h") || !strcmp(argv[numargs], "--help"))
			help = 1;
		else if (!strcmp(argv[numargs], "."))
			current = 1;
		else if (!strcmp(argv[numargs], "-p") || !strcmp(argv[numargs], "--push"))
			push = 1;
		else if (!strcmp(argv[numargs], "-"))
			desc = 1;
		else if (!strcmp(argv[numargs], "+"))
			asc = 1;
		else
			uk++;
	}

	int args;
	args = argc;

	if (uk > 2) {
		printUsage();
		return EXIT_FAILURE;
	}

	if (args == 1) {
			printUsage();
			return EXIT_FAILURE;
	} else if (args == 2) {
		// Show the help menu
		if (help) {
			printUsage();
			return EXIT_FAILURE;
		}
		// Show all the files
		else if (all) {
			makeDir(cwd);
			printFiles(cwd);
		}
		// Show comment for the current directory
		else if (current) {
			// checkComment(argv[1], cwd);
			printCurrentComment(cwd);
		}
		// Show comment for the entered file
		else {
			// char *file = argv[1];
			makeDir(cwd);
			checkComment(argv[1], cwd);
		}
	} else if (args == 3) {
		if (current) {                               // Add comment to the current directory
			// char *file;
			// file = "CURRENT";
			addComment(argv[1], cwd, argv[2], false);
		} else if (all && desc) {                    // Print all files in descending order
			makeDir(cwd);
			printAllOrder(cwd, true);
		} else if (all && asc) {                     // Print all files in ascending order
			makeDir(cwd);
			printAllOrder(cwd, false);
		} else {                                     // Add comment to to given file
			makeDir(cwd);
			addComment(argv[1], cwd, argv[2], false);
		}
	} else if (args == 4) {
		if (current && push) { // Push comment to the current directory
			// Add a comment to the given filename
			addComment(argv[1], cwd, argv[3], true);
		} else if (push) {       // Push comment to given file
			makeDir(cwd);
			addComment(argv[1], cwd, argv[3], true);
		}
	} else {
		// Not certain
		printUsage();
	}
	return EXIT_SUCCESS;
}