echo.c 991 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. void
  12. main(int argc, char *argv[])
  13. {
  14. int nflag;
  15. int i, len;
  16. char *buf, *p;
  17. nflag = 0;
  18. if(argc > 1 && strcmp(argv[1], "-n") == 0)
  19. nflag = 1;
  20. len = 1;
  21. for(i = 1+nflag; i < argc; i++)
  22. len += strlen(argv[i])+1;
  23. buf = malloc(len);
  24. if(buf == 0)
  25. exits("no memory");
  26. p = buf;
  27. for(i = 1+nflag; i < argc; i++){
  28. strcpy(p, argv[i]);
  29. p += strlen(p);
  30. if(i < argc-1)
  31. *p++ = ' ';
  32. }
  33. if(!nflag)
  34. *p++ = '\n';
  35. if(write(1, buf, p-buf) < 0){
  36. fprint(2, "echo: write error: %r\n");
  37. exits("write error");
  38. }
  39. exits((char *)0);
  40. }