1STRCMP(3P) POSIX Programmer's Manual STRCMP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 strcmp — compare two strings
13
15 #include <string.h>
16
17 int strcmp(const char *s1, const char *s2);
18
20 The functionality described on this reference page is aligned with the
21 ISO C standard. Any conflict between the requirements described here
22 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
23 defers to the ISO C standard.
24
25 The strcmp() function shall compare the string pointed to by s1 to the
26 string pointed to by s2.
27
28 The sign of a non-zero return value shall be determined by the sign of
29 the difference between the values of the first pair of bytes (both
30 interpreted as type unsigned char) that differ in the strings being
31 compared.
32
34 Upon completion, strcmp() shall return an integer greater than, equal
35 to, or less than 0, if the string pointed to by s1 is greater than,
36 equal to, or less than the string pointed to by s2, respectively.
37
39 No errors are defined.
40
41 The following sections are informative.
42
44 Checking a Password Entry
45 The following example compares the information read from standard input
46 to the value of the name of the user entry. If the strcmp() function
47 returns 0 (indicating a match), a further check will be made to see if
48 the user entered the proper old password. The crypt() function shall
49 encrypt the old password entered by the user, using the value of the
50 encrypted password in the passwd structure as the salt. If this value
51 matches the value of the encrypted passwd in the structure, the entered
52 password oldpasswd is the correct user's password. Finally, the program
53 encrypts the new password so that it can store the information in the
54 passwd structure.
55
56
57 #include <string.h>
58 #include <unistd.h>
59 #include <stdio.h>
60 ...
61 int valid_change;
62 struct passwd *p;
63 char user[100];
64 char oldpasswd[100];
65 char newpasswd[100];
66 char savepasswd[100];
67 ...
68 if (strcmp(p->pw_name, user) == 0) {
69 if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
70 strcpy(savepasswd, crypt(newpasswd, user));
71 p->pw_passwd = savepasswd;
72 valid_change = 1;
73 }
74 else {
75 fprintf(stderr, "Old password is not valid\n");
76 }
77 }
78 ...
79
81 None.
82
84 None.
85
87 None.
88
90 strncmp()
91
92 The Base Definitions volume of POSIX.1‐2017, <string.h>
93
95 Portions of this text are reprinted and reproduced in electronic form
96 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
97 table Operating System Interface (POSIX), The Open Group Base Specifi‐
98 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
99 Electrical and Electronics Engineers, Inc and The Open Group. In the
100 event of any discrepancy between this version and the original IEEE and
101 The Open Group Standard, the original IEEE and The Open Group Standard
102 is the referee document. The original Standard can be obtained online
103 at http://www.opengroup.org/unix/online.html .
104
105 Any typographical or formatting errors that appear in this page are
106 most likely to have been introduced during the conversion of the source
107 files to man page format. To report such errors, see https://www.ker‐
108 nel.org/doc/man-pages/reporting_bugs.html .
109
110
111
112IEEE/The Open Group 2017 STRCMP(3P)