home *** CD-ROM | disk | FTP | other *** search
- /*
- * mkfile.c
- *
- * Create a file. Tries to mimic the SunOS 4.x command "mkfile" on a
- * System V Release 3.1 and later system (will probably run on most
- * earlier versions as well).
- *
- * Current version is 1.0.
- *
- * Copyright 1990 by Robert Claeson
- *
- * This software can be freely distributed in source code as long as
- * you don't charge for it.
- */
-
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #include <errno.h>
-
-
- #define BUFFER_SIZE 65536 /* 64 K buffer size */
-
-
- /* standard declarations for getopt() */
-
- extern int optind, opterr;
- extern char *optarg;
-
-
- /*
- * print_usage()
- *
- * Print usage information.
- */
-
- void print_usage()
- {
- (void)fprintf(stderr, "mkfile: usage: mkfile [-nv] size[k|b|m] files...\n");
- }
-
-
- /*
- * create_file()
- *
- * Create a file.
- */
-
- void create_file(name, size, empty)
- char *name;
- long size;
- int empty;
- {
- int fd; /* file descriptor for new file */
- long written; /* no. of written bytes */
- char buffer[BUFFER_SIZE];
-
-
- /* initialize values */
-
- written = 0L;
- (void)memset(buffer, 0, BUFFER_SIZE);
-
-
- /* create the file */
-
- if ((fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1)
- (void)fprintf(stderr, "mkfile: could not create new file %s\n", name);
- else {
- if (empty) {
- (void)lseek(fd, size, 0);
- while (write(fd, "\0", 1) == -1 && errno == EINTR)
- ;
- } else {
- for (; written < size && size >= (long)BUFFER_SIZE; written += (long)BUFFER_SIZE)
- while (write(fd, buffer, BUFFER_SIZE) == -1 && errno == EINTR)
- ;
- if (written < size)
- while (write(fd, buffer, (int)(size - written)) == -1 && errno == EINTR)
- ;
- }
- }
-
- (void)close(fd);
- }
-
-
- /*
- * main()
- *
- * Scan command-line arguments and initialize.
- */
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int c, create_empty, verbose, error;
- long scale, size;
- char *scale_index;
-
-
- /* initialize defaults */
-
- create_empty = 0;
- verbose = 0;
- error = 0;
- scale = 1L;
- size = 0L;
-
-
- /* get options */
-
- while ((c = getopt(argc, argv, "nv")) != -1) {
- switch (c) {
- case 'n':
- create_empty = 1;
- break;
- case 'v': /* -v not implemented yet */
- verbose = 1;
- break;
- default:
- error = 1;
- break;
- }
- }
-
-
- /* check for errors */
-
- if (error || optind > argc - 2) {
- print_usage();
- exit(1);
- }
-
-
- /* check remaining arguments */
-
- if ((scale_index = strpbrk(argv[optind], "kbm")) != (char *)0) {
- switch (*scale_index) {
- case 'k':
- scale = 1024L;
- break;
- case 'b':
- scale = 512L;
- break;
- case 'm':
- scale = 1024L * 1024L;
- break;
- default:
- print_usage();
- exit(1);
- }
- }
-
- argv[optind][strspn(argv[optind], "0123456789")] = '\0';
-
- size = strtol(argv[optind], (char **)0, 10);
- size *= scale;
-
- optind++;
-
-
- /* parse file names */
-
- for (; optind < argc; optind++)
- create_file(argv[optind], size, create_empty);
-
- exit(0);
- }
-