Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // unite files to one
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #define MAXNAME 100
- // routines
- void showusage();
- void diskwritefailure();
- // constants & variables
- char *myname;
- int main(int argc, char *argv[])
- {
- int i, c, infile, outfile;
- char buf[BUFSIZ], destinationfilename[MAXNAME];
- size_t nread, nwrite;
- strcpy((myname=malloc(sizeof(argv[0]))), argv[0]);
- // parse command line options
- while ((c=getopt(argc, argv, ":o:")) != -1)
- switch (c) {
- case 'o':
- strcpy(destinationfilename, optarg);
- break;
- case '?':
- showusage();
- break;
- default:
- abort();
- break; }
- if (argc<3)
- showusage();
- if ((outfile=open(destinationfilename, O_WRONLY| O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))==-1)
- diskwritefailure();
- // unite files
- for (i=optind;i<argc;i++) {
- if ((infile=open(argv[i], O_RDONLY))==-1)
- break;
- while ((nread=read(infile, buf, sizeof(buf))))
- if ((nwrite=write(outfile, buf, nread)==-1))
- diskwritefailure();
- close(infile); }
- close(outfile);
- return 0;
- }
- // show usage
- void showusage()
- {
- printf("%s [-o output file] [files ...]\n", myname);
- exit(EXIT_FAILURE);
- }
- // error writing to disk
- void diskwritefailure()
- {
- printf("error writing to disk!\n");
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement