fork download
  1. #define MAX_SEND_SIZE 80
  2. struct mymsgbuf {
  3. long mtype;
  4. char mtext[MAX_SEND_SIZE];
  5. };
  6. void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text);
  7. void read_message(int qid, struct mymsgbuf *qbuf, long type);
  8. void remove_queue(int qid);
  9. void change_queue_mode(int qid, char *mode);
  10. void usage(void);
  11. int main(int argc, char *argv[])
  12. {
  13. key_t key;
  14. int msgqueue_id;
  15. struct mymsgbuf qbuf;
  16. if(argc == 1)
  17. usage();
  18. /* Создаем уникальный ключ через вызов ftok() */
  19. key = ftok(".",'m');
  20. /* Открываем очередь - при необходимости создаем */
  21. if((msgqueue_id = msgget(key, IPC_CREAT|0660)) == -1) {
  22. perror("msgget");
  23. exit(1);
  24. }
  25. switch(tolower(argv[1][0]))
  26. {
  27. case 's': send_message(msgqueue_id, (struct mymsg buf *)&qbuf,
  28. atol(argv[2]), argv[3]);
  29. break;
  30. case 'r': read_message(msgqueue_id, &qbuf, atol(argv[2]));
  31. break;
  32. case 'd': remove_queue(msgqueue_id);
  33. break;
  34. case 'm': change_queue_mode(msgqueue_id, argv[2]);
  35. break;
  36. default: usage();
  37. } return(0);
  38. }
  39. void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text)
  40. {
  41. /* Посылаем сообщение в очередь */
  42. printf("Sending a message ...\n");
  43. qbuf->mtype = type;
  44. strcopy(qbuf->mtext, text);
  45. if((msgsnd(qid, (struct msgbuf *)qbuf,
  46. strlen(qbuf->mtext)+1, 0)) == -1)
  47. {
  48. perror("msgsnd");
  49. exit(1);
  50. }
  51. }
  52. void read_message(int qid, struct mymsgbuf *qbuf, long type)
  53. {
  54. /* Вычитываем сообщение из очереди */
  55. printf("Reading a message ...\");
  56. qbuf->mtype = type;
  57. msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
  58. printf("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
  59. }
  60. void remove_queue(int qid)
  61. {
  62. /* Удаляем очередь */
  63. msgctl(qid, IPC_RMID, 0);
  64. }
  65. void change_queue_mode(int qid, char *mode)
  66. {
  67. struct msqid_ds myqueue_ds;
  68. /* Получаем текущее состояние */
  69. msgctl(qid, IPC_STAT, &myqueue_ds);
  70. /* Меняем состояние в копии внутренней структуры данных */
  71. sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode)
  72. /* Обновляем состояние в самой внутренней структуре данных */
  73. msgctl(qid, IPC_SET, &myqueue_ds);
  74. }
  75. void usage(void)
  76. {
  77. fprintf(stderr, "msgtool - A utility for tinkering with msg queues\n");
  78. fprintf(stderr, "\nUSAGE: msgtool (s)end \n");
  79. fprintf(stderr, " (r)ecv \n");
  80. fprintf(stderr, " (d)elete\n");
  81. fprintf(stderr, " (m)ode \n");
  82. exit(1);
  83. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:13:4: error: unknown type name ‘key_t’
prog.c:19:4: warning: implicit declaration of function ‘ftok’ [-Wimplicit-function-declaration]
prog.c:21:4: warning: implicit declaration of function ‘msgget’ [-Wimplicit-function-declaration]
prog.c:21:34: error: ‘IPC_CREAT’ undeclared (first use in this function)
prog.c:21:34: note: each undeclared identifier is reported only once for each function it appears in
prog.c:22:7: warning: implicit declaration of function ‘perror’ [-Wimplicit-function-declaration]
prog.c:23:7: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
prog.c:23:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
prog.c:25:4: warning: implicit declaration of function ‘tolower’ [-Wimplicit-function-declaration]
prog.c:27:57: error: expected ‘)’ before ‘buf’
prog.c:27:57: error: conversion to non-scalar type requested
prog.c:28:30: warning: implicit declaration of function ‘atol’ [-Wimplicit-function-declaration]
prog.c: In function ‘send_message’:
prog.c:42:4: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
prog.c:42:4: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
prog.c:44:4: warning: implicit declaration of function ‘strcopy’ [-Wimplicit-function-declaration]
prog.c:45:4: warning: implicit declaration of function ‘msgsnd’ [-Wimplicit-function-declaration]
prog.c:46:11: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
prog.c:46:11: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
prog.c:49:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
prog.c: In function ‘read_message’:
prog.c:55:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
prog.c:55:10: warning: missing terminating " character [enabled by default]
prog.c:55:3: error: missing terminating " character
prog.c:56:21: error: expected ‘)’ before ‘;’ token
prog.c:59:1: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
prog.c:59:1: note: expected ‘const char *’ but argument is of type ‘long int’
prog.c:59:1: error: expected ‘;’ before ‘}’ token
prog.c: In function ‘remove_queue’:
prog.c:63:4: warning: implicit declaration of function ‘msgctl’ [-Wimplicit-function-declaration]
prog.c:63:16: error: ‘IPC_RMID’ undeclared (first use in this function)
prog.c: In function ‘change_queue_mode’:
prog.c:67:20: error: storage size of ‘myqueue_ds’ isn’t known
prog.c:69:16: error: ‘IPC_STAT’ undeclared (first use in this function)
prog.c:71:4: warning: implicit declaration of function ‘sscanf’ [-Wimplicit-function-declaration]
prog.c:71:4: warning: incompatible implicit declaration of built-in function ‘sscanf’ [enabled by default]
prog.c:73:4: error: expected ‘;’ before ‘msgctl’
prog.c:67:20: warning: unused variable ‘myqueue_ds’ [-Wunused-variable]
prog.c: In function ‘usage’:
prog.c:77:4: warning: implicit declaration of function ‘fprintf’ [-Wimplicit-function-declaration]
prog.c:77:4: warning: incompatible implicit declaration of built-in function ‘fprintf’ [enabled by default]
prog.c:77:12: error: ‘stderr’ undeclared (first use in this function)
prog.c:82:4: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
stdout
Standard output is empty