home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1472 / mkfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.9 KB  |  172 lines

  1. /*
  2.  * mkfile.c
  3.  *
  4.  * Create a file. Tries to mimic the SunOS 4.x command "mkfile" on a
  5.  * System V Release 3.1 and later system (will probably run on most
  6.  * earlier versions as well).
  7.  *
  8.  * Current version is 1.0.
  9.  *
  10.  * Copyright 1990 by Robert Claeson
  11.  *
  12.  * This software can be freely distributed in source code as long as
  13.  * you don't charge for it.
  14.  */
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <errno.h>
  21.  
  22.  
  23. #define BUFFER_SIZE 65536    /* 64 K buffer size */
  24.  
  25.  
  26. /* standard declarations for getopt() */
  27.  
  28. extern int    optind, opterr;
  29. extern char    *optarg;
  30.  
  31.  
  32. /*
  33.  * print_usage()
  34.  *
  35.  * Print usage information.
  36.  */
  37.  
  38. void print_usage()
  39. {
  40.     (void)fprintf(stderr, "mkfile: usage: mkfile [-nv] size[k|b|m] files...\n");
  41. }
  42.  
  43.  
  44. /*
  45.  * create_file()
  46.  *
  47.  * Create a file.
  48.  */
  49.  
  50. void create_file(name, size, empty)
  51. char    *name;
  52. long    size;
  53. int    empty;
  54. {
  55.     int        fd;        /* file descriptor for new file */
  56.     long    written;    /* no. of written bytes */
  57.     char    buffer[BUFFER_SIZE];
  58.  
  59.  
  60.     /* initialize values */
  61.  
  62.     written = 0L;
  63.     (void)memset(buffer, 0, BUFFER_SIZE);
  64.  
  65.  
  66.     /* create the file */
  67.  
  68.     if ((fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1)
  69.     (void)fprintf(stderr, "mkfile: could not create new file %s\n", name);
  70.     else {
  71.         if (empty) {
  72.         (void)lseek(fd, size, 0);
  73.         while (write(fd, "\0", 1) == -1 && errno == EINTR)
  74.         ;
  75.     } else {
  76.         for (; written < size && size >= (long)BUFFER_SIZE; written += (long)BUFFER_SIZE)
  77.         while (write(fd, buffer, BUFFER_SIZE) == -1 && errno == EINTR)
  78.             ;
  79.         if (written < size)
  80.         while (write(fd, buffer, (int)(size - written)) == -1 && errno == EINTR)
  81.             ;
  82.     }
  83.     }
  84.  
  85.     (void)close(fd);
  86. }
  87.  
  88.  
  89. /*
  90.  * main()
  91.  *
  92.  * Scan command-line arguments and initialize.
  93.  */
  94.  
  95. main(argc, argv)
  96. int argc;
  97. char *argv[];
  98. {
  99.     int        c, create_empty, verbose, error;
  100.     long    scale, size;
  101.     char    *scale_index;
  102.  
  103.  
  104.     /* initialize defaults */
  105.  
  106.     create_empty = 0;
  107.     verbose = 0;
  108.     error = 0;
  109.     scale = 1L;
  110.     size = 0L;
  111.  
  112.  
  113.     /* get options */
  114.  
  115.     while ((c = getopt(argc, argv, "nv")) != -1) {
  116.     switch (c) {
  117.     case 'n':
  118.         create_empty = 1;
  119.         break;
  120.     case 'v':        /* -v not implemented yet */
  121.         verbose = 1;
  122.         break;
  123.     default:
  124.         error = 1;
  125.         break;
  126.     }
  127.     }
  128.  
  129.  
  130.     /* check for errors */
  131.  
  132.     if (error || optind > argc - 2) {
  133.     print_usage();
  134.     exit(1);
  135.     }
  136.  
  137.  
  138.     /* check remaining arguments */
  139.  
  140.     if ((scale_index = strpbrk(argv[optind], "kbm")) != (char *)0) {
  141.     switch (*scale_index) {
  142.     case 'k':
  143.         scale = 1024L;
  144.         break;
  145.     case 'b':
  146.         scale = 512L;
  147.         break;
  148.     case 'm':
  149.         scale = 1024L * 1024L;
  150.         break;
  151.     default:
  152.         print_usage();
  153.         exit(1);
  154.     }
  155.     }
  156.  
  157.     argv[optind][strspn(argv[optind], "0123456789")] = '\0';
  158.  
  159.     size = strtol(argv[optind], (char **)0, 10);
  160.     size *= scale;
  161.  
  162.     optind++;
  163.  
  164.  
  165.     /* parse file names */
  166.  
  167.     for (; optind < argc; optind++)
  168.     create_file(argv[optind], size, create_empty);
  169.  
  170.     exit(0);
  171. }
  172.